-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.js
91 lines (74 loc) · 2.65 KB
/
shapes.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
/*
Copyright 2010 Ashley Hewson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// a thing a day #2 - sun 14 nov
addToy({
name: "shapes",
contributors: "ash",
constructor: function(ctx, width, height) {
var toy = this
var data = ctx.createImageData(width,height)
var count = new Array(width*height)
for (var i = 0; i < width*height; i++) {
count[i] = 0
}
function colour() {
var colours = [
[0xbb, 0xbb, 0xbb, 255],
[0xbb, 0x22, 0x44, 255],
[0x44, 0x22, 0xbb, 255],
[0x22, 0x22, 0x22, 255]
]
for (var i = 0; i < width*height; i++) {
var c = colours[count[i] % colours.length]
for (var j = 0; j < 4; j++) {
data.data[4*i + j] = c[j]
}
}
ctx.putImageData(data, 0, 0)
}
function addCircle(x0,y0,r) {
function line(x1,x2,y1) {
if (y1 >= 0 && y1 < height) {
if (x1 < 0) { x1 = 0}
if (x2 >= width) { x2 = width -1 }
var i1 = y1*width + x1
var i2 = y1*width + x2
for (var i = i1; i <= i2; i++) {
count[i] ++
}
}
}
line(x0-r,x0+r,y0)
for (var dy = 1; dy <= r; dy++) {
var dx = Math.round(Math.sqrt(r*r - dy*dy))
line(x0-dx, x0+dx, y0-dy)
line(x0-dx, x0+dx, y0+dy)
}
}
function addShape() {
var x = Math.round(Math.random()*width)
var y = Math.round(Math.random()*height)
var r = Math.round(Math.random()*width/1.5)
addCircle(x,y,r)
}
function go() {
for (var i = 0; i < 7; i++) {
addShape();
}
colour();
}
go();
toy.clickHandler = function(e) { go(); }
}
})