{"id":44866988,"url":"https://github.com/loganzartman/zecs","last_synced_at":"2026-02-17T11:36:17.707Z","repository":{"id":266025581,"uuid":"897127128","full_name":"loganzartman/zecs","owner":"loganzartman","description":"strongly-typed, functional, unopinionated, fast-enough entity-component-system system for hobby use","archived":false,"fork":false,"pushed_at":"2025-05-31T08:35:22.000Z","size":165,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-24T09:43:52.241Z","etag":null,"topics":["data-oriented","ecs","entity-component-system","functional","typed","typescript","zod"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/loganzartman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-12-02T04:33:03.000Z","updated_at":"2025-08-26T22:16:14.000Z","dependencies_parsed_at":"2024-12-17T06:22:56.544Z","dependency_job_id":"2282e15b-5b4b-4a9d-8ce9-24f3162c20eb","html_url":"https://github.com/loganzartman/zecs","commit_stats":null,"previous_names":["loganzartman/zecs"],"tags_count":32,"template":false,"template_full_name":null,"purl":"pkg:github/loganzartman/zecs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loganzartman%2Fzecs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loganzartman%2Fzecs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loganzartman%2Fzecs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loganzartman%2Fzecs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/loganzartman","download_url":"https://codeload.github.com/loganzartman/zecs/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loganzartman%2Fzecs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29542528,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-17T08:11:05.436Z","status":"ssl_error","status_checked_at":"2026-02-17T08:09:38.860Z","response_time":100,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-oriented","ecs","entity-component-system","functional","typed","typescript","zod"],"created_at":"2026-02-17T11:36:16.402Z","updated_at":"2026-02-17T11:36:17.422Z","avatar_url":"https://github.com/loganzartman.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zecs\n\nstrongly-typed, unopinionated, fast-enough **entity-component-system** system for hobby use[^1]\n\n[^1]: that's what i use it for, so ymmv\n\n_zecs_ is a **composition-based** approach to organizing data and updates!\n\ntightly integrated with [zod](https://zod.dev/) for schemas\n\n```sh\npnpm add zecs\n```\n\n```sh\nnpm install --save zecs\n```\n\n```sh\nyarn add zecs\n```\n\nyou can import individual pieces, or the whole thing:\n\n```ts\nimport {component, query} from 'zecs';\nimport {zecs} from 'zecs';\n```\n\n## basics\n\n### component\n\n**components** are just [zod types](https://zod.dev/?id=introduction) with names:\n\n```ts\n// docs/010-component.ts\n\nimport { zecs } from 'zecs';\nimport { z } from 'zod/v4';\n\nexport const health = zecs.component('health', z.number());\nexport const position = zecs.component(\n  'position',\n  z.object({ x: z.number(), y: z.number() }),\n);\nexport const velocity = zecs.component(\n  'velocity',\n  z.object({ x: z.number(), y: z.number() }),\n);\n\n```\n\n### entity\n\n**entities** are plain objects where each property matches a component:\n\n```ts\n// docs/020-entity.ts\n\nexport const player = {\n  health: 100,\n  position: { x: 10, y: 20 },\n};\n\n```\n\n### ecs\n\nan **ecs** stores entities that conform to some set of components:\n\n```ts\n// docs/030-ecs.ts\n\nimport { zecs } from 'zecs';\nimport { health, position, velocity } from './010-component';\nimport { player } from './020-entity';\n\nexport const ecs = zecs.ecs([health, position, velocity]);\necs.add(player);\n\n```\n\n\u003e [!NOTE]\n\u003e every component is optional! this allows behavior to differ between entities, and it's why we need queries.\n\n### query\n\n**queries** select entities based on what components they have...\n\n```ts\n// docs/040-query.ts\n\nimport { zecs } from 'zecs';\nimport { position, velocity } from './010-component';\nimport { ecs } from './030-ecs';\n\nconst movable = zecs.query().has(position, velocity);\n\n// queries are reusable and not bound to a specific ECS!\nfor (const entity of movable.query(ecs)) {\n  entity.position.x += entity.velocity.x;\n  entity.position.y += entity.velocity.y;\n}\n\n```\n\nor any condition you want:\n\n```ts\n// docs/041-query-refinement.ts\n\nimport { zecs } from 'zecs';\nimport { position, velocity } from './010-component';\nimport { ecs } from './030-ecs';\n\ndeclare const keys: Record\u003cstring, boolean\u003e;\n\nconst canJump = zecs\n  .query()\n  .has(position, velocity)\n  .where(({ position: { y } }) =\u003e y === 0);\n\nfor (const entity of canJump.query(ecs)) {\n  if (keys.space) {\n    entity.velocity.y = -10;\n  }\n}\n\n```\n\n## serialization\n\nevery zecs ECS is serializable, meaning that it can be converted to and from a plain object:\n\n```ts\n// docs/050-serialization.ts\n\nimport { ecs } from './030-ecs';\n\ndeclare function mySave(data: string): void;\ndeclare function myLoad(): string;\n\nconst data = ecs.toJSON();\n\n// your serializing and saving logic\nmySave(JSON.stringify(data));\nconst loaded = JSON.parse(myLoad());\n\necs.loadJSON(loaded);\n\n```\n\ncomponents may contain other entities, and zecs will automatically convert them to and from references when serializing:\n\n```ts\n// docs/051-serializing-references.ts\n\nimport { zecs } from 'zecs';\nimport { z } from 'zod/v4';\n\nconst name = zecs.component('name', z.string());\nconst friend = zecs.component('friend', zecs.entitySchema([name]));\nconst friendlyEcs = zecs.ecs([name, friend]);\n\nconst kai = friendlyEcs.add({ name: 'kai' });\nconst jules = friendlyEcs.add({ name: 'jules' });\nkai.friend = jules;\njules.friend = kai;\n\nfriendlyEcs.loadJSON(friendlyEcs.toJSON());\n\n```\n\n## system\n\nit's just fine to put a query in a function and be done with it.\n\ni'll also offer you the **system**. the simplest system is just like looping over every entity in a query:\n\n```ts\n// docs/060-system.ts\n\nimport { zecs } from 'zecs';\nimport { z } from 'zod/v4';\nimport { position, velocity } from './010-component';\nimport { ecs } from './030-ecs';\n\nconst kinematics = zecs.system({\n  name: 'kinematics',\n  query: zecs.query().has(position, velocity),\n\n  updateParams: z.object({ dt: z.number() }),\n\n  onUpdated({ entity: { position, velocity }, updateParams: { dt } }) {\n    position.x += velocity.x * dt;\n    position.y += velocity.y * dt;\n  },\n});\n\nconst kinematicsHandle = await zecs.attachSystem(ecs, kinematics, {});\n\nkinematicsHandle.update({ dt: 0.016 });\n\n```\n\na system, much like a component or a query, is just a description. **attaching** the system to an ECS returns a stateful \"handle\", which can be `.update()` every frame or step.\n\n```ts\n// docs/061-system-lifecycle.ts\n\nimport { zecs } from 'zecs';\nimport { z } from 'zod/v4';\n\nconst pointSchema = z.object({ x: z.number(), y: z.number() });\nconst line = zecs.component(\n  'line',\n  z.object({ a: pointSchema, b: pointSchema }),\n);\n\nconst drawLines = zecs.system({\n  name: 'drawLines',\n  query: zecs.query().has(line),\n\n  updateParams: z.object({\n    ctx: z.custom\u003cCanvasRenderingContext2D\u003e(),\n  }),\n\n  onPreUpdate({ updateParams: { ctx } }) {\n    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);\n    ctx.save();\n    ctx.strokeStyle = 'red';\n  },\n\n  onUpdated({ entity: { line }, updateParams: { ctx } }) {\n    ctx.beginPath();\n    ctx.moveTo(line.a.x, line.a.y);\n    ctx.lineTo(line.b.x, line.b.y);\n    ctx.stroke();\n  },\n\n  onPostUpdate({ updateParams: { ctx } }) {\n    ctx.restore();\n  },\n});\n\nconst lineEcs = zecs.ecs([line]);\n\nconst canvas = document.getElementById('canvas') as HTMLCanvasElement;\nconst ctx = canvas.getContext('2d');\nif (!ctx) {\n  throw new Error('Failed to get canvas context');\n}\n\nconst drawLinesHandle = await zecs.attachSystem(lineEcs, drawLines, {\n  ctx,\n});\ndrawLinesHandle.update({ ctx });\n\n```\n\n### resources\n\na system can also have **resources**--both **shared** and for **each** entity:\n\n```ts\n// docs/062-system-resources.ts\n\nimport { zecs } from 'zecs';\nimport { z } from 'zod/v4';\nimport { position } from './010-component';\nimport { ecs } from './030-ecs';\n\n// pixi.js stubs\ndeclare class Asset {}\ndeclare const Assets: { load(src: string): Promise\u003cAsset\u003e };\ndeclare class Sprite {\n  position: { x: number; y: number };\n  constructor(p: { texture: Asset; position: { x: number; y: number } });\n  destroy(): void;\n}\n\nconst drawSprites = zecs.system({\n  name: 'drawSprites',\n  query: zecs.query().has(position),\n\n  initParams: z.object({\n    texturePath: z.string(),\n  }),\n\n  shared: {\n    async create({ initParams: { texturePath } }) {\n      const texture = await Assets.load(texturePath);\n      return { texture };\n    },\n  },\n\n  each: {\n    create({ shared: { texture }, entity }) {\n      const sprite = new Sprite({\n        texture,\n        position: { x: entity.position.x, y: entity.position.y },\n      });\n      return { sprite };\n    },\n    destroy({ each: { sprite } }) {\n      sprite.destroy();\n    },\n  },\n\n  onUpdated({ each: { sprite }, entity }) {\n    sprite.position.x = entity.position.x;\n    sprite.position.y = entity.position.y;\n  },\n});\n\nconst drawSpritesHandle = await zecs.attachSystem(ecs, drawSprites, {\n  texturePath: './texture.png',\n});\n\ndrawSpritesHandle.update({});\n\n```\n\n**shared** resources get created when the system is **attached** to an ECS. in most cases, this is just once.\n\n**each** resources are created for each entity that matches the query. they persist as long as it keeps matching, and then they get destroyed.\n\n## schedule\n\nyou can attach an individual system to an ECS and update it by hand, but what if you have a lot?\n\nsure can be tedious to juggle a bunch of system `update()`s.\n\nschedules take a list of systems and give you a handle to update all of them at once (in a strongly-typed fashion, of course.)\n\n```ts\n// docs/070-schedule.ts\n\nimport { zecs } from 'zecs';\nimport { z } from 'zod/v4';\nimport { position, velocity } from './010-component';\n\nconst gravitySystem = zecs.system({\n  name: 'gravity',\n  query: zecs.query().has(position, velocity),\n\n  updateParams: z.object({ dt: z.number() }),\n\n  onUpdated({ entity, updateParams }) {\n    entity.velocity.y -= 9.81 * updateParams.dt;\n  },\n});\n\nconst kinematicsSystem = zecs.system({\n  name: 'kinematics',\n  query: zecs.query().has(position, velocity),\n\n  updateParams: z.object({ dt: z.number() }),\n\n  onUpdated({ entity, updateParams }) {\n    entity.position.x += entity.velocity.x * updateParams.dt;\n    entity.position.y += entity.velocity.y * updateParams.dt;\n  },\n});\n\nconst scheduleEcs = zecs.ecs([position, velocity]);\n\nconst schedule = await zecs.scheduleSystems(\n  scheduleEcs,\n  [gravitySystem, kinematicsSystem],\n  {},\n);\n\nschedule.update({ dt: 0.016 });\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floganzartman%2Fzecs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Floganzartman%2Fzecs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floganzartman%2Fzecs/lists"}