Convert Figma logo to code with AI

craftyjs logoCrafty

JavaScript Game Engine

3,409
560
3,409
68

Top Related Projects

43,513

The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.

5,851

a fresh, modern & lightweight HTML5 game engine

36,911

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.

Cocos2d for Web Browsers. Built using JavaScript.

1,403

Kiwi.js is a blazingly fast mobile & desktop browser based HTML5 game framework. It uses CocoonJS for publishing to the AppStore.

2D JavaScript Physics Engine

Quick Overview

Crafty is a flexible and lightweight JavaScript game engine. It provides a powerful entity-component system for building 2D games and interactive applications, allowing developers to create complex game logic with ease.

Pros

  • Lightweight and fast, with a small footprint
  • Flexible entity-component system for modular game development
  • Cross-platform support, including mobile devices
  • Active community and extensive documentation

Cons

  • Limited 3D capabilities
  • Steeper learning curve compared to some other game engines
  • Less suitable for large-scale, complex games
  • Fewer built-in features compared to more comprehensive game engines

Code Examples

Creating a simple entity:

Crafty.init(400, 400);
Crafty.e('2D, DOM, Color')
  .attr({x: 0, y: 0, w: 100, h: 100})
  .color('red');

Adding movement to an entity:

Crafty.e('2D, DOM, Color, Fourway')
  .attr({x: 0, y: 0, w: 50, h: 50})
  .color('blue')
  .fourway(200);

Handling collisions:

Crafty.e('2D, DOM, Color, Collision')
  .attr({x: 100, y: 100, w: 50, h: 50})
  .color('green')
  .checkHits('SolidObject')
  .bind('HitOn', function(hitData) {
    console.log('Collision detected!');
  });

Getting Started

  1. Include Crafty in your HTML file:
<script src="https://rawgithub.com/craftyjs/Crafty/release/dist/crafty-min.js"></script>
  1. Initialize Crafty and create a simple game:
Crafty.init(400, 400);

Crafty.e('2D, DOM, Color, Fourway')
  .attr({x: 0, y: 0, w: 50, h: 50})
  .color('red')
  .fourway(200);

Crafty.e('2D, DOM, Color')
  .attr({x: 200, y: 200, w: 100, h: 100})
  .color('blue');

This creates a red square that can be moved with arrow keys and a static blue square.

Competitor Comparisons

43,513

The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.

Pros of PixiJS

  • More powerful rendering capabilities, especially for complex 2D graphics
  • Larger community and ecosystem, with more frequent updates and contributions
  • Better performance for games with many sprites or particles

Cons of PixiJS

  • Steeper learning curve, especially for beginners
  • Less built-in game development features compared to Crafty
  • Requires more setup and configuration for game-specific functionality

Code Comparison

Crafty.js:

Crafty.init(400, 400);
Crafty.e('2D, DOM, Color')
  .attr({x: 0, y: 0, w: 100, h: 100})
  .color('red');

PixiJS:

const app = new PIXI.Application({width: 400, height: 400});
document.body.appendChild(app.view);
const graphics = new PIXI.Graphics();
graphics.beginFill(0xFF0000);
graphics.drawRect(0, 0, 100, 100);
app.stage.addChild(graphics);

Summary

PixiJS offers more powerful rendering capabilities and better performance for complex 2D graphics, but comes with a steeper learning curve. Crafty provides a more beginner-friendly approach with built-in game development features. The code comparison shows that PixiJS requires more setup but offers more control over rendering, while Crafty provides a simpler, more declarative approach to creating game entities.

5,851

a fresh, modern & lightweight HTML5 game engine

Pros of melonJS

  • More comprehensive documentation and examples
  • Better support for mobile devices and touch controls
  • Integrated physics engine (based on Box2D)

Cons of melonJS

  • Steeper learning curve for beginners
  • Less flexible entity-component system compared to Crafty
  • Larger file size, which may impact load times for smaller projects

Code Comparison

melonJS:

me.game.world.addChild(new me.Sprite(0, 0, {
    image: "background"
}));

class Player extends me.Sprite {
    constructor(x, y) {
        super(x, y, {image: "player"});
    }
}

Crafty:

