@@ -1,202 +1,212 @@
1-/*jshint browser:true, curly:true, eqeqeq:true, onevar:true */
1+/*jslint browser:true, nomen:true, devel:true */
22 (function (win, doc) {
3-
4- var body = doc.querySelector('body'),
5- canvas = doc.createElement('canvas'),
6- c = canvas.getContext('2d'),
7- radians = Math.PI /180,
8- redrawRain = doc.querySelector('#redraw'),
9- redrawSpiral= doc.querySelector('#redrawSpiral');
10-
11- body.appendChild(canvas);
12- canvas.width = win.innerWidth -10;
13- canvas.height = win.innerHeight -10;
14-
15- /* tool */
16- function rrange(min, max, noFloor){
17- var result = Math.random()*(max-min) + min;
18- return noFloor ? result : Math.floor(result);
19- }
20-
21- function Particle (x, y, size, color) {
22- this.x = x;
23- this.y = y;
24- this.size = size;
25- this.color = color || rrange(1, 359);
26- }
27-
28- Particle.prototype = {
29- draw: function () {
30- c.save();
31- c.fillStyle = "hsl("+ this.color +", 100%, 50%)";
32- c.beginPath();
33- c.arc(this.x, this.y, this.size, 0, Math.PI * 2, true);
34- c.fill();
35- c.restore();
36- }
37- };
38-
39- /* first routine */
40- function spiral () {
41- var particle,
42- particles = [],
43- maxParticles = parseInt(doc.querySelector('#spiralCount').value, 10) || 6000,
44- angle = 0,
45- size = parseInt(doc.querySelector('#spiralSize').value, 10) || 5,
46- x = 0,
47- y = 0,
48- xyModifier = parseFloat(doc.querySelector('#spiralPositionMod').value, 10) || 1.1,
49- color = 1;
50-
51- canvas.width = canvas.width;
52- canvas.height = canvas.height;
53-
54- c.save();
55- c.translate(canvas.width /2, canvas.height /2);
56-
57- while (--maxParticles > 0) {
58- angle += 2;
59- y += xyModifier;
60- x += xyModifier;
61- color = color >= 360 ? 1 : color +1;
62-
63- if (x >= canvas.width || y >= canvas.height || size <= 0) {
64- console.log(maxParticles -1000, x, y, size);
65- continue;
66- }
67-
68- c.save();
69- c.rotate(angle * radians);
70-
71- particle = new Particle(x, y, size, color);
72- particles.push(particle);
73- particle.draw();
74-
75- c.restore();
76- }
77-
78- c.restore();
79- }
80-
81- /* recursion play */
82- function fractal () {
83- var gens = 0,
84- max = 10,
85- size = 2,
86- startColor = rrange(260, 360);
87-
88- canvas.width = canvas.width;
89- canvas.height = canvas.height;
90-
91- c.save();
92- c.translate(canvas.width /2, canvas.height /2);
93- new Particle(0, 0, size, startColor).draw();
94-
95- (function _init (angle, color, distance, scale) {
96- gens++;
97- angle = angle || rrange(1, 90);
98-
99- c.save();
100- c.rotate(angle * radians);
101-
102- c.beginPath();
103- c.moveTo(0,0);
104- c.lineTo(distance, 0);
105-
106- c.strokeStyle = 'hsl('+color+', 100%, 50%)';
107- c.stroke();
108-
109- c.translate(distance, 0);
110- console.log(scale);
111- c.scale(scale, scale);
112-
113- if (gens < max) {
114- new Particle(0, 0, size, color).draw();
115-
116- color = color >= 360 ? 1 : color + 20 * gens;
117-
118- _init(rrange(1, 270), color, rrange(200, 300),
119- rrange(0.75, 1, true));
120-
121- _init(rrange(1, 270), color, rrange(200, 300),
122- rrange(0.75, 1, true));
123-
124- _init(rrange(1, 270), color, rrange(200, 300),
125- rrange(0.75, 1, true));
126-
127- _init(rrange(1, 270), color, rrange(200, 300),
128- rrange(0.75, 1, true));
129- }
130-
131- if (gens === max) {
132- new Particle(0, 0, size, color).draw();
133- }
134-
135- c.restore();
136- gens--;
137- }) (0, startColor, 0, 1);
138-
139- c.restore();
140- }
141-
142- function rain () {
143- var dash,
144- dashes = [],
145- max = parseInt(doc.querySelector('#dropCount').value, 10),
146- startColor = parseInt(doc.querySelector('#startColor').value, 10) || 180,
147- endColor = parseInt(doc.querySelector('#endColor').value, 10) || 200,
148- startLeap = parseInt(doc.querySelector('#startLeap').value, 10) || 0.5,
149- endLeap = parseInt(doc.querySelector('#endLeap').value, 10) || 3;
150-
151- canvas.width = canvas.width;
152- canvas.height = canvas.height;
153- if (win.interval) {
154- clearInterval(win.interval);
155- }
156-
157- win.interval = setInterval(function () {
158- var i = 0;
159-
160- c.save();
161- c.fillStyle = 'hsla(0, 0%, 0%, 0.2)';
162- c.fillRect(0, 0, canvas.width, canvas.height);
163- c.restore();
164-
165- if (dashes.length <= max) {
166- dash = new Particle( rrange(1, canvas.width, true),
167- rrange(1, canvas.height, true),
168- rrange(startLeap, endLeap, true),
169- rrange(startColor, endColor));
170- dashes.push(dash);
171- }
172-
173- for (i; i < dashes.length; i++) {
174- dash = dashes[i];
175-
176- dash.y = dash.y >= canvas.height ? rrange(1, canvas.height/2):
177- dash.y + rrange(1, 6);
178-
179- dash.draw();
180- }
181- }, 1000/30);
182- }
183-
184- //spiral();
185- fractal();
186- //rain();
187-
188- redrawRain.addEventListener('click', function (ev) {
189- ev.preventDefault();
190- rain();
191- }, false);
192-
193- redrawSpiral.addEventListener('click', function (ev) {
194- ev.preventDefault();
195- if (win.interval) {
196- clearInterval(win.interval);
197- }
198- spiral();
199- }, false);
200-
201-
202-}) (window, document);
3+ 'use strict';
4+
5+ var body = doc.querySelector('body'),
6+ canvas = doc.createElement('canvas'),
7+ c = canvas.getContext('2d'),
8+ radians = Math.PI / 180,
9+ redrawRain = doc.querySelector('#redraw'),
10+ redrawSpiral = doc.querySelector('#redrawSpiral');
11+
12+ body.appendChild(canvas);
13+ canvas.width = win.innerWidth - 10;
14+ canvas.height = win.innerHeight - 10;
15+
16+ /* tool */
17+ function rrange(min, max, noFloor) {
18+ var result = Math.random() * (max - min) + min;
19+ return noFloor ? result : Math.floor(result);
20+ }
21+
22+ function Particle(x, y, size, color) {
23+ this.x = x;
24+ this.y = y;
25+ this.size = size;
26+ this.color = color || rrange(1, 359);
27+ }
28+
29+ Particle.prototype = {
30+ draw: function () {
31+ c.save();
32+ c.fillStyle = "hsl(" + this.color + ", 100%, 50%)";
33+ c.beginPath();
34+ c.arc(this.x, this.y, this.size, 0, Math.PI * 2, true);
35+ c.fill();
36+ c.restore();
37+ }
38+ };
39+
40+ /* first routine */
41+ function spiral() {
42+ var particle,
43+ particles = [],
44+ maxParticles = parseInt(doc.querySelector('#spiralCount').value, 10) || 6000,
45+ angle = 0,
46+ size = parseInt(doc.querySelector('#spiralSize').value, 10) || 5,
47+ x = 0,
48+ y = 0,
49+ xyModifier = parseFloat(doc.querySelector('#spiralPositionMod').value, 10) || 1.1,
50+ color = 180;
51+
52+ canvas.width = canvas.width;
53+ canvas.height = canvas.height;
54+
55+ c.save();
56+ c.translate(canvas.width / 2, canvas.height / 2);
57+
58+ while (--maxParticles > 0) {
59+ angle += 2;
60+ y += xyModifier;
61+ x += xyModifier;
62+ color = color >= 360 ? 180 : color + 1;
63+
64+ if (!(x >= canvas.width || y >= canvas.height || size <= 0)) {
65+ c.save();
66+ c.rotate(angle * radians);
67+
68+ particle = new Particle(x, y, size, color);
69+ particles.push(particle);
70+ particle.draw();
71+
72+ c.restore();
73+ }
74+ }
75+
76+ c.restore();
77+ }
78+
79+ /* recursion play */
80+ function fractal() {
81+ var gens = 0,
82+ max = 5,
83+ size = 4,
84+ startColor = rrange(260, 360);
85+
86+ canvas.width = canvas.width;
87+ canvas.height = canvas.height;
88+
89+ c.save();
90+ c.translate(canvas.width / 2, canvas.height / 2);
91+ new Particle(0, 0, size, startColor).draw();
92+
93+ (function _init(angle, color, distance, scale) {
94+ gens++;
95+ angle = angle || rrange(1, 90);
96+
97+ c.save();
98+ c.rotate(angle * radians);
99+ c.beginPath();
100+ c.moveTo(0, 0);
101+ c.lineTo(distance, 0);
102+
103+ c.strokeStyle = 'hsl(' + color + ', 100%, 50%)';
104+ c.stroke();
105+
106+ c.translate(distance, 0);
107+ console.log(scale);
108+ c.scale(scale, scale);
109+
110+ if (gens < max) {
111+ new Particle(0, 0, size, color).draw();
112+
113+ color = color >= 360 ? 1 : color + 20 * gens;
114+
115+ _init(rrange(1, 270), color, rrange(200, 300),
116+ rrange(0.75, 1, true));
117+
118+ _init(rrange(1, 270), color, rrange(200, 300),
119+ rrange(0.75, 1, true));
120+ }
121+
122+ if (gens === max) {
123+ new Particle(0, 0, size, color).draw();
124+ }
125+ c.restore();
126+ gens--;
127+ }(0, startColor, 0, 1));
128+
129+ c.restore();
130+ }
131+
132+ function rain() {
133+ var dash,
134+ dashes = [],
135+ max = parseInt(doc.querySelector('#dropCount').value, 10),
136+ startColor = parseInt(doc.querySelector('#startColor').value, 10) || 180,
137+ endColor = parseInt(doc.querySelector('#endColor').value, 10) || 200,
138+ startLeap = parseInt(doc.querySelector('#startLeap').value, 10) || 0.5,
139+ endLeap = parseInt(doc.querySelector('#endLeap').value, 10) || 3;
140+
141+ canvas.width = canvas.width;
142+ canvas.height = canvas.height;
143+
144+ if (win.interval) {
145+ clearInterval(win.interval);
146+ }
147+
148+ win.interval = setInterval(function () {
149+ var i = 0;
150+
151+ c.save();
152+ c.fillStyle = 'hsla(0, 0%, 0%, 0.2)';
153+ c.fillRect(0, 0, canvas.width, canvas.height);
154+ c.restore();
155+
156+ if (dashes.length <= max) {
157+ dash = new Particle(rrange(1, canvas.width, true),
158+ rrange(1, canvas.height, true),
159+ rrange(startLeap, endLeap, true),
160+ rrange(startColor, endColor));
161+
162+ dashes.push(dash);
163+ }
164+
165+ for (i; i < dashes.length; i++) {
166+ dash = dashes[i];
167+ dash.y = dash.y >= canvas.height ?
168+ rrange(1, canvas.height / 2) :
169+ dash.y + rrange(1, 6);
170+
171+ dash.draw();
172+ }
173+ }, 1000 / 30);
174+ }
175+
176+ //spiral();
177+ //fractal();
178+ //rain();
179+
180+ window.spiral = spiral;
181+ window.fractal = fractal;
182+ window.rain = rain;
183+
184+ window.help = function () {
185+ console.log([
186+ 'Welcome to particle playground!',
187+ 'There are 3 functions for you to use:',
188+ ' spiral();',
189+ ' fractal();',
190+ ' rain();',
191+ 'No arguments (comming soon)',
192+ '',
193+ 'You can see this prompt by calling help();'
194+ ].join('\n'));
195+ };
196+ window.help();
197+
198+ redrawRain.addEventListener('click', function (ev) {
199+ ev.preventDefault();
200+ rain();
201+ }, false);
202+
203+ redrawSpiral.addEventListener('click', function (ev) {
204+ ev.preventDefault();
205+ if (win.interval) {
206+ clearInterval(win.interval);
207+ }
208+ spiral();
209+ }, false);
210+
211+
212+}(window, document));