-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-falling-squares.js
239 lines (190 loc) · 7.99 KB
/
canvas-falling-squares.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
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
(function(factory) {
//AMD
if (typeof define === 'function' && define.amd) {
define(function() {
return factory();
});
//Browser global
} else {
window.FallingSquares = factory();
}
}(function() {
// Rectangle with rounded corners
// from http://js-bits.blogspot.co.uk/2010/07/canvas-rounded-corner-rectangles.html
CanvasRenderingContext2D.prototype.roundRect = CanvasRenderingContext2D.prototype.roundRect ||
function(x, y, width, height, radius, fill, stroke) {
if (typeof stroke === "undefined" ) {
stroke = true;
}
if (typeof radius === "undefined") {
radius = 5;
}
this.beginPath();
this.moveTo(x + radius, y);
this.lineTo(x + width - radius, y);
this.quadraticCurveTo(x + width, y, x + width, y + radius);
this.lineTo(x + width, y + height - radius);
this.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
this.lineTo(x + radius, y + height);
this.quadraticCurveTo(x, y + height, x, y + height - radius);
this.lineTo(x, y + radius);
this.quadraticCurveTo(x, y, x + radius, y);
this.closePath();
if (stroke) {
this.stroke();
}
if (fill) {
this.fill();
}
};
// FallingSquares
// ---------------
// The main FallingSquares constructor that will be exported.
// Applies a number of "sensible" defaults for demonstration
// but you should override these for your own purposes by
// passing a configuration object.
function FallingSquares(options) {
var canvas = options.canvas,
context = options.canvas.getContext('2d'),
colours = options.colours || ['#70A8FF', '#FFBA7A'],
size = options.size || 10,
spacing = options.spacing || 5,
maxSpeed = options.maxSpeed || 2,
minSpeed = options.minSpeed || 0.5,
numberOfColumns = options.numberOfColumns || 5,
numberOfSquares = options.numberOfSquares || 3,
backgroundColour = options.backgroundColour || '#FFFFFF',
xOffset = options.xOffset || 10,
columns = [],
// Helper functions for determining random positions / colours / speeds
randomHelper = (function () {
// Determine whether a new square would be overlapping an existing square
var isOverlapping = function (yNew, yExisting) {
return !(yNew + size < yExisting - spacing) && !(yNew > yExisting + spacing + size);
};
return {
// Square fall speed, allocated randomly between a maximum and minimum
// Assigned when a column is generated, stays constant for the life of
// the column
randomSpeed: function () {
var speed = (Math.random() * maxSpeed);
speed = (speed < minSpeed) ? speed + minSpeed : speed;
return speed;
},
// Colours will be assigned randomly, but distributed evenly
randomColour: function () {
var division = 1 / colours.length,
random = Math.random(),
i = 0,
currDivision = division;
for (; i < colours.length; i += 1) {
if (currDivision >= random) {
return colours[i];
}
currDivision += division;
}
},
// Randomise the starting position of a square that has reached the bottom.
// The square will always restart off-canvas and fall into it from the top.
randomY: function(column) {
var yPos = Math.floor(Math.random() * -canvas.height),
randomY = this.randomY.bind(this, column);
column.squares.forEach(function (square) {
if (isOverlapping(yPos, square.y)) {
// if new square overlaps existing square, recursively find a new y position
yPos = randomY();
}
});
return yPos;
}
};
}());
// Let's get this started
function begin() {
buildColumns();
animate();
}
// Generate the given number of columns, with given size and spacing options
function buildColumns() {
var i = 0,
currPos = xOffset;
for (; i < numberOfColumns; i += 1) {
columns.push(new Column(currPos, randomHelper.randomSpeed()));
currPos += size + spacing;
}
}
// Every tick, cause animation to happen
function animate() {
requestAnimationFrame(animate);
resetSquares();
redraw();
}
// Fill a blank rectangle over the top of all columns to reset the canvas
// for the next animation tick
function resetSquares() {
var width = (size * numberOfColumns) + (spacing * (numberOfColumns - 1)) + xOffset;
context.fillStyle = backgroundColour;
context.fillRect(xOffset - 1, 0, width, canvas.height);
}
// Redraw each column in turn
function redraw() {
columns.forEach(function (column) {
column.redraw();
});
}
// Column
// ---------------
// Each column has its own x position and speed, that will remain constant
// for the life of the column. The column also maintains a reference to its
// child squares
function Column(x, speed) {
this.squares = [];
this.speed = speed;
this.x = x;
this.buildSquares();
}
// Redraw each square in turn
Column.prototype.redraw = function () {
this.squares.forEach(function (square) {
square.redraw();
});
};
// Generate the given number of squares, with randomly assigned
// colours and starting positions
Column.prototype.buildSquares = function () {
var i = 0;
for (; i < numberOfSquares; i += 1) {
this.squares.push(new Square({
y: randomHelper.randomY(this),
colour: randomHelper.randomColour(),
column: this
}));
}
};
// Square
// ---------------
// Each square has its own colour, that will remain constant for the
// life of the square. The square also maintains a reference back to
// its parent column for determining speed, x position and ensuring it
// doesn't overlap any other squares in this column when it is redrawn
// at the top of the canvas.
function Square(options) {
this.y = options.y;
this.colour = options.colour,
this.column = options.column;
}
Square.prototype.redraw = function () {
this.y += this.column.speed;
//fallen off the bottom, randomise new starting position
if (this.y > canvas.height) {
this.y = randomHelper.randomY(this.column);
}
context.fillStyle = this.colour;
context.strokeStyle = this.colour;
context.roundRect(this.column.x, this.y, size, size, 1, true, true);
};
// Start the fun
begin();
}
return FallingSquares;
}));