Skip to content

Commit

Permalink
add Spinner#stop()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 14, 2013
1 parent cd5e2a8 commit 683c592
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Readme.md
Expand Up @@ -43,6 +43,12 @@ document.body.appendChild(spinner.el);


Change the speed to `n` rpm, defaults to 60. Change the speed to `n` rpm, defaults to 60.


### Spinner#stop()

Stop the animation. This is important when removing
the spinner, otherwise the requestAnimationFrame cycle
will continue.

## License ## License


MIT MIT
14 changes: 13 additions & 1 deletion index.js
Expand Up @@ -17,22 +17,34 @@ module.exports = Spinner;
*/ */


function Spinner() { function Spinner() {
var self = this;
this.percent = 0; this.percent = 0;
this.el = document.createElement('canvas'); this.el = document.createElement('canvas');
this.ctx = this.el.getContext('2d'); this.ctx = this.el.getContext('2d');
this.size(50); this.size(50);
this.fontSize(11); this.fontSize(11);
this.speed(60); this.speed(60);
this.font('helvetica, arial, sans-serif'); this.font('helvetica, arial, sans-serif');
this.stopped = false;


var self = this;
(function animate() { (function animate() {
if (self.stopped) return;
raf(animate); raf(animate);
self.percent = (self.percent + self._speed / 36) % 100; self.percent = (self.percent + self._speed / 36) % 100;
self.draw(self.ctx); self.draw(self.ctx);
})(); })();
} }


/**
* Stop the animation.
*
* @api public
*/

Spinner.prototype.stop = function(){
this.stopped = true;
};

/** /**
* Set spinner size to `n`. * Set spinner size to `n`.
* *
Expand Down
3 changes: 3 additions & 0 deletions test/index.html
Expand Up @@ -57,6 +57,9 @@
var spinner = new Spinner; var spinner = new Spinner;
spinner.size(25).speed(30).light(); spinner.size(25).speed(30).light();
document.querySelector('.dark').appendChild(spinner.el); document.querySelector('.dark').appendChild(spinner.el);
setTimeout(function(){
spinner.stop();
}, 2000);
})(); })();


</script> </script>
Expand Down

0 comments on commit 683c592

Please sign in to comment.