From 8efae01677fd0684995a788734e457552a4fb950 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Tue, 6 Sep 2011 14:28:52 -0500 Subject: [PATCH] Add HexaSpinner hack --- Main.js | 1 + depends.js | 1 + hacks/HexaSpinner/HexaSpinner.js | 138 +++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 hacks/HexaSpinner/HexaSpinner.js diff --git a/Main.js b/Main.js index 63c46b6..2ec09a8 100644 --- a/Main.js +++ b/Main.js @@ -16,6 +16,7 @@ enyo.kind({ ], hacksList: [ {name: "Nimbus", kind: "Nimbus", dark: false}, + {name: "HexaSpinner", kind: "HexaSpinnerHack", dark: false}, {name: "Landscape", kind: "Landscape", dark: false}, {name: "Munch", kind: "Munch", dark: true}, {name: "Orbit", kind: "Orbit", dark: false}, diff --git a/depends.js b/depends.js index 6e775a3..5a1a9df 100644 --- a/depends.js +++ b/depends.js @@ -4,6 +4,7 @@ enyo.depends( //"HacksSelector.js", "hacks/Hack.js", "hacks/Nimbus/Nimbus.js", + "hacks/HexaSpinner/HexaSpinner.js", "hacks/Landscape/Landscape.js", "hacks/Munch/Munch.js", "hacks/Orbit/Orbit.js", diff --git a/hacks/HexaSpinner/HexaSpinner.js b/hacks/HexaSpinner/HexaSpinner.js new file mode 100644 index 0000000..4fc1c30 --- /dev/null +++ b/hacks/HexaSpinner/HexaSpinner.js @@ -0,0 +1,138 @@ +enyo.kind({ + name: "Point", + constructor: function(x, y) { + this.x = x; + this.y = y; + }, + add: function(p) { + this.x += p.x; + this.y += p.y; + } +}); + +enyo.kind({ + name: "RandomPoint", + kind: "Point", + constructor: function(x, y, w, h) { + this.x = Math.random() * w + x; + this.y = Math.random() * h + y; + } +}); + +enyo.kind({ + name: "HexaSpinner", + constructor: function(position, velocity, scale, color, rate) { + this.position = position; + this.velocity = velocity; + this.scale = scale; + this.color = color; + if (rate) { + this.rate = rate; + } else { + this.rate = Math.PI / 40; + } + this.rotation = 0; + }, + update: function(bb) { + this.position.add(this.velocity); + if (this.position.x > bb.right) { + this.position.x -= (this.position.x - bb.right) * 2; + this.velocity.x *= -1; + } else if (this.position.x < bb.left) { + this.position.x -= (this.position.x - bb.left) * 2; + this.velocity.x *= -1; + } + if (this.position.y > bb.bottom) { + this.position.y -= (this.position.y - bb.bottom) * 2; + this.velocity.y *= -1; + } else if (this.position.y < bb.top) { + this.position.y -= (this.position.y - bb.top) * 2; + this.velocity.y *= -1; + } + }, + draw: function(ctx) { + ctx.save(); + ctx.translate(this.position.x, this.position.y); + ctx.rotate(this.rotation); + ctx.scale(this.scale, this.scale); + ctx.fillStyle = this.color; + for (i = 0; i < 6; i++) { + ctx.fillRect(-10, -50, 20, 34); + ctx.rotate(2 * Math.PI / 6); + } + ctx.restore(); + this.rotation += this.rate; + if (this.rotation > Math.PI * 2) + this.rotation -= Math.PI * 2; + else if (this.rotation < 0) + this.rotation += Math.PI * 2; + } +}); + +enyo.kind({ + name: "HexaSpinnerHack", + kind: "Hack", + style: "background-color: white", + components: [ + {name: "canvas", nodeTag: "canvas"} + ], + rendered: function() { + this.canvas = this.$.canvas.hasNode(); + this.ctx = this.canvas.getContext('2d'); + this.resize(window.innerWidth, window.innerHeight); + + this.spinners = []; + this.spinners.push(new HexaSpinner( + new RandomPoint(0, 0, this.w, this.h), + new RandomPoint(-0.5, -0.5, 1.0, 1.0), + 18.0, 'black', -Math.PI / 150)); + this.spinners.push(new HexaSpinner( + new RandomPoint(0, 0, this.w, this.h), + new RandomPoint(-0.8, -0.8, 1.6, 1.6), + 12.0, 'white', Math.PI / 120)); + this.spinners.push(new HexaSpinner( + new RandomPoint(0, 0, this.w, this.h), + new RandomPoint(-0.9, -0.9, 1.8, 1.8), + 9.0, 'black', -Math.PI / 140)); + this.spinners.push(new HexaSpinner( + new RandomPoint(0, 0, this.w, this.h), + new RandomPoint(-0.5, -0.5, 1.0, 1.0), + 4.0, 'white', -Math.PI / 100)); + this.spinners.push(new HexaSpinner( + new RandomPoint(0, 0, this.w, this.h), + new RandomPoint(-1.2, -1.2, 2.4, 2.4), + 3.0, 'black')); + this.spinners.push(new HexaSpinner( + new RandomPoint(0, 0, this.w, this.h), + new RandomPoint(-1.8, -1.8, 3.2, 3.2), + 1.8, 'white', -Math.PI / 30)); + + this.engine(); + }, + start: function() { + if (!this.timer) + this.timer = setInterval(this.engine.bind(this), 30); + }, + stop: function() { + clearInterval(this.timer); + this.timer = null; + }, + resize: function(w, h) { + this.canvas.width = this.w = w; + this.canvas.height = this.h = h; + this.bb = { + left: 50, + right: w - 50, + top: 50, + bottom: h - 50 + }; + }, + engine: function() { + this.ctx.fillStyle = 'white'; + this.ctx.fillRect(0, 0, this.w, this.h); + for (i in this.spinners) { + this.spinners[i].update(this.bb); + this.spinners[i].draw(this.ctx); + } + } +}); -- 2.34.1