Skip to content

Commit e3e787f

Browse files
committed
Improve variant of Noam's algorithm in radius-expansion.html
1 parent c81c6ab commit e3e787f

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

css-backgrounds-3/radius-expansion.html

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -371,35 +371,35 @@
371371
};
372372
}
373373
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-
}
374+
for (let corner in radii) {
375+
// Calculate the min diameter's ratio to the overall dimension, clamping at 1.
376+
const dimRatio = Math.min(
377+
1,
378+
(2 * radii[corner][0]) / testCase.width,
379+
(2 * radii[corner][1]) / testCase.height,
380+
);
381+
382+
r[corner] = radii[corner].map(radius => {
383+
// If there's no radius, there's no spread to apply.
384+
if (radius === 0) {
385+
return 0;
386+
}
379387

380-
// Calculate the radius's ratio to the spread, clamping at 1.
381-
const spreadRatio = 1 - Math.min(1, radius / testCase.spread);
388+
// Calculate the radius's ratio to the spread, clamping at 1.
389+
const spreadRatio = 1 - Math.min(1, radius / testCase.spread);
382390

383-
// Calculate the diameter's ratio to the overall dimension, clamping at 1.
384-
const dimRatio = Math.min(1, (2 * radius) / dim);
391+
// These factors determine the amount of easing. They both approach 0
392+
// as their respective ratios approach 1, which reduces the easing effect.
393+
const spreadEasingFactor = spreadRatio ** 3;
394+
const dimEasingFactor = 1 - dimRatio ** 3;
385395

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;
396+
// The total reduction in spread is the product of the two easing factors.
397+
const totalReduction = dimEasingFactor * spreadEasingFactor;
390398

391-
// The total reduction in spread is the product of the two easing factors.
392-
const totalReduction = dimEasingFactor * spreadEasingFactor;
393-
return 1 - totalReduction;
394-
}
399+
// Apply the reduction to the spread and add it to the original radius.
400+
const easedSpread = testCase.spread * (1 - totalReduction);
395401

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;
402+
return radius + easedSpread;
403403
});
404404
}
405405
}

0 commit comments

Comments
 (0)