-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
140 lines (114 loc) · 3.28 KB
/
index.html
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
<style>
canvas {
display: block;
}
</style>
<canvas></canvas>
<div>
<input id="color1" type="color" value="#ff0000" onchange="updateColors()">
<input id="color2" type="color" value="#ffa500" onchange="updateColors()">
<input id="color3" type="color" value="#ffff00" onchange="updateColors()">
</div>
<div>
<label for="">Height</label>
<input id="height-input" type="number" value="200" onchange="updateDimensions()">
<label for="">Width</label>
<input id="width-input" type="number" value="200" onchange="updateDimensions()">
</div>
<div>
<label for="">Speed</label>
<input id="speed-input" type="number" value="10" onchange="updateSpeed()">
</div>
<div>
<label for="">Bleed Rate</label>
<input id="bleed-input" type="number" value="2" onchange="updateBleed()">
</div>
<script>
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 200;
canvas.height = 200;
//paintCanvas(canvas);
var lastColor = "";
const colors = [
"red", "orange", "yellow"
];
var bleed = 2;
function getFillStyle(x,y) {
if(!x && !y) return getRandomColor();
const useRandomColor = !getRandomInt(0,bleed);
if(useRandomColor) {
return getRandomColor();
} else {
if(x === 0 && y===0) {
return getRandomColor();
}
if(x === 0) {
return pixels[x][y-1];
}
if(y === 0) {
return pixels[x-1][y];
}
const usePixelAbove = !!getRandomInt(0,2);
if(usePixelAbove) {
return pixels[x-1][y];
} else {
return pixels[x][y-1];
}
}
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
var x = 0;
var y = 0;
var pixels = [];
setInterval(()=>{
for(let i=0; i<speed; i++) {
drawPixel();
//drawPixelAtRandom();
}
},1);
function getRandomColor() {
const randomColorIndex = getRandomInt(0, colors.length);
return colors[randomColorIndex];
}
function drawPixel() {
ctx.fillStyle = getFillStyle(x,y);
ctx.fillRect(x,y,1,1);
if(!pixels[x]) pixels[x] = [];
pixels[x][y] = ctx.fillStyle;
x++;
if(x >= canvas.width) {
x = 0;
y++;
}
if(y >= canvas.height) {
y=0;
}
}
function updateColors() {
colors[0] = document.querySelector('#color1').value;
colors[1] = document.querySelector('#color2').value;
colors[2] = document.querySelector('#color3').value;
}
function updateDimensions() {
canvas.height = document.getElementById('height-input').value;
canvas.width = document.getElementById('width-input').value;
}
var speed = 10;
function updateSpeed() {
speed = document.getElementById('speed-input').value;
}
function updateBleed() {
bleed = document.getElementById('bleed-input').value;
}
function drawPixelAtRandom() {
const x = getRandomInt(0, canvas.width);
const y = getRandomInt(0, canvas.height);
ctx.fillStyle = getFillStyle(x,y);
ctx.fillRect(x,y,1,1);
}
</script>