transform.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 = +((offset.s * (1 + (transEvent.offsetX * 2) / transEvent.width)).toFixed(2))
  21. }
  22. return offset;
  23. }
  24. const transform: TransCreateFn = (transType: string) => ({
  25. mousemove() {
  26. const style = this.module.helper.createStyle({
  27. transform: getTransform.call(this, transType),
  28. });
  29. Object.entries(style).forEach(([key, value]: any[]) => {
  30. this.compEl.style[key] = value;
  31. });
  32. },
  33. mouseup() {
  34. this.currComp.layout.transform = getTransform.call(this, transType);
  35. },
  36. });
  37. export const move = transform.bind(null, "move");
  38. export const rotate = transform.bind(null, "rotate");
  39. export const scale = transform.bind(null, "scale");