1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- function BoxGame() {
- var container = null;
- var canvas = null;
- var contextType = '2d';
- var contextAttributes = {};
- var context = null;
- var game = null;
- var boxes = [];
- this.create = function(elementName) {
- game = this;
- container = document.getElementById(elementName);
- canvas = container.getElementsByTagName('canvas')[0];
- window.addEventListener('resize', game.handleResize);
- canvas.addEventListener('mousemove', game.handleMouseMove);
- window.requestAnimationFrame(game.handleRequestAnimationFrame);
- this.handleResize(null);
- };
- this.handleResize = function(event) {
- canvas.width = container.offsetWidth;
- canvas.height = container.offsetHeight;
- context = canvas.getContext(contextType, contextAttributes);
- context.translate(0.5, 0.5);
- game.init(canvas);
- }
- this.handleRequestAnimationFrame = function(event) {
- game.update(canvas);
- context.fillStyle = "#FFFFFF";
- context.fillRect(0 ,0, canvas.width, canvas.height);
- game.draw(context);
- window.requestAnimationFrame(game.handleRequestAnimationFrame);
- }
- this.handleMouseMove = function(event) {
- game.mouseMove(canvas, event.offsetX, event.offsetY);
- }
- this.init = function(canvas) {
- boxes = [];
- var boxDensity = Math.floor((canvas.width * canvas.height) / 2000);
- for(var i = 0; i < boxDensity; i++) {
- boxes.push(new Box());
- }
- /*for(var i = 0; i < 100; i++) {
- boxes.push(new Circle());
- }*/
-
- /*for(var i = 0; i < boxDensity; i++) {
- boxes.push(new BinaryFloater());
- }*/
- /*for(var i = 0; i < 25; i++) {
- boxes.push(new Triangle());
- }*/
- /*for(var i = 0; i < 25; i++) {
- boxes.push(new StarShape());
- }*/
- for(var i = 0, box = boxes[0]; box = boxes[i]; i++) {
- box.init(canvas);
- }
- };
- this.update = function(canvas) {
- for(var i = 0, box = boxes[0]; box = boxes[i]; i++) {
- box.update(canvas);
- }
- };
- this.draw = function(context) {
- for(var i = 0, box = boxes[0]; box = boxes[i]; i++) {
- box.draw(context);
- }
- };
- this.mouseMove = function(canvas, x, y) {
- };
- }
|