Candybar 🍫 ![Build Status](https://travis-ci.org/gushers/candybar.svg?branch=master)
A simple <canvas>
rendering engine and collection of classes and utils. And by "engine" I mean about as advanced as a broken scooter 🛴 on fire 🔥
🚨 This is an alpha release
Really, don't use this thing for anything yet.
Getting started
First, install it
yarn add @gush/candybar
then create a canvas
import { Canvas } from '@gush/candybar';
const canvas = new Canvas({
canvas: document.getElementById('canvas'),
});
which will leave you with a beautiful blank canvas. 🙌🏻
Canvas options
Canvas classes can be created with a few options:
canvas
container
hasPointer
entities
pauseInBackground
new Canvas({
canvas: document.getElementById('canvas'),
container: document.getElementById('container'),
hasPointer: true,
pauseInBackground: true,
entities: [...things],
});
Entities
The engine will loop through each entity and call a couple methods to "render" the entity. First a draw()
method is called where you should do any drawing using the HTML canvas API. Then an update()
method is called which should be used to update any properties based on the engine.
class MyThing {
constructor() {
this.x = 0;
this.y = 0;
}
setup = ({ canvas }) => {
this.w = canvas.width / 10;
window.addEventListener('resize', handleResize);
};
destroy = () => {
window.removeEventListener('resize', handleResize);
};
draw = ({ ctx }) => {
ctx.fillRect(this.x, this.y, 100, 100);
};
update = ({ pointer }) => {
const { x, y } = this.pointer.position;
this.x = x;
this.y = y;
};
resize = (context, event) => {
this.w = context.canvas.width / 10;
};
}