delta-remove.mjs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { mixNumber } from '../../utils/mix/number.mjs';
  2. import { percent } from '../../value/types/numbers/units.mjs';
  3. import { scalePoint } from './delta-apply.mjs';
  4. /**
  5. * Remove a delta from a point. This is essentially the steps of applyPointDelta in reverse
  6. */
  7. function removePointDelta(point, translate, scale, originPoint, boxScale) {
  8. point -= translate;
  9. point = scalePoint(point, 1 / scale, originPoint);
  10. if (boxScale !== undefined) {
  11. point = scalePoint(point, 1 / boxScale, originPoint);
  12. }
  13. return point;
  14. }
  15. /**
  16. * Remove a delta from an axis. This is essentially the steps of applyAxisDelta in reverse
  17. */
  18. function removeAxisDelta(axis, translate = 0, scale = 1, origin = 0.5, boxScale, originAxis = axis, sourceAxis = axis) {
  19. if (percent.test(translate)) {
  20. translate = parseFloat(translate);
  21. const relativeProgress = mixNumber(sourceAxis.min, sourceAxis.max, translate / 100);
  22. translate = relativeProgress - sourceAxis.min;
  23. }
  24. if (typeof translate !== "number")
  25. return;
  26. let originPoint = mixNumber(originAxis.min, originAxis.max, origin);
  27. if (axis === originAxis)
  28. originPoint -= translate;
  29. axis.min = removePointDelta(axis.min, translate, scale, originPoint, boxScale);
  30. axis.max = removePointDelta(axis.max, translate, scale, originPoint, boxScale);
  31. }
  32. /**
  33. * Remove a transforms from an axis. This is essentially the steps of applyAxisTransforms in reverse
  34. * and acts as a bridge between motion values and removeAxisDelta
  35. */
  36. function removeAxisTransforms(axis, transforms, [key, scaleKey, originKey], origin, sourceAxis) {
  37. removeAxisDelta(axis, transforms[key], transforms[scaleKey], transforms[originKey], transforms.scale, origin, sourceAxis);
  38. }
  39. /**
  40. * The names of the motion values we want to apply as translation, scale and origin.
  41. */
  42. const xKeys = ["x", "scaleX", "originX"];
  43. const yKeys = ["y", "scaleY", "originY"];
  44. /**
  45. * Remove a transforms from an box. This is essentially the steps of applyAxisBox in reverse
  46. * and acts as a bridge between motion values and removeAxisDelta
  47. */
  48. function removeBoxTransforms(box, transforms, originBox, sourceBox) {
  49. removeAxisTransforms(box.x, transforms, xKeys, originBox ? originBox.x : undefined, sourceBox ? sourceBox.x : undefined);
  50. removeAxisTransforms(box.y, transforms, yKeys, originBox ? originBox.y : undefined, sourceBox ? sourceBox.y : undefined);
  51. }
  52. export { removeAxisDelta, removeAxisTransforms, removeBoxTransforms, removePointDelta };