var width = 400, height = 300; Crafty.init(width, height); Crafty.c("Box", { init: function() { this.addComponent("2D, Canvas, Color"); }, makeBox: function(color){ var x = Math.random() * 100 >> 0; var y = Math.random() * 100 >> 0; this.attr({x: x, y: y, w: 20, h: 20}).color(color); return this; } }); $("#addBox").bind("click", function(){ var box = Crafty.e("Box").makeBox("#FF0000"); });
<h1>Demo 3: Crafty.c Entity Instances</h1> <button id="addBox">Add Box</button> <div id="cr-stage"></div>