From 7ce7b84383b85ff2e2bfd72e9e12665b7d350f3d Mon Sep 17 00:00:00 2001 From: Igor Babuschkin Date: Thu, 7 Aug 2014 10:24:15 +0200 Subject: [PATCH] Take duration into account when fading out event --- js/detector.js | 16 ++++++++++++---- js/event.js | 7 ++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/js/detector.js b/js/detector.js index b6959fb..95d1258 100644 --- a/js/detector.js +++ b/js/detector.js @@ -75,10 +75,18 @@ var detector = } ], - animate: function() + lastRender: 0, + + animate: function(time) { + if (detector.lastRender == 0) { + detector.lastRender = typeof performance !== 'undefined' ? performance.now() : new Date().getTime(); + } + var duration = typeof time !== 'undefined' ? time - detector.lastRender : 16; + detector.lastRender = typeof performance !== 'undefined' ? performance.now() : new Date().getTime(); + requestAnimFrame(detector.animate); - detector.draw(); + detector.draw(duration); }, init: function() @@ -283,14 +291,14 @@ var detector = } }, - draw: function() + draw: function(duration) { detector.events.ctx.clearRect(0, 0, 400, 400); var del = -1; for (var e in detector.events.list) { if (detector.events.list[e].alpha > 0) { - detector.events.list[e].draw(); + detector.events.list[e].draw(duration); } else { del = detector.events.list[e].count; } diff --git a/js/event.js b/js/event.js index a21ee8a..7c4abe4 100644 --- a/js/event.js +++ b/js/event.js @@ -28,10 +28,10 @@ function ParticleEvent(type, count, external) break; } - this.draw(true); + this.draw(16, true); }; -ParticleEvent.prototype.draw = function(init) +ParticleEvent.prototype.draw = function(duration, init) { init = typeof init !== 'undefined' ? init : false; @@ -57,6 +57,7 @@ ParticleEvent.prototype.draw = function(init) ctx.restore(); if (!init) { - this.alpha -= 0.02; + this.alpha -= 0.02 / 16 * duration; } }; +