transform.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { TransCreateFn, TransferCtrl } from "..";
  2. function getTransform(this: TransferCtrl, transType: string) {
  3. const { transEvent } = this;
  4. const { transform = {} } = this.currComp?.layout;
  5. const offset = {
  6. x: transform.x || 0,
  7. y: transform.y || 0,
  8. r: transform.r || 0,
  9. s: transform.s || 1,
  10. };
  11. switch (transType) {
  12. case "move":
  13. offset.x += transEvent.offsetX * 2;
  14. offset.y += transEvent.offsetY * 2;
  15. break;
  16. case "rotate":
  17. offset.r = (offset.r + transEvent.offsetX * -1) % 360;
  18. break;
  19. case "scale":
  20. offset.s = +(
  21. offset.s *
  22. (1 + (transEvent.offsetX * 2) / transEvent.width)
  23. ).toFixed(2);
  24. }
  25. return offset;
  26. }
  27. const transform: TransCreateFn = (transType: string) => ({
  28. mousemove() {
  29. const style = this.module.helper.createStyle({
  30. transform: getTransform.call(this, transType),
  31. });
  32. Object.entries(style).forEach(([key, value]: any[]) => {
  33. this.compEl.style[key] = value;
  34. });
  35. },
  36. mouseup() {
  37. this.module.actions.setCompTransform(
  38. this.currComp,
  39. getTransform.call(this, transType)
  40. );
  41. },
  42. });
  43. export const move = transform.bind(null, "move");
  44. export const rotate = transform.bind(null, "rotate");
  45. export const scale = transform.bind(null, "scale");