aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'Code/ZJW/javascripts/garden_dev.js')
-rw-r--r--Code/ZJW/javascripts/garden_dev.js199
1 files changed, 199 insertions, 0 deletions
diff --git a/Code/ZJW/javascripts/garden_dev.js b/Code/ZJW/javascripts/garden_dev.js
new file mode 100644
index 0000000..21af667
--- /dev/null
+++ b/Code/ZJW/javascripts/garden_dev.js
@@ -0,0 +1,199 @@
1 function Vector(x, y) {
2 this.x = x;
3 this.y = y;
4 };
5
6 Vector.prototype = {
7 rotate: function (theta) {
8 var x = this.x;
9 var y = this.y;
10 this.x = Math.cos(theta) * x - Math.sin(theta) * y;
11 this.y = Math.sin(theta) * x + Math.cos(theta) * y;
12 return this;
13 },
14 mult: function (f) {
15 this.x *= f;
16 this.y *= f;
17 return this;
18 },
19 clone: function () {
20 return new Vector(this.x, this.y);
21 },
22 length: function () {
23 return Math.sqrt(this.x * this.x + this.y * this.y);
24 },
25 subtract: function (v) {
26 this.x -= v.x;
27 this.y -= v.y;
28 return this;
29 },
30 set: function (x, y) {
31 this.x = x;
32 this.y = y;
33 return this;
34 }
35 };
36
37 function Petal(stretchA, stretchB, startAngle, angle, growFactor, bloom) {
38 this.stretchA = stretchA;
39 this.stretchB = stretchB;
40 this.startAngle = startAngle;
41 this.angle = angle;
42 this.bloom = bloom;
43 this.growFactor = growFactor;
44 this.r = 1;
45 this.isfinished = false;
46 //this.tanAngleA = Garden.random(-Garden.degrad(Garden.options.tanAngle), Garden.degrad(Garden.options.tanAngle));
47 //this.tanAngleB = Garden.random(-Garden.degrad(Garden.options.tanAngle), Garden.degrad(Garden.options.tanAngle));
48 }
49 Petal.prototype = {
50 draw: function () {
51 var ctx = this.bloom.garden.ctx;
52 var v1, v2, v3, v4;
53 v1 = new Vector(0, this.r).rotate(Garden.degrad(this.startAngle));
54 v2 = v1.clone().rotate(Garden.degrad(this.angle));
55 v3 = v1.clone().mult(this.stretchA); //.rotate(this.tanAngleA);
56 v4 = v2.clone().mult(this.stretchB); //.rotate(this.tanAngleB);
57 ctx.strokeStyle = this.bloom.c;
58 ctx.beginPath();
59 ctx.moveTo(v1.x, v1.y);
60 ctx.bezierCurveTo(v3.x, v3.y, v4.x, v4.y, v2.x, v2.y);
61 ctx.stroke();
62 },
63 render: function () {
64 if (this.r <= this.bloom.r) {
65 this.r += this.growFactor; // / 10;
66 this.draw();
67 } else {
68 this.isfinished = true;
69 }
70 }
71 }
72
73 function Bloom(p, r, c, pc, garden) {
74 this.p = p;
75 this.r = r;
76 this.c = c;
77 this.pc = pc;
78 this.petals = [];
79 this.garden = garden;
80 this.init();
81 this.garden.addBloom(this);
82 }
83 Bloom.prototype = {
84 draw: function () {
85 var p, isfinished = true;
86 this.garden.ctx.save();
87 this.garden.ctx.translate(this.p.x, this.p.y);
88 for (var i = 0; i < this.petals.length; i++) {
89 p = this.petals[i];
90 p.render();
91 isfinished *= p.isfinished;
92 }
93 this.garden.ctx.restore();
94 if (isfinished == true) {
95 this.garden.removeBloom(this);
96 }
97 },
98 init: function () {
99 var angle = 360 / this.pc;
100 var startAngle = Garden.randomInt(0, 90);
101 for (var i = 0; i < this.pc; i++) {
102 this.petals.push(new Petal(Garden.random(Garden.options.petalStretch.min, Garden.options.petalStretch.max), Garden.random(Garden.options.petalStretch.min, Garden.options.petalStretch.max), startAngle + i * angle, angle, Garden.random(Garden.options.growFactor.min, Garden.options.growFactor.max), this));
103 }
104 }
105 }
106
107 function Garden(ctx, element) {
108 this.blooms = [];
109 this.element = element;
110 this.ctx = ctx;
111 }
112 Garden.prototype = {
113 render: function () {
114 for (var i = 0; i < this.blooms.length; i++) {
115 this.blooms[i].draw();
116 }
117 },
118 addBloom: function (b) {
119 this.blooms.push(b);
120 },
121 removeBloom: function (b) {
122 var bloom;
123 for (var i = 0; i < this.blooms.length; i++) {
124 bloom = this.blooms[i];
125 if (bloom === b) {
126 this.blooms.splice(i, 1);
127 return this;
128 }
129 }
130 },
131 createRandomBloom: function (x, y) {
132 this.createBloom(x, y, Garden.randomInt(Garden.options.bloomRadius.min, Garden.options.bloomRadius.max), Garden.randomrgba(Garden.options.color.rmin, Garden.options.color.rmax, Garden.options.color.gmin, Garden.options.color.gmax, Garden.options.color.bmin, Garden.options.color.bmax, Garden.options.color.opacity), Garden.randomInt(Garden.options.petalCount.min, Garden.options.petalCount.max));
133 },
134 createBloom: function (x, y, r, c, pc) {
135 new Bloom(new Vector(x, y), r, c, pc, this);
136 },
137 clear: function () {
138 this.blooms = [];
139 this.ctx.clearRect(0, 0, this.element.width, this.element.height);
140 }
141 }
142
143 Garden.options = {
144 petalCount: {
145 min: 8,
146 max: 15
147 },
148 petalStretch: {
149 min: 0.1,
150 max: 3
151 },
152 growFactor: {
153 min: 0.1,
154 max: 1
155 },
156 bloomRadius: {
157 min: 4,
158 max: 7
159 },
160 density: 10,
161 growSpeed: 1500 / 60,
162 color: {
163 rmin: 128,
164 rmax: 255,
165 gmin: 0,
166 gmax: 128,
167 bmin: 0,
168 bmax: 128,
169 opacity: 0.1
170 },
171 tanAngle: 60
172 };
173 Garden.random = function (min, max) {
174 return Math.random() * (max - min) + min;
175 };
176 Garden.randomInt = function (min, max) {
177 return Math.floor(Math.random() * (max - min + 1)) + min;
178 };
179 Garden.circle = 2 * Math.PI;
180 Garden.degrad = function (angle) {
181 return Garden.circle / 360 * angle;
182 };
183 Garden.raddeg = function (angle) {
184 return angle / Garden.circle * 360;
185 };
186 Garden.rgba = function (r, g, b, a) {
187 return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
188 };
189 Garden.randomrgba = function (rmin, rmax, gmin, gmax, bmin, bmax, a) {
190 var r = Math.round(Garden.random(rmin, rmax));
191 var g = Math.round(Garden.random(gmin, gmax));
192 var b = Math.round(Garden.random(bmin, bmax));
193 var limit = 5;
194 if (Math.abs(r - g) <= limit && Math.abs(g - b) <= limit && Math.abs(b - r) <= limit) {
195 return Garden.rgba(rmin, rmax, gmin, gmax, bmin, bmax, a);
196 } else {
197 return Garden.rgba(r, g, b, a);
198 }
199 }; \ No newline at end of file
Powered by cgit v1.2.3 (git 2.41.0)