npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ecsy-portobello

v0.2.4

Published

Entity Component System in JS

Downloads

7

Readme

ecsy

NPM package Build Size Dev Dependencies Build Status

ECSY (pronounced as "eck-see") is an highly experimental Entity Component System framework implemented in javascript, aiming to be lightweight, easy to use and with good performance.

For detailed information on the architecture and API please visit the documentation page

Features

  • Framework agnostic
  • Focused on providing a simple but yet efficient API
  • Designed to avoid garbage collection as possible
  • Systems, entities and components are scoped in a world instance
  • Multiple queries per system
  • Reactive support:
    • Support for reactive behaviour on systems (React to changes on entities and components)
    • System can query mutable or immutable components
  • Predictable:
    • Systems will run on the order they were registered or based on the priority defined when registering them
    • Reactive events will not generate a random callback when emited but queued and be processed in order
  • Modern Javascript: ES6, classes, modules,...
  • Pool for components and entities

Examples

  • Ball example:
    • three.js: https://ecsy.io/examples/ball-example/three
    • babylon: https://ecsy.io/examples/ball-example/babylon
  • 2D Canvas https://ecsy.io/examples/canvas
  • Factory pattern with Not operator: https://ecsy.io/examples/factory
  • System state component example: https://ecsy.io/examples/systemstatecomponents

Usage

Installing the package via npm:

npm install --save ecsy
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Hello!</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
      html, body: {
        margin: 0;
        padding: 0;
      }
    </style>
    
    <script type="module">
      import { World, System, TagComponent } from "https://ecsy.io/build/ecsy.module.js";

      const NUM_ELEMENTS = 50;
      const SPEED_MULTIPLIER = 0.3;
      const SHAPE_SIZE = 50;
      const SHAPE_HALF_SIZE = SHAPE_SIZE / 2;
      
      // Initialize canvas
      let canvas = document.querySelector("canvas");
      let canvasWidth = canvas.width = window.innerWidth;
      let canvasHeight = canvas.height = window.innerHeight;
      let ctx = canvas.getContext("2d");

      //----------------------
      // Components
      //----------------------
      
      // Velocity component
      class Velocity {
        constructor() {
          this.x = this.y = 0;
        }
      }

      // Position component
      class Position {
        constructor() {
          this.x = this.y = 0;
        }
      }
      
      // Shape component
      class Shape {
        constructor() {
          this.primitive = 'box';
        }
      }
      
      // Renderable component
      class Renderable extends TagComponent {}
      
      //----------------------
      // Systems
      //----------------------
      
      // MovableSystem
      class MovableSystem extends System {
        // This method will get called on every frame by default
        execute(delta, time) {
          // Iterate through all the entities on the query
          this.queries.moving.results.forEach(entity => {
            var velocity = entity.getComponent(Velocity);
            var position = entity.getMutableComponent(Position);
            position.x += velocity.x * delta;
            position.y += velocity.y * delta;
            
            if (position.x > canvasWidth + SHAPE_HALF_SIZE) position.x = - SHAPE_HALF_SIZE;
            if (position.x < - SHAPE_HALF_SIZE) position.x = canvasWidth + SHAPE_HALF_SIZE;
            if (position.y > canvasHeight + SHAPE_HALF_SIZE) position.y = - SHAPE_HALF_SIZE;
            if (position.y < - SHAPE_HALF_SIZE) position.y = canvasHeight + SHAPE_HALF_SIZE;
          });
        }
      }

      // Define a query of entities that have "Velocity" and "Position" components
      MovableSystem.queries = {
        moving: {
          components: [Velocity, Position]
        }
      }

      // RendererSystem
      class RendererSystem extends System {
        // This method will get called on every frame by default
        execute(delta, time) {
          
          ctx.fillStyle = "#d4d4d4";
          ctx.fillRect(0, 0, canvasWidth, canvasHeight);
          
          // Iterate through all the entities on the query
          this.queries.renderables.results.forEach(entity => {
            var shape = entity.getComponent(Shape);
            var position = entity.getComponent(Position);
            if (shape.primitive === 'box') {
              this.drawBox(position);
            } else {
              this.drawCircle(position);
            }
          });
        }
        
        drawCircle(position) {
          ctx.beginPath();
          ctx.arc(position.x, position.y, SHAPE_HALF_SIZE, 0, 2 * Math.PI, false);
          ctx.fillStyle= "#39c495";
          ctx.fill();
          ctx.lineWidth = 2;
          ctx.strokeStyle = "#0b845b";
          ctx.stroke();          
        }
        
        drawBox(position) {
          ctx.beginPath();
          ctx.rect(position.x - SHAPE_HALF_SIZE, position.y - SHAPE_HALF_SIZE, SHAPE_SIZE, SHAPE_SIZE);
          ctx.fillStyle= "#e2736e";
          ctx.fill();
          ctx.lineWidth = 2;
          ctx.strokeStyle = "#b74843";
          ctx.stroke();                      
        }
      }

      // Define a query of entities that have "Renderable" and "Shape" components
      RendererSystem.queries = {
        renderables: { components: [Renderable, Shape] }
      }
      
      // Create world and register the systems on it
      var world = new World();
      world
        .registerSystem(MovableSystem)
        .registerSystem(RendererSystem);

      // Some helper functions when creating the components
      function getRandomVelocity() {
        return {
          x: SPEED_MULTIPLIER * (2 * Math.random() - 1), 
          y: SPEED_MULTIPLIER * (2 * Math.random() - 1)
        };
      }
      
      function getRandomPosition() {
        return { 
          x: Math.random() * canvasWidth, 
          y: Math.random() * canvasHeight
        };
      }
      
      function getRandomShape() {
         return {
           primitive: Math.random() >= 0.5 ? 'circle' : 'box'
         };
      }
      
      for (let i = 0; i < NUM_ELEMENTS; i++) {
        world
          .createEntity()
          .addComponent(Velocity, getRandomVelocity())
          .addComponent(Shape, getRandomShape())
          .addComponent(Position, getRandomPosition())
          .addComponent(Renderable)        
      }
            
      // Run!
      function run() {
        // Compute delta and elapsed time
        var time = performance.now();
        var delta = time - lastTime;

        // Run all the systems
        world.execute(delta, time);

        lastTime = time;
        requestAnimationFrame(run);
      }

      var lastTime = performance.now();
      run();      
    </script>
  </head>  
  <body>
    <canvas width="500" height="500"></canvas>
  </body>
</html>

Try it on glitch

You can also include the hosted javascript directly on your HTML:

<!-- Using UMD (It will expose a global ECSY namespace) -->
<script src="https://ecsy.io/build/ecsy.js"></script>

<!-- Using ES6 modules -->
<script src="https://ecsy.io/build/ecsy.module.js"></script>