Crafty.e("2D, DOM, Color")
    .attr({x: 0, y: 0, w: 100, h: 100})
    .color("red");

Crafty.c("Player", {
    init: function() {
        this.requires("2D, Canvas, Color");
    }
});

Both frameworks offer different approaches to game development. melonJS provides a more structured, feature-rich environment with built-in physics and mobile support, while Crafty offers a more flexible and lightweight approach with its entity-component system. The choice between them depends on the specific needs of your project and your preferred development style.

36,911

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.

Pros of Phaser

  • More extensive documentation and larger community support
  • Built-in physics engines (Arcade Physics, Matter.js, and P2)
  • Better performance for complex games with many sprites

Cons of Phaser

  • Steeper learning curve for beginners
  • Larger file size, which may impact load times for web games
  • More opinionated structure, potentially limiting flexibility for experienced developers

Code Comparison

Phaser (creating a simple sprite):

function create() {
    this.add.sprite(400, 300, 'player');
}

Crafty (creating a simple entity):

Crafty.e('2D, DOM, Color')
    .attr({x: 0, y: 0, w: 100, h: 100})
    .color('#F00');

Both frameworks offer ways to create game objects, but Phaser's approach is more focused on sprites and scene management, while Crafty uses a component-based entity system.

Phaser is generally better suited for larger, more complex games with advanced graphics and physics requirements. Crafty, on the other hand, excels in simplicity and flexibility, making it a good choice for smaller projects or developers who prefer a more modular approach to game development.

Cocos2d for Web Browsers. Built using JavaScript.

Pros of cocos2d-html5

  • More comprehensive framework with a wider range of features for game development
  • Better performance for complex 2D games with hardware acceleration
  • Larger community and ecosystem, providing more resources and plugins

Cons of cocos2d-html5

  • Steeper learning curve due to its complexity and extensive API
  • Heavier framework, which may impact load times for simpler games
  • Less flexibility for non-game web applications compared to Crafty

Code Comparison

Crafty:

Crafty.init(400, 320, document.getElementById('game'));
Crafty.e('2D, DOM, Color').attr({x: 0, y: 0, w: 100, h: 100}).color('#F0F');

cocos2d-html5:

var scene = new cc.Scene();
var layer = new cc.LayerColor(cc.color(255, 0, 255, 255));
layer.setContentSize(cc.size(100, 100));
scene.addChild(layer);

Both frameworks allow for easy creation of game objects, but cocos2d-html5 uses a more structured, scene-based approach, while Crafty focuses on entity-component systems. cocos2d-html5 provides more built-in functionality for game development, whereas Crafty offers a lighter, more flexible foundation for web-based projects.

1,403

Kiwi.js is a blazingly fast mobile & desktop browser based HTML5 game framework. It uses CocoonJS for publishing to the AppStore.

Pros of Kiwi.js

  • More modern architecture with better support for mobile and touch devices
  • Extensive plugin system for easy extensibility
  • Built-in scene management and state machine for complex game logic

Cons of Kiwi.js

  • Smaller community and fewer resources compared to Crafty
  • Less frequent updates and maintenance
  • Steeper learning curve for beginners

Code Comparison

Kiwi.js:

var game = new Kiwi.Game('content', 'MyGame', state, { renderer: Kiwi.RENDERER_WEBGL });
var state = new Kiwi.State('Play');
state.create = function () {
    this.player = new Kiwi.GameObjects.Sprite(this, this.textures.playerImage, 100, 100);
    this.addChild(this.player);
};

Crafty:

Crafty.init(800, 600);
Crafty.e('2D, DOM, Color')
    .attr({ x: 0, y: 0, w: 100, h: 100 })
    .color('red');
Crafty.e('2D, Canvas, player')
    .attr({ x: 100, y: 100 });

Both frameworks offer different approaches to game development. Kiwi.js provides a more structured, object-oriented approach with built-in scene management, while Crafty focuses on a component-based entity system. The choice between them depends on the specific needs of your project and your preferred development style.

2D JavaScript Physics Engine

Pros of planck.js

  • Focused on 2D physics simulation, providing a more specialized and potentially optimized solution
  • Lightweight and efficient, suitable for both browser and Node.js environments
  • Based on Box2D, offering a familiar API for developers experienced with that library

