• Stars
    star
    176
  • Rank 216,987 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated about 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

2d canvas-based rapid prototyping game engine

chem

canvas-based game engine and toolchain optimized for rapid development.

Features

  • Automatically creates a spritesheet for your assets and then loads the assets at runtime.
  • Provides API for drawing animated sprites in a canvas
    • Supports anchor points and rotation
  • Write code in JavaScript or other compile-to-javascript languages such as Coffee-Script.
  • Uses browserify to compile your code which allows you to harness the power of code on npm.
  • Everything from code to spritesheet is compiled automatically when you save.
  • Handles main loop and frame skipping.
  • API for keyboard and mouse input.

Featured Game Demos

Face the Music Purgatory: Lost Doorways Pillagers!

Usage

# install dependencies in ubuntu
# for other OSes see https://github.com/LearnBoost/node-canvas/wiki/
sudo apt-get install libcairo2-dev

# start with a nearly-empty project, such as a freshly created project
# from github with only a .git/ and README.md.
cd my-project

# use npm init to create a package.json file so that we can install
# dependencies locally instead of globally.
# feel free to mash enter through the series of questions.
npm init

# init the project with chem-cli
npm install chem-cli
./node_modules/.bin/chem init

# the `dev` command will run a development server which will automatically
# recompile your code, generate your spritesheets, and serve your assets.
npm run dev

# see more commands
./node_modules/.bin/chem

See chem-cli for more information.

Synopsis

Layout

Source files

./chemfile.js
./src/main.js
./public/index.html
./assets/img/ship.png
./assets/img/explosion/01.png
...
./assets/img/explosion/12.png

Generated files

./public/main.js
./public/spritesheet.png
./public/animations.json

./src/main.js

var chem = require("chem");
var v = chem.vec2d;
var ani = chem.resources.animations;
var canvas = document.getElementById("game");
var engine = new chem.Engine(canvas);
engine.showLoadProgressBar();
engine.start();
canvas.focus();

chem.resources.on('ready', function () {
  var batch = new chem.Batch();
  var boom = new chem.Sound('sfx/boom.ogg');
  var ship = new chem.Sprite(ani.ship, {
    batch: batch,
    pos: v(200, 200),
    rotation: Math.PI / 2
  });
  var shipVel = v();
  var rotationSpeed = Math.PI * 0.04;
  var thrustAmt = 0.1;
  var fpsLabel = engine.createFpsLabel();
  engine.on('update', function (dt, dx) {
    ship.pos.add(shipVel);

    // rotate the ship with left and right arrow keys
    var rotateAmt = rotationSpeed * dx;
    if (engine.buttonState(chem.button.KeyLeft)) ship.rotation -= rotateAmt;
    if (engine.buttonState(chem.button.KeyRight)) ship.rotation += rotateAmt;

    // apply forward and backward thrust with up and down arrow keys
    var thrust = v.unit(ship.rotation).scale(thrustAmt * dx);
    if (engine.buttonState(chem.button.KeyUp)) shipVel.add(thrust);
    if (engine.buttonState(chem.button.KeyDown)) shipVel.sub(thrust);

    // press space to blow yourself up
    if (engine.buttonJustPressed(chem.button.KeySpace)) {
      boom.play();
      ship.setAnimation(ani.boom);
      ship.setFrameIndex(0);
      ship.on('animationend', function() {
        ship.delete();
      });
    }
  });
  engine.on('draw', function (context) {
    // clear canvas to black
    context.fillStyle = '#000000'
    context.fillRect(0, 0, engine.size.x, engine.size.y);

    // draw all sprites in batch
    batch.draw(context);

    // draw a little fps counter in the corner
    fpsLabel.draw(context);
  });
});

./chemfile.js

// the main source file which depends on the rest of your source files.
exports.main = 'src/main';

