-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
227 lines (197 loc) · 5.78 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Canvas Experiments</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Helvetica, Arial, sans-serif;
font-size: 62.5%;
color: #000;
}
canvas {
display: block;
}
form {
font-size: 130%;
background-color: rgba(0,0,0,0.75);
position: absolute;
width: 100%;
padding: 15px 0 0;
box-sizing: border-box;
display: flex;
display: -webkit-flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
-webkit-flex-direction: row;
-webkit-flex-wrap: wrap;
-webkit-justify-content: center;
transition: opacity 0.5s;
}
form.disabled {
opacity: 0;
}
input {
margin-left: 5px;
border: 0;
padding: 3px;
width: 5em;
font-size: 100%;
border-radius: 2px;
}
label {
color: #fff;
margin: 0 15px 15px;
}
</style>
</head>
<body>
<form class="controls">
<label>Grid size:<input name="grid" type="number" min="1" max="30" step="1" inputmode="numeric"></label>
<label>Radius:<input name="radius" type="number" min="0.1" max="2" step="0.1" inputmode="numeric"></label>
<label>Color offset:<input name="colorOffset" type="number" min="0" max="6" step="1" inputmode="numeric"></label>
<label>Frequency:<input name="frequency" type="number" min="0" max="20" step="1" inputmode="numeric"></label>
<label>Horizontal gradient:<input name="hGradient" type="number" min="0" max="1" step="0.1" inputmode="numeric"></label>
<label>Vertical gradient:<input name="vGradient" type="number" min="0" max="1" step="0.1" inputmode="numeric"></label>
<label>Horizontal gradient start:<input name="hStart" type="number" min="0.5" max="2" step="0.01" inputmode="numeric"></label>
<label>Vertical gradient start:<input name="vStart" type="number" min="0.5" max="2" step="0.01" inputmode="numeric"></label>
</form>
<canvas id="canvas-one"></canvas>
<script>
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
function showControls() {
if (editMode) {
return;
}
if (!controlsVisible) {
document.querySelector(".controls").classList.remove("disabled");
}
controlsVisible = true;
clearTimeout(controlTimer);
controlTimer = setTimeout(hideControls, 3000);
}
function hideControls() {
controlsVisible = false;
document.querySelector(".controls").classList.add("disabled");
}
function startEditMode() {
showControls();
editMode = true;
clearTimeout(controlTimer);
}
function endEditMode() {
editMode = false;
hideControls();
}
function initControls() {
var d = document;
Object.keys(config).forEach(function(key) {
var el = d.querySelector("[name=" + key + "]");
el.setAttribute("value", String(config[key]));
el.addEventListener("input", function(e) {
config[key] = Number(el.value);
drawAll();
});
});
}
function fillAll(filler) {
ctx.fillStyle = filler;
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fill();
}
function fillSpot(color, x, y, radius) {
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2, true);
ctx.fill();
}
function fillPoly(color, x, y, size) {
var sides = 6;
ctx.beginPath();
ctx.moveTo(x + size * Math.cos(0), y + size * Math.sin(0));
for (var i = 1; i <= sides; i++) {
ctx.lineTo(x + size * Math.cos(i * 2 * Math.PI / sides), y + size * Math.sin(i * 2 * Math.PI / sides));
}
ctx.fillStyle = color;
ctx.fill();
}
function prettyLinearGradient() {
var colors = [
{"name": "green", "rgb": "146,220,1"},
{"name": "turquoise", "rgb": "1,220,146"},
{"name": "blue", "rgb": "1,146,220"},
{"name": "violet", "rgb": "146,1,220"},
{"name": "purple", "rgb": "220,1,146"},
{"name": "orange", "rgb": "220,146,1"},
{"name": "yellow", "rgb": "220,220,1"}
];
var linear = ctx.createLinearGradient(0, 0, ctx.canvas.width, ctx.canvas.height);
var index = 0;
for (var c = 0, l = colors.length; c < l; c++) {
index = (c + config.colorOffset) % l;
linear.addColorStop(c / l, "rgba("+colors[index].rgb+",1)")
}
linear.addColorStop(1, "rgba("+colors[config.colorOffset].rgb+",1)");
return linear;
}
function drawPoints() {
var gradient = prettyLinearGradient();
var cw = canvas.width;
var ch = canvas.height;
var grid = Math.round(config.grid * Math.log(cw));
var maxRadius = config.radius * grid;
for (var y = 0, row = 0; y < ch - grid/2; y += Math.sqrt(3)/2 * grid, row++) {
var normalizedHeight = y / ch;
for (var x = 0; x < cw - grid/2; x += grid) {
var normalizedWidth = x / cw;
var fraction = (Math.pow(Math.sin(config.frequency*Math.log(normalizedWidth)), 2) + Math.log(1/normalizedWidth+1)) * Math.log(config.hStart + config.hGradient*normalizedWidth)*Math.log(config.vStart + config.vGradient * normalizedHeight);
var radius = fraction * maxRadius;
var xDist = 0;
if (row % 2 === 0) {
xDist = grid / 2;
}
fillSpot(gradient, x + xDist, y + grid / 2, radius);
}
}
}
function drawAll() {
fillAll("rgb(254,253,249)");
drawPoints();
}
////////
var controlTimer = setTimeout(hideControls, 3000);
var canvas = document.getElementById("canvas-one");
var ctx = canvas.getContext("2d");
var controlsVisible = true;
var editMode = false;
var config = {
colorOffset: 0,
grid: 6,
radius: 1.2,
frequency: 5,
hGradient: 0.6,
vGradient: 0.7,
hStart: 1.06,
vStart: 1.05
};
window.addEventListener('resize', function(){resizeCanvas(); drawAll()}, false);
window.addEventListener('orientationchange', function(){resizeCanvas(); drawAll()}, false);
window.addEventListener('mousemove', showControls, false);
window.addEventListener('click', showControls, false);
window.addEventListener('touchend', showControls, false);
Array.prototype.slice.call(document.querySelectorAll(".controls input")).forEach(function(inputEl) {
inputEl.addEventListener("focus", startEditMode);
inputEl.addEventListener("blur", endEditMode);
});
resizeCanvas();
drawAll();
initControls();
</script>
</body>
</html>