-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloaty.js
80 lines (63 loc) · 1.76 KB
/
Floaty.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
/*global document*/
"use strict";
function Floaty(x, y, r, speed) {
this.x = x;
this.y = y;
this.r = r;
this.speed = speed;
this.angle = 0;
this.color = '#8E8800';
this.prerender = null;
this.extra = 40;
}
Floaty.prototype.cacheBubble = function () {
var size = this.r * 2 + this.extra,
canvas = document.createElement('canvas'),
context = canvas.getContext('2d');
canvas.height = size;
canvas.width = size;
this.renderTo(context);
this.prerender = canvas;
};
Floaty.prototype.setColor = function (color) {
this.color = color;
this.cacheBubble();
};
Floaty.prototype.draw = function (context) {
var angleAmp = 30;
context.drawImage(this.prerender, this.x + Math.sin(this.angle) * angleAmp, this.y);
};
Floaty.prototype.drawRaw = function (context) {
var angleAmp = 30;
context.save();
context.beginPath();
context.lineWidth = 8;
context.shadowBlur = 20;
context.shadowColor = this.color;
context.strokeStyle = this.color;
context.arc(this.x + Math.sin(this.angle) * angleAmp, this.y, this.r, 0, 2 * Math.PI);
context.stroke();
context.restore();
};
Floaty.prototype.renderTo = function (context) {
context.save();
context.beginPath();
context.lineWidth = 8;
context.shadowBlur = 20;
context.shadowColor = this.color;
context.strokeStyle = this.color;
context.arc(this.r + this.extra / 2, this.r + this.extra / 2, this.r, 0, 2 * Math.PI);
context.stroke();
context.restore();
};
Floaty.prototype.move = function () {
var angleSpeed = 0.01;
this.y -= this.speed;
this.angle += angleSpeed;
};
Floaty.prototype.xPos = function () {
return this.x;
};
Floaty.prototype.yPos = function () {
return this.y;
};