exports.spritesheet = {
  // you can override any of these in individual animation declarations
  defaults: {
    delay: 0.05,
    loop: false,
    // possible values: a Vec2d instance, or one of:
    // ["center", "topleft", "topright", "bottomleft", "bottomright",
    //  "top", "right", "bottom", "left"]
    anchor: "center"
  },
  animations: {
    boom: {
      // frames can be a list of filenames or a string to match the beginning
      // of files with. If you leave it out entirely, it defaults to the
      // animation name.
      frames: "explosion"
    },
    ship: {}
  }
};

./public/index.html

<!doctype html>
<html>
  <head>
    <title>Chem Example</title>
  </head>
  <body style="text-align: center">
    <canvas id="game" width="853" height="480"></canvas>
    <p>Use the arrow keys to move around and space to destroy yourself.</p>
    <script type="text/javascript" src="main.js"></script>
  </body>
</html>

See the demo in action.

More Demo Projects Using Chem

  • Planetarius - Networked multiplayer arcade shooter, made in 48 hours.
  • Lemming - Use your own dead bodies to beat side-scrolling platformer levels. Made in 7 days.
  • Meteor Attack - dodge meteors in space
  • Disinfecticide - use extreme measures to control a deadly disease outbreak.
  • holocaust - rebuild society after a nuclear holocaust ravages the world
  • Dr. Chemical's Lab - puzzle game that should have been an action game.

Developing With Chem

Chemfile

Start by looking at your chemfile. This file contains all the instructions on how to build your game.

This file, like every other source file in your game, can be in any compile- to-JavaScript language (including JavaScript itself) that you choose.

  • main - this is the entry point into your game. Chem will use browserify with this as the input file. Often this is set to src/main.js.

  • spritesheet

    • defaults - for each animation, these are the default values that will be used if you do not specify one.
    • animations - these will be available when you create a sprite.
      • anchor - the "center of gravity" point. pos is centered here, and a sprite's rotation rotates around this point. Use a Vec2d instance for this value.
      • frames - frames can be a list of filenames or a string to match the beginning of files with. if you leave it out entirely, it defaults to the animation name.
      • delay - number of seconds between frames.
      • loop - whether an animation should start over when it ends. You can override this in individual sprites.
  • autoBootstrap - set this to false if you do not want public/bootstrap.js to be auto generated.

If you leave the spritesheet export undefined, no spritesheet will be generated or used at runtime.

Use any "compile to JS" language

Supported languages:

Getting Started

The first step is to require "chem":

var chem = require('chem');

// Next, locate your canvas:
var canvas = document.getElementById("the-canvas-id");

// Create the main game engine:
var engine = new Engine(canvas);

// Display a nice loading progress bar while we serve assets:
// (it automatically goes away once loading is complete)
engine.showLoadProgressBar()

// Start the main loop:
engine.start()

// Focus the canvas so that keyboard input works:
canvas.focus()

// Finally, wait until resources are done loading:
chem.resources.on('ready', function() {
  // Now you can go for it. All asssets are loaded.
});

Vec2d Convention

As a convention, any Vec2d instances you get from Chem are not clones. That is, pay careful attention not to perform destructive behavior on the Vec2d instances returned from the API.

Resource Locations

Text files placed in public/text/ will be available in the chem.resources.text object once the 'ready' event has fired.

Image files placed in public/img/ will be available in the chem.resources.images object once the 'ready' event has fired.

Reference API Documentation

See doc/api.md.

History / Changelog

See doc/history.md.

More Repositories

1

libsoundio

C library for cross-platform real-time audio input and output
C
1,933
star
2

groovebasin

Music player server with a web-based user interface.
JavaScript
1,851
star
3

node-s3-client

high level amazon s3 client for node.js
JavaScript
1,003
star
4

poop

Performance Optimizer Observation Platform
Zig
882
star
5

naught

Zero downtime deployment for your Node.js server using builtin cluster API
JavaScript
793
star
6

jamulator

(unmaintained) recompiling NES roms into native executables
Go
388
star
7

tetris

A simple tetris clone written in zig programming language.
Zig
381
star
8

libgroove

streaming audio processing library
C
290
star
9

HellOS

"hello world" x86 kernel example
Zig
262
star
10

node-diacritics

remove diacritics from strings ("ascii folding") - Node.js module
JavaScript
259
star
11

waveform

