Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play/Pause + small performance improvement #5

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
<body>

<canvas id="zodiac"></canvas>
<div style="position: fixed; top: 0; left: 0;">
<button onClick="window.zodiac.play();">play</button>
<button onClick="window.zodiac.pause();">pause</button>
</div>

<script src="../zodiac.js"></script>
<script>

document.addEventListener('DOMContentLoaded', function () {

new Zodiac('zodiac', // HTMLCanvasElement or id
window.zodiac = new Zodiac('zodiac', // HTMLCanvasElement or id
{ //// OPTIONS
directionX: 0, // -1:left;0:random;1:right
directionY: -1, // -1:up;0:random;1:down
Expand Down
2 changes: 2 additions & 0 deletions minify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
closure-compiler -O ADVANCED --js zodiac.js --js_output_file zodiac.min.js --create_source_map zodiac.min.js.map
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "zodiac",
"version": "1.0.0",
"description": "rewrite of jnicol/particleground",
"main": "zodiac.min.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/FallingSnow/Zodiac.git"
},
"author": "indus (Stefan Keim)",
"contributors": [
"FallingSnow (Ayrton Sparling) <[email protected]>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/FallingSnow/Zodiac/issues"
},
"homepage": "https://github.com/FallingSnow/Zodiac#readme"
}
31 changes: 27 additions & 4 deletions zodiac.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
var Zodiac = (function () {
function Zodiac(canvas, options) {
var _this = this;
this.playing = false;
if (options === void 0) { options = {}; }
this.options = {
directionX: -1,
Expand All @@ -40,6 +41,7 @@ var Zodiac = (function () {
this.options[key] = options[key];
}
options = this.options;
options.linkDistanceSquared = options.linkDistance * options.linkDistance;
var ctx = this._ctx = canvas.getContext('2d', { alpha: !options.backgroundColor }), tilt = { x: 0, y: 0 }, _, w, h;
var update = function () {
if (options.backgroundColor) {
Expand Down Expand Up @@ -72,8 +74,8 @@ var Zodiac = (function () {
ctx.moveTo(x + p.r, y);
ctx.arc(x, y, p.r, 0, Math.PI * 2);
for (var j = i - 1; j >= 0; j--) {
var q = _[j], dx = q.x - p.x, dy = q.y - p.y, dist = Math.sqrt((dx * dx) + (dy * dy));
if (dist < options.linkDistance) {
var q = _[j], dx = q.x - p.x, dy = q.y - p.y, distSquared = (dx * dx) + (dy * dy);
if (distSquared < options.linkDistanceSquared) {
var x = p.x + p.dx, y = p.y + p.dy, x2 = q.x + q.dx, y2 = q.y + q.dy, a = Math.atan2(y2 - y, x2 - x), cos = Math.cos(a), sin = Math.sin(a);
x += p.r * cos;
y += p.r * sin;
Expand All @@ -87,7 +89,7 @@ var Zodiac = (function () {
;
ctx.stroke();
options.dotColor && ctx.fill();
requestAnimationFrame(update);
_this.animationRequest = requestAnimationFrame(update);
};
function onMousemove(ev) {
tilt.x = ev.pageX - window.innerWidth / 2;
Expand Down Expand Up @@ -133,11 +135,29 @@ var Zodiac = (function () {
ctx.lineWidth = options.linkWidth;
ctx.fillStyle = options.dotColor;
};
this['pause'] = function() {
if (_this.playing) {
cancelAnimationFrame(_this.animationRequest);
_this.playing = false;
}
};
this['play'] = function() {
if (!_this.playing) {
_this.animationRequest = requestAnimationFrame(update);
_this.playing = true;
}
}
this['destroy'] = function() {
_this.pause();
window.removeEventListener('resize', onResize);
document.removeEventListener('mousemove', onMousemove, false);
window.removeEventListener('deviceorientation', onOrientation, false);
}
window.addEventListener('resize', onResize, false);
document.addEventListener('mousemove', onMousemove, false);
window.addEventListener('deviceorientation', onOrientation, false);
onResize();
update();
this.play();
}
return Zodiac;
})();
Expand All @@ -163,3 +183,6 @@ var Zodiac = (function () {
clearTimeout(id);
};
}());

if (typeof module === "object" && module['exports'])
module['exports'] = Zodiac;
20 changes: 9 additions & 11 deletions zodiac.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading