/
canvas.htm
79 lines (59 loc) · 1.95 KB
/
canvas.htm
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
<head>
<style>
body {
margin: 0;
}
</style>
</head>
<body onresize="resize()" onload="resize()">
<canvas id="screen"></canvas>
<script src="https://code.createjs.com/easeljs-0.8.2.min.js"></script>
<script>
var canvas = document.getElementById('screen');
// Initialize
var stage = new createjs.Stage('screen');
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener('tick', function() {
stage.children.forEach(function(shape) {
shape.rotation = (shape.rotation + shape.rotationSpeed) % 360;
shape.x = (shape.x + shape.vx + canvas.width) % canvas.width;
});
stage.update();
});
for (var i = 0; i < 2000; i++) {
var shape = new createjs.Shape();
var r = random(0, 50);
var g = random(100, 200);
var b = random(200, 255);
var x = random(0, window.innerWidth);
var y = random(0, window.innerHeight);
var width = random(40, 80);
var height = random(40, 80);
// draw
shape.graphics.beginFill('rgb(' + r + ',' + g + ',' + b +')').drawRect(0, 0, width, height);
// set center
shape.regX = width / 2;
shape.regY = height / 2;
// set position
shape.x = x;
shape.y = y;
// set other attributes
shape.alpha = random(5, 10) / 10;
shape.rotation = random(0, 359);
shape.rotationSpeed = random(1, 2) * (random(0, 1) ? 1 : -1);
shape.vx = random(1, 3) * (random(0, 1) ? 1 : -1);
// add to stage
stage.addChild(shape);
}
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
stage.update();
}
// Library functions
// Returns a random integer between low and high, both inclusive
function random(low, high) {
return Math.floor(Math.random() * (high - low + 1) + low);
}
</script>
</body>