simultaneously transcode and generate visuals for an audio file
C
251
star
12

clashos

multiplayer arcade game for bare metal Raspberry Pi 3 B+
Zig
216
star
13

genesis

Genesis Digital Audio Workstation
C++
176
star
14

swig-email-templates

Node.js module for rendering emails with swig templates and email-friendly inline CSS using boost.
JavaScript
162
star
15

node-groove

bindings to libgroove - music player backend library
C++
157
star
16

node-mv

Like `fs.rename`, but works across devices, and works with directories. Think of the unix utility `mv`.
JavaScript
155
star
17

ffmpeg

FFmpeg Zig package
C
137
star
18

zig-window

window client library
C++
116
star
19

sdl-zig-demo

SDL2 hello world in zig
Zig
115
star
20

node-waveform

simultaneously transcode audio and generate visuals - Node.js module
C
101
star
21

zig-vulkan-triangle

simple triangle displayed using vulkan, xcb, and zig
Zig
98
star
22

node-s3-cli

command line utility to go along with node s3 module
JavaScript
97
star
23

malcheck

Test your code with malcheck to make sure it handles out of memory conditions correctly.
C
96
star
24

zig-wasi

Minimal WASI interpreter
C
91
star
25

zasm

multi-target assembler and disassembler
Zig
90
star
26

mpd.js

Connect to a music player daemon server, send commands, emit events.
JavaScript
89
star
27

PyDaw

python library to mess with Digital Audio Workstations. FL Studio project files (.flp) supported.
C++
87
star
28

node-flp

FL Studio project file parser for node.js
JavaScript
77
star
29

node-tmx-parser

node.js module to parse and load tiled map editor maps (see mapeditor.org)
JavaScript
71
star
30

node-astar

Generic A* algorithm for node.js
JavaScript
67
star
31

node-sox

(unmaintained) node.js interface to the sox audio utility
JavaScript
61
star
32

zig-async-demo

Comparing concurrent code example programs between other languages and Zig
Zig
55
star
33

mcserve

wraps minecraft server and gives you a web interface
JavaScript
54
star
34

StaticHttpFileServer

Zig module for serving a directory of files from memory via HTTP
Zig
49
star
35

zig-general-purpose-allocator

work-in-progress general purpose allocator intended to be eventually merged into Zig standard library. live streamed development
Zig
46
star
36

autodoc

Zig Documentation Generator
Zig
45
star
37

liblaxjson

C library for parsing JSON config files
C
33
star
38

node-perlin-noise

perlin noise generator for node.js
JavaScript
33
star
39

lua-in-the-browser

using zig to build lua for webassembly
C
32
star
40

node-fd-slicer

safely create multiple ReadStream or WriteStream objects from the same file descriptor
JavaScript
30
star
41

flag2struct

simple CLI tool for converting zig source code using flags-style declarations to packed structs
Zig
29
star
42

rucksack

texture packer and resource bundler
C
28
star
43

mime

zig package for mapping extensions to mime types
Zig
28
star
44

connect-sse

connect middleware for server sent events (EventSource)
JavaScript
27
star
45

PyWaveform

Python library to create an image of a song's waveform
C
26
star
46

zig-stage1

Exploring replacing Zig's stage1 compiler with pure C code that outputs pure C code
C
25
star
47

xml

Tokenize XML
Zig
24
star
48

libavfilter-example

small example of using libavfilter to filter audio
C
23
star
49

node-plan

(unmaintained, deprecated, abandoned) Execute a complicated dependency graph of tasks with smooth progress events.
JavaScript
20
star
50

connect-static

static file server middleware for connect. loads files once at startup and saves gzipped versions in memory
JavaScript
19
star
51

dotfiles

linux yo
Nix
18
star
52

pyedsdk

Python library to control cameras via EDSDK
C
18
star
53

groove-rs

rust bindings to libgroove - streaming audio processing library
Rust
16
star
54

node-pend

dead-simple optimistic async helper in javascript
JavaScript
16
star
55

evo

specification, reference implementation, and examples of Evo, the programming language made for being the DNA of genetic algorithms
Zig
15
star
56

