-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspace_bird.ts
401 lines (342 loc) · 14.3 KB
/
space_bird.ts
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
(() => {
let canvas: HTMLCanvasElement;
let draft_context: CanvasRenderingContext2D;
let space: Space;
let currentbirdindex = -1;
let load = () => {
canvas = <HTMLCanvasElement>document.getElementById("space");
let draft_canvas = <HTMLCanvasElement>document.createElement("canvas");
if (!canvas || !canvas.getContext) return false;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
draft_canvas.width = 1;
draft_canvas.height = 1;
draft_context = draft_canvas.getContext("2d");
const [down, move, up, cancel] = navigator.pointerEnabled ? ["pointerdown", "pointermove", "pointerup", "pointercancel"] :
(navigator.msPointerEnabled ? ["MSPointerDown", "MSPointerMove", "MSPointerUp", "MSPointerCancel"] :
["mousedown", "mousemove", "mouseup", "mousecancel"]);
canvas.addEventListener(down, (e: MouseEvent) => {
currentbirdindex = getPointingBird(e);
if (currentbirdindex != -1) {
space.birds[currentbirdindex].onPointerDownEvent(e.pageX, e.pageY);
}
}, false);
canvas.addEventListener(move, (e: MouseEvent) => {
if (currentbirdindex != -1) {
space.birds[currentbirdindex].onPointerMoveEvent(e.pageX, e.pageY);
}
}, false);
canvas.addEventListener(up, e => {
if (currentbirdindex != -1) {
space.birds[currentbirdindex].onPointerUpEvent();
currentbirdindex = -1;
}
}, false);
canvas.addEventListener(cancel, e => {
if (currentbirdindex != -1) {
space.birds[currentbirdindex].onPointerUpEvent();
currentbirdindex = -1;
}
}, false);
if (!navigator.pointerEnabled && !navigator.msPointerEnabled) {
canvas.addEventListener("touchstart", e => {
currentbirdindex = getPointingBirdTouch(e);
if (currentbirdindex != -1) {
space.birds[currentbirdindex].onPointerDownEvent(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
}
}, false);
canvas.addEventListener("touchmove", e => {
if (currentbirdindex != -1) {
space.birds[currentbirdindex].onPointerMoveEvent(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
}
}, false);
canvas.addEventListener("touchend", e => {
if (currentbirdindex != -1) {
space.birds[currentbirdindex].onPointerUpEvent();
currentbirdindex = -1;
}
}, false);
canvas.addEventListener("touchcancel", e => {
if (currentbirdindex != -1) {
space.birds[currentbirdindex].onPointerUpEvent();
currentbirdindex = -1;
}
}, false);
}
space = new Space(canvas.getContext("2d"), window.innerWidth, window.innerHeight);
};
window.addEventListener("resize", () => {
if (!canvas || !canvas.getContext) return false;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
if (space) {
space.onSizeChanged(window.innerWidth, window.innerHeight);
}
});
function getPointingBird(e) {
draft_context.clearRect(0, 0, 1, 1);
for (let i = space.birds.length - 1; i >= 0; i--) {
if (!space.birds[i].parhapsInside(e.clientX, e.clientY)) continue;
space.birds[i].draw(draft_context, e.clientX, e.clientY);
var image = draft_context.getImageData(0, 0, 1, 1);
if (image.data[3] > 0x7F) {
return i;
}
}
return -1;
}
function getPointingBirdTouch(e) {
draft_context.clearRect(0, 0, 1, 1);
for (let i = space.birds.length - 1; i >= 0; i--) {
if (!space.birds[i].parhapsInside(e.targetTouches[0].pageX, e.targetTouches[0].pageY)) continue;
space.birds[i].draw(draft_context, e.targetTouches[0].pageX, e.targetTouches[0].pageY);
var image = draft_context.getImageData(0, 0, 1, 1);
if (image.data[3] > 0x7F) {
return i;
}
}
return -1;
}
class Space {
static lerp(a: number, b: number, f: number): number {
return (b - a) * f + a;
}
static randfrange(a: number, b: number): number {
return this.lerp(a, b, Math.random());
}
static randsign(): number {
return Math.random() < 0.5 ? 1 : -1;
}
static flip(): boolean {
return Math.random() < 0.5;
}
static mag(x: number, y: number): number {
return Math.sqrt(x * x + y * y);
}
static clamp(x: number, a: number, b: number): number {
return ((x < a) ? a : ((x > b) ? b : x));
}
static dot(x1: number, y1: number, x2: number, y2: number): number {
return x1 * x2 + y1 + y2;
}
static pick(array: Array<any>): any {
if (array.length == 0) return null;
return array[Math.floor(Math.random() * array.length)];
}
static pickString(array: Array<string>): string {
if (array.length == 0) return null;
return array[Math.floor(Math.random() * array.length)];
}
static NUM_BEANS: number = 40;
static MIN_SCALE: number = 0.2;
static MAX_SCALE: number = 1;
static LUCKY: number = 0.001;
static MAX_RADIUS: number = Math.floor(576 * Space.MAX_SCALE);
static TIMEOUT: number = 17;
static BIRDS: Array<string> = [
"img/bird1.png"
];
static BACKGROUND: string = "img/background.jpg"
mAnim: number;
private lastPrint: number;
public birds: Array<Bird>;
private background: HTMLImageElement;
private context: CanvasRenderingContext2D;
private grabbing_bird;
public board: Board;
public constructor(context: CanvasRenderingContext2D, boardWidth: number, boardHeight: number) {
this.grabbing_bird = null;
this.context = context;
this.board = new Board(boardWidth, boardHeight);
this.background = new Image();
this.background.src = Space.BACKGROUND;
this.background.addEventListener("load", () => {
this.startAnimation();
})
}
private addView(bird: Bird) {
if (!this.birds) {
this.birds = new Array<Bird>();
}
this.birds[this.birds.length] = bird;
}
private removeAllViews() {
this.birds = null;
}
private reset(): void {
this.removeAllViews();
for (let i = 0; i < Space.NUM_BEANS; i++) {
var nv: Bird = new Bird(this.board);
this.addView(nv);
nv.z = i / Space.NUM_BEANS;
nv.z *= nv.z;
nv.reset(Space.randfrange(0, this.board.width), Space.randfrange(0, this.board.height));
}
}
public onSizeChanged(w: number, h: number): void {
this.board.width = w;
this.board.height = h;
}
public stopAnimation(): void {
if (this.mAnim) {
window.clearInterval(this.mAnim);
this.mAnim = 0;
}
}
public startAnimation(): void {
this.stopAnimation();
this.reset();
this.move();
}
move = () => {
this.context.setTransform(1, 0, 0, 1, 0, 0);
var widthScale = this.board.width / this.background.width;
var heightScale = this.board.height / this.background.height;
var sx, sy, sw, sh;
if (widthScale < heightScale) {
sw = this.board.width / heightScale;
sh = this.background.height;
sx = (this.background.width - sw) / 2;
sy = 0;
} else {
sw = this.background.width;
sh = this.board.height / widthScale;
sx = 0;
sy = (this.background.height - sh) / 2;
}
this.context.drawImage(this.background, sx, sy, sw, sh, 0, 0, this.board.width, this.board.height);
for (let i = 0; i < this.birds.length; i++) {
var nv: Bird = this.birds[i];
nv.update(Space.TIMEOUT / 1000);
for (let j = i + 1; j < this.birds.length; j++) {
var nv2: Bird = this.birds[j];
var overlap: number = nv.overlap(nv2);
}
nv.draw(this.context);
if (nv.x < - Space.MAX_RADIUS
|| nv.x > this.board.width + Space.MAX_RADIUS
|| nv.y < - Space.MAX_RADIUS
|| nv.y > this.board.height + Space.MAX_RADIUS) {
nv.reset();
}
}
requestAnimationFrame(this.move);
}
}
class Board {
constructor(public width: number, public height: number) {
}
}
class Bird {
public static VMAX = 1000.0;
public static VMIN = 100.0;
public x: number;
public y: number;
public a: number;
public pivotx: number;
public pivoty: number;
public va: number;
public vx: number;
public vy: number;
public r: number;
public z: number;
public h: number;
public w: number;
public radius: number;
public scale: number;
public grabbed: boolean;
public grabx: number;
public graby: number;
private grabx_offset: number;
private graby_offset: number;
private image: HTMLImageElement;
constructor(private board: Board) {
}
public toString(): String {
return "<bean (" + Math.round(this.x * 10) / 10 + ", " + Math.round(this.y * 10) / 10 + ") (" + this.w + " x " + this.h + ")>";
}
private pickBird(x?: number, y?: number): void {
var birdURL: string = Space.pickString(Space.BIRDS);
this.image = new Image();
this.image.src = birdURL;
this.image.addEventListener("load", () => {
this.scale = Space.lerp(Space.MIN_SCALE, Space.MAX_SCALE, this.z);
this.h = this.image.height;
this.w = this.image.width;
this.r = 0.3 * Math.max(this.h, this.w) * this.scale;
this.a = (Space.randfrange(0, 360));
this.va = Space.randfrange(-30, 30);
this.vx = Space.randfrange(-40, 40) * this.z;
this.vy = Space.randfrange(-40, 40) * this.z;
var boardh = this.board.height;
var boardw = this.board.width;
if (!x) {
if (Space.flip()) {
this.x = (this.vx < 0 ? boardw + 2 * this.r : -this.r * 4);
this.y = (Space.randfrange(0, boardh - 3 * this.r) * 0.5 + ((this.vy < 0) ? boardh * 0.5 : 0));
} else {
this.y = (this.vy < 0 ? boardh + 2 * this.r : -this.r * 4);
this.x = (Space.randfrange(0, boardw - 3 * this.r) * 0.5 + ((this.vx < 0) ? boardw * 0.5 : 0));
}
} else {
this.x = x;
this.y = y;
}
this.radius = Math.sqrt(Math.pow(this.w * this.scale, 2) + Math.pow(this.h * this.scale, 2)) / 2;
});
}
public reset(x?: number, y?: number) {
this.pickBird(x, y);
}
public update(dt: number) {
if (this.grabbed) {
this.vx = (this.vx * 0.75) + ((this.grabx - this.x) / dt) * 0.25;
this.x = this.grabx;
this.vy = (this.vy * 0.75) + ((this.graby - this.y) / dt) * 0.25;
this.y = this.graby;
} else {
this.x = (this.x + this.vx * dt);
this.y = (this.y + this.vy * dt);
this.a = (this.a + this.va * dt);
}
this.radius = Math.sqrt(Math.pow(this.w * this.scale, 2) + Math.pow(this.h * this.scale, 2)) / 2;
}
public overlap(other: Bird): number {
var dx: number = (this.x - other.x);
var dy: number = (this.y - other.y);
return Space.mag(dx, dy) - this.r - other.r;
}
public onPointerDownEvent(x: number, y: number) {
this.grabbed = true;
this.grabx_offset = x - this.x;
this.graby_offset = y - this.y;
this.grabx = x - this.grabx_offset;
this.graby = y - this.graby_offset;
this.va = 0;
};
public onPointerMoveEvent(x: number, y: number) {
this.grabx = x - this.grabx_offset;
this.graby = y - this.graby_offset;
};
public onPointerUpEvent() {
this.grabbed = false;
var a = Space.randsign() * Space.clamp(Space.mag(this.vx, this.vy) * 0.33, 0, 1080);
this.va = Space.randfrange(a * 0.5, a);
};
public parhapsInside(x: number, y: number): boolean {
return Math.sqrt(Math.pow(x - (this.x + this.w * this.scale / 2), 2) + Math.pow(y - (this.y + this.h * this.scale / 2), 2)) <= this.radius;
}
public draw(context: CanvasRenderingContext2D, preX = 0, preY = 0) {
context.save();
// Move registration point to the center of the canvas
context.translate(this.x + this.w * this.scale / 2 - preX, this.y + this.h * this.scale / 2 - preY);
// Rotate 1 degree
context.rotate(Math.PI * this.a / 180);
// Move registration point back to the top left corner of canvas
context.translate(-(this.x + this.w * this.scale / 2 - preX), -(this.y + this.h * this.scale / 2 - preY));
context.drawImage(this.image, this.x - preX, this.y - preY, this.w * this.scale, this.h * this.scale);
context.restore();
}
}
load();
})()