-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
314 lines (252 loc) · 10.6 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OGL Mouse Trail</title>
<style>
:root {
overflow: hidden;
height: 100%;
}
body {
margin: 0;
}
</style>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button style="top:0" id="addLine" class="ui">Add Line</button>
<button style="right:0" id="removeLine" class="ui">Remove Line</button>
<button style="right: 50%" id="randomColor" class="ui">Random Background Color</button>
<script type="module">
import {
Renderer,
Camera,
Orbit,
Transform,
Geometry,
Vec3,
Color,
Polyline
} from "https://cdn.jsdelivr.net/npm/ogl@0.0.32/dist/ogl.mjs";//from "../../OGL/index.js";
const vertex = `
attribute vec3 position;
attribute vec3 next;
attribute vec3 prev;
attribute vec2 uv;
attribute float side;
uniform vec2 uResolution;
uniform float uDPR;
uniform float uThickness;
vec4 getPosition() {
vec2 aspect = vec2(uResolution.x / uResolution.y, 1);
vec2 nextScreen = next.xy * aspect;
vec2 prevScreen = prev.xy * aspect;
// determine the direction from the previous to the next point
vec2 tangent = normalize(nextScreen - prevScreen);
// rotate it 90 degrees to find the normal
vec2 normal = vec2(-tangent.y, tangent.x);
normal /= aspect;
// line's width, without this, the line width = whole screen
normal *= 1.0 - pow(abs(uv.y - 0.5) * 2.0, 2.0);
float pixelWidth = 1.0 / (uResolution.y / uDPR);
normal *= pixelWidth * uThickness;
// When the points are on top of each other,
// shrink the line to avoid artifacts.
float dist = length(nextScreen - prevScreen);
normal *= smoothstep(0.0, 0.02, dist);
vec4 current = vec4(position, 1);
// push our vertices apart along the normal
current.xy -= normal * side;
return current;
}
void main() {
gl_Position = getPosition();
}
`;
let lineAmount = 1;
//let buttons = document.getElementsByTagName("button");
let addButton = document.getElementById("addLine");
let removeButton = document.getElementById("removeLine");
let randomBGColorButton = document.getElementById("randomColor");
addButton.addEventListener("click", addLine)
removeButton.addEventListener("click", removeLine);
randomBGColorButton.addEventListener("click", getRandomBGColor);
let colors = ["#e09f7d", "#ef5d60", "#ec4067", "#a01a7d", "#311847", "#FF9AA2",
"#C7CEEA", "#FFDAC1", "#B5EAD7", "#C8C4E8", "#8DD8FF", "#FF677B",
"#8192A6", "#BB8188", "#75D0FF", "#6BDEE8", "#6BE8AC", "#FCFFF5",
"#CBDBD7", "#91AAC4", "#193441", "#3E606F", "#17162F", "#89346D",
"#C76058", "#FFB248", "#E8C475", "#C78C5D", "#8EB5FB", "#715DC7"];
// some colors from a pallete made by Chan Alex
// https://color.adobe.com/search?q=storm&t=tag
function getRandomBGColor(event) {
event.preventDefault();
for (let i = 0; i < rgb.length; i++) {
rgb[i] = Math.random();
}
gl.clearColor(rgb[0], rgb[1], rgb[2], 1);
}
function addLine(event) {
event.preventDefault();
//alert("add line button clicked");
if (lineAmount < 15) {
lineAmount++;
//testing
const line = {
spring: random(0.02, 0.1),
friction: random(0.7, 0.95),
mouseVelocity: new Vec3(),
mouseOffset: new Vec3(random(-1, 1) * 0.02)
};
// Create an array of Vec3s (eg [[0, 0, 0], ...])
const count = 20;
const points = (line.points = []);
let randColor = colors[Math.floor(Math.random() * colors.length)];
for (let i = 0; i < count; i++) points.push(new Vec3());
let totallyRandomColorChance = Math.floor(Math.random() * 10);
if (totallyRandomColorChance <= 2) {
let hexVal = Math.floor(Math.random() * 0xFFFFFF);
let randColor = "#" + hexVal.toString(16);
console.log(typeof(randColor));
console.log("generating totally random color");
}
console.log(randColor);
line.polyline = new Polyline(gl, {
points,
vertex,
uniforms: {
//uColor: { value: new Color(randColor) },
uColor: { value: new Color(randColor) },
uThickness: { value: random(20, 50) }
}
});
line.polyline.mesh.setParent(scene);
lines.push(line);
}
}
function removeLine(event) {
event.preventDefault();
//alert("REMOVE line button clicked");
if (lineAmount > 0) {
lineAmount--;
//line.polyline.mesh.setParent(scene);
console.log(lines);
//let templine = lines.pop();
//scene.removeChild(lines.length - 1);
let lastLine = lines[lines.length - 1];
scene.removeChild(lastLine.polyline.mesh)
lines.pop();
console.log(lastLine);
console.log(lines);
}
}
// Create the WebGL context, and add the canvas to the DOM
const renderer = new Renderer({ dpr: 2 });
const gl = renderer.gl;
document.body.appendChild(gl.canvas);
let rgb = [0.3, 0.05, 0.1]
//gl.clearColor(0.3, 0.05, 0.1, 1);
gl.clearColor(rgb[0], rgb[1], rgb[2], 1);
// Create camera and scene
//const camera = new Camera(gl);
//camera.position.z = 3;
//const controls = new Orbit(camera);
const scene = new Transform();
//let polyline;
let lines = [];
function resize() {
renderer.setSize(window.innerWidth, window.innerHeight);
// We call resize on the polylines to update their resolution uniforms
lines.forEach(line => line.polyline.resize());
}
window.addEventListener("resize", resize, false);
function random(a, b) {
const alpha = Math.random();
return a * (1.0 - alpha) + b * alpha;
}
let tempLineAmount = lineAmount;
// We're going to make a number of different coloured lines for fun.
colors.forEach(
(color, i) => {
if (i < tempLineAmount) {
// Store a few values for each lines' randomised spring movement
const line = {
spring: random(0.02, 0.1),
friction: random(0.7, 0.95),
mouseVelocity: new Vec3(),
mouseOffset: new Vec3(random(-1, 1) * 0.02)
};
// Create an array of Vec3s (eg [[0, 0, 0], ...])
const count = 20;
const points = (line.points = []);
for (let i = 0; i < count; i++) points.push(new Vec3());
line.polyline = new Polyline(gl, {
points,
vertex,
uniforms: {
uColor: { value: new Color(colors[Math.floor(Math.random() * colors.length)]) },
uThickness: { value: random(20, 50) }
}
});
line.polyline.mesh.setParent(scene);
lines.push(line);
}
});
// Call initial resize after creating the polyline to update
// its resolution uniforms
resize();
// Add handlers to get mouse position
const mouse = new Vec3();
if ("ontouchstart" in window) {
window.addEventListener("touchstart", updateMouse, false);
window.addEventListener("touchmove", updateMouse, false);
} else {
window.addEventListener("mousemove", updateMouse, false);
}
function updateMouse(e) {
if (e.changedTouches && e.changedTouches.length) {
e.x = e.changedTouches[0].pageX;
e.y = e.changedTouches[0].pageY;
}
if (e.x === undefined) {
e.x = e.pageX;
e.y = e.pageY;
}
// Get mouse value in -1 to 1 range, with y flipped
mouse.set(
(e.x / gl.renderer.width) * 2 - 1,
(e.y / gl.renderer.height) * -2 + 1,
0
);
}
const tmp = new Vec3();
// render scene in an update loop
requestAnimationFrame(update);
function update(t) {
requestAnimationFrame(update);
// Update polyline input points
lines.forEach(line => {
// Update polyline input points
for (let i = line.points.length - 1; i >= 0; i--) {
if (!i) {
// For the first point, spring ease it to the mouse position
tmp
.copy(mouse)
.add(line.mouseOffset)
.sub(line.points[i])
.multiply(line.spring);
line.mouseVelocity.add(tmp).multiply(line.friction);
line.points[i].add(line.mouseVelocity);
} else {
// The rest of the points ease to the point in front of them, making a line
line.points[i].lerp(line.points[i - 1], 0.9);
}
}
line.polyline.updateGeometry();
});
renderer.render({ scene });
}
</script>
</body>
</html>