mediablast

(unmaintained, deprecated, abandoned) open source media processing server
JavaScript
14
star
57

purgatory

escape from the circles of hell - 7 hour game jam
JavaScript
14
star
58

browserify-lite

browserify, minus some of the advanced features and heavy dependencies
JavaScript
14
star
59

pillagers

Real time strategy game with space physics
JavaScript
12
star
60

hackerrank

my solutions to hackerrank puzzles
Go
11
star
61

connect-nocache

connect middleware to insert no cache headers
JavaScript
11
star
62

andrewkelley.me

my personal site
HTML
11
star
63

pulseaudio

pulseaudio with the build system replaced by zig
C
11
star
64

SIMD-test

exploring SIMD optimization
C
10
star
65

truthfinder

TruthFinder.org website
Python
10
star
66

mc-bot-server

(unmaintained) server that spins up minecraft bots
JavaScript
9
star
67

zig-mandelbrot-gl

mandelbrot set in zig
Zig
9
star
68

node-yawl

yet another websockets library for Node.js
JavaScript
8
star
69

clashproto

prototyping the game for andrewrk/clashos
Zig
8
star
70

planet-evo

evolution simulation software
C++
8
star
71

advent-of-code

https://adventofcode.com
Zig
8
star
72

node-music-library-index

node module to build a searchable javascript object model given track metadata objects
JavaScript
7
star
73

github-popularity-contest

see who has the most collective stars
JavaScript
7
star
74

node-human-size

tiny node.js module to get human readable file size from byte count
JavaScript
7
star
75

libmp3lame

libmp3lame with the build system replaced by zig
C
7
star
76

node-stream-counter

node.js module to keep track of how many bytes have been written to a stream
JavaScript
7
star
77

mpd

a fork of mpd to add library management, better search, and a sophisticated dynamic playlist
C
7
star
78

boost

Inline CSS into your HTML source
JavaScript
6
star
79

Secure-WordVault

(unmaintained, deprecated, abandoned) Enables you to store sensitive information in a portable manner
C++
6
star
80

dominion

Node.js module and command line program to play Dominion, the card game by Donald X. Vaccarino.
JavaScript
6
star
81

TrenchBowl

simple music player UI to demonstrate libgroove
C++
6
star
82

node-spritesheet

node.js module: given a list of image files, create a spritesheet using cairo
JavaScript
6
star
83

chem-cli

html5 canvas game engine optimized for rapid development - command line interface
JavaScript
5
star
84

face-the-music

indie speed run game jam
JavaScript
5
star
85

ruff

little tool to help my dad quickly find info in a .csv file
C++
5
star
86

gbremote

Groove Basin remote control command line app and Node.js module
JavaScript
5
star
87

planetarius

Ludum Dare 30 Entry. networked multiplayer arcade shooter
JavaScript
5
star
88

archerbot

mineflayer bot that engages you in a gentlemanly duel
JavaScript
5
star
89

holocaust

html5 video game - rebuild society after a nuclear holocaust ravages the world
JavaScript
4
star
90

Camlift-Controller

Controls a Canon camera and operates a motorized lift
Visual Basic
4
star
91

swig-dummy-context

given a swig template, create a dummy context which is useful for template composing tools
JavaScript
4
star
92

node-passthrough-truncate

truncate the last N bytes of a stream - Node.js module
JavaScript
3
star
93

scrabble

Scrabble solving AI
3
star
94

spacefight

vaporware 3D space-dogfighting simulator game
C++
3
star
95

math3d-rs

computer-graphics matrix calculations for dummies like me
Rust
3
star
96

lemming-js

PyWeek #12 entry ported to JavaScript with chem
JavaScript
3
star
97

pypowerusb

Python library to control a PowerUSB
C
3
star
98

opengl-multi-window-test

see if multiple windows in opengl causes a framerate issue
C
3
star
99

disinfecticide

A game about controlling a disease outbreak.
JavaScript
2
star
100

socketio-ssl-test

test whether we can use socket.io with xhr requests securely on an insecure page
JavaScript
2
star