-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparade.js
197 lines (176 loc) · 5.54 KB
/
parade.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
var PEPPER_MAX_SPEED = 6;
var PEPPER_MAX_DRIFT = 2;
// determines density - lower = more peppers for your party
var PIXELS_PER_PEPPER = 30;
var SECONDS_PER_BURST = 1.2;
var CANVAS;
var PARADE;
function createCanvasOverlay()
{
// code is from http://permadi.com/2009/04/usng-html-5-canvas-to-draw-over-my-web-page-part-2/
// Create a blank div where we are going to put the canvas into.
var canvasContainer = document.createElement('div');
// Add the div into the document
document.body.appendChild(canvasContainer);
canvasContainer.style.position="absolute";
// Set to 100% so that it will have the dimensions of the document
canvasContainer.style.left="0px";
canvasContainer.style.top="0px";
canvasContainer.style.width="100%";
canvasContainer.style.height="100%";
// Set to high index so that this is always above everything else
// (might need to be increased if you have other element at higher index)
canvasContainer.style.zIndex="5";
// Now we create the canvas
myCanvas = document.createElement('canvas');
myCanvas.style.width = canvasContainer.scrollWidth+"px";
myCanvas.style.height = canvasContainer.scrollHeight+"px";
// You must set this otherwise the canvas will be streethed to fit the container
myCanvas.width=canvasContainer.scrollWidth;
myCanvas.height=canvasContainer.scrollHeight;
myCanvas.style.overflow = 'visible';
myCanvas.style.position = 'absolute';
// Add int into the container
canvasContainer.appendChild(myCanvas);
return myCanvas;
}
function hideCanvas()
{
CANVAS.parentNode.style.visibility='hidden';
}
function showCanvas()
{
if (myCanvas)
{
myCanvas.parentNode.style.visibility='visible';
}
}
var startParade = function(){
if (typeof CANVAS === 'undefined') {
CANVAS = createCanvasOverlay();
}
showCanvas();
var ctx = CANVAS.getContext("2d");
PARADE = new Parade(ctx);
PARADE.start();
};
var stopParade = function(){
PARADE.stop();
};
var Parade = function(ctx) {
this.ctx = ctx;
this.peppers = [];
};
var PEPPER_IMGS = {};
var loadImages = function() {
var pepper;
CardConstants.COLORS.forEach(function(color, idx){
CardConstants.SHAPES.forEach(function(shape, idx){
pepper = "" + shape + color;
PEPPER_IMGS[pepper] = new Image();
PEPPER_IMGS[pepper].src = "./images/" + pepper + ".png";
});
});
};
Parade.prototype.start = function(){
var that = this;
loadImages();
showCanvas();
that.addPeppers();
this.clickInt = setInterval(function(){
that.click();
}, 20);
this.rainInt = setInterval(function(){
that.addPeppers();
}, SECONDS_PER_BURST * 1000);
};
Parade.prototype.stop = function() {
this.peppers = [];
this.ctx.clearRect(0,0,CANVAS.width,CANVAS.height);
clearInterval(this.clickInt);
clearInterval(this.rainInt);
hideCanvas();
};
Parade.prototype.addPeppers = function(){
var that = this;
var newPeppers = makeRandomPeppers(CANVAS.width / PIXELS_PER_PEPPER, that.ctx);
that.peppers = that.peppers.concat(newPeppers);
};
Parade.prototype.click = function(){
this.moveAll();
this.ctx.clearRect(0,0,CANVAS.width,CANVAS.height);
this.drawAll();
};
Parade.prototype.removePepper = function(pepper) {
// this.peppers.forEach(function(pepper, idx){
// if (this.outOfBounds(pepper)) {
// this.peppers.splice(idx);
// }
// }.bind(this));
var index = this.peppers.indexOf(pepper);
this.peppers.splice(index, 1);
};
Parade.prototype.moveAll = function(){
this.peppers.forEach(function(pepper){
pepper.move();
});
};
Parade.prototype.drawAll = function(){
this.peppers.forEach(function(pepper){
pepper.draw();
});
};
// returns an array of n peppers of randomized colors/shapes in string format
// takes a reference to the ctx to pass to peppers & to determing starting pos
// this function is too long right meow
var makeRandomPeppers = function(n, ctx) {
var pepperImages = [];
for (var i = 0; i < n; i++) {
var randomColor = CardConstants.COLORS[Math.floor(Math.random() * CardConstants.COLORS.length)];
var randomShape = CardConstants.SHAPES[Math.floor(Math.random() * CardConstants.SHAPES.length)];
pepperImages.push("" + randomShape + randomColor);
}
var peppers = [];
// peppers start at the top and are evenly distributed across width of canvas
// they have a random speed (downward movement of pixels per second)
// and a random drift on the x-axis (moving either left or right)
pepperImages.forEach(function(pepper, idx) {
var pepperOptions = {
img: PEPPER_IMGS[pepper],
pos: [(PIXELS_PER_PEPPER * idx), - PEPPER_IMGS[pepper].height],
ctx: ctx,
speed: Math.floor(Math.random() * PEPPER_MAX_SPEED) + 1,
drift: Math.random() < 0.5 ? -1 : 1
};
peppers.push(new Pepper(pepperOptions));
});
return peppers;
};
var Pepper = function(options) {
this.img = options.img;
this.pos = options.pos;
this.ctx = options.ctx;
this.speed = options.speed;
this.drift = options.drift;
};
Pepper.prototype.outOfBounds = function() {
if (this.pos[1] > (CANVAS.height + this.img.height)) {
return true;
} else { return false; }
};
Pepper.prototype.draw = function() {
this.ctx.drawImage(this.img, this.pos[0], this.pos[1]);
};
Pepper.prototype.move = function() {
// move down
this.pos[1] += this.speed;
// randomly also move left or right or not at all
this.pos[0] += randomX() * this.drift;
if (this.outOfBounds()) {
PARADE.removePepper(this);
}
};
var randomX = function() {
var x = Math.floor(Math.random() * PEPPER_MAX_DRIFT);
return x;
};