-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwavySvg.js
164 lines (142 loc) · 3.92 KB
/
wavySvg.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
import { prng_alea } from "./external.js";
import SvgPath from "./svgPath.js";
let r = () => Math.random();
export function resetSeed(seed) {
r = seed === undefined ? () => Math.rand() : prng_alea(seed);
}
export function randControl({
minLen = 200,
maxLen = 500,
maxShift = 0.25,
angleRange = 0.5,
}) {
const angle = (r() * Math.PI - Math.PI / 2) * angleRange;
const length = r() * (maxLen - minLen) + minLen;
const shift = r() * ((length / 2) * maxShift);
// soh cah toa
const x1 = -Math.cos(angle) * (length / 2 + shift);
const y1 = Math.sin(angle) * (length / 2 + shift);
const x2 = Math.cos(angle) * (length / 2 - shift);
const y2 = -Math.sin(angle) * (length / 2 - shift);
return { x1, y1, x2, y2 };
}
export function randControls(num, options) {
return [...Array(num)].map(() => randControl(options));
}
export function randHeight(HH, range) {
return r() * range * HH + HH * ((1 - range) / 2);
}
export function randomPoints(WW, HH, points, { heightRange = 0.5 }) {
if (points < 2) throw "Must be at least 2";
const extraPoints = points - 2;
const minSpace = 0.25;
const pointsX = [0, WW];
function addPoint() {
const pointsDX = [];
for (let i = 0; i < pointsX.length - 1; ++i) {
pointsDX.push(pointsX[i + 1] - pointsX[i]);
}
const pointsDXMax = Math.max(...pointsDX);
const pointsDXIndex = pointsDX.indexOf(pointsDXMax);
const newPointX =
pointsX[pointsDXIndex] +
r() * pointsDXMax * (1 - minSpace * 2) +
pointsDXMax * minSpace;
pointsX.splice(pointsDXIndex + 1, 0, Math.round(newPointX));
}
for (let i = 0; i < extraPoints; ++i) {
addPoint();
}
return pointsX.map((x) => ({ x, y: randHeight(HH, heightRange) }));
}
export function wavySvg(config) {
const {
seed,
boxW,
boxH,
points,
angleRange,
controlMinRate,
controlMaxLen,
controlMaxShift,
heightRange,
} = config;
const fillPath = new SvgPath();
resetSeed(seed);
const p = randomPoints(boxW, boxH, points, { heightRange });
const c = randControls(points, {
minLen: controlMinRate * controlMaxLen,
maxLen: controlMaxLen,
angleRange,
maxShift: controlMaxShift,
});
fillPath.move(p[0].x, p[0].y);
for (let i = 1; i < points; ++i) {
const p0 = p[i - 1];
const c0 = c[i - 1];
const p1 = p[i];
const c1 = c[i];
fillPath.curve(
p0.x + c0.x2,
p0.y + c0.y2,
p1.x + c1.x1,
p1.y + c1.y1,
p1.x,
p1.y
);
}
const strokePath = fillPath.clone();
fillPath.line(boxW, boxH);
fillPath.line(0, boxH);
fillPath.end();
const result = {
config,
fillPath: fillPath.result(),
strokePath: strokePath.result(),
debugPoints: () => {
const points = [];
fillPath.instructions.forEach(([command, ...args]) => {
if (command === "M") points.push(args);
else if (command === "C") {
points.push([args[4], args[5]]);
}
});
return points;
},
debugControlPoints: () => {
const points = [];
fillPath.instructions.forEach(([command, ...args]) => {
if (command === "C") {
points.push([args[0], args[1]]);
points.push([args[2], args[3]]);
}
});
return points;
},
debugControlLines: () => {
const points = [];
strokePath.instructions.forEach(([command, ...args]) => {
if (command === "M") {
points.push([args[0], args[1]]);
} else if (command === "C") {
points.push([args[0], args[1]]);
points.push([args[2], args[3]]);
points.push([args[4], args[5]]);
points.push([args[4], args[5]]);
}
});
points.pop();
let lines = [];
for (let i = 0; i < points.length; i += 2) {
lines.push([
points[i][0],
points[i][1],
points[i + 1][0],
points[i + 1][1],
]);
}
return lines;
},
};
return result;
}