forked from joshmarinacci/node-pureimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolytest1.html
More file actions
157 lines (136 loc) · 4.17 KB
/
polytest1.html
File metadata and controls
157 lines (136 loc) · 4.17 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
.fill {
padding:0;
margin: 0;
background-color: black;
display: flex;
align-content: center;
align-items: center;
justify-content: center;
border: 0px solid blue;
position: absolute;
top:0;
bottom: 0;
left:0;
right:0;
}
</style>
</head>
<body>
<div class="fill">
<canvas id='canvas' width="1000" height="600"></canvas>
</div>
<script type="application/javascript">
function rand(min,max) {
return min + Math.random()*(max-min);
}
/**
* Converts an HSV color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes h, s, and v are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param Number h The hue
* @param Number s The saturation
* @param Number v The value
* @return Array The RGB representation
*/
function hsvToRgb(h, s, v){
var r, g, b;
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch(i % 6){
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return [r * 255, g * 255, b * 255];
}
function pick(arr) {
return arr[Math.floor(Math.random()*arr.length)];
}
var canvas = document.getElementById("canvas");
function draw() {
var ctx = canvas.getContext('2d');
var w = canvas.width;
var h = canvas.height;
var gw = Math.floor(w/43);
var gh = Math.floor(h/43);
ctx.fillStyle = 'black';
ctx.fillRect(0,0,w,h);
var grid = [];
for(var j=0; j<gh; j++) {
for(var i=0; i<gw; i++) {
grid.push({x:40+i*40, y:40+j*40});
}
}
function xy(x,y) {
return grid[y*gw+x];
}
var colors = [];
for(var s=0.2; s<1; s+=0.1) {
colors.push(hsvToRgb(1.0, 1.0, s));
}
var cells = [];
for(var i=0; i<gw-1; i++) {
for(var j=0; j<gh-1; j++) {
var pt1 = xy(i,j);
var pt2 = xy(i+1,j);
var pt3 = xy(i+1,j+1);
var pt4 = xy(i,j+1);
var color = colors[i%6];//pick(colors);
//var color = pick(colors);
cells.push([pt1,pt2,pt3,pt4, color]);
}
}
//randomly shift the grid
grid.forEach(function(pt){
var dist = 19;
pt.x = pt.x + rand(-dist,dist);
pt.y = pt.y + rand(-dist,dist);
});
//draw the dots
ctx.fillStyle = 'white';
grid.forEach(function(pt){
// ctx.fillRect(pt.x,pt.y, 4, 4)
});
//fill the polys
cells.forEach(function(cell) {
ctx.fillStyle = 'green';
ctx.beginPath();
ctx.moveTo(cell[0].x, cell[0].y);
ctx.lineTo(cell[1].x, cell[1].y);
ctx.lineTo(cell[2].x, cell[2].y);
ctx.lineTo(cell[3].x, cell[3].y);
ctx.closePath();
var c = cell[4];
var color = 'rgb('+Math.floor(c[0])+','+Math.floor(c[1])+','+Math.floor(c[2])+')';
ctx.fillStyle = color;
ctx.fill();
});
cells.forEach(function(cell) {
ctx.strokeStyle = 'black';
ctx.beginPath();
ctx.moveTo(cell[0].x, cell[0].y);
ctx.lineTo(cell[1].x, cell[1].y);
ctx.lineTo(cell[2].x, cell[2].y);
ctx.lineTo(cell[3].x, cell[3].y);
ctx.closePath();
ctx.stroke();
});
}
draw();
</script>
</body>
</html>