-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinting.html
90 lines (68 loc) · 2.97 KB
/
tinting.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://pixijs.download/release/pixi.js"></script>
</head>
<body>
</body>
<script>
const app = new PIXI.Application();
document.body.appendChild(app.view);
// holder to store the aliens
const aliens = [];
const totalDudes = 20;
for (let i = 0; i < totalDudes; i++) {
// create a new Sprite that uses the image name that we just generated as its source
const dude = PIXI.Sprite.from('/assets/eggHead.png');
// set the anchor point so the texture is centered on the sprite
dude.anchor.set(0.5);
// set a random scale for the dude - no point them all being the same size!
dude.scale.set(0.8 + Math.random() * 0.3);
// finally lets set the dude to be at a random position..
dude.x = Math.random() * app.screen.width;
dude.y = Math.random() * app.screen.height;
dude.tint = Math.random() * 0xFFFFFF;
// create some extra properties that will control movement :
// create a random direction in radians. This is a number between 0 and PI*2 which is the equivalent of 0 - 360 degrees
dude.direction = Math.random() * Math.PI * 2;
// this number will be used to modify the direction of the dude over time
dude.turningSpeed = Math.random() - 0.8;
// create a random speed for the dude between 2 - 4
dude.speed = 2 + Math.random() * 2;
// finally we push the dude into the aliens array so it it can be easily accessed later
aliens.push(dude);
app.stage.addChild(dude);
}
// create a bounding box for the little dudes
const dudeBoundsPadding = 100;
const dudeBounds = new PIXI.Rectangle(-dudeBoundsPadding,
-dudeBoundsPadding,
app.screen.width + dudeBoundsPadding * 2,
app.screen.height + dudeBoundsPadding * 2);
app.ticker.add(() => {
// iterate through the dudes and update their position
for (let i = 0; i < aliens.length; i++) {
const dude = aliens[i];
dude.direction += dude.turningSpeed * 0.01;
dude.x += Math.sin(dude.direction) * dude.speed;
dude.y += Math.cos(dude.direction) * dude.speed;
dude.rotation = -dude.direction - Math.PI / 2;
// wrap the dudes by testing their bounds...
if (dude.x < dudeBounds.x) {
dude.x += dudeBounds.width;
} else if (dude.x > dudeBounds.x + dudeBounds.width) {
dude.x -= dudeBounds.width;
}
if (dude.y < dudeBounds.y) {
dude.y += dudeBounds.height;
} else if (dude.y > dudeBounds.y + dudeBounds.height) {
dude.y -= dudeBounds.height;
}
}
});
</script>
</html>