-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexture.js
43 lines (30 loc) · 984 Bytes
/
texture.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
function Texture(w, h, colors) {
var self = this;
self.colors = colors;
self.canvas = createCanvas(w, h);
self.ctx = self.canvas.getContext('2d');
disableCanvasSmoothness(this.ctx);
self.imageData = self.ctx.createImageData(w, h);
}
Texture.prototype.generate = function (factor) {
var self = this,
data = self.imageData.data,
color,
i;
factor = factor || PIXEL_SIZE;
// Fill ImageData object with random colors.
for (i = 0; i < data.length; i += 4) {
color = self.colors[random(0, self.colors.length - 1)];
data[i] = color[0];
data[i + 1] = color[1];
data[i + 2] = color[2];
data[i + 3] = 255;
}
self.ctx.putImageData(self.imageData, 0, 0);
self.ctx.save();
self.ctx.scale(factor, factor);
self.ctx.drawImage(self.canvas, 0, 0);
self.ctx.restore();
// Return a texture pattern.
return self.ctx.createPattern(self.canvas, 'repeat');
};