xonecas.io
212
Ca

canvas-rings / files

canvas-rings / script.js

2.7 KB
1/*jshint expr: true, onevar: true */
2var rings = !function (win, doc) {
3   var
4      body     = doc.querySelector('body'),
5      canvas   = doc.createElement('canvas'),
6      c        = canvas.getContext('2d'),
7      width    = window.innerWidth,
8      height   = window.innerHeight;
9
10   //random range
11   function _rr(min,max){
12      return Math.random()*(max-min) + min;
13   }
14   
15   //takes the values returns an hsla(...) css string
16   function _hsla (h, s, l, a) {
17      h = h || Math.floor(_rr(80, 200));
18      s = s || 100;
19      l = l || 50;
20      a = a || 1.0;
21
22      return "hsla("+ h +", "+ s +"%, "+ l +"%, "+ a +")";
23   }
24
25   window._hsla = _hsla;
26
27   //takes degrees returns radians
28   function _rad(deg) {
29      return deg * (Math.PI /180);
30   }
31
32   function Ring (options) {
33      options = options || {};
34
35      this.radius = 1;
36      this.max = 1;
37      this.counter = 0.1;
38      this.length = Math.floor(_rr(10, 300));
39      this.x = options.x || Math.floor(width /2);
40      this.y = options.y || Math.floor(height /2);
41      this.color = options.color || _hsla();
42
43      return this;
44   }
45
46   Ring.prototype = {
47      update: function () {
48         if (this.radius < 1) {
49            this.counter = 0.1;
50         }
51
52         this.radius = this.length * Math.sin(this.counter);
53         this.counter += 0.01;
54
55         if (this.radius < this.max) {
56            this.destroy = true;
57         } else {
58            this.max = this.radius;
59         }
60      },
61
62      draw: function () {
63         c.beginPath();
64         c.arc(this.x, this.y, this.radius, 0, _rad(360), true);
65         c.strokeStyle = this.color;
66         c.lineWidth = 2.5;
67         c.stroke();
68         c.closePath();
69      }
70   };
71
72   function init () {
73      var
74         rings = [],
75         hue = 1;
76
77      canvas.width = width;
78      canvas.height = height;
79      body.appendChild(canvas);
80      canvas.addEventListener('mousemove', function (ev) {
81         hue = hue+1 === 360 ? 1 : hue+1;
82         var ring = new Ring({ 
83            x:       ev.clientX, 
84            y:       ev.clientY,
85            color:   _hsla(hue)
86         });
87         rings.push(ring);
88         console.log(ring);
89      }, false);
90
91      window.rings = rings;
92
93      setInterval(function () {
94         c.save();
95         c.fillStyle = "hsla(1,1%,0%, 0.1)";
96         c.fillRect(0, 0, width, height);
97         c.restore();
98
99         if (rings.length === 0) {
100            return;
101         }
102
103         _.each(rings, function (ring) {
104            ring.update();
105            ring.draw();
106         });
107
108         rings = _.reject(rings, function (ring) {
109            return ring.destroy;
110         });
111      }, 1000/60);
112   }
113
114   init();
115
116} (window, document);