-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.js
122 lines (110 loc) · 2.86 KB
/
button.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
Button = function(x, y) {
this.x = x;
this.y = y;
this.rad = POWER_BUTTON_SIZE;
this.glowRad = 2;
this.color = POWER_OFF_COLOR;
this.pressed = false;
this.state = 'off';
this.pulseDir;
this.pulseInterval;
this.pulse();
return this;
}
Button.prototype.destroy = function() {
clearInterval(this.pulseInterval);
};
Button.prototype.detectMouseOver = function(event) {
if (event.offsetX < this.x) {
return false;
}
else if (event.offsetX > (this.x + (2 * this.rad))) {
return false;
}
else if (event.offsetY < this.y) {
return false;
}
else if (event.offsetY > (this.y + (2 * this.rad))) {
return false;
}
return true;
};
Button.prototype.press = function(event) {
this.pressed = true;
};
Button.prototype.release = function(event) {
this.pressed = false;
return this.toggle();
};
Button.prototype.toggle = function() {
switch(this.state) {
case 'off':
this.state = 'standby'
this.color = POWER_COLOR;
this.pulse();
break;
case 'standby':
this.state = 'on';
this.pulse();
this.glowRad = this.rad - 10;
break;
case 'on':
this.state = 'off';
this.stopPulse();
this.color = POWER_OFF_COLOR;
break;
}
return this.state;
};
Button.prototype.pulse = function() {
this.stopPulse();
this.pulseDir = 'grow';
var minRad = this.state == 'standby' ? 2 : 9;
var maxRad = this.rad - (this.state == 'standby' ? 10 : 5);
this.glowRad = minRad;
var pulseButton = function(direction) {
if (this.pulseDir == 'grow') this.glowRad++;
else this.glowRad--;
if (this.glowRad <= minRad) this.pulseDir = 'grow';
else if (this.glowRad >= maxRad) this.pulseDir = 'shrink';
}
this.pulseInterval = setInterval(pulseButton.bind(this), BUTTON_PULSE_DELAY);
};
Button.prototype.stopPulse = function() {
clearInterval(this.pulseInterval);
};
Button.prototype.render = function(context) {
var x = this.x + this.rad;
var y = this.y + this.rad;
context.fillStyle = 'slategray';
context.beginPath();
context.arc(x, y, this.rad, 0, 2 * Math.PI, false);
context.closePath();
context.fill();
if (not(this.pressed) && this.state !== 'on') {
x++;
y--;
}
context.fillStyle = modHexColor(this.color, 0.5);
context.beginPath();
context.arc(x, y, this.rad - 2, 0, 2 * Math.PI, false);
context.closePath();
context.fill();
if (not(this.pressed)) {
x += this.state == 'off' ? 2 : 1;
y -= this.state == 'off' ? 2 : 1;
}
if (this.state == 'off') {
context.fillStyle = this.color;
}
else {
var gradient = context.createRadialGradient(x, y - 1, this.glowRad, x, y, this.rad - 4);
gradient.addColorStop(0, 'red');
gradient.addColorStop(1, this.color);
context.fillStyle = gradient;
}
context.beginPath();
context.arc(x, y, this.rad - 3, 0, 2 * Math.PI, false);
context.closePath();
context.fill();
};