xonecas.io
186
Ft

fractal-tree / files

fractal-tree / script.js

2.9 KB
1/* browser:true, onevar:true, eqeqeq:true, curly:true, maxlen:80 */
2
3(function () {
4
5   var body          = document.querySelector('body'),
6      depth          = document.querySelector('#depth'),
7      color          = document.querySelector('#color'),
8      redraw         = document.querySelector('#redraw'),
9      reset          = document.querySelector('#reset'),
10      canvas         = document.createElement('canvas'),
11      ctx            = canvas.getContext('2d'),
12      middle         = Math.floor(220 + window.innerWidth / 2),
13      maxGenerations = parseInt(localStorage.getItem('depth'), 10),
14      colorDefault   = parseInt(localStorage.getItem('color'), 10),
15      generation     = 0,
16      scale          = 0;
17
18
19   /* Defaults */
20   depth.value = maxGenerations  = isNaN(maxGenerations) ? 10 : maxGenerations;
21   color.value = colorDefault    = isNaN(colorDefault) ? 280 : colorDefault;
22
23   /* Redraw button listener */
24   redraw.addEventListener('click', function (ev) {
25      ev.preventDefault();
26
27      var depthValue = depth.value,
28         colorValue  = color.value;
29
30      if (depthValue > 14 || depthValue <= 0) {
31         return smoke.alert("Sorry, invalid depth. Keep it between 1 and 14");
32      } else if (colorValue < 100 || colorValue > 360) {
33         return smoke.alert("Sorry, invalid color. Keep it between 100 and 360");
34      } else {
35         localStorage.setItem('depth', depth.value);
36         localStorage.setItem('color', color.value);
37
38         location.reload();
39      }
40   }, false);
41
42   /* Reset button listener */
43   reset.addEventListener('click', function (ev) {
44      ev.preventDefault();
45
46      localStorage.setItem('depth', 10);
47      localStorage.setItem('color', 280);
48
49      location.reload();
50   }, false);
51
52
53   body.appendChild(canvas);
54   canvas.width   = window.innerWidth -25;
55   canvas.height  = window.innerHeight -5;
56   ctx.translate(middle, window.innerHeight -75);
57
58   /* draw the first stem */
59   init(-Math.PI/2);
60
61   function init (angle) {
62      generation++;
63
64      ctx.save();
65
66      ctx.strokeStyle = "hsl("+ (colorDefault - generation * 10) +", 100%, 50%)";
67      if (generation > maxGenerations) {
68         ctx.strokeStyle = "white";
69      }
70
71      ctx.lineWidth = 6;
72      ctx.rotate(angle);
73      ctx.beginPath();
74      ctx.moveTo(0, 0);
75      ctx.lineTo(80, 0);
76      ctx.stroke();
77      ctx.translate(80, 0);
78      scale = randomRange(0.75, 1);
79      ctx.scale(scale, scale);
80
81      /* branch out twice */
82      if (generation < maxGenerations) {
83         init(randomRange(0, Math.PI/4));
84         init(randomRange(-Math.PI/4, 0));
85      }
86
87      /* blooms */
88      if (generation === maxGenerations) {
89         ctx.fillStyle = "white";
90         ctx.beginPath();
91         ctx.arc(0, 0, 20, 0, Math.PI * 2, true);
92         ctx.fill();
93      }
94
95      ctx.restore();
96      generation--;
97   }
98
99   function randomRange(min,max){
100      return Math.random()*(max-min) + min;
101   }
102
103}) ();