A quick, configurable way of getting squares to fall down your canvas. Sometimes, you just want falling squares.
In your HTML:
<script src="canvas-falling-squares.js"></script>
In an AMD loader:
require(['canvas-falling-squares'], function(FallingSquares) {/*…*/});
Or using Bower:
bower install canvas-falling-squares
var canvas = document.getElementById('my-canvas');
new FallingSquares( { canvas: canvas } );
var canvas = document.getElementById('canvas');
canvas.height = 500;
canvas.width = 500;
new FallingSquares({
canvas: canvas,
colours: ['#70A8FF', '#FFBA7A'],
size: 10,
spacing: 5,
maxSpeed: 2,
minSpeed: 0.5,
numberOfColumns: 5,
numberOfSquares: 3,
backgroundColour: '#FFFFFF',
xOffset: 10
});
The following options may be passed as a hash to the FallingSquares()
constructor. Only the canvas
property is mandatory. The rest revert to defaults, which you can (and should!) override. Some of the configuration is random, so you only have the option to specify boundaries (e.g. for speed), or a choice of possibilities (e.g. for colours).
A reference to your canvas element.
options.canvas = document.getElementById('my-canvas');
An array of hex colours that are randomly assigned to your falling squares. For now, the colour distribution is always even.
options.colours = ['#70A8FF', '#FFBA7A'];
The size in pixels of your squares
options.size = 10;
The horizontal spacing between columns in pixels. This is also the minimum vertical spacing between squares in the same column.
options.spacing = 5;
The maximum possible speed (in pixels per tick) of squares in a column. All squares in a column fall at the same speed. The speed is assigned randomly.
options.maxSpeed = 2;
The minimum possible speed (in pixels per tick) of squares in a column.
options.minSpeed = 0.5;
The number of columns of falling squares that will be generated.
options.numberOfColumns = 5;
The maximum number of falling squares that will appear in any one column.
options.numberOfSquares = 3;
The hex background colour of the canvas. This will be applied to the area of falling squares every tick, to refresh the canvas for the next tick.
options.backgroundColour = '#FFFFFF';
The horizontal offset in pixels from the left edge of canvas to the position at which you want the first column to appear.
options.xOffset = 10;
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D