From 6bddca5788ca0aa27b3bf8d2cbf8973e554e20d1 Mon Sep 17 00:00:00 2001 From: James Liu Date: Sun, 24 May 2015 03:06:22 +0800 Subject: [PATCH] v2.1.3: fix 2xcall acc bug; suspend/resume feature --- README.md | 12 ++++++++++++ damoo.js | 36 +++++++++++++++++++++++++++++++----- damoo.min.js | 15 ++++++++------- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1a4d6f9..2b7b5d8 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,18 @@ Clear the screen. damoo.clear(); ``` +If needed, you may suspend the animation. + +```javascript +damoo.suspend(); +``` + +And resume it when ready to go. + +```javascript +damoo.resume(); +``` + Enjoy! And explore more of the code! :) Contributing diff --git a/damoo.js b/damoo.js index bc48af6..214efdf 100644 --- a/damoo.js +++ b/damoo.js @@ -1,5 +1,5 @@ /*! - * Damoo - HTML5 Danmaku Engine v2.1.2 + * Damoo - HTML5 Danmaku Engine v2.1.3 * https://github.com/jamesliu96/Damoo * * Copyright (c) 2015 James Liu @@ -12,9 +12,11 @@ } this.canvas = new Canvas(m, n, r, t); this.thread = new Thread(r); + this.afid = null; + this.state = null; }; - Damoo.version = "v2.1.2"; + Damoo.version = "v2.1.3"; Damoo.dom = window.document; @@ -44,6 +46,14 @@ window.oRequestAnimationFrame || function(cb) { setTimeout(cb, 17); }; + var _CAF = window.cancelAnimationFrame || + window.mozCancelAnimationFrame || + window.webkitCancelAnimationFrame || + window.webkitCancelRequestAnimationFrame || + window.msCancelAnimationFrame || + window.oCancelAnimationFrame || + function(id) { clearTimeout(id); }; + Damoo.prototype.show = function() { this.canvas.parent.appendChild(this.canvas.layer); }; @@ -70,7 +80,7 @@ this.thread.empty(); }; - Damoo.prototype.start = function() { + var _render = function() { this.canvas.clear(); for (var i = 0; i < this.thread.length; i++) { var d = this.thread.get(i), @@ -82,13 +92,29 @@ this.thread.remove(i); } } - _RAF(function(self) { + this.afid = _RAF(function(self) { return function() { - self.start(); + _render.call(self); }; }(this)); }; + Damoo.prototype.start = function() { + if (!this.state) { + _render.call(this); + this.state = 1; + } + }; + + Damoo.prototype.suspend = function() { + _CAF(this.afid); + this.state = 0; + }; + + Damoo.prototype.resume = function() { + this.start(); + }; + var Canvas = function(m, n, r, t) { this.parent = Damoo.dom.getElementById(m); this.parent.style.position = "relative"; diff --git a/damoo.min.js b/damoo.min.js index ed62a01..28a6b05 100644 --- a/damoo.min.js +++ b/damoo.min.js @@ -1,7 +1,8 @@ -/*! Damoo v2.1.2 | Copyright (c) 2015 James Liu | Released under the MIT license */ -;(function(f){var e=function(a,b,c,d){if(!(this instanceof e))return new e(a,b,c,d);this.canvas=new h(a,b,c,d);this.thread=new g(c)};e.version="v2.1.2";e.dom=f.document;var l=f.requestAnimationFrame||f.mozRequestAnimationFrame||f.webkitRequestAnimationFrame||f.msRequestAnimationFrame||f.oRequestAnimationFrame||function(a){setTimeout(a,17)};e.prototype.show=function(){this.canvas.parent.appendChild(this.canvas.layer)};e.prototype.hide=function(){this.canvas.parent.removeChild(this.canvas.layer)};e.prototype.emit= -function(a){var b=this.canvas.font,c=e.dom.createElement("canvas"),d=c.getContext("2d");c.width=b.size*a.text.length*1.2;c.height=1.2*b.size;d.font=b.value;d.textAlign="start";d.textBaseline="top";a.shadow&&(d.fillStyle="#fff",d.fillStyle=a.shadow.color,d.fillText(a.text,1,1));d.fillStyle="#fff";d.fillStyle=a.color;d.fillText(a.text,0,0);this.thread.push({canvas:c,fixed:a.fixed,index:this.thread.index,speed:.6*Math.pow(c.width,1/3),offset:{x:this.canvas.width,y:this.canvas.font.size*this.thread.index}})}; -e.prototype.clear=function(){this.thread.empty()};e.prototype.start=function(){this.canvas.clear();for(var a=0;athis.height/this.rows&&(this.rows=this.height/12);this.width= -this.parent.offsetWidth;this.height=this.parent.offsetHeight;this.font=new k(this.height/this.rows,d||"sans-serif");this.layer=e.dom.createElement("canvas");this.context=this.layer.getContext("2d");this.layer.id=this.id;this.layer.width=this.width;this.layer.height=this.height;this.layer.style.display="block";this.layer.style.backgroundColor="transparent";this.layer.style.position="absolute";this.layer.style.left=0;this.layer.style.top=0;this.layer.style.zIndex=99999};h.prototype.clear=function(){this.context.clearRect(0, -0,this.width,this.height)};h.prototype.draw=function(a,b,c){a.fixed?this.context.drawImage(a.canvas,(this.width-a.canvas.width)/2+.5|0,c+.5|0):this.context.drawImage(a.canvas,b+.5|0,c+.5|0)};var k=function(a,b){this.size=a;this.family=b};Object.defineProperty(k.prototype,"value",{get:function(){return this.size+"px "+this.family}});var g=function(a){this.index=0;this.rows=a;this.pool=[]};g.prototype.push=function(a){this.index++;this.index>=this.rows&&(this.index=0);this.pool.push(a)};g.prototype.get= -function(a){return this.pool[a]};g.prototype.remove=function(a){var b=this.get(a).index;this.index>b&&(this.index=b);this.pool.splice(a,1)};g.prototype.empty=function(){this.index=0;this.pool=[]};Object.defineProperty(g.prototype,"length",{get:function(){return this.pool.length}});f.Damoo=e})(window); \ No newline at end of file +/*! Damoo v2.1.3 | Copyright (c) 2015 James Liu | Released under the MIT license */ +;(function(f){var d=function(a,b,c,e){if(!(this instanceof d))return new d(a,b,c,e);this.canvas=new h(a,b,c,e);this.thread=new g(c);this.state=this.afid=null};d.version="v2.1.3";d.dom=f.document;var m=f.requestAnimationFrame||f.mozRequestAnimationFrame||f.webkitRequestAnimationFrame||f.msRequestAnimationFrame||f.oRequestAnimationFrame||function(a){setTimeout(a,17)},n=f.cancelAnimationFrame||f.mozCancelAnimationFrame||f.webkitCancelAnimationFrame||f.webkitCancelRequestAnimationFrame||f.msCancelAnimationFrame|| +f.oCancelAnimationFrame||function(a){clearTimeout(a)};d.prototype.show=function(){this.canvas.parent.appendChild(this.canvas.layer)};d.prototype.hide=function(){this.canvas.parent.removeChild(this.canvas.layer)};d.prototype.emit=function(a){var b=this.canvas.font,c=d.dom.createElement("canvas"),e=c.getContext("2d");c.width=b.size*a.text.length*1.2;c.height=1.2*b.size;e.font=b.value;e.textAlign="start";e.textBaseline="top";a.shadow&&(e.fillStyle="#fff",e.fillStyle=a.shadow.color,e.fillText(a.text, +1,1));e.fillStyle="#fff";e.fillStyle=a.color;e.fillText(a.text,0,0);this.thread.push({canvas:c,fixed:a.fixed,index:this.thread.index,speed:.6*Math.pow(c.width,1/3),offset:{x:this.canvas.width,y:this.canvas.font.size*this.thread.index}})};d.prototype.clear=function(){this.thread.empty()};var k=function(){this.canvas.clear();for(var a=0;athis.height/this.rows&&(this.rows=this.height/12);this.width=this.parent.offsetWidth;this.height=this.parent.offsetHeight;this.font=new l(this.height/this.rows, +e||"sans-serif");this.layer=d.dom.createElement("canvas");this.context=this.layer.getContext("2d");this.layer.id=this.id;this.layer.width=this.width;this.layer.height=this.height;this.layer.style.display="block";this.layer.style.backgroundColor="transparent";this.layer.style.position="absolute";this.layer.style.left=0;this.layer.style.top=0;this.layer.style.zIndex=99999};h.prototype.clear=function(){this.context.clearRect(0,0,this.width,this.height)};h.prototype.draw=function(a,b,c){a.fixed?this.context.drawImage(a.canvas, +(this.width-a.canvas.width)/2+.5|0,c+.5|0):this.context.drawImage(a.canvas,b+.5|0,c+.5|0)};var l=function(a,b){this.size=a;this.family=b};Object.defineProperty(l.prototype,"value",{get:function(){return this.size+"px "+this.family}});var g=function(a){this.index=0;this.rows=a;this.pool=[]};g.prototype.push=function(a){this.index++;this.index>=this.rows&&(this.index=0);this.pool.push(a)};g.prototype.get=function(a){return this.pool[a]};g.prototype.remove=function(a){var b=this.get(a).index;this.index> +b&&(this.index=b);this.pool.splice(a,1)};g.prototype.empty=function(){this.index=0;this.pool=[]};Object.defineProperty(g.prototype,"length",{get:function(){return this.pool.length}});f.Damoo=d})(window); \ No newline at end of file