transform-utils.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. const applyTranslateX = ({ scaleX = 1, translateX = 0 }, x) => (x * scaleX) + translateX;
  2. const applyTranslateY = ({ scaleY = 1, translateY = 0 }, y) => (y * scaleY) + translateY;
  3. const applyScaleX = ({ scaleX = 1 }, width) => width * scaleX;
  4. const applyScaleY = ({ scaleY = 1 }, height) => height * scaleY;
  5. export const applyTransform = (transform, {
  6. width = 0, height = 0, x, y
  7. }) => ({
  8. height: applyScaleY(transform, height),
  9. width: applyScaleX(transform, width),
  10. x: applyTranslateX(transform, x),
  11. y: applyTranslateY(transform, y),
  12. });
  13. const inverseTranslateX = ({ scaleX = 1, translateX = 0 }, x) => (x - translateX) / scaleX;
  14. const inverseTranslateY = ({ scaleY = 1, translateY = 0 }, y) => (y - translateY) / scaleY;
  15. const inverseScaleX = ({ scaleX = 1 }, width) => width / scaleX;
  16. const inverseScaleY = ({ scaleY = 1 }, height) => height / scaleY;
  17. export const inverseTransform = (transform, {
  18. width = 0, height = 0, x, y
  19. }) => ({
  20. height: inverseScaleY(transform, height),
  21. width: inverseScaleX(transform, width),
  22. x: inverseTranslateX(transform, x),
  23. y: inverseTranslateY(transform, y),
  24. });
  25. export const transformToString = ({
  26. translateX = 0, translateY = 0, scaleX = 1, scaleY = 1
  27. }) => (
  28. `translate(${translateX},${translateY}) scale(${scaleX},${scaleY})`
  29. );