-
Notifications
You must be signed in to change notification settings - Fork 0
/
blinker.html
210 lines (181 loc) · 4.9 KB
/
blinker.html
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
<canvas id="PlayArea" width="1000" height="800" style="border:1px solid blue;">
</canvas>
<script>
class Blinker
{
constructor(id, size, center)
{
this.id = id;
this.size = size;
this.center = center;
this.touchingSet = new Set();
this.gauge = 0;
this.power = 10;
this.light = 0;
}
toString()
{
return "[" + this.id + "/" + this.size + "/" + this.center.x + "," + this.center.y + "]";
}
}
function distance(x1,y1,x2,y2)
{
var a = x1 - x2;
var b = y1 - y2;
return Math.sqrt(a*a + b*b);
}
var stringToColour = function(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
var colour = '#';
for (var i = 0; i < 3; i++) {
var value = (hash >> (i * 8)) & 0xFF;
colour += ('00' + value.toString(16)).substr(-2);
}
return colour;
}
function hitBlinkerTest(e)
{
var rect = e.target.getBoundingClientRect();
var x = e.clientX - rect.left; //x position within the element.
var y = e.clientY - rect.top; //y position within the element.
for (var i = 0; i < blinkers.length; i++)
{
let blinker = blinkers[i];
if (distance(x, y, blinker.center.x, blinker.center.y) <= blinker.size)
{
return blinker;
}
}
return null;
}
var blinkers = [];
for(var i = 0; i < 10; i++)
{
let blinker = new Blinker(i, 50, {x: 50 + 101 * i, y: 50});
blinkers.push(blinker);
}
var c = document.getElementById("PlayArea");
var ctx = c.getContext("2d");
var selectedBlinker = null;
window.oncontextmenu = function (e) //reset blink
{
e.preventDefault();
let blinker = hitBlinkerTest(e);
if (blinker)
blinker.gauge = 0;
return false; // cancel default menu
}
c.addEventListener('mousedown', function(e)
{
e.preventDefault();
let blinker = hitBlinkerTest(e);
if (blinker)
{
var rect = e.target.getBoundingClientRect();
startX = e.clientX - rect.left; //x position within the element.
startY = e.clientY - rect.top; //y position within the element.
selectedBlinker = blinker;
}
}, false);
var startX, startY;
c.addEventListener('mousemove', function(e)
{
e.preventDefault();
if (selectedBlinker)
{
var rect = e.target.getBoundingClientRect();
var x = e.clientX - rect.left; //x position within the element.
var y = e.clientY - rect.top; //y position within the element.
var dx = x - startX;
var dy = y - startY;
startX = x;
startY = y;
selectedBlinker.center.x += dx;
selectedBlinker.center.y += dy;
}
}, false);
c.addEventListener('mouseup', function(e)
{
e.preventDefault();
let blinker = hitBlinkerTest(e);
if (blinker)
{
selectedBlinker = null;
}
}, false);
c.addEventListener('mouseout', function(e)
{
e.preventDefault();
let blinker = hitBlinkerTest(e);
if (blinker)
{
selectedBlinker = null;
}
}, false);
for(var i = 0; i < blinkers.length; i++)
{
(function()
{
var j = i;
setTimeout(function(){ blinkers[j].gauge = 0;}, Math.random() * 500)
})();
}
setInterval(function()
{
//check collision
for (var i = 0; i < blinkers.length; i++)
{
for (var j = 0; j < blinkers.length; j++)
{
if (i == j) continue;
let outerDistance = distance(blinkers[i].center.x, blinkers[i].center.y, blinkers[j].center.x, blinkers[j].center.y) - blinkers[i].size - blinkers[j].size;
//console.log(blinkers[i] + " vs " + blinkers[j] + "->distance" + outerDistance);
if (outerDistance <= 0) //touching
{
blinkers[i].touchingSet.add(blinkers[j]);
blinkers[j].touchingSet.add(blinkers[i]);
}
else //not touching
{
blinkers[i].touchingSet.delete(blinkers[j]);
blinkers[j].touchingSet.delete(blinkers[i]);
}
}
}
//add gauge
for (var i = 0; i < blinkers.length; i++)
{
blinkers[i].gauge += blinkers[i].power;
blinkers[i].light = Math.max(blinkers[i].light - 0.1, 0);
if (blinkers[i].gauge >= 1000)
{
blinkers[i].light = 1;
blinkers[i].gauge = 0;
//callback touching sets
for (var it = blinkers[i].touchingSet.values(), val= null; val=it.next().value; )
{
//console.log("callback from " + blinkers[i].id + "to" + val.id );
val.gauge += parseInt(val.gauge * 0.2);
}
}
}
//draw
ctx.clearRect(0, 0, c.width, c.height);
for (var i = 0; i < blinkers.length; i++)
{
let blinker = blinkers[i];
ctx.beginPath();
ctx.arc(blinker.center.x, blinker.center.y, blinker.size, 0, 2 * Math.PI);
ctx.fillStyle = "hsl(0, 100%, " + (100 - 50 * blinkers[i].light) + "%)";
ctx.fill();
ctx.stroke();
ctx.font = "10px Arial";
ctx.fillStyle = "black";
ctx.fillText("id=" + blinker.id + " g=" + blinker.gauge + " l=" + blinker.light.toFixed(2), blinker.center.x - blinker.size/1.2, blinker.center.y - 20);
ctx.fillText(Array.from(blinker.touchingSet).map(x => x.id).join(','), blinker.center.x, blinker.center.y);
}
}, 20);
</script>