svgDraggableArea.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import * as React from "react";
  2. import { Vector2 } from 'babylonjs/Maths/math.vector';
  3. import { KeyframeSvgPoint, IKeyframeSvgPoint } from './keyframeSvgPoint';
  4. interface ISvgDraggableAreaProps {
  5. keyframeSvgPoints: IKeyframeSvgPoint[];
  6. updatePosition: (updatedKeyframe: IKeyframeSvgPoint, index: number) => void;
  7. scale: number;
  8. viewBoxScale: number;
  9. selectKeyframe: (id: string) => void;
  10. selectedControlPoint: (type: string, id: string) => void;
  11. }
  12. export class SvgDraggableArea extends React.Component<ISvgDraggableAreaProps>{
  13. private _active: boolean;
  14. private _isCurrentPointControl: string;
  15. private _currentPointIndex: number;
  16. private _draggableArea: React.RefObject<SVGSVGElement>;
  17. private _panStart: Vector2;
  18. private _panStop: Vector2;
  19. private _width: number;
  20. constructor(props: ISvgDraggableAreaProps) {
  21. super(props);
  22. this._currentPointIndex = -1;
  23. this._isCurrentPointControl = "";
  24. this._draggableArea = React.createRef();
  25. this._panStart = new Vector2(0, 0);
  26. this._panStop = new Vector2(0, 0);
  27. }
  28. componentDidMount() {
  29. this._draggableArea.current?.addEventListener("keydown", this.keyDown.bind(this));
  30. this._draggableArea.current?.addEventListener("keyup", this.keyUp.bind(this));
  31. setTimeout(() => {
  32. this._width = this._draggableArea.current?.clientWidth !== undefined ? this._draggableArea.current?.clientWidth : 0;
  33. console.log(this._width);
  34. }, 500);
  35. }
  36. dragStart(e: React.TouchEvent<SVGSVGElement>): void;
  37. dragStart(e: React.MouseEvent<SVGSVGElement, MouseEvent>): void;
  38. dragStart(e: any): void {
  39. e.preventDefault();
  40. if (e.target.classList.contains("draggable")) {
  41. this._active = true;
  42. this._currentPointIndex = parseInt(e.target.getAttribute('data-id'));
  43. if (e.target.classList.contains("control-point")) {
  44. this._isCurrentPointControl = e.target.getAttribute("type");
  45. }
  46. }
  47. if (e.target.classList.contains("pannable")) {
  48. if (e.buttons === 1 && e.ctrlKey) {
  49. this._panStart.set(e.clientX, e.clientY);
  50. }
  51. }
  52. }
  53. drag(e: React.TouchEvent<SVGSVGElement>): void;
  54. drag(e: React.MouseEvent<SVGSVGElement, MouseEvent>): void;
  55. drag(e: any): void {
  56. if (this._active) {
  57. e.preventDefault();
  58. var coord = this.getMousePosition(e);
  59. if (coord !== undefined) {
  60. var newPoints = [...this.props.keyframeSvgPoints];
  61. // Check for NaN values here.
  62. if (this._isCurrentPointControl === "left") {
  63. newPoints[this._currentPointIndex].leftControlPoint = coord;
  64. } else if (this._isCurrentPointControl === "right") {
  65. newPoints[this._currentPointIndex].rightControlPoint = coord;
  66. } else {
  67. newPoints[this._currentPointIndex].keyframePoint = coord;
  68. }
  69. this.props.updatePosition(newPoints[this._currentPointIndex], this._currentPointIndex);
  70. }
  71. }
  72. }
  73. dragEnd(e: React.TouchEvent<SVGSVGElement>): void;
  74. dragEnd(e: React.MouseEvent<SVGSVGElement, MouseEvent>): void;
  75. dragEnd(e: any): void {
  76. e.preventDefault();
  77. this._active = false;
  78. this._currentPointIndex = -1;
  79. this._isCurrentPointControl = "";
  80. if (e.target.classList.contains("pannable")) {
  81. if (this._panStart.x !== 0 && this._panStart.y !== 0) {
  82. this._panStop.set(e.clientX, e.clientY);
  83. this.panDirection();
  84. }
  85. }
  86. }
  87. getMousePosition(e: React.TouchEvent<SVGSVGElement>): Vector2 | undefined;
  88. getMousePosition(e: React.MouseEvent<SVGSVGElement, MouseEvent>): Vector2 | undefined;
  89. getMousePosition(e: any): Vector2 | undefined {
  90. if (e.touches) { e = e.touches[0]; }
  91. if (this._draggableArea.current) {
  92. var svg = this._draggableArea.current as SVGSVGElement;
  93. var CTM = svg.getScreenCTM();
  94. if (CTM) {
  95. return new Vector2((e.clientX - CTM.e) / CTM.a, (e.clientY - CTM.f) / CTM.d);
  96. } else {
  97. return undefined;
  98. }
  99. } else {
  100. return undefined;
  101. }
  102. }
  103. panDirection() {
  104. // Movement Right to Left
  105. if (this._panStart.x > this._panStop.x) {
  106. console.log("right to left");
  107. this.panTo("right", Math.abs(this._panStart.x - this._panStop.x));
  108. }
  109. // Movement Right to Left
  110. if (this._panStart.x < this._panStop.x) {
  111. this.panTo("left", Math.abs(this._panStart.x - this._panStop.x));
  112. console.log("left to right");
  113. }
  114. // Movement Bottom to Up
  115. if (this._panStart.y > this._panStop.y) {
  116. console.log("down up");
  117. }
  118. // Movement Up to Bottom
  119. if (this._panStart.y < this._panStop.y) {
  120. console.log("up down");
  121. }
  122. this._panStart.set(0, 0);
  123. this._panStop.set(0, 0);
  124. }
  125. panTo(direction: string, value: number) {
  126. switch (direction) {
  127. case "left":
  128. (this._draggableArea.current?.parentElement as HTMLDivElement).scrollLeft -= (value * 1);
  129. break;
  130. case "right":
  131. (this._draggableArea.current?.parentElement as HTMLDivElement).scrollLeft += (value * 1);
  132. break;
  133. case "top":
  134. break;
  135. case "down":
  136. break;
  137. }
  138. }
  139. keyDown(e: KeyboardEvent) {
  140. e.preventDefault();
  141. if (e.keyCode === 17) {
  142. this._draggableArea.current?.style.setProperty("cursor", "grab");
  143. }
  144. }
  145. keyUp(e: KeyboardEvent) {
  146. e.preventDefault();
  147. if (e.keyCode === 17) {
  148. this._draggableArea.current?.style.setProperty("cursor", "initial");
  149. }
  150. }
  151. focus(e: React.MouseEvent<SVGSVGElement>) {
  152. e.preventDefault();
  153. this._draggableArea.current?.focus();
  154. }
  155. render() {
  156. return (
  157. <>
  158. <svg className="linear pannable" ref={this._draggableArea} tabIndex={0}
  159. onMouseMove={(e) => this.drag(e)}
  160. onTouchMove={(e) => this.drag(e)}
  161. onTouchStart={(e) => this.dragStart(e)}
  162. onTouchEnd={(e) => this.dragEnd(e)}
  163. onMouseDown={(e) => this.dragStart(e)}
  164. onMouseUp={(e) => this.dragEnd(e)}
  165. onMouseLeave={(e) => this.dragEnd(e)}
  166. // Add way to add new keyframe
  167. onClick={(e) => this.focus(e)}
  168. viewBox={`0 0 ${Math.round(this.props.scale * 200)} ${Math.round(this.props.scale * 100)}`}>
  169. {this.props.children}
  170. {this.props.keyframeSvgPoints.map((keyframe, i) =>
  171. <KeyframeSvgPoint
  172. key={i}
  173. id={i.toString()}
  174. keyframePoint={keyframe.keyframePoint}
  175. leftControlPoint={keyframe.leftControlPoint}
  176. rightControlPoint={keyframe.rightControlPoint}
  177. isLeftActive={keyframe.isLeftActive}
  178. isRightActive={keyframe.isRightActive}
  179. selected={keyframe.selected}
  180. selectedControlPoint={(type: string, id: string) => this.props.selectedControlPoint(type, id)}
  181. selectKeyframe={(id: string) => this.props.selectKeyframe(id)} />
  182. )}
  183. </svg>
  184. </>)
  185. }
  186. }