-
Notifications
You must be signed in to change notification settings - Fork 707
/
Copy pathradius-expansion-continuity.html
343 lines (340 loc) · 11.6 KB
/
radius-expansion-continuity.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<!DOCTYPE html>
<style>
body {
display: flex;
height: 100vh;
margin: 0;
}
#output {
flex: 1;
border-left: 1px solid;
margin-left: 1em;
overflow: auto;
}
#output, form {
padding: 1em;
}
hr {
border: none;
border-top: 1px dotted;
}
label {
display: flex;
width: max-content;
margin: .5em 0;
}
</style>
<form>
<label>
<input type="radio" name="algorithm" value="do-not-polyfill" checked>
Do not polyfill
</label>
<label>
<input type="radio" name="algorithm" value="increase-by-spread">
Increase radius by spread
</label>
<label>
<input type="radio" name="algorithm" value="old-spec">
Old spec (discontinuous)
</label>
<label>
<input type="radio" name="algorithm" value="current-spec">
Current spec
</label>
<label>
<input type="radio" name="algorithm" value="percentage-min-axis">
Percentage of min axis
</label>
<label>
<input type="radio" name="algorithm" value="percentage-max-axis">
Percentage of max axis
</label>
<label>
<input type="radio" name="algorithm" value="percentage-same-axis">
Percentage of same axis
</label>
<label>
<input type="radio" name="algorithm" value="percentage-same-axis-ratio">
Percentage of same axis,<br>ceiling to keep ratio if possible
</label>
<label>
<input type="radio" name="algorithm" value="linear-edge-portion">
Cap by rounded portion of edges (linear)
</label>
<label>
<input type="radio" name="algorithm" value="cubic-edge-portion">
Cap by rounded portion of edges (cubic)
</label>
<label>
<input type="radio" name="algorithm" value="linear-edge-ratio">
Cap by rounded/straight ratio of edges (linear)
</label>
<label>
<input type="radio" name="algorithm" value="cubic-edge-ratio">
Cap by rounded/straight ratio of edges (cubic)
</label>
<label>
<input type="radio" name="algorithm" value="interpolate-straight-rounded-portion">
Interpolation based on rounded portion of edges
</label>
<label>
<input type="radio" name="algorithm" value="interpolate-straight-rounded-ratio">
Interpolation based on rounded/straight ratio
</label>
</form>
<div id="output"></div>
<script>
const output = document.getElementById("output");
const {algorithm} = document.forms[0].elements;
const testCases = [
{width: 50, height: 50, spread: 50, borderRadius: "0px"},
{width: 50, height: 50, spread: 50, borderRadius: "1px"},
{width: 10, height: 10, spread: 70, borderRadius: "100%"},
{width: 200, height: 40, spread: 50, borderRadius: "100px / 20px"},
{width: 200, height: 40, spread: 50, borderRadius: "20px / 4px"},
{width: 500, height: 50, spread: 30, borderRadius: "15px"},
{width: 500, height: 50, spread: 30, borderRadius: "25px"},
{width: 500, height: 50, spread: 30, borderRadius: "1px 1px 49px 49px"},
{width: 500, height: 50, spread: 30, borderRadius: "50%"},
{width: 500, height: 50, spread: 30, borderRadius: "50% 50% 1px 50%"},
];
function show() {
output.textContent = "";
for (let testCase of testCases) {
output.append(JSON.stringify(testCase));
const inner = document.createElement("div");
inner.style.width = testCase.width + "px";
inner.style.height = testCase.height + "px";
inner.style.borderRadius = testCase.borderRadius;
inner.style.backgroundColor = "#fff";
const outer = document.createElement("div");
outer.appendChild(inner);
if (algorithm.value === "do-not-polyfill") {
inner.style.boxShadow = `0 0 0 ${testCase.spread}px #000`;
outer.style.padding = testCase.spread + "px";
output.appendChild(outer);
} else {
outer.style.backgroundColor = "#000";
outer.style.borderStyle = "solid";
outer.style.borderWidth = testCase.spread + "px";
outer.style.width = "max-content";
output.appendChild(outer);
outer.style.borderRadius = resolve(testCase, inner);
}
output.append(document.createElement("hr"));
}
}
function resolve(testCase, element) {
function parseLength(value, percentageBasis) {
if (value.endsWith("%")) {
return parseFloat(value) * percentageBasis / 100;
}
return parseFloat(value);
}
function parseCorner(value) {
const raw = value.split(" ");
if (raw.length === 1) {
raw[1] = raw[0];
}
return [
parseLength(raw[0], testCase.width),
parseLength(raw[1], testCase.height),
];
}
const cs = getComputedStyle(element);
const radii = {
topLeft: parseCorner(cs.borderTopLeftRadius),
topRight: parseCorner(cs.borderTopRightRadius),
bottomLeft: parseCorner(cs.borderBottomLeftRadius),
bottomRight: parseCorner(cs.borderBottomRightRadius),
};
const f = Math.min(
testCase.width / (radii.topLeft[0] + radii.topRight[0]),
testCase.width / (radii.bottomLeft[0] + radii.bottomRight[0]),
testCase.height / (radii.topLeft[1] + radii.bottomLeft[1]),
testCase.height / (radii.topRight[1] + radii.bottomRight[1])
);
if (f < 1) {
for (let corner in radii) {
radii[corner] = radii[corner].map(v => v * f);
}
}
let r;
switch (algorithm.value) {
case "increase-by-spread": {
const map = (value) => value + testCase.spread;
r = {
topLeft: radii.topLeft.map(map),
topRight: radii.topRight.map(map),
bottomLeft: radii.bottomLeft.map(map),
bottomRight: radii.bottomRight.map(map),
};
break;
}
case "old-spec": {
const map = (value) => {
if (value == 0)
return 0;
return value + testCase.spread;
}
r = {
topLeft: radii.topLeft.map(map),
topRight: radii.topRight.map(map),
bottomLeft: radii.bottomLeft.map(map),
bottomRight: radii.bottomRight.map(map),
};
break;
}
case "current-spec": {
const map = (value) => {
if (value >= testCase.spread) {
return value + testCase.spread;
}
let r = value / testCase.spread;
return value + testCase.spread * (1 + (r - 1)**3);
}
r = {
topLeft: radii.topLeft.map(map),
topRight: radii.topRight.map(map),
bottomLeft: radii.bottomLeft.map(map),
bottomRight: radii.bottomRight.map(map),
};
break;
}
case "percentage-min-axis": {
const min = Math.min(testCase.width, testCase.height);
const ratio = 1 + 2 * testCase.spread / min;
const map = (value) => value * ratio;
r = {
topLeft: radii.topLeft.map(map),
topRight: radii.topRight.map(map),
bottomLeft: radii.bottomLeft.map(map),
bottomRight: radii.bottomRight.map(map),
};
break;
}
case "percentage-max-axis": {
const min = Math.max(testCase.width, testCase.height);
const ratio = 1 + 2 * testCase.spread / min;
const map = (value) => value * ratio;
r = {
topLeft: radii.topLeft.map(map),
topRight: radii.topRight.map(map),
bottomLeft: radii.bottomLeft.map(map),
bottomRight: radii.bottomRight.map(map),
};
break;
}
case "percentage-same-axis": {
const xRatio = 1 + 2 * testCase.spread / testCase.width;
const yRatio = 1 + 2 * testCase.spread / testCase.height;
const map = ([x, y]) => [x * xRatio, y * yRatio];
r = {
topLeft: map(radii.topLeft),
topRight: map(radii.topRight),
bottomLeft: map(radii.bottomLeft),
bottomRight: map(radii.bottomRight),
};
break;
}
case "percentage-same-axis-ratio": {
const xRatio = 1 + 2 * testCase.spread / testCase.width;
const yRatio = 1 + 2 * testCase.spread / testCase.height;
const map = ([x, y]) => [x * xRatio, y * yRatio];
const map2 = ([x, y]) => [x * yRatio, y * xRatio];
r = {
topLeft: map(radii.topLeft),
topRight: map(radii.topRight),
bottomLeft: map(radii.bottomLeft),
bottomRight: map(radii.bottomRight),
};
const other = {
topLeft: map2(radii.topLeft),
topRight: map2(radii.topRight),
bottomLeft: map2(radii.bottomLeft),
bottomRight: map2(radii.bottomRight),
};
const shadowWidth = testCase.width + 2 * testCase.spread;
const shadowHeight = testCase.height + 2 * testCase.spread;
const adjust = (prop1, prop2, idx) => {
if (r[prop1][idx] < other[prop1][idx] || r[prop2][idx] < other[prop2][idx]) {
const desiredGrowths = [
Math.max(0, other[prop1][idx] - r[prop1][idx]),
Math.max(0, other[prop2][idx] - r[prop2][idx]),
];
const desiredGrowth = desiredGrowths[0] + desiredGrowths[1];
const available = (idx ? shadowHeight : shadowWidth) - r[prop1][idx] - r[prop2][idx];
const factor = Math.min(1, available / desiredGrowth);
r[prop1][idx] += desiredGrowths[0] * factor;
r[prop2][idx] += desiredGrowths[1] * factor;
}
}
adjust("topLeft", "topRight", 0);
adjust("bottomLeft", "bottomRight", 0);
adjust("topLeft", "bottomLeft", 1);
adjust("topRight", "bottomRight", 1);
break;
}
case "linear-edge-portion":
case "cubic-edge-portion":
case "linear-edge-ratio":
case "cubic-edge-ratio":
case "interpolate-straight-rounded-portion":
case "interpolate-straight-rounded-ratio": {
// The portion of each edge that is rounded
const portion = {
top: (radii.topLeft[0] + radii.topRight[0]) / testCase.width,
right: (radii.topRight[1] + radii.bottomRight[1]) / testCase.height,
bottom: (radii.bottomLeft[0] + radii.bottomRight[0]) / testCase.width,
left: (radii.topLeft[1] + radii.bottomLeft[1]) / testCase.height,
};
const map = (value, ratio, idx) => {
if (value >= testCase.spread) {
return value + testCase.spread;
}
switch (algorithm.value) {
case "linear-edge-ratio":
ratio = Math.min(ratio / (1 - ratio), 1);
// fallthrough
case "linear-edge-portion": {
let r = value / testCase.spread;
return value + testCase.spread * Math.max(1 + (r - 1)**3, ratio);
}
case "cubic-edge-ratio":
ratio = Math.min(ratio / (1 - ratio), 1);
// fallthrough
case "cubic-edge-portion": {
let r = Math.max(value / testCase.spread, ratio);
return value + testCase.spread * (1 + (r - 1)**3);
}
case "interpolate-straight-rounded-ratio":
ratio = 1 - Math.min((1 - ratio) / ratio, 1);
// fallthrough
case "interpolate-straight-rounded-portion": {
ratio = 1 - ratio;
const r = value / testCase.spread;
const cubic = value + testCase.spread * (1 + (r - 1)**3);
const pctratio = 1 + 2 * testCase.spread / ((idx == 0) ? testCase.width : testCase.height);
const percentage = value * pctratio;
return (1 - ratio) * percentage + ratio * cubic;
}
}
}
r = {
topLeft: radii.topLeft.map((r, idx) => map(r, Math.max(portion.top, portion.left), idx)),
topRight: radii.topRight.map((r, idx) => map(r, Math.max(portion.top, portion.right), idx)),
bottomLeft: radii.bottomLeft.map((r, idx) => map(r, Math.max(portion.bottom, portion.left), idx)),
bottomRight: radii.bottomRight.map((r, idx) => map(r, Math.max(portion.bottom, portion.right), idx)),
};
break;
}
default: {
throw "Not implemented: " + algorithm.value;
break;
}
}
return `${r.topLeft[0]}px ${r.topRight[0]}px ${r.bottomRight[0]}px ${r.bottomLeft[0]}px / ${r.topLeft[1]}px ${r.topRight[1]}px ${r.bottomRight[1]}px ${r.bottomLeft[1]}px`;
}
show();
document.querySelector("form").addEventListener("change", show);
</script>