xonecas.io
2495
Xg

xonecas.github.com / commit e43b7f5

tweaking with the canvas

xonecas2013-03-26 e43b7f526e6a103bac65c7f4dbc65ae6b22f3ed9 parent 06233f1

1 file changed

js/rings.js+67−24
@@ -7,6 +7,42 @@ var rings = (function (win, doc) {
77 width = window.innerWidth,
88 height = window.innerHeight;
99
10+ // Opera engineer Erik Möller wrote about rAF and developed a polyfill that
11+ // better handles browsers without native support. You can read about it, but
12+ // basically his code will choose a delay of between 4ms and 16ms in order to
13+ // more closely match 60fps. Here it is, in case you’d like to use it. Note it
14+ // uses the standard method name. I have also fixed the cancel* method’s name,
15+ // as it has changed in WebKit.
16+ //
17+ // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
18+ (function() {
19+ var lastTime = 0,
20+ vendors = ['webkit', 'moz'],
21+ x;
22+
23+ for(x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
24+ window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
25+ window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] ||
26+ window[vendors[x]+'CancelRequestAnimationFrame'];
27+ }
28+
29+ if (!window.requestAnimationFrame) {
30+ window.requestAnimationFrame = function(callback, element) {
31+ var currTime = new Date().getTime(),
32+ timeToCall = Math.max(0, 16 - (currTime - lastTime)),
33+ id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall);
34+ lastTime = currTime + timeToCall;
35+ return id;
36+ };
37+ }
38+
39+ if (!window.cancelAnimationFrame) {
40+ window.cancelAnimationFrame = function(id) {
41+ clearTimeout(id);
42+ };
43+ }
44+ }());
45+
1046 //random range
1147 function _rr(min, max) {
1248 return Math.random() * (max - min) + min;
@@ -79,8 +115,8 @@ var rings = (function (win, doc) {
79115 canvas.height = height;
80116 body.appendChild(canvas);
81117 body.addEventListener('mousemove', function (ev) {
82- // only make rings 50% of the time
83- if (Math.random() > 0.5) { return; }
118+ // only make rings 30% of the time
119+ if (Math.random() > 0.333) { return; }
84120 hue = hue + 1 === 360 ? 1 : hue + 1;
85121 var ring = new Ring({
86122 x: ev.clientX,
@@ -90,33 +126,40 @@ var rings = (function (win, doc) {
90126 rings.push(ring);
91127 }, false);
92128
93- setInterval(function () {
94- var len,
95- idx,
96- _rings = [],
97- _ring;
129+ function draw() {
130+ setTimeout(function () {
131+ window.requestAnimationFrame(draw);
98132
99- c.save();
100- c.fillStyle = "rgba(0, 0, 0, 0.01)";
101- c.fillRect(0, 0, width, height);
102- c.restore();
133+ var len,
134+ idx,
135+ _rings = [],
136+ _ring;
103137
104- if (rings.length === 0) {
105- return;
106- }
138+ c.save();
139+ c.fillStyle = "rgba(0, 0, 0, 0.5)";
140+ c.fillRect(0, 0, width, height);
141+ c.restore();
107142
108- for (idx = 0, len = rings.length; idx < len; idx += 1) {
109- _ring = rings[idx];
110- if (!_ring.destroy) {
111- _rings.push(_ring);
143+ if (rings.length === 0) {
144+ return;
112145 }
113- _ring
114- .update()
115- .draw();
116- }
117146
118- rings = _rings;
119- }, 1000 / 60);
147+ for (idx = 0, len = rings.length; idx < len; idx += 1) {
148+ _ring = rings[idx];
149+ if (!_ring.destroy) {
150+ _rings.push(_ring);
151+ }
152+ _ring
153+ .update()
154+ .draw();
155+ }
156+
157+ rings = _rings;
158+ }, 1000 / 60);
159+ }
160+
161+ draw();
162+
120163 }
121164
122165 win.onload = init;