scale-box-shadow.mjs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { mixNumber } from '../../utils/mix/number.mjs';
  2. import { complex } from '../../value/types/complex/index.mjs';
  3. const correctBoxShadow = {
  4. correct: (latest, { treeScale, projectionDelta }) => {
  5. const original = latest;
  6. const shadow = complex.parse(latest);
  7. // TODO: Doesn't support multiple shadows
  8. if (shadow.length > 5)
  9. return original;
  10. const template = complex.createTransformer(latest);
  11. const offset = typeof shadow[0] !== "number" ? 1 : 0;
  12. // Calculate the overall context scale
  13. const xScale = projectionDelta.x.scale * treeScale.x;
  14. const yScale = projectionDelta.y.scale * treeScale.y;
  15. shadow[0 + offset] /= xScale;
  16. shadow[1 + offset] /= yScale;
  17. /**
  18. * Ideally we'd correct x and y scales individually, but because blur and
  19. * spread apply to both we have to take a scale average and apply that instead.
  20. * We could potentially improve the outcome of this by incorporating the ratio between
  21. * the two scales.
  22. */
  23. const averageScale = mixNumber(xScale, yScale, 0.5);
  24. // Blur
  25. if (typeof shadow[2 + offset] === "number")
  26. shadow[2 + offset] /= averageScale;
  27. // Spread
  28. if (typeof shadow[3 + offset] === "number")
  29. shadow[3 + offset] /= averageScale;
  30. return template(shadow);
  31. },
  32. };
  33. export { correctBoxShadow };