scale-border-radius.mjs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { px } from '../../value/types/numbers/units.mjs';
  2. function pixelsToPercent(pixels, axis) {
  3. if (axis.max === axis.min)
  4. return 0;
  5. return (pixels / (axis.max - axis.min)) * 100;
  6. }
  7. /**
  8. * We always correct borderRadius as a percentage rather than pixels to reduce paints.
  9. * For example, if you are projecting a box that is 100px wide with a 10px borderRadius
  10. * into a box that is 200px wide with a 20px borderRadius, that is actually a 10%
  11. * borderRadius in both states. If we animate between the two in pixels that will trigger
  12. * a paint each time. If we animate between the two in percentage we'll avoid a paint.
  13. */
  14. const correctBorderRadius = {
  15. correct: (latest, node) => {
  16. if (!node.target)
  17. return latest;
  18. /**
  19. * If latest is a string, if it's a percentage we can return immediately as it's
  20. * going to be stretched appropriately. Otherwise, if it's a pixel, convert it to a number.
  21. */
  22. if (typeof latest === "string") {
  23. if (px.test(latest)) {
  24. latest = parseFloat(latest);
  25. }
  26. else {
  27. return latest;
  28. }
  29. }
  30. /**
  31. * If latest is a number, it's a pixel value. We use the current viewportBox to calculate that
  32. * pixel value as a percentage of each axis
  33. */
  34. const x = pixelsToPercent(latest, node.target.x);
  35. const y = pixelsToPercent(latest, node.target.y);
  36. return `${x}% ${y}%`;
  37. },
  38. };
  39. export { correctBorderRadius, pixelsToPercent };