E58E Update radius-expansion.html with variant of Noam's algorithm · w3c/csswg-drafts@c81c6ab · GitHub
[go: up one dir, main page]

Skip to content

Commit c81c6ab

Browse files
committed
Update radius-expansion.html with variant of Noam's algorithm
1 parent 2a29c11 commit c81c6ab

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

css-backgrounds-3/radius-expansion.html

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282
<input type="radio" name="algorithm" value="spectrum">
8383
Noam's old/new spectrum
8484
</label>
85+
<label>
86+
<input type="radio" name="algorithm" value="spectrum2">
87+
Noam's old/new spectrum, preserving circular corners
88+
</label>
8589
</form>
8690
<output id="output"></output>
8791
<script>
@@ -329,20 +333,6 @@
329333
}
330334
}
331335
else if (algorithm === "spectrum") {
332-
const old_spec = (value) => {
333-
if (value == 0)
334-
return 0;
335-
return value + testCase.spread;
336-
}
337-
338-
const new_spec = (value) => {
339-
if (value >= testCase.spread) {
340-
return value + testCase.spread;
341-
}
342-
let r = value / testCase.spread;
343-
return value + testCase.spread * (1 + (r - 1)**3);
344-
}
345-
346336
const map_dim = (radius, dim) => {
347337
// If there's no radius, there's no spread to apply.
348338
if (radius === 0) {
@@ -380,6 +370,39 @@
380370
bottomLeft: map(radii.bottomLeft)
381371
};
382372
}
373+
else if (algorithm === "spectrum2") {
374+
const axial_spread_factor = (radius, dim) => {
375+
// If there's no radius, there's no spread to apply.
376+
if (radius === 0) {
377+
return 0;
378+
}
379+
380+
// Calculate the radius's ratio to the spread, clamping at 1.
381+
const spreadRatio = 1 - Math.min(1, radius / testCase.spread);
382+
383+
// Calculate the diameter's ratio to the overall dimension, clamping at 1.
384+
const dimRatio = Math.min(1, (2 * radius) / dim);
385+
386+
// These factors determine the amount of easing. They both approach 0
387+
// as their respective ratios approach 1, which reduces the easing effect.
388+
const spreadEasingFactor = spreadRatio ** 3;
389+
const dimEasingFactor = 1 - dimRatio ** 3;
390+
391+
// The total reduction in spread is the product of the two easing factors.
392+
const totalReduction = dimEasingFactor * spreadEasingFactor;
393+
return 1 - totalReduction;
394+
}
395+
396+
for (let corner in radii) {
397+
let spread_factor = Math.min(
398+
axial_spread_factor(radii[corner][0], testCase.width),
399+
axial_spread_factor(radii[corner][1], testCase.height),
400+
);
401+
r[corner] = radii[corner].map(radius => {
402+
return radius + testCase.spread * spread_factor;
403+
});
404+
}
405+
}
383406

384407
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`;
385408
}

0 commit comments

Comments
 (0)
0