-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
72 lines (57 loc) · 1.98 KB
/
canvas.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
const setBackground= canvas
var canvas = document.querySelector('canvas');
var canvasWidth = canvas.width = window.innerWidth;
var canvasHeight = canvas.height = window.innerHeight;
var ctx = canvas.getContext('2d');
var heightScale = 0.866;
function rnd(min, max) {
return Math.floor((Math.random() * (max - min + 1)) + min);
};
function render() {
ctx.fillStyle = 'rgb(246,249,252)';
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
ctx.lineWidth = 0;
var hueStart = rnd(0, 0);
var triSide = 40;
var halfSide = triSide / 2;
var rowHeight = Math.floor(triSide * heightScale);
var columns = Math.ceil(canvasWidth / triSide) + 1;
var rows = Math.ceil(canvasHeight / rowHeight);
var col, row;
for (row = 0; row < rows; row++) {
var hue = hueStart + (row * 3);
for (col = 0; col < columns; col++) {
var x = col * triSide;
var y = row * rowHeight;
var clr;
if (row % 2 != 0) {
x -= halfSide;
}
// upward pointing triangle
clr = 'hsl(' + hue + ', 0%, ' + rnd(90, 240) + '%)';
ctx.fillStyle = clr;
ctx.strokeStyle = clr;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + halfSide, y + rowHeight);
ctx.lineTo(x - halfSide, y + rowHeight);
ctx.closePath();
ctx.fill();
//ctx.stroke(); // needed to fill antialiased gaps on edges
// downward pointing triangle
clr = 'hsl(' + hue + ', 0%, ' + rnd(90, 245) + '%)';
ctx.fillStyle = clr;
ctx.strokeStyle = clr;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + triSide, y);
ctx.lineTo(x + halfSide, y + rowHeight);
ctx.closePath();
ctx.fill();
//ctx.stroke();
};
};
};
document.body.appendChild(canvas);
render();
document.body.addEventListener('dblclick', render);