-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
207 lines (166 loc) · 5.24 KB
/
script.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { Dot } from "./dot.js";
import * as fonkyMath from "./fonkyMath.js";
import { Palettes } from "./colorPalettes.js";
import { getRandomColorFromPalette } from "./colorPalettes.js";
var sliderDotCount = document.getElementById("dotCount");
var outputDotCount = document.getElementById("outputDotCount");
outputDotCount.innerHTML = sliderDotCount.value;
var sliderDotScale = document.getElementById("dotScale");
var outputDotScale = document.getElementById("outputDotScale");
outputDotScale.innerHTML = sliderDotScale.value;
var sliderMaxUrge = document.getElementById("maxUrge");
var outputMaxUrge = document.getElementById("outputMaxUrge");
outputMaxUrge.innerHTML = sliderMaxUrge.value;
var sliderXBias = document.getElementById("xBias");
var outputXBias = document.getElementById("outputXBias");
outputXBias.innerHTML = sliderXBias.value;
var sliderYBias = document.getElementById("yBias");
var outputYBias = document.getElementById("outputYBias");
outputYBias.innerHTML = sliderYBias.value;
var selectColorPalette = document.getElementById("colorPalette");
CanvasRenderingContext2D.prototype.circle = function (x, y, radius) {
this.beginPath();
this.arc(x, y, radius, 0, Math.PI * 2, true);
this.fill();
}
var dotScale = 50;
var dotCount = 50;
const dots = [];
const canvas = document.getElementById("canvas");
var maxUrge = 10;
var xBias = -1.0;
var yBias = -1.0;
var colorPalette = Palettes.DEFAULT;
function draw() {
canvas.width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
canvas.width -= 16;
canvas.height = (window.innerHeight > 0) ? window.innerHeight : screen.height;
canvas.height *= 0.9;
if (canvas.getContext) {
const ctx = canvas.getContext("2d");
dotCount = sliderDotCount.value;
dotScale = sliderDotScale.value;
maxUrge = sliderMaxUrge.value;
xBias = parseFloat(sliderXBias.value);
yBias = parseFloat(sliderYBias.value);
//Drawing white background
ctx.fillStyle = `white`;
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
while (dots.length < dotCount) {
const coordinates = getNextCoordinates();
dots.push(new Dot(
coordinates[0],
coordinates[1],
Math.random(), //radius
getRandomColorFromPalette(colorPalette) //color
));
}
while (dots.length > dotCount) {
dots.pop();
}
for (var dot of dots) {
ctx.fillStyle = dot.color;
ctx.circle(dot.x, dot.y, dot.radius * dotScale);
}
}
}
draw();
/*
function chooses next coordinates with homemade function based on
randomly choosing step size and direction
then rolling for urge to go again
it stops rolling when urge < maxUrge
*/
function getNextCoordinates() {
var x, y;
var urgeToDraw = 0;
const StepSize = 10;
if (!Array.isArray(dots) || !dots.length) {
x = fonkyMath.getRandomIntMax(canvas.width);
y = fonkyMath.getRandomIntMax(canvas.height);
} else {
x = dots[dots.length-1].x;
y = dots[dots.length-1].y;
}
while (urgeToDraw < maxUrge) {
x += fonkyMath.getRandomIntInclusive(-StepSize, StepSize)+xBias;
y += fonkyMath.getRandomIntInclusive(-StepSize, StepSize)+yBias;
urgeToDraw += fonkyMath.getRandomIntMax(StepSize);
}
//clamping coordinates to canvas so if they go out of bounds they come up on the other side (snake like)
if (x > canvas.width) {
x -= canvas.width;
}
else if (x < 0) {
x += canvas.width;
}
if (y > canvas.height) {
y -= canvas.height;
}
else if (y < 0) {
y += canvas.height;
}
return [x,y];
}
function reroll() {
while (dots.length > 0) {
dots.pop();
}
}
function rerollColors() {
for (var dot of dots) {
dot.color = getRandomColorFromPalette(colorPalette);
}
}
window.onload = function () {loadColorPalettes()};
function loadColorPalettes() {
for (const key of Object.keys(Palettes)) {
var option = document.createElement("option");
option.value = key;
option.text = key;
selectColorPalette.appendChild(option);
}
}
selectColorPalette.oninput = function () {
const value = this.value;
colorPalette = Palettes[value];
rerollColors();
draw();
}
//sliders functions
sliderDotCount.oninput = function () {
outputDotCount.innerHTML = this.value;
draw();
}
sliderDotScale.oninput = function () {
outputDotScale.innerHTML = this.value;
draw();
}
sliderMaxUrge.oninput = function () {
outputMaxUrge.innerHTML = this.value;
reroll();
draw();
}
sliderXBias.oninput = function () {
outputXBias.innerHTML = this.value;
reroll();
draw();
}
sliderYBias.oninput = function () {
outputYBias.innerHTML = this.value;
reroll();
draw();
}
document.getElementById("buttonReload").addEventListener('click', () => {
reroll();
draw();
});
document.getElementById("buttonRerollColors").addEventListener('click', () => {
rerollColors();
draw();
});
function dlCanvas() {
const dt = canvas.toDataURL('image/jpg');
this.href = dt;
}
document.getElementById("dl").addEventListener('click', dlCanvas, false);