-
Notifications
You must be signed in to change notification settings - Fork 0
/
plant.js
506 lines (386 loc) · 15.9 KB
/
plant.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
Written by Albert Khamidullin
*/
(function () {
"use strict";
var plant = {
_animFrame: null,
Scene: function(options) {
options = options || {};
this.useTimer = options.useTimer || false;
this.width = options.width || 320;
this.height = options.height || 320;
this.background = options.background || 'black';
// create canvas if not specified existing
if (options.htmlNodeId === undefined) {
this.htmlNode = document.createElement('canvas');
document.body.appendChild(this.htmlNode);
} else {
this.htmlNode = document.getElementById(options.htmlNodeId);
}
// create additional hidden canvas for image processing
this._processingCanvasNode = document.createElement('canvas');
this._processingCanvasNode.style.display = 'none';
document.body.appendChild(this._processingCanvasNode);
// all scene's objects goes here
this.nodes = [];
// mouse position on scene
this.mouseX = 0;
this.mouseY = 0;
// function expected
this.onClick = null;
if (this.htmlNode.getContext) {
this.context = this.htmlNode.getContext('2d');
this.htmlNode.width = this.width;
this.htmlNode.height = this.height;
} else {
throw new Error('Plant.js: Unable to get canvas context.');
}
var self = this;
// update mouse position on scene
this.htmlNode.addEventListener('mousemove', function(e) {
self.mouseX = e.clientX - self.htmlNode.offsetLeft;
self.mouseY = e.clientY - self.htmlNode.offsetTop;
}, false);
this._changeImageOpacity = function (imagenode, opacity) {
if (this._processingCanvasNode.getContext) {
this._processingCanvasCtx = this._processingCanvasNode.getContext('2d');
// set canvas width and height to match image's size
this._processingCanvasNode.width = imagenode.width;
this._processingCanvasNode.height = imagenode.height;
// set canvas opacity
this._processingCanvasCtx.globalAlpha = opacity;
// draw image
this._processingCanvasCtx.drawImage(imagenode, 0, 0);
// export base64 encoded image data
var imgdata = this._processingCanvasNode.toDataURL('image/png');
return imgdata;
} else {
throw new Error('Plant.js: Unable to get canvas context');
}
};
// Check for click on canvas itself
// or on any object attached to current scene
this.htmlNode.addEventListener('click', function(e) {
// scene itself
// if function set to onClick call it
if (typeof(self.onClick) === 'function') {
self.onClick();
}
// check for "collision" between mouse pointer and any other object
// on current scene
var curX = e.clientX - self.htmlNode.offsetLeft;
var curY = e.clientY - self.htmlNode.offsetTop;
var x1 = curX;
var y1 = curY;
var w1;
var h1;
var length = self.nodes.length;
for (var i = 0; i < length; i++) {
var T = self.nodes[i];
// mouse pointer
w1 = 1;
h1 = 1;
var x2 = T.x;
var y2 = T.y;
var w2 = T.width;
var h2 = T.height;
var isCollision = true;
w2 += x2;
w1 += x1;
if (x2 > w1 || x1 > w2) {
isCollision = false;
}
h2 += y2;
h1 += y1;
if (y2 > h1 || y1 > h2) {
isCollision = false;
}
// if there is any collision, execute onClick function of
// scene's child object clicked, if it defined (not null)
if (isCollision && T.onClick !== null) {
T.onClick();
}
}
}, false);
},
Rectangle: function(options) {
options = options || {};
this.width = options.width || 50;
this.height = options.height || 50;
// plan works with hex colors, in full notation
this.color = options.color || '#ffffff';
this.x = options.x || 0;
this.y = options.y || 0;
this.zindex = options.zindex || 1;
this.visible = options.visible || true;
this.opacity = options.opacity || 1;
// function expected
this.onClick = null;
},
Ellipse: function(options) {
options = options || {};
this.width = options.width || 50;
this.height = options.height || 50;
this.color = options.color || '#ffffff';
this.x = options.x || 0;
this.y = options.y || 0;
this.zindex = options.zindex || 1;
this.visible = options.visible || true;
this.opacity = options.opacity || 1;
this.onClick = null;
},
Sprite: function(options) {
// src option required
if (options.src === undefined){
throw new Error('Plant.js: resourse src required');
} else {
this.node = new Image();
this.src = options.src;
this.node.src = options.src;
}
this.width = options.width || this.node.width;
this.height = options.height || this.node.height;
this.frameWidth = options.frameWidth || this.node.width;
this.frameHeight = options.frameHeight || this.node.height;
// current x and y frames
this.xFrame = options.xFrame || 0;
this.yFrame = options.yFrame || 0;
this.x = options.x || 0;
this.y = options.y || 0;
this.opacity = options.opacity || 1;
// save previous opacity and angle values, watch for a change
this._opacityCache = 1;
this.zindex = options.zindex || 1;
this.visible = options.visible || true;
this._isFadingOut = false;
this._fadingFrame = null;
this.onClick = null;
},
Text: function(options) {
options = options || {};
this.font = options.font || '10pt sans-serif';
this.color = options.color || '#ffffff';
this.x = options.x || 0;
this.y = options.y || 0;
this.text = options.text || 'Sample text';
this.zindex = options.zindex || 1;
this.visible = options.visible || true;
this.onClick = function() {
// nop
};
},
GameLoop: function(options) {
// scene is required
if (options.scene === undefined){
throw new Error('Plant.js: scene is required');
} else {
this.scene = options.scene;
}
// 50ms default
this.interval = options.interval || 50;
this._isActive = false;
},
// check for collision
// @TODO move collision check to scene method?
isCollision: function(obj1, obj2) {
var x1 = obj1.x;
var y1 = obj1.y;
var w1 = obj1.width;
var h1 = obj1.height;
var x2 = obj2.x;
var y2 = obj2.y;
var w2 = obj2.width;
var h2 = obj2.height;
w2 += x2;
w1 += x1;
if (x2 > w1 || x1 > w2) {
return false;
}
h2 += y2;
h1 += y1;
if (y2 > h1 || y1 > h2) {
return false;
}
return true;
},
// random int
Random: function (from, to) {
return Math.floor(Math.random() * (to - from + 1) + from);
},
// sort array's objects by key
_sortBy: function (prop, arr) {
prop = prop.split('.');
var len = prop.length;
arr.sort(function (a, b) {
var i = 0;
while (i < len) {
a = a[prop[i]];
b = b[prop[i]];
i++;
}
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
return arr;
},
_componentToHex: function(c) {
var hex = c.toString(16);
return hex.length == 1 ? '0' + hex : hex;
},
_rgbToHex: function(r, g, b) {
return '#' + this._componentToHex(r) + this._componentToHex(g) + this._componentToHex(b);
},
_hexToRgb: function(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
};
plant.Scene.prototype.add = function(toAdd) {
// array of objects
if (toAdd instanceof Array) {
var length = toAdd.length;
for (var i = 0; i < length; i++) {
this.nodes.push(toAdd[i]);
}
// single object
} else {
this.nodes.push(toAdd);
}
};
plant.Scene.prototype.update = function() {
// clear canvas
this.context.fillStyle = this.background;
this.context.fillRect(0, 0, this.htmlNode.width, this.htmlNode.height);
// sort scene's objects by z-indexes
this.nodes = plant._sortBy('zindex', this.nodes);
var length = this.nodes.length;
for (var i = 0; i < length; i++) {
var T = this.nodes[i];
var ctx = this.context;
// don't render invisible objects
if (T.visible) {
switch (T.type()) {
case 'rectangle':
if (T.opacity >= 0 && T.opacity <= 1) {
if (T.opacity < 1) {
var r = plant._hexToRgb(T.color).r;
var g = plant._hexToRgb(T.color).g;
var b = plant._hexToRgb(T.color).b;
ctx.fillStyle = 'rgba(' + r + ', ' + g + ', ' + b + ', ' + T.opacity + ')';
} else {
// opaque
ctx.fillStyle = T.color;
}
} else {
throw new Error('Plant.js: invalid opacity value');
}
ctx.fillRect(T.x, T.y, T.width, T.height);
break;
case 'ellipse':
if (T.opacity >= 0 && T.opacity <= 1) {
if (T.opacity < 1) {
var r = plant._hexToRgb(T.color).r;
var g = plant._hexToRgb(T.color).g;
var b = plant._hexToRgb(T.color).b;
ctx.fillStyle = 'rgba(' + r + ', ' + g + ', ' + b + ', ' + T.opacity + ')';
} else {
// opaque
ctx.fillStyle = T.color;
}
} else {
throw new Error('invalid opacity value');
}
var kappa = .5522848;
var ox = (T.width / 2) * kappa;
var oy = (T.height / 2) * kappa;
var xe = T.x + T.width;
var ye = T.y + T.height;
var xm = T.x + T.width / 2;
var ym = T.y + T.height / 2;
ctx.beginPath();
ctx.moveTo(T.x, ym);
ctx.bezierCurveTo(T.x, ym - oy, xm - ox, T.y, xm, T.y);
ctx.bezierCurveTo(xm + ox, T.y, xe, ym - oy, xe, ym);
ctx.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
ctx.bezierCurveTo(xm - ox, ye, T.x, ym + oy, T.x, ym);
ctx.closePath();
ctx.fill();
//ctx.restore();
break;
case 'sprite':
// if opacity has changed, convert image
if (T.opacity !== T._opacityCache) {
T.node.src = this._changeImageOpacity(T.node, T.opacity);
T._opacityCache = T.opacity;
}
// find out what area of sprite we should draw
var sx = T.frameWidth * T.xFrame;
var sy = T.frameHeight * T.yFrame;
ctx.save();
ctx.drawImage(T.node, sx, sy, T.frameWidth, T.frameHeight, T.x, T.y, T.width, T.height);
ctx.restore();
break;
case 'text':
ctx.font = T.font;
ctx.fillStyle = T.color;
ctx.textBaseline = 'top';
ctx.fillText(T.text, T.x, T.y);
break;
}
}
}
};
plant.Rectangle.prototype.type = function() {
return 'rectangle';
};
plant.Ellipse.prototype.type = function() {
return 'ellipse';
};
plant.Sprite.prototype.type = function() {
return 'sprite';
};
plant.Sprite.prototype.fadeOut = function() {
this._fadingFrame = 10;
this._isFadingOut = true;
};
plant.Text.prototype.type = function() {
return 'text';
};
plant.GameLoop.prototype.start = function(scene) {
if (scene && !scene.useTimer) {
plant._animFrame = requestAnimationFrame(this.code);
} else {
if (!this._isActive) {
this.handle = setInterval(this.code, this.interval);
this._isActive = true;
return true;
} else {
return false;
}
}
};
plant.GameLoop.prototype.stop = function(scene) {
if (scene && !scene.useTimer) {
cancelAnimationFrame(plant._animFrame);
} else {
if (this._isActive) {
clearInterval(this.handle);
this._isActive = false;
return true;
} else {
return false;
}
}
};
window.plant = plant;
})();