forked from w3c/csswg-drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradius-expansion.html
More file actions
416 lines (364 loc) · 11.7 KB
/
radius-expansion.html
File metadata and controls
416 lines (364 loc) · 11.7 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Radius expansion algorithms</title>
<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;
}
article {
padding: 1em;
}
input:not([type]) {
font: 100%/1.5 Consolas, Monaco, monospace;
width: 80ch;
margin-bottom: .5em;
}
</style>
</head>
<body>
<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-same-axis">
Percentage of same axis
</label>
<label>
<input type="radio" name="algorithm" value="elika">
Elika’s Interpolation based on rounded/straight ratio
</label>
<label>
<input type="radio" name="algorithm" value="oriol">
Oriol's variant of current spec (adding a cubic factor)
</label>
<label>
<input type="radio" name="algorithm" value="oriol2">
Oriol's variant of current spec (adding a linear factor)
</label>
<label>
<input type="radio" name="algorithm" value="spectrum">
Noam's old/new spectrum
</label>
<label>
<input type="radio" name="algorithm" value="spectrum2">
Noam's old/new spectrum, preserving circular corners
</label>
</form>
<output id="output"></output>
<script>
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: 60, spread: 30, borderRadius: "20px 20px 40px 40px"},
{width: 500, height: 50, spread: 30, borderRadius: "1px 1px 49px 49px"},
{width: 500, height: 60, spread: 30, borderRadius: "0px 0px 30px 30px"},
{width: 250, height: 35, spread: 50, borderRadius: "0px 0px 25px 25px"},
{width: 500, height: 50, spread: 30, borderRadius: "50%"},
{width: 500, height: 50, spread: 30, borderRadius: "50% 50% 1px 50%"},
{width: 200, height: 40, spread: 50, borderRadius: "0 100% 0 100%"},
{width: 200, height: 40, spread: 50, borderRadius: "10% 90% / 5%"},
{width: 200, height: 40, spread: 50, borderRadius: "0 50% / 50%"},
{width: 0, height: 0, spread: 30, borderRadius: "50px 0px 0px 0px"},
{width: 500, height: 500, spread: 30, borderRadius: "50px"},
{width: 250, height: 30, spread: 100, borderRadius: "10px"},
];
function show({incremental = false} = {}) {
for (let i = 0, testCase; testCase = testCases[i]; i++) {
let container = output.children[i];
if (!container) {
container = document.createElement("article");
container.id = `testcase${i}`;
output.appendChild(container);
}
if (!incremental) {
let input = document.createElement("input");
input.value = JSON.stringify(testCase);
input.oninput = () => {
try {
testCases[i] = JSON.parse(input.value);
show({incremental: true});
}
catch (e) {}
};
container.append(input);
}
Array.from(container.querySelectorAll("div")).forEach(e => e.remove());
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";
container.appendChild(outer);
}
else {
outer.style.backgroundColor = "#000";
outer.style.borderStyle = "solid";
outer.style.borderWidth = testCase.spread + "px";
outer.style.width = "max-content";
container.appendChild(outer);
outer.style.borderRadius = resolve(testCase, inner);
if (!outer.style.borderRadius) {
outer.style.outline = "solid red";
}
}
}
}
function parseCorner(value, testCase) {
const raw = value.split(" ");
if (raw.length === 1) {
raw[1] = raw[0];
}
return [
parseLength(raw[0], testCase.width),
parseLength(raw[1], testCase.height),
];
}
function parseLength(value, percentageBasis) {
if (value.endsWith("%")) {
return parseFloat(value) * percentageBasis / 100;
}
return parseFloat(value);
}
function resolve(testCase, box) {
const cs = getComputedStyle(box);
// Corners to array[2] of radii
const radii = {
topLeft: parseCorner(cs.borderTopLeftRadius, testCase),
topRight: parseCorner(cs.borderTopRightRadius, testCase),
bottomLeft: parseCorner(cs.borderBottomLeftRadius, testCase),
bottomRight: parseCorner(cs.borderBottomRightRadius, testCase),
};
// Normalize radii that add up to > 100%
let 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 (Number.isNaN(f)) {
f = 0;
}
if (f < 1) {
for (let corner in radii) {
radii[corner] = radii[corner].map(v => v * f);
}
}
let r = {
topLeft: [],
topRight: [],
bottomLeft: [],
bottomRight: [],
};
const algorithm = document.forms[0].elements.algorithm.value;
let {width, height} = testCase;
let spreadWidth = width + testCase.spread * 2;
let spreadHeight = height + testCase.spread * 2;
let percentageSameAxis = {};
for (let corner in radii) {
let c = radii[corner];
let [rx, ry] = c;
let px = rx / width;
let py = ry / height;
percentageSameAxis[corner] = [px * spreadWidth, py * spreadHeight];
}
let currentSpec = {};
for (let corner in radii) {
currentSpec[corner] = radii[corner].map(value => {
if (value >= testCase.spread) {
return value + testCase.spread;
}
let r = value / testCase.spread;
return value + testCase.spread * (1 + (r - 1)**3);
});
}
if (algorithm === "increase-by-spread") {
for (let corner in radii) {
r[corner] = radii[corner].map(v => v + testCase.spread);
}
}
else if (algorithm === "old-spec") {
for (let corner in radii) {
let c = radii[corner];
r[corner] = c[0] + c[1] === 0 ? [0, 0] : [c[0] + testCase.spread, c[1] + testCase.spread];
}
}
else if (algorithm === "percentage-same-axis") {
r = percentageSameAxis;
}
else if (algorithm === "current-spec") {
r = currentSpec;
}
else if (algorithm === "elika") {
let {width, height} = testCase;
let spreadWidth = width + testCase.spread * 2;
let spreadHeight = height + testCase.spread * 2;
let straights = {
top: width - radii.topLeft[0] - radii.topRight[0],
bottom: width - radii.bottomLeft[0] - radii.bottomRight[0],
left: height - radii.topLeft[1] - radii.bottomLeft[1],
right: height - radii.topRight[1] - radii.bottomRight[1],
}
function getStraightSegment(corner, axis) {
/*
Example straight segment returned:
topLeft, 0 --> top
topLeft, 1 --> left
bottomRight, 0 --> bottom
bottomRight, 1 --> right
*/
let parts = corner.split(/(?=[A-Z])/).map(part => part.toLowerCase());
return straights[parts[axis]];
}
for (let corner in radii) {
r[corner] = radii[corner].map((value, axis) => {
let straight = getStraightSegment(corner, axis);
let ratio = straight / value;
ratio = Math.min(ratio, 1);
let ret = ratio * currentSpec[corner][axis] + (1 - ratio) * percentageSameAxis[corner][axis];
return Math.min(ret, straight + value + testCase.spread);
});
}
}
else if (algorithm === "oriol") {
for (let corner in radii) {
let coverage = Math.min(
2 * radii[corner][0] / testCase.width,
2 * radii[corner][1] / testCase.height,
) || 0;
r[corner] = radii[corner].map(value => {
if (value >= testCase.spread || coverage >= 1) {
return value + testCase.spread;
}
let r = (1 - value / testCase.spread) * (1 - coverage);
return value + testCase.spread * (1 - r**3);
});
}
}
else if (algorithm === "oriol2") {
for (let corner in radii) {
let coverage = Math.min(
2 * radii[corner][0] / testCase.width,
2 * radii[corner][1] / testCase.height,
) || 0;
r[corner] = radii[corner].map(value => {
if (value >= testCase.spread || coverage >= 1) {
return value + testCase.spread;
}
let r = 1 - value / testCase.spread;
return value + testCase.spread * (1 - r**3 * (1 - coverage));
});
}
}
else if (algorithm === "spectrum") {
const map_dim = (radius, dim) => {
// If there's no radius, there's no spread to apply.
if (radius === 0) {
return 0;
}
// Calculate the radius's ratio to the spread, clamping at 1.
const spreadRatio = 1 - Math.min(1, radius / testCase.spread);
// Calculate the diameter's ratio to the overall dimension, clamping at 1.
const dimRatio = Math.min(1, (2 * radius) / dim);
// These factors determine the amount of easing. They both approach 0
// as their respective ratios approach 1, which reduces the easing effect.
const spreadEasingFactor = spreadRatio ** 3;
const dimEasingFactor = 1 - dimRatio ** 3;
// The total reduction in spread is the product of the two easing factors.
const totalReduction = dimEasingFactor * spreadEasingFactor;
// Apply the reduction to the spread and add it to the original radius.
const easedSpread = testCase.spread * (1 - totalReduction);
return radius + easedSpread;
}
const map = ([h, v]) => {
return [map_dim(h, testCase.width), map_dim(v, testCase.height)];
}
r = {
topLeft: map(radii.topLeft),
topRight: map(radii.topRight),
bottomRight: map(radii.bottomRight),
bottomLeft: map(radii.bottomLeft)
};
}
else if (algorithm === "spectrum2") {
for (let corner in radii) {
// Calculate the min diameter's ratio to the overall dimension, clamping at 1.
const dimRatio = Math.min(
1,
(2 * radii[corner][0]) / testCase.width,
(2 * radii[corner][1]) / testCase.height,
);
r[corner] = radii[corner].map(radius => {
// If there's no radius, there's no spread to apply.
if (radius === 0) {
return 0;
}
// Calculate the radius's ratio to the spread, clamping at 1.
const spreadRatio = 1 - Math.min(1, radius / testCase.spread);
// These factors determine the amount of easing. They both approach 0
// as their respective ratios approach 1, which reduces the easing effect.
const spreadEasingFactor = spreadRatio ** 3;
const dimEasingFactor = 1 - dimRatio ** 3;
// The total reduction in spread is the product of the two easing factors.
const totalReduction = dimEasingFactor * spreadEasingFactor;
// Apply the reduction to the spread and add it to the original radius.
const easedSpread = testCase.spread * (1 - totalReduction);
return radius + easedSpread;
});
}
}
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", evt => show({incremental: true}));
</script>
</body>
</html>