-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
142 lines (113 loc) · 3.12 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
console.clear();
import { SVG } from "https://cdn.skypack.dev/@svgdotjs/svg.js";
import {
random,
map,
spline,
pointsInPath
} from "https://cdn.skypack.dev/@georgedoescode/[email protected]";
import tinycolor from "https://cdn.skypack.dev/[email protected]";
import "https://cdn.skypack.dev/@svgdotjs/svg.filter.js";
import copy from "https://cdn.skypack.dev/[email protected]";
import { gsap } from "https://cdn.skypack.dev/[email protected]";
const svg = SVG(".canvas");
const { width, height } = svg.viewbox();
const regenerateBtn = document.querySelector(".regenerate");
const downloadBtn = document.querySelector(".download");
function wave(start, end, gradient) {
const numSteps = random(4, 8, true);
const step = 1 / numSteps;
const randomRange = random(32, 64);
const points = [];
let pointPosition = 0;
for (let i = 0; i <= numSteps; i++) {
const step = map(i, 0, numSteps, 0, 1);
let x = lerp(start.x, end.x, step);
let y = lerp(start.y, end.y, step);
if (i !== 0 && i !== numSteps) {
x += random(-randomRange, randomRange);
y += random(-randomRange, randomRange);
}
points.push({ x, y });
}
const pathData =
spline(points, 1, false) + `L ${end.x} ${height} L ${start.x} ${height} Z`;
const path = svg.path(pathData).attr("fill", gradient);
}
function lerp(v0, v1, t) {
return v0 * (1 - t) + v1 * t;
}
function generate() {
const numWaves = 7;
const base = tinycolor(`hsl(${random(0, 360)}, 65%, 55%)`);
const colors = base.analogous(6);
svg.rect(width, height).fill(random(colors).clone().darken(40).toString());
for (let i = 0; i < numWaves; i++) {
const randomOffset = random(-50, 50);
const originY = map(i, 0, numWaves, -height / 2, height / 3) + randomOffset;
const endY = map(i, 0, numWaves, 0, 1000) + randomOffset;
const color = random(colors).clone();
if (i < 3) {
color.darken(50).desaturate(10);
}
const gradientOffset = map(i, 0, numWaves, 0.1, 1);
let gradient = svg.gradient("linear", function (add) {
add.stop(0, color.clone().lighten(30).toHexString());
add.stop(gradientOffset, color.clone().spin(60).toHexString());
});
gradient.from(0.5, 0).to(0, 1);
wave({ x: 0, y: originY }, { x: width, y: endY }, gradient);
}
}
generate();
regenerateBtn.addEventListener("click", () => {
svg.clear();
generate();
});
downloadBtn.addEventListener("click", () => {
copy(svg.node.outerHTML);
downloadBtn.innerHTML = "Copied to Clipboard!";
setTimeout(() => {
downloadBtn.innerHTML = "Copy SVG";
}, 1500);
});
gsap.fromTo(
".controls",
{
opacity: 0,
y: 24
},
{
opacity: 1,
y: 0,
ease: "back.out(1.7)",
delay: 0.175
}
);
gsap.fromTo(
"button",
{
opacity: 0,
y: 12
},
{
opacity: 1,
y: 0,
stagger: 0.125,
delay: 0.175,
ease: "back.out(1.7)"
}
);
gsap.fromTo(
"p",
{
opacity: 0,
y: 12
},
{
opacity: 1,
y: 0,
ease: "back.out(1.7)",
delay: 0.175
}
);