Cons of planck.js

  • Limited to physics simulation, requiring additional libraries for game development features
  • Steeper learning curve for developers not familiar with physics engines
  • Less comprehensive documentation compared to Crafty

Code Comparison

planck.js:

var world = planck.World();
var body = world.createBody();
var circle = planck.Circle(0.5);
body.createFixture(circle);
world.step(1/60);

Crafty:

Crafty.init(800, 600);
Crafty.e('2D, DOM, Color')
  .attr({x: 0, y: 0, w: 100, h: 100})
  .color('red');
Crafty.e('2D, Canvas, Color')
  .attr({x: 150, y: 150, w: 100, h: 100})
  .color('blue');

Summary

planck.js is a specialized 2D physics engine, while Crafty is a more comprehensive game development framework. planck.js excels in physics simulations but requires additional tools for full game development. Crafty offers a broader set of features out-of-the-box, making it more suitable for rapid game prototyping and development. The choice between the two depends on the specific needs of the project and the developer's expertise.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

# Crafty JS Travis Build Status AppVeyor Build Status Sauce Test Status

Crafty is a JavaScript game library that can help you create games in a structured way…

Key Features:

  • Entities & Components - A clean and decoupled way to organize game elements. No inheritance needed!
  • Eventbinding - Event system for custom events that can be triggered whenever, whatever and bound just as easily.
  • No dom manipulation or custom drawing routines required.

Other Goodies:

  • Thriving community - Help is readily available in the forum.
  • Community modules - A growing collection of user-generated code you can use.
  • Pure JavaScript - No magic. Works in all major browsers and can be combined with your favorite js library.

Using Crafty

A simple game of pong:

Crafty.init(600, 300);
Crafty.background('rgb(127,127,127)');

//Paddles
Crafty.e("Paddle, 2D, DOM, Color, Multiway")
    .color('rgb(255,0,0)')
    .attr({ x: 20, y: 100, w: 10, h: 100 })
    .multiway(200, { W: -90, S: 90 });
Crafty.e("Paddle, 2D, DOM, Color, Multiway")
    .color('rgb(0,255,0)')
    .attr({ x: 580, y: 100, w: 10, h: 100 })
    .multiway(200, { UP_ARROW: -90, DOWN_ARROW: 90 });

//Ball
Crafty.e("2D, DOM, Color, Collision")
    .color('rgb(0,0,255)')
    .attr({ x: 300, y: 150, w: 10, h: 10,
            dX: Crafty.math.randomInt(2, 5),
            dY: Crafty.math.randomInt(2, 5) })
    .bind('UpdateFrame', function () {
        //hit floor or roof
        if (this.y <= 0 || this.y >= 290)
            this.dY *= -1;

        // hit left or right boundary
        if (this.x > 600) {
            this.x = 300;
            Crafty("LeftPoints").each(function () {
                this.text(++this.points + " Points") });
        }
        if (this.x < 10) {
            this.x = 300;
            Crafty("RightPoints").each(function () {
                this.text(++this.points + " Points") });
        }

        this.x += this.dX;
        this.y += this.dY;
    })
    .onHit('Paddle', function () {
        this.dX *= -1;
    });

//Score boards
Crafty.e("LeftPoints, DOM, 2D, Text")
    .attr({ x: 20, y: 20, w: 100, h: 20, points: 0 })
    .text("0 Points");
Crafty.e("RightPoints, DOM, 2D, Text")
    .attr({ x: 515, y: 20, w: 100, h: 20, points: 0 })
    .text("0 Points");

Left paddle is controlled by W & S, right paddle by UpArrow & DownArrow.
Check it out online and try to modify it yourself here.

Developing

If you want to fix a bug, please submit a pull request against the development branch. Some guides to help you can be found on the wiki

If you would like to make larger contributions please catch us in the forum and we will help you get started. Much appreciated :-)

Quick build instructions

The easiest way to build crafty is to use gruntjs, which requires node and npm. If you have grunt, node, and npm already installed, then run npm install from Crafty's root directory. (This will pull down about 30MB of node packages.) From then on, just run grunt to build.

You can also use yarn instead of npm.

(Full instructions here.)

NPM DownloadsLast 30 Days