animationCurveEditorComponent.tsx 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848
  1. import * as React from "react";
  2. import { Animation } from "babylonjs/Animations/animation";
  3. import { Vector2, Vector3, Quaternion } from "babylonjs/Maths/math.vector";
  4. import { Color3, Color4 } from "babylonjs/Maths/math.color";
  5. import { Size } from "babylonjs/Maths/math.size";
  6. import { EasingFunction } from "babylonjs/Animations/easing";
  7. import { IAnimationKey } from "babylonjs/Animations/animationKey";
  8. import { IKeyframeSvgPoint } from "./keyframeSvgPoint";
  9. import { SvgDraggableArea } from "./svgDraggableArea";
  10. import { Timeline } from "./timeline";
  11. import { Notification } from "./notification";
  12. import { GraphActionsBar } from "./graphActionsBar";
  13. import { Scene } from "babylonjs/scene";
  14. import { IAnimatable } from "babylonjs/Animations/animatable.interface";
  15. import { Animatable } from "babylonjs/Animations/animatable";
  16. import { TargetedAnimation } from "babylonjs/Animations/animationGroup";
  17. import { EditorControls } from "./editorControls";
  18. import { SelectedCoordinate } from "./animationListTree";
  19. import { LockObject } from "../lockObject";
  20. import { GlobalState } from "../../../../globalState";
  21. import { Nullable } from "babylonjs/types";
  22. import { Observer } from "babylonjs/Misc/observable";
  23. import { ScaleLabel } from "./scale-label";
  24. require("./curveEditor.scss");
  25. interface IAnimationCurveEditorComponentProps {
  26. playOrPause?: () => void;
  27. scene: Scene;
  28. entity: IAnimatable | TargetedAnimation;
  29. lockObject: LockObject;
  30. globalState: GlobalState;
  31. }
  32. interface ICanvasAxis {
  33. value: number;
  34. label: number;
  35. }
  36. export enum CurveScale {
  37. float,
  38. radians,
  39. degrees,
  40. integers,
  41. default,
  42. }
  43. export interface IActionableKeyFrame {
  44. frame?: number | string;
  45. value?: any;
  46. }
  47. interface ICurveData {
  48. pathData: string;
  49. pathLength: number;
  50. domCurve: React.RefObject<SVGPathElement>;
  51. color: string;
  52. id: string;
  53. }
  54. export class AnimationCurveEditorComponent extends React.Component<
  55. IAnimationCurveEditorComponentProps,
  56. {
  57. isOpen: boolean;
  58. selected: Animation | null;
  59. svgKeyframes: IKeyframeSvgPoint[] | undefined;
  60. currentFrame: number;
  61. currentValue: number;
  62. frameAxisLength: ICanvasAxis[];
  63. valueAxisLength: ICanvasAxis[];
  64. isFlatTangentMode: boolean;
  65. isTangentMode: boolean;
  66. isBrokenMode: boolean;
  67. lerpMode: boolean;
  68. scale: number;
  69. playheadOffset: number;
  70. notification: string;
  71. currentPoint: SVGPoint | undefined;
  72. playheadPos: number;
  73. isPlaying: boolean;
  74. selectedPathData: ICurveData[] | undefined;
  75. selectedCoordinate: number;
  76. animationLimit: number;
  77. fps: number;
  78. isLooping: boolean;
  79. panningY: number;
  80. panningX: number;
  81. repositionCanvas: boolean;
  82. actionableKeyframe: IActionableKeyFrame;
  83. valueScale: CurveScale;
  84. canvasLength: number;
  85. lastKeyframeCreated: Nullable<string>;
  86. canvasWidthScale: number;
  87. }
  88. > {
  89. private _snippetUrl = "https://snippet.babylonjs.com";
  90. // Height scale *Review this functionaliy
  91. private _heightScale: number = 100;
  92. private _scaleFactor: number = 2;
  93. private _currentScale: number = 10;
  94. // Canvas Length *Review this functionality
  95. readonly _entityName: string;
  96. //private _canvasLength: number;
  97. private _svgKeyframes: IKeyframeSvgPoint[] = [];
  98. private _isPlaying: boolean = false;
  99. private _graphCanvas: React.RefObject<HTMLDivElement>;
  100. private _editor: React.RefObject<HTMLDivElement>;
  101. //private _selectedCurve: React.RefObject<SVGPathElement>;
  102. private _svgCanvas: React.RefObject<SvgDraggableArea>;
  103. private _isTargetedAnimation: boolean;
  104. private _pixelFrameUnit: number;
  105. private _onBeforeRenderObserver: Nullable<Observer<Scene>>;
  106. private _mainAnimatable: Nullable<Animatable>;
  107. constructor(props: IAnimationCurveEditorComponentProps) {
  108. super(props);
  109. this._entityName = (this.props.entity as any).id;
  110. this._editor = React.createRef();
  111. this._graphCanvas = React.createRef();
  112. this._svgCanvas = React.createRef();
  113. this._pixelFrameUnit = 10;
  114. let initialSelection;
  115. let initialPathData;
  116. let initialLerpMode;
  117. if (this.props.entity instanceof TargetedAnimation) {
  118. this._isTargetedAnimation = true;
  119. initialSelection = this.props.entity.animation;
  120. initialLerpMode = initialSelection !== undefined ? this.analizeAnimationForLerp(initialSelection) : false;
  121. initialPathData = initialSelection !== undefined ? this.getPathData(initialSelection) : undefined;
  122. } else {
  123. this._isTargetedAnimation = false;
  124. let hasAnimations = this.props.entity.animations !== undefined || this.props.entity.animations !== null ? this.props.entity.animations : false;
  125. initialSelection = hasAnimations !== false ? hasAnimations && hasAnimations[0] : null;
  126. initialLerpMode = initialSelection !== undefined ? this.analizeAnimationForLerp(this.props.entity.animations && initialSelection) : false;
  127. initialPathData = initialSelection && this.getPathData(initialSelection);
  128. initialPathData = initialPathData === null || initialPathData === undefined ? undefined : initialPathData;
  129. }
  130. const _canvasLength = 240;
  131. this.stopAnimation();
  132. // will update this until we have a top scroll/zoom feature
  133. let valueInd = [2, 1.8, 1.6, 1.4, 1.2, 1, 0.8, 0.6, 0.4, 0.2, 0];
  134. this.state = {
  135. selected: initialSelection,
  136. isOpen: true,
  137. svgKeyframes: this._svgKeyframes,
  138. currentFrame: 0,
  139. currentValue: 1,
  140. isFlatTangentMode: false,
  141. isTangentMode: false,
  142. isBrokenMode: false,
  143. lerpMode: initialLerpMode,
  144. playheadOffset: this._graphCanvas.current ? this._graphCanvas.current.children[0].clientWidth / (_canvasLength * 10) : 0,
  145. frameAxisLength: this.setFrameAxis(_canvasLength),
  146. valueAxisLength: new Array(10).fill(0).map((s, i) => {
  147. return { value: i * 10, label: valueInd[i] };
  148. }),
  149. notification: "",
  150. currentPoint: undefined,
  151. scale: 1,
  152. playheadPos: 0,
  153. isPlaying: false,
  154. selectedPathData: initialPathData,
  155. selectedCoordinate: 0,
  156. animationLimit: _canvasLength / 2,
  157. canvasLength: _canvasLength,
  158. fps: 60,
  159. isLooping: true,
  160. panningY: 0,
  161. panningX: 0,
  162. repositionCanvas: false,
  163. actionableKeyframe: { frame: undefined, value: undefined },
  164. valueScale: CurveScale.default,
  165. lastKeyframeCreated: null,
  166. canvasWidthScale: 200,
  167. };
  168. }
  169. componentDidMount() {
  170. this.state.selected && this.selectAnimation(this.state.selected);
  171. }
  172. componentDidUpdate(prevProps: IAnimationCurveEditorComponentProps, prevState: any) {
  173. if (prevState.currentFrame !== this.state.currentFrame) {
  174. this.onCurrentFrameChangeChangeScene(this.state.currentFrame);
  175. }
  176. }
  177. onCurrentFrameChangeChangeScene(value: number) {
  178. if (!this._mainAnimatable) {
  179. return;
  180. }
  181. this._mainAnimatable.goToFrame(value);
  182. }
  183. /**
  184. * Notifications
  185. * To add notification we set the state and clear to make the notification bar hide.
  186. */
  187. clearNotification() {
  188. this.setState({ notification: "" });
  189. }
  190. /**
  191. * Zoom and Scroll
  192. * This section handles zoom and scroll
  193. * of the graph area.
  194. */
  195. zoom(e: React.WheelEvent<HTMLDivElement>) {
  196. e.nativeEvent.stopImmediatePropagation();
  197. let scaleX = 1;
  198. if (Math.sign(e.deltaY) === -1) {
  199. scaleX = this.state.scale; //- 0.01; //+ 0.01;
  200. }
  201. this.setState({ scale: scaleX });
  202. }
  203. setFrameAxis(currentLength: number) {
  204. let halfNegative = new Array(currentLength).fill(0).map((s, i) => {
  205. return { value: -i * 10, label: -i };
  206. });
  207. let halfPositive = new Array(currentLength).fill(0).map((s, i) => {
  208. return { value: i * 10, label: i };
  209. });
  210. return [...halfNegative, ...halfPositive];
  211. }
  212. setValueLines(type: CurveScale) {
  213. switch (type) {
  214. case CurveScale.default:
  215. this._heightScale = 100;
  216. this._scaleFactor = 2;
  217. break;
  218. case CurveScale.float:
  219. this._scaleFactor = 2.5;
  220. this._heightScale = 120;
  221. break;
  222. case CurveScale.degrees:
  223. this._scaleFactor = 50;
  224. this._heightScale = 200;
  225. break;
  226. case CurveScale.integers:
  227. this._scaleFactor = 320;
  228. break;
  229. case CurveScale.radians:
  230. this._scaleFactor = 0.8;
  231. break;
  232. }
  233. const lineV = this._heightScale / 10;
  234. const initialValues = new Array(this._currentScale).fill(0).map((_, i) => {
  235. return {
  236. value: i * lineV,
  237. label: (this._scaleFactor * ((this._currentScale - i) / this._currentScale)).toFixed(2),
  238. };
  239. });
  240. initialValues.shift();
  241. const valueHeight = Math.abs(Math.round(this.state.panningY / this._currentScale));
  242. const sign = Math.sign(this.state.panningY);
  243. const pannedValues = new Array(valueHeight).fill(0).map((s, i) => {
  244. return sign === -1
  245. ? {
  246. value: -i * lineV,
  247. label: (i + this._currentScale) / (this._currentScale / this._scaleFactor),
  248. }
  249. : {
  250. value: (i + lineV) * this._currentScale,
  251. label: (i * -1) / (this._currentScale / this._scaleFactor),
  252. };
  253. });
  254. return [...initialValues, ...pannedValues];
  255. }
  256. getValueLabel(i: number) {
  257. // Need to update this when Y axis grows
  258. let label = 0;
  259. if (i === 0) {
  260. label = 2;
  261. }
  262. if (i === 50) {
  263. label = 1;
  264. } else {
  265. label = (100 - i * 2) * 0.01 + 1;
  266. }
  267. return label;
  268. }
  269. resetPlayheadOffset() {
  270. if (this._graphCanvas && this._graphCanvas.current) {
  271. this.setState({
  272. playheadOffset: this._graphCanvas.current.children[0].clientWidth / (this.state.canvasLength * 10 * this.state.scale),
  273. });
  274. }
  275. }
  276. encodeCurveId(animationName: string, coordinate: number) {
  277. return animationName + "_" + coordinate;
  278. }
  279. decodeCurveId(id: string) {
  280. const order = parseInt(id.split("_")[3]);
  281. const coordinate = parseInt(id.split("_")[2]);
  282. return { order, coordinate };
  283. }
  284. getKeyframeValueFromAnimation(id: string) {
  285. const animation = this.state.selected as Animation;
  286. const { order, coordinate } = this.decodeCurveId(id);
  287. const keys = [...animation.getKeys()];
  288. const key = keys.find((_, i) => i === order);
  289. if (key) {
  290. const valueAsArray = this.getValueAsArray(animation.dataType, key.value);
  291. return { frame: key?.frame, value: valueAsArray[coordinate] };
  292. } else {
  293. return undefined;
  294. }
  295. }
  296. /**
  297. * Keyframe Manipulation
  298. * This section handles events from SvgDraggableArea.
  299. */
  300. selectKeyframe(id: string, multiselect: boolean) {
  301. let frameValue: IActionableKeyFrame | undefined;
  302. const selectedKeyFrame = this.state.svgKeyframes?.find((kf) => kf.id === id)?.selected;
  303. if (!multiselect) {
  304. frameValue = this.getKeyframeValueFromAnimation(id);
  305. this.deselectKeyframes();
  306. } else {
  307. frameValue = { frame: undefined, value: undefined };
  308. }
  309. if (selectedKeyFrame) {
  310. frameValue = { frame: undefined, value: undefined };
  311. }
  312. const updatedKeyframes = this.state.svgKeyframes?.map((kf) => {
  313. if (kf.id === id) {
  314. kf.selected = !selectedKeyFrame;
  315. }
  316. return kf;
  317. });
  318. this.setState({
  319. svgKeyframes: updatedKeyframes,
  320. actionableKeyframe: frameValue ?? this.state.actionableKeyframe,
  321. });
  322. }
  323. resetActionableKeyframe() {
  324. this.setState({
  325. actionableKeyframe: { frame: undefined, value: undefined },
  326. });
  327. }
  328. selectedControlPoint(type: string, id: string) {
  329. let updatedKeyframes = this.state.svgKeyframes?.map((kf) => {
  330. if (kf.id === id) {
  331. //this.setState({ isFlatTangentMode: false });
  332. if (type === "left") {
  333. kf.isLeftActive = !kf.isLeftActive;
  334. kf.isRightActive = false;
  335. }
  336. if (type === "right") {
  337. kf.isRightActive = !kf.isRightActive;
  338. kf.isLeftActive = false;
  339. }
  340. }
  341. return kf;
  342. });
  343. this.setState({ svgKeyframes: updatedKeyframes });
  344. }
  345. deselectKeyframes() {
  346. let updatedKeyframes = this.state.svgKeyframes?.map((kf) => {
  347. kf.isLeftActive = false;
  348. kf.isRightActive = false;
  349. kf.selected = false;
  350. return kf;
  351. });
  352. this.setState({
  353. svgKeyframes: updatedKeyframes,
  354. actionableKeyframe: { frame: undefined, value: undefined },
  355. });
  356. }
  357. updateValuePerCoordinate(dataType: number, value: number | Vector2 | Vector3 | Color3 | Color4 | Size | Quaternion, newValue: number, coordinate?: number) {
  358. if (dataType === Animation.ANIMATIONTYPE_FLOAT) {
  359. value = newValue;
  360. }
  361. if (dataType === Animation.ANIMATIONTYPE_VECTOR2) {
  362. switch (coordinate) {
  363. case SelectedCoordinate.x:
  364. (value as Vector2).x = newValue;
  365. break;
  366. case SelectedCoordinate.y:
  367. (value as Vector2).y = newValue;
  368. break;
  369. }
  370. }
  371. if (dataType === Animation.ANIMATIONTYPE_VECTOR3) {
  372. switch (coordinate) {
  373. case SelectedCoordinate.x:
  374. (value as Vector3).x = newValue;
  375. break;
  376. case SelectedCoordinate.y:
  377. (value as Vector3).y = newValue;
  378. break;
  379. case SelectedCoordinate.z:
  380. (value as Vector3).z = newValue;
  381. break;
  382. }
  383. }
  384. if (dataType === Animation.ANIMATIONTYPE_QUATERNION) {
  385. switch (coordinate) {
  386. case SelectedCoordinate.x:
  387. (value as Quaternion).x = newValue;
  388. break;
  389. case SelectedCoordinate.y:
  390. (value as Quaternion).y = newValue;
  391. break;
  392. case SelectedCoordinate.z:
  393. (value as Quaternion).z = newValue;
  394. break;
  395. case SelectedCoordinate.w:
  396. (value as Quaternion).w = newValue;
  397. break;
  398. }
  399. }
  400. if (dataType === Animation.ANIMATIONTYPE_COLOR3) {
  401. switch (coordinate) {
  402. case SelectedCoordinate.r:
  403. (value as Color3).r = newValue;
  404. break;
  405. case SelectedCoordinate.g:
  406. (value as Color3).g = newValue;
  407. break;
  408. case SelectedCoordinate.b:
  409. (value as Color3).b = newValue;
  410. break;
  411. }
  412. }
  413. if (dataType === Animation.ANIMATIONTYPE_COLOR4) {
  414. switch (coordinate) {
  415. case SelectedCoordinate.r:
  416. (value as Color4).r = newValue;
  417. break;
  418. case SelectedCoordinate.g:
  419. (value as Color4).g = newValue;
  420. break;
  421. case SelectedCoordinate.b:
  422. (value as Color4).b = newValue;
  423. break;
  424. case SelectedCoordinate.a:
  425. (value as Color4).a = newValue;
  426. break;
  427. }
  428. }
  429. if (dataType === Animation.ANIMATIONTYPE_SIZE) {
  430. switch (coordinate) {
  431. case SelectedCoordinate.width:
  432. (value as Size).width = newValue;
  433. break;
  434. case SelectedCoordinate.g:
  435. (value as Size).height = newValue;
  436. break;
  437. }
  438. }
  439. return value;
  440. }
  441. renderPoints(updatedSvgKeyFrame: IKeyframeSvgPoint, id: string) {
  442. let animation = this.state.selected as Animation;
  443. const { order: index, coordinate } = this.decodeCurveId(id);
  444. let keys = [...animation.getKeys()];
  445. let newFrame = 0;
  446. if (updatedSvgKeyFrame.keyframePoint.x !== 0) {
  447. if (updatedSvgKeyFrame.keyframePoint.x > 0 && updatedSvgKeyFrame.keyframePoint.x < 1) {
  448. newFrame = 1;
  449. } else {
  450. newFrame = Math.round(updatedSvgKeyFrame.keyframePoint.x / this._pixelFrameUnit);
  451. }
  452. }
  453. if (newFrame > keys[index].frame) {
  454. if (index === keys.length - 1) {
  455. keys[index].frame = newFrame;
  456. } else {
  457. const nextKf = keys[index + 1];
  458. if (nextKf) {
  459. if (nextKf.frame <= newFrame) {
  460. keys[index].frame = keys[index].frame;
  461. } else {
  462. keys[index].frame = newFrame;
  463. }
  464. }
  465. }
  466. }
  467. if (newFrame < keys[index].frame) {
  468. if (index === 0) {
  469. keys[index].frame = newFrame;
  470. } else {
  471. const prevKf = keys[index - 1];
  472. if (prevKf) {
  473. if (prevKf.frame >= newFrame) {
  474. keys[index].frame = keys[index].frame;
  475. } else {
  476. keys[index].frame = newFrame;
  477. }
  478. }
  479. }
  480. }
  481. let updatedValue = ((this._heightScale - updatedSvgKeyFrame.keyframePoint.y) / this._heightScale) * this._scaleFactor;
  482. const updatedValueInCoordinate = this.updateValuePerCoordinate(animation.dataType, keys[index].value, updatedValue, coordinate);
  483. keys[index].value = updatedValueInCoordinate;
  484. this.updateLeftControlPoint(updatedSvgKeyFrame, keys[index], animation.dataType, coordinate);
  485. this.updateRightControlPoint(updatedSvgKeyFrame, keys[index], animation.dataType, coordinate);
  486. animation.setKeys(keys);
  487. this.setState({
  488. actionableKeyframe: { frame: newFrame, value: updatedValueInCoordinate },
  489. });
  490. this.selectAnimation(animation, coordinate);
  491. }
  492. updateLeftControlPoint(updatedSvgKeyFrame: IKeyframeSvgPoint, key: IAnimationKey, dataType: number, coordinate: number) {
  493. if (updatedSvgKeyFrame.isLeftActive) {
  494. if (updatedSvgKeyFrame.leftControlPoint !== null) {
  495. // Rotate Control Points
  496. // Get the previous svgKeyframe and measure distance between these two points
  497. let distanceWithPreviousKeyframe = this.getControlPointWeight(updatedSvgKeyFrame);
  498. // Get a quarter of that value for amplitude
  499. let distanceAmplitudeOfX = updatedSvgKeyFrame.leftControlPoint.x - distanceWithPreviousKeyframe;
  500. let slope = (updatedSvgKeyFrame.leftControlPoint.y - updatedSvgKeyFrame.keyframePoint.y) / (updatedSvgKeyFrame.leftControlPoint.x - updatedSvgKeyFrame.keyframePoint.x);
  501. let newValueOfY = (distanceAmplitudeOfX - updatedSvgKeyFrame.leftControlPoint.x) * slope + updatedSvgKeyFrame.keyframePoint.y;
  502. let updatedValue = ((newValueOfY - updatedSvgKeyFrame.keyframePoint.y) * this._scaleFactor) / this._heightScale;
  503. key.inTangent = this.updateValuePerCoordinate(dataType, key.inTangent, updatedValue, coordinate);
  504. if (!this.state.isBrokenMode) {
  505. // Right control point if exists
  506. if (updatedSvgKeyFrame.rightControlPoint !== null) {
  507. // Sets opposite value
  508. key.outTangent = key.inTangent * -1;
  509. }
  510. }
  511. }
  512. }
  513. }
  514. updateRightControlPoint(updatedSvgKeyFrame: IKeyframeSvgPoint, key: IAnimationKey, dataType: number, coordinate: number) {
  515. if (updatedSvgKeyFrame.isRightActive) {
  516. if (updatedSvgKeyFrame.rightControlPoint !== null) {
  517. // Rotate Control Points
  518. // Get the next svgKeyframe and measure distance between these two points
  519. let distanceWithNextKeyframe = this.getControlPointWeight(updatedSvgKeyFrame);
  520. let distanceAmplitudeOfX = updatedSvgKeyFrame.rightControlPoint.x + distanceWithNextKeyframe;
  521. let slope = (updatedSvgKeyFrame.rightControlPoint.y - updatedSvgKeyFrame.keyframePoint.y) / (updatedSvgKeyFrame.rightControlPoint.x - updatedSvgKeyFrame.keyframePoint.x);
  522. let newValueOfY = (distanceAmplitudeOfX - updatedSvgKeyFrame.rightControlPoint.x) * slope + updatedSvgKeyFrame.keyframePoint.y;
  523. let updatedValue = ((newValueOfY - updatedSvgKeyFrame.keyframePoint.y) * this._scaleFactor) / this._heightScale;
  524. key.outTangent = this.updateValuePerCoordinate(dataType, key.outTangent, updatedValue, coordinate);
  525. if (!this.state.isBrokenMode) {
  526. if (updatedSvgKeyFrame.leftControlPoint !== null) {
  527. // Sets opposite value
  528. key.inTangent = key.outTangent * -1;
  529. }
  530. }
  531. }
  532. }
  533. }
  534. getControlPointWeight(updatedSvgKeyFrame: IKeyframeSvgPoint) {
  535. let distanceWithPreviousKeyframe = this.state.canvasWidthScale / 4;
  536. if (this.state.svgKeyframes) {
  537. let indexOfKeyframe = this.state.svgKeyframes.indexOf(updatedSvgKeyFrame);
  538. let previousKeyframe = this.state.svgKeyframes[indexOfKeyframe - 1];
  539. if (previousKeyframe?.keyframePoint) {
  540. distanceWithPreviousKeyframe = Vector2.Distance(updatedSvgKeyFrame.keyframePoint, previousKeyframe.keyframePoint) / 2;
  541. }
  542. }
  543. let distanceWithNextKeyframe = this.state.canvasWidthScale / 4;
  544. if (this.state.svgKeyframes) {
  545. let indexOfKeyframe = this.state.svgKeyframes.indexOf(updatedSvgKeyFrame);
  546. let nextKeyframe = this.state.svgKeyframes[indexOfKeyframe + 1];
  547. if (nextKeyframe?.keyframePoint) {
  548. distanceWithNextKeyframe = Vector2.Distance(nextKeyframe.keyframePoint, updatedSvgKeyFrame.keyframePoint) / 2;
  549. }
  550. }
  551. if (distanceWithPreviousKeyframe < distanceWithNextKeyframe) {
  552. return distanceWithPreviousKeyframe;
  553. } else {
  554. return distanceWithNextKeyframe;
  555. }
  556. }
  557. handleFrameChange(event: React.ChangeEvent<HTMLInputElement>) {
  558. event.preventDefault();
  559. let frame;
  560. if (event.target.value === "") {
  561. frame = "";
  562. } else {
  563. frame = parseInt(event.target.value);
  564. }
  565. this.setState({
  566. actionableKeyframe: {
  567. frame: frame,
  568. value: this.state.actionableKeyframe.value,
  569. },
  570. });
  571. }
  572. handleValueChange(event: React.ChangeEvent<HTMLInputElement>) {
  573. event.preventDefault();
  574. let value;
  575. if (event.target.value !== undefined) {
  576. if (event.target.value !== "") {
  577. value = parseFloat(event.target.value);
  578. } else {
  579. value = "";
  580. }
  581. this.setState({
  582. actionableKeyframe: {
  583. frame: this.state.actionableKeyframe.frame,
  584. value: value,
  585. },
  586. });
  587. }
  588. }
  589. setKeyframeValueFromInput = (actionableKeyframe: IActionableKeyFrame) => {
  590. this.setState(
  591. {
  592. actionableKeyframe,
  593. },
  594. this.setKeyframeValue
  595. );
  596. };
  597. setKeyframeValue = () => {
  598. if (this.state.actionableKeyframe.frame !== "" && this.state.actionableKeyframe.frame !== undefined && this.state.actionableKeyframe.value !== "" && this.state.actionableKeyframe.value !== undefined) {
  599. if (this.state.selected !== null) {
  600. let currentSelected = this.state.svgKeyframes?.find((kf) => kf.selected);
  601. if (currentSelected) {
  602. let { order, coordinate } = this.decodeCurveId(currentSelected.id);
  603. let animation = this.state.selected;
  604. let keys = animation.getKeys();
  605. let isKeyframe = keys.find((_, i) => i === order);
  606. if (isKeyframe) {
  607. let updatedKeys = keys.map((k, i) => {
  608. if (i === order) {
  609. k.frame = this.state.actionableKeyframe.frame as number;
  610. const currentValue = this.getValueAsArray(animation.dataType, k.value);
  611. currentValue[coordinate] = this.state.actionableKeyframe.value;
  612. k.value = currentValue.length === 1 ? currentValue[0] : currentValue;
  613. }
  614. return k;
  615. });
  616. this.state.selected.setKeys(updatedKeys);
  617. this.selectAnimation(animation);
  618. }
  619. }
  620. }
  621. }
  622. };
  623. setFlatTangent() {
  624. const keyframes = this.state.svgKeyframes?.filter((kf) => kf.selected).map((k) => this.decodeCurveId(k.id));
  625. if (this.state.selected !== null) {
  626. let currentAnimation = this.state.selected;
  627. const keys = currentAnimation.getKeys();
  628. keyframes?.forEach((k) => {
  629. const keyframe = keys[k.order];
  630. keyframe.inTangent = this.returnZero(currentAnimation.dataType);
  631. keyframe.outTangent = this.returnZero(currentAnimation.dataType);
  632. });
  633. currentAnimation.setKeys(keys);
  634. this.selectAnimation(currentAnimation, this.state.selectedCoordinate);
  635. }
  636. }
  637. // Use this for Bezier curve mode
  638. setTangentMode() {
  639. if (this.state.selected !== null) {
  640. let animation = this.state.selected;
  641. this.setState({ isTangentMode: !this.state.isTangentMode }, () => this.selectAnimation(animation));
  642. }
  643. }
  644. setBrokenMode() {
  645. if (this.state.selected !== null) {
  646. let animation = this.state.selected;
  647. this.setState({ isBrokenMode: !this.state.isBrokenMode }, () => this.selectAnimation(animation));
  648. }
  649. }
  650. setLerpToActiveControlPoint() {
  651. const animation = this.state.selected;
  652. if (this.state.svgKeyframes && animation) {
  653. const keys = animation.getKeys();
  654. const selectedControlPoint = this.state.svgKeyframes.find((keyframe: IKeyframeSvgPoint) => keyframe.selected && (keyframe.isLeftActive || keyframe.isRightActive));
  655. if (selectedControlPoint !== null && selectedControlPoint) {
  656. const { order, coordinate } = this.decodeCurveId(selectedControlPoint.id);
  657. const key = keys[order];
  658. if (selectedControlPoint.isLeftActive && selectedControlPoint.leftControlPoint !== null) {
  659. key.inTangent = undefined;
  660. } else if (selectedControlPoint.isRightActive && selectedControlPoint.rightControlPoint !== null) {
  661. key.outTangent = undefined;
  662. }
  663. this.selectAnimation(animation, coordinate);
  664. }
  665. }
  666. }
  667. addKeyframeClick() {
  668. if (this.state.selected !== null) {
  669. let currentAnimation = this.state.selected;
  670. let keys = currentAnimation.getKeys();
  671. let x = this.state.currentFrame;
  672. let existValue = keys.find((k) => k.frame === x);
  673. if (existValue === undefined) {
  674. let y = this.state.actionableKeyframe.value ?? 1;
  675. // check if value exists...
  676. let arrayValue: any = [];
  677. let emptyValue = this.returnZero(currentAnimation.dataType);
  678. if (emptyValue) {
  679. arrayValue = this.getValueAsArray(currentAnimation.dataType, emptyValue);
  680. }
  681. // calculate point between prevkeyframe and nextkeyframe.
  682. const previousKFs = keys.filter((kf) => kf.frame < x);
  683. const nextKFs = keys.filter((kf) => kf.frame > x);
  684. const prev = previousKFs.slice(-1)[0];
  685. const next = nextKFs[0];
  686. if (prev === undefined && next) {
  687. y = next.value;
  688. }
  689. if (prev && next === undefined) {
  690. y = prev.value;
  691. }
  692. if (prev && next) {
  693. const value1 = new Vector2(prev.frame, prev.value);
  694. const tangent1 = new Vector2(prev.outTangent, prev.outTangent);
  695. const value2 = new Vector2(next.frame, next.value);
  696. const tangent2 = new Vector2(next.inTangent, next.inTangent);
  697. const amount = (x - prev.frame) / (next.frame - prev.frame);
  698. const newV = Vector2.Hermite(value1, tangent1, value2, tangent2, amount);
  699. y = newV.y;
  700. }
  701. arrayValue[this.state.selectedCoordinate] = y;
  702. let actualValue = this.setValueAsType(currentAnimation.dataType, arrayValue);
  703. const recentlyCreated = {
  704. frame: x,
  705. value: actualValue,
  706. inTangent: emptyValue,
  707. outTangent: emptyValue,
  708. };
  709. keys.push(recentlyCreated);
  710. keys.sort((a, b) => a.frame - b.frame);
  711. const newIndex = keys.findIndex((kf) => kf.frame === x);
  712. const id = `${currentAnimation.name}_${currentAnimation.targetProperty}_${this.state.selectedCoordinate}`;
  713. const curvedId = this.encodeCurveId(id, newIndex);
  714. this.setState({ lastKeyframeCreated: curvedId });
  715. currentAnimation.setKeys(keys);
  716. this.selectAnimation(currentAnimation, this.state.selectedCoordinate);
  717. }
  718. }
  719. }
  720. removeKeyframeClick() {
  721. if (this.state.selected !== null) {
  722. let currentAnimation = this.state.selected;
  723. if (currentAnimation.dataType === Animation.ANIMATIONTYPE_FLOAT) {
  724. let keys = currentAnimation.getKeys();
  725. let x = this.state.currentFrame;
  726. let filteredKeys = keys.filter((kf) => kf.frame !== x);
  727. currentAnimation.setKeys(filteredKeys);
  728. this.selectAnimation(currentAnimation, this.state.selectedCoordinate);
  729. }
  730. }
  731. }
  732. removeKeyframes(points: IKeyframeSvgPoint[]) {
  733. if (this.state.selected !== null) {
  734. let currentAnimation = this.state.selected;
  735. const indexesToRemove = points.map((p) => {
  736. return {
  737. index: parseInt(p.id.split("_")[3]),
  738. coordinate: parseInt(p.id.split("_")[2]),
  739. };
  740. });
  741. if (currentAnimation.dataType === Animation.ANIMATIONTYPE_FLOAT) {
  742. let keys = currentAnimation.getKeys();
  743. let filteredKeys = keys.filter((_, i) => {
  744. return !indexesToRemove.find((x) => x.index === i);
  745. });
  746. currentAnimation.setKeys(filteredKeys);
  747. this.deselectKeyframes();
  748. this.selectAnimation(currentAnimation, this.state.selectedCoordinate);
  749. }
  750. }
  751. }
  752. addKeyFrame(event: React.MouseEvent<SVGSVGElement>) {
  753. event.preventDefault();
  754. if (this.state.selected !== null) {
  755. var svg = event.target as SVGSVGElement;
  756. var pt = svg.createSVGPoint();
  757. pt.x = event.clientX;
  758. pt.y = event.clientY;
  759. var inverse = svg.getScreenCTM()?.inverse();
  760. var cursorpt = pt.matrixTransform(inverse);
  761. var currentAnimation = this.state.selected;
  762. var keys = currentAnimation.getKeys();
  763. var height = 100;
  764. var middle = height / 2;
  765. var keyValue;
  766. if (cursorpt.y < middle) {
  767. keyValue = 1 + (100 / cursorpt.y) * 0.1;
  768. }
  769. if (cursorpt.y > middle) {
  770. keyValue = 1 - (100 / cursorpt.y) * 0.1;
  771. }
  772. keys.push({ frame: cursorpt.x, value: keyValue });
  773. currentAnimation.setKeys(keys);
  774. this.selectAnimation(currentAnimation);
  775. }
  776. }
  777. /**
  778. * Curve Rendering Functions
  779. * This section handles how to render curves.
  780. */
  781. setKeyframePointLinear(point: Vector2, index: number) {
  782. // here set the ID to a unique id
  783. let svgKeyframe = {
  784. keyframePoint: point,
  785. rightControlPoint: null,
  786. leftControlPoint: null,
  787. id: index.toString(),
  788. selected: false,
  789. isLeftActive: false,
  790. isRightActive: false,
  791. };
  792. this._svgKeyframes.push(svgKeyframe);
  793. }
  794. flatTangents(keyframes: IAnimationKey[], dataType: number) {
  795. // Checks if Flat Tangent is active (tangents are set to zero)
  796. let flattened;
  797. if (this.state && this.state.isFlatTangentMode) {
  798. flattened = keyframes.map((kf) => {
  799. if (kf.inTangent !== undefined) {
  800. kf.inTangent = this.returnZero(dataType);
  801. }
  802. if (kf.outTangent !== undefined) {
  803. kf.outTangent = this.returnZero(dataType);
  804. }
  805. return kf;
  806. });
  807. } else {
  808. flattened = keyframes;
  809. }
  810. this.setState({ isFlatTangentMode: false });
  811. return flattened;
  812. }
  813. returnZero(dataType: number) {
  814. switch (dataType) {
  815. case Animation.ANIMATIONTYPE_FLOAT:
  816. return 0;
  817. case Animation.ANIMATIONTYPE_VECTOR3:
  818. return Vector3.Zero();
  819. case Animation.ANIMATIONTYPE_VECTOR2:
  820. return Vector2.Zero();
  821. case Animation.ANIMATIONTYPE_QUATERNION:
  822. return Quaternion.Zero();
  823. case Animation.ANIMATIONTYPE_COLOR3:
  824. return new Color3(0, 0, 0);
  825. case Animation.ANIMATIONTYPE_COLOR4:
  826. return new Color4(0, 0, 0, 0);
  827. case Animation.ANIMATIONTYPE_SIZE:
  828. return new Size(0, 0);
  829. default:
  830. return 0;
  831. }
  832. }
  833. getValueAsArray(valueType: number, value: number | Vector2 | Vector3 | Color3 | Color4 | Size | Quaternion) {
  834. switch (valueType) {
  835. case Animation.ANIMATIONTYPE_FLOAT:
  836. return [value as number];
  837. case Animation.ANIMATIONTYPE_VECTOR3:
  838. return (value as Vector3).asArray();
  839. case Animation.ANIMATIONTYPE_VECTOR2:
  840. return (value as Vector2).asArray();
  841. case Animation.ANIMATIONTYPE_QUATERNION:
  842. return (value as Quaternion).asArray();
  843. case Animation.ANIMATIONTYPE_COLOR3:
  844. return (value as Color3).asArray();
  845. case Animation.ANIMATIONTYPE_COLOR4:
  846. return (value as Color4).asArray();
  847. case Animation.ANIMATIONTYPE_SIZE:
  848. return [(value as Size).width, (value as Size).height];
  849. default:
  850. return [];
  851. }
  852. }
  853. setValueAsType(valueType: number, arrayValue: number[]) {
  854. switch (valueType) {
  855. case Animation.ANIMATIONTYPE_FLOAT:
  856. return arrayValue[0];
  857. case Animation.ANIMATIONTYPE_VECTOR3:
  858. return new Vector3(arrayValue[0], arrayValue[1], arrayValue[2]);
  859. case Animation.ANIMATIONTYPE_VECTOR2:
  860. return new Vector2(arrayValue[0], arrayValue[1]);
  861. case Animation.ANIMATIONTYPE_QUATERNION:
  862. return new Quaternion(arrayValue[0], arrayValue[1], arrayValue[2], arrayValue[3]);
  863. case Animation.ANIMATIONTYPE_COLOR3:
  864. return new Color3(arrayValue[0], arrayValue[1], arrayValue[2]);
  865. case Animation.ANIMATIONTYPE_COLOR4:
  866. return new Color4(arrayValue[0], arrayValue[1], arrayValue[2], arrayValue[3]);
  867. case Animation.ANIMATIONTYPE_SIZE:
  868. return new Size(arrayValue[0], arrayValue[1]);
  869. default:
  870. return arrayValue[0];
  871. }
  872. }
  873. getPathData(animation: Animation | null) {
  874. if (animation === null) {
  875. return undefined;
  876. }
  877. var keyframes = animation.getKeys();
  878. if (keyframes === undefined || keyframes.length === 0) {
  879. return undefined;
  880. } else {
  881. const { easingMode, easingType, usesTangents, valueType, highestFrame, name, targetProperty } = this.getAnimationData(animation);
  882. //keyframes = this.flatTangents(keyframes, valueType); // This will break because we are using setState before mounted...
  883. const startKey = keyframes[0];
  884. let middle = this._heightScale / this._scaleFactor; //?
  885. let collection: ICurveData[] = [];
  886. const colors = ["red", "green", "blue", "white", "#7a4ece"];
  887. const startValue = this.getValueAsArray(valueType, startKey.value);
  888. for (var d = 0; d < startValue.length; d++) {
  889. const id = `${name}_${targetProperty}_${d}`;
  890. const curveColor = valueType === Animation.ANIMATIONTYPE_FLOAT ? colors[4] : colors[d];
  891. // START OF LINE/CURVE
  892. let data: string | undefined = `M${startKey.frame * this._pixelFrameUnit}, ${this._heightScale - startValue[d] * middle}`; //
  893. if (this.state) {
  894. if (usesTangents) {
  895. data = this.curvePathWithTangents(keyframes, data, middle, valueType, d, id);
  896. } else {
  897. if (easingType !== undefined && easingMode !== undefined) {
  898. let easingFunction = animation.getEasingFunction();
  899. data = this.curvePath(keyframes, data, middle, easingFunction as EasingFunction);
  900. } else {
  901. if (this.state !== undefined) {
  902. data = this.curvePathWithTangents(keyframes, data, middle, valueType, d, id);
  903. }
  904. }
  905. }
  906. }
  907. collection.push({
  908. pathData: data,
  909. pathLength: highestFrame,
  910. domCurve: React.createRef(),
  911. color: curveColor,
  912. id: id,
  913. });
  914. }
  915. return collection;
  916. }
  917. }
  918. getAnimationData(animation: Animation) {
  919. // General Props
  920. let loopMode = animation.loopMode;
  921. let name = animation.name;
  922. let blendingSpeed = animation.blendingSpeed;
  923. let targetProperty = animation.targetProperty;
  924. let targetPropertyPath = animation.targetPropertyPath;
  925. let framesPerSecond = animation.framePerSecond;
  926. let highestFrame = animation.getHighestFrame();
  927. //let serialized = animation.serialize();
  928. let usesTangents = animation.getKeys().find((kf) => kf.hasOwnProperty("inTangent") || kf.hasOwnProperty("outTangent")) !== undefined ? true : false;
  929. let valueType = animation.dataType;
  930. // easing properties
  931. let easingType, easingMode;
  932. let easingFunction: EasingFunction = animation.getEasingFunction() as EasingFunction;
  933. if (easingFunction === undefined) {
  934. easingType = undefined;
  935. easingMode = undefined;
  936. } else {
  937. easingType = easingFunction.constructor.name;
  938. easingMode = easingFunction.getEasingMode();
  939. }
  940. return {
  941. loopMode,
  942. name,
  943. blendingSpeed,
  944. targetPropertyPath,
  945. targetProperty,
  946. framesPerSecond,
  947. highestFrame,
  948. usesTangents,
  949. easingType,
  950. easingMode,
  951. valueType,
  952. };
  953. }
  954. curvePathWithTangents(keyframes: IAnimationKey[], data: string, middle: number, type: number, coordinate: number, animationName: string) {
  955. keyframes.forEach((key, i) => {
  956. // Create a unique id for curve
  957. const curveId = this.encodeCurveId(animationName, i);
  958. // identify type of value and split...
  959. const keyframe_valueAsArray = this.getValueAsArray(type, key.value)[coordinate];
  960. let svgKeyframe;
  961. let outTangent;
  962. let inTangent;
  963. let defaultWeight = this.state.canvasWidthScale / 2; // Get weight depending on prev and next frame distance
  964. //distanceWithPreviousKeyframe = Vector2.Distance(updatedSvgKeyFrame.keyframePoint, previousKeyframe.keyframePoint) / 4;
  965. // For inTangent
  966. // has prev frame?
  967. let weightIn = 0;
  968. if (keyframes[i - 1] !== undefined) {
  969. let prevIn = new Vector2(keyframes[i - 1].frame, keyframes[i - 1].value);
  970. let currIn = new Vector2(key.frame, key.value);
  971. weightIn = (Vector2.Distance(prevIn, currIn) / 2) * this._pixelFrameUnit;
  972. }
  973. // For outTangent
  974. // has next frame?
  975. let weightOut = 0;
  976. if (keyframes[i + 1] !== undefined) {
  977. let prevOut = new Vector2(keyframes[i + 1].frame, keyframes[i + 1].value);
  978. let currOut = new Vector2(key.frame, key.value);
  979. weightOut = (Vector2.Distance(prevOut, currOut) / 2) * this._pixelFrameUnit;
  980. }
  981. if (weightIn !== 0 && weightOut !== 0) {
  982. if (weightIn < weightOut) {
  983. defaultWeight = weightIn > defaultWeight ? defaultWeight : weightIn;
  984. } else {
  985. defaultWeight = weightOut > defaultWeight ? defaultWeight : weightOut;
  986. }
  987. }
  988. if (weightIn === 0 && weightOut !== 0) {
  989. defaultWeight = weightOut > defaultWeight ? defaultWeight : weightOut;
  990. }
  991. if (weightIn !== 0 && weightOut === 0) {
  992. defaultWeight = weightIn > defaultWeight ? defaultWeight : weightIn;
  993. }
  994. // if curve doesnt have tangents then must be null to have linear
  995. // right now has 0 then the linear will show a slight curve as flat tangents...
  996. let defaultTangent: number | null = null;
  997. if (i !== 0 || i !== keyframes.length - 1) {
  998. defaultTangent = null;
  999. }
  1000. var inT = key.inTangent === undefined ? defaultTangent : this.getValueAsArray(type, key.inTangent)[coordinate];
  1001. var outT = key.outTangent === undefined ? defaultTangent : this.getValueAsArray(type, key.outTangent)[coordinate];
  1002. let y = this._heightScale - keyframe_valueAsArray * middle;
  1003. if (inT !== null) {
  1004. let valueIn = y * inT + y;
  1005. inTangent = new Vector2(key.frame * this._pixelFrameUnit - defaultWeight, valueIn);
  1006. } else {
  1007. inTangent = null;
  1008. }
  1009. if (outT !== null) {
  1010. let valueOut = y * outT + y;
  1011. outTangent = new Vector2(key.frame * this._pixelFrameUnit + defaultWeight, valueOut);
  1012. } else {
  1013. outTangent = null;
  1014. }
  1015. if (i === 0) {
  1016. svgKeyframe = {
  1017. keyframePoint: new Vector2(key.frame * this._pixelFrameUnit, this._heightScale - keyframe_valueAsArray * middle),
  1018. rightControlPoint: outTangent,
  1019. leftControlPoint: null,
  1020. id: curveId,
  1021. selected: false,
  1022. isLeftActive: false,
  1023. isRightActive: false,
  1024. };
  1025. if (outTangent !== null) {
  1026. data += ` C${outTangent.x} ${outTangent.y} `;
  1027. }
  1028. } else {
  1029. svgKeyframe = {
  1030. keyframePoint: new Vector2(key.frame * this._pixelFrameUnit, this._heightScale - keyframe_valueAsArray * middle),
  1031. rightControlPoint: outTangent,
  1032. leftControlPoint: inTangent,
  1033. id: curveId,
  1034. selected: false,
  1035. isLeftActive: false,
  1036. isRightActive: false,
  1037. };
  1038. if (outTangent !== null && inTangent !== null) {
  1039. data += ` ${inTangent.x} ${inTangent.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} C${outTangent.x} ${outTangent.y}`;
  1040. } else if (inTangent !== null) {
  1041. data += ` ${inTangent.x} ${inTangent.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y}`;
  1042. }
  1043. if (outTangent === null && inTangent !== null) {
  1044. data += ` ${inTangent.x} ${inTangent.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} C${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y}`;
  1045. }
  1046. if (inTangent === null && outTangent !== null) {
  1047. const prev = this._svgKeyframes[i - 1];
  1048. data += ` ${prev.keyframePoint.x} ${prev.keyframePoint.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} C${outTangent.x} ${outTangent.y}`;
  1049. }
  1050. if (inTangent === null && outTangent === null) {
  1051. const prev = this._svgKeyframes[i - 1];
  1052. data += ` ${prev.keyframePoint.x} ${prev.keyframePoint.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} C${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y}`;
  1053. }
  1054. }
  1055. if (this.state) {
  1056. let prev = this.state.svgKeyframes?.find((kf) => kf.id === curveId);
  1057. if (prev) {
  1058. svgKeyframe.isLeftActive = prev?.isLeftActive;
  1059. svgKeyframe.isRightActive = prev?.isRightActive;
  1060. svgKeyframe.selected = prev?.selected;
  1061. }
  1062. }
  1063. this._svgKeyframes.push(svgKeyframe);
  1064. }, this);
  1065. const lastCurveEnd = data.lastIndexOf("C");
  1066. const cleanedData = data.substring(0, lastCurveEnd);
  1067. return cleanedData;
  1068. }
  1069. curvePath(keyframes: IAnimationKey[], data: string, middle: number, easingFunction: EasingFunction) {
  1070. // This will get 1/4 and 3/4 of points in eased curve
  1071. const u = 0.25;
  1072. const v = 0.75;
  1073. keyframes.forEach((key, i) => {
  1074. // identify type of value and split...
  1075. // Gets previous initial point of curve segment
  1076. var pointA = new Vector2(0, 0);
  1077. if (i === 0) {
  1078. pointA.x = key.frame;
  1079. pointA.y = this._heightScale - key.value * middle;
  1080. this.setKeyframePoint([pointA], i, keyframes.length);
  1081. } else {
  1082. pointA.x = keyframes[i - 1].frame;
  1083. pointA.y = this._heightScale - keyframes[i - 1].value * middle;
  1084. // Gets the end point of this curve segment
  1085. var pointB = new Vector2(key.frame, this._heightScale - key.value * middle);
  1086. // Get easing value of percentage to get the bezier control points below
  1087. let du = easingFunction.easeInCore(u); // What to do here, when user edits the curve? Option 1: Modify the curve with the new control points as BezierEaseCurve(x,y,z,w)
  1088. let dv = easingFunction.easeInCore(v); // Option 2: Create a easeInCore function and adapt it with the new control points values... needs more revision.
  1089. // Direction of curve up/down
  1090. let yInt25 = 0;
  1091. if (pointB.y > pointA.y) {
  1092. // if pointB.y > pointA.y = goes down
  1093. yInt25 = (pointB.y - pointA.y) * du + pointA.y;
  1094. } else if (pointB.y < pointA.y) {
  1095. // if pointB.y < pointA.y = goes up
  1096. yInt25 = pointA.y - (pointA.y - pointB.y) * du;
  1097. }
  1098. let yInt75 = 0;
  1099. if (pointB.y > pointA.y) {
  1100. yInt75 = (pointB.y - pointA.y) * dv + pointA.y;
  1101. } else if (pointB.y < pointA.y) {
  1102. yInt75 = pointA.y - (pointA.y - pointB.y) * dv;
  1103. }
  1104. // Intermediate points in curve
  1105. let intermediatePoint25 = new Vector2((pointB.x - pointA.x) * u + pointA.x, yInt25);
  1106. let intermediatePoint75 = new Vector2((pointB.x - pointA.x) * v + pointA.x, yInt75);
  1107. // Gets the four control points of bezier curve
  1108. let controlPoints = this.interpolateControlPoints(pointA, intermediatePoint25, u, intermediatePoint75, v, pointB);
  1109. if (controlPoints !== undefined) {
  1110. this.setKeyframePoint(controlPoints, i, keyframes.length);
  1111. data += ` C${controlPoints[1].x} ${controlPoints[1].y} ${controlPoints[2].x} ${controlPoints[2].y} ${controlPoints[3].x} ${controlPoints[3].y}`;
  1112. }
  1113. }
  1114. });
  1115. return data;
  1116. }
  1117. setKeyframePoint(controlPoints: Vector2[], index: number, keyframesCount: number) {
  1118. let svgKeyframe;
  1119. if (index === 0) {
  1120. svgKeyframe = {
  1121. keyframePoint: controlPoints[0],
  1122. rightControlPoint: null,
  1123. leftControlPoint: null,
  1124. id: index.toString(),
  1125. selected: false,
  1126. isLeftActive: false,
  1127. isRightActive: false,
  1128. };
  1129. } else {
  1130. this._svgKeyframes[index - 1].rightControlPoint = controlPoints[1];
  1131. svgKeyframe = {
  1132. keyframePoint: controlPoints[3],
  1133. rightControlPoint: null,
  1134. leftControlPoint: controlPoints[2],
  1135. id: index.toString(),
  1136. selected: false,
  1137. isLeftActive: false,
  1138. isRightActive: false,
  1139. };
  1140. }
  1141. this._svgKeyframes.push(svgKeyframe);
  1142. }
  1143. interpolateControlPoints(p0: Vector2, p1: Vector2, u: number, p2: Vector2, v: number, p3: Vector2): Vector2[] | undefined {
  1144. let a = 0.0;
  1145. let b = 0.0;
  1146. let c = 0.0;
  1147. let d = 0.0;
  1148. let det = 0.0;
  1149. let q1: Vector2 = new Vector2();
  1150. let q2: Vector2 = new Vector2();
  1151. let controlA: Vector2 = p0;
  1152. let controlB: Vector2 = new Vector2();
  1153. let controlC: Vector2 = new Vector2();
  1154. let controlD: Vector2 = p3;
  1155. if (u <= 0.0 || u >= 1.0 || v <= 0.0 || v >= 1.0 || u >= v) {
  1156. return undefined;
  1157. }
  1158. a = 3 * (1 - u) * (1 - u) * u;
  1159. b = 3 * (1 - u) * u * u;
  1160. c = 3 * (1 - v) * (1 - v) * v;
  1161. d = 3 * (1 - v) * v * v;
  1162. det = a * d - b * c;
  1163. if (det == 0.0) return undefined;
  1164. q1.x = p1.x - ((1 - u) * (1 - u) * (1 - u) * p0.x + u * u * u * p3.x);
  1165. q1.y = p1.y - ((1 - u) * (1 - u) * (1 - u) * p0.y + u * u * u * p3.y);
  1166. q2.x = p2.x - ((1 - v) * (1 - v) * (1 - v) * p0.x + v * v * v * p3.x);
  1167. q2.y = p2.y - ((1 - v) * (1 - v) * (1 - v) * p0.y + v * v * v * p3.y);
  1168. controlB.x = (d * q1.x - b * q2.x) / det;
  1169. controlB.y = (d * q1.y - b * q2.y) / det;
  1170. controlC.x = (-c * q1.x + a * q2.x) / det;
  1171. controlC.y = (-c * q1.y + a * q2.y) / det;
  1172. return [controlA, controlB, controlC, controlD];
  1173. }
  1174. deselectAnimation() {
  1175. this.setState({
  1176. selected: null,
  1177. svgKeyframes: [],
  1178. selectedPathData: [],
  1179. selectedCoordinate: 0,
  1180. });
  1181. }
  1182. /**
  1183. * Core functions
  1184. * This section handles main Curve Editor Functions.
  1185. */
  1186. selectAnimation(animation: Animation, coordinate?: SelectedCoordinate) {
  1187. this._svgKeyframes = [];
  1188. let updatedPath;
  1189. let filteredSvgKeys;
  1190. let selectedCurve = 0;
  1191. if (coordinate === undefined) {
  1192. this.stopAnimation();
  1193. updatedPath = this.getPathData(animation);
  1194. } else {
  1195. let curves = this.getPathData(animation);
  1196. updatedPath = [];
  1197. filteredSvgKeys = this._svgKeyframes?.filter((curve) => {
  1198. let id = parseInt(curve.id.split("_")[2]);
  1199. if (id === coordinate) {
  1200. return true;
  1201. } else {
  1202. return false;
  1203. }
  1204. });
  1205. curves?.map((curve) => {
  1206. let id = parseInt(curve.id.split("_")[2]);
  1207. if (id === coordinate) {
  1208. updatedPath.push(curve);
  1209. }
  1210. });
  1211. selectedCurve = coordinate;
  1212. }
  1213. // check for empty svgKeyframes, lastframe, selected
  1214. this.setState(
  1215. {
  1216. selected: animation,
  1217. svgKeyframes: coordinate !== undefined ? filteredSvgKeys : this._svgKeyframes,
  1218. selectedPathData: updatedPath,
  1219. selectedCoordinate: selectedCurve,
  1220. fps: animation.framePerSecond,
  1221. },
  1222. this.postSelectionEvents
  1223. );
  1224. }
  1225. postSelectionEvents = () => {
  1226. if (this.state.lastKeyframeCreated !== null) {
  1227. this.deselectKeyframes();
  1228. this.selectKeyframe(this.state.lastKeyframeCreated, false);
  1229. this.setState({ lastKeyframeCreated: null });
  1230. }
  1231. this.setMainAnimatable();
  1232. };
  1233. setMainAnimatable() {
  1234. if (this.state.selected !== null) {
  1235. let target = this.props.entity;
  1236. if (this.props.entity instanceof TargetedAnimation) {
  1237. target = this.props.entity.target;
  1238. }
  1239. this.props.scene.stopAllAnimations();
  1240. if (this._mainAnimatable?.target !== target) {
  1241. const keys = this.state.selected.getKeys();
  1242. if (keys.length !== 0) {
  1243. const firstFrame = keys[0].frame;
  1244. const LastFrame = this.state.selected.getHighestFrame();
  1245. this._mainAnimatable = this.props.scene.beginAnimation(target, firstFrame, LastFrame, this.state.isLooping);
  1246. this._mainAnimatable.stop();
  1247. }
  1248. }
  1249. }
  1250. }
  1251. isAnimationPlaying() {
  1252. let target = this.props.entity;
  1253. if (this.props.entity instanceof TargetedAnimation) {
  1254. target = this.props.entity.target;
  1255. }
  1256. return this.props.scene.getAllAnimatablesByTarget(target).length > 0;
  1257. }
  1258. stopAnimation() {
  1259. let target = this.props.entity;
  1260. if (this.props.entity instanceof TargetedAnimation) {
  1261. target = this.props.entity.target;
  1262. }
  1263. this._isPlaying = this.props.scene.getAllAnimatablesByTarget(target).length > 0;
  1264. if (this._isPlaying) {
  1265. this.props.playOrPause && this.props.playOrPause();
  1266. }
  1267. }
  1268. setIsLooping() {
  1269. this.setState({ isLooping: !this.state.isLooping, isPlaying: false }, () => this.stopAnimation());
  1270. }
  1271. setFramesPerSecond(fps: number) {
  1272. this.setState({ fps: fps, isPlaying: false }, () => this.stopAnimation());
  1273. }
  1274. analizeAnimationForLerp(animation: Animation | null) {
  1275. if (animation !== null) {
  1276. const { easingMode, easingType, usesTangents } = this.getAnimationData(animation);
  1277. if (easingType === undefined && easingMode === undefined && !usesTangents) {
  1278. return true;
  1279. } else {
  1280. return false;
  1281. }
  1282. } else {
  1283. return false;
  1284. }
  1285. }
  1286. /**
  1287. * Timeline
  1288. * This section controls the timeline.
  1289. */
  1290. changeCurrentFrame(frame: number) {
  1291. this.stopAnimation();
  1292. const currentValue = this.calculateCurrentPointInCurve(frame);
  1293. const value = currentValue ?? 0;
  1294. const keyframe: IAnimationKey = { frame, value };
  1295. this.setState(
  1296. {
  1297. currentFrame: frame,
  1298. isPlaying: false,
  1299. },
  1300. () => this.setCanvasPosition(keyframe)
  1301. );
  1302. }
  1303. calculateCurrentPointInCurve = (frame: number): number | undefined => {
  1304. if (this.state.selectedPathData !== undefined) {
  1305. const selectedCurve = this.state.selectedPathData[this.state.selectedCoordinate].domCurve.current;
  1306. if (selectedCurve !== null) {
  1307. const curveLength = selectedCurve.getTotalLength();
  1308. const frameValue = (frame * curveLength) / 100;
  1309. const currentPointInCurve = selectedCurve.getPointAtLength(frameValue);
  1310. const middle = this._heightScale / 2;
  1311. const offset = (currentPointInCurve?.y * this._heightScale - this._heightScale ** 2 / 2) / middle / this._heightScale;
  1312. const unit = Math.sign(offset);
  1313. const currentValue = unit === -1 ? Math.abs(offset + unit) : unit - offset;
  1314. this.setState({
  1315. currentValue: currentValue,
  1316. currentPoint: currentPointInCurve,
  1317. });
  1318. return currentValue;
  1319. }
  1320. }
  1321. return undefined;
  1322. };
  1323. setCanvasPosition(keyframe: IAnimationKey) {
  1324. if (this.state.selected) {
  1325. const positionX = (keyframe.frame - 10) * 10;
  1326. let value = 0;
  1327. if (keyframe.value === null) {
  1328. value = this.state.panningY;
  1329. } else {
  1330. value = this.getValueAsArray(this.state.selected.dataType, keyframe.value)[this.state.selectedCoordinate];
  1331. }
  1332. const centerCanvas = this._heightScale / 2;
  1333. const positionY = centerCanvas - value * centerCanvas;
  1334. this.setState({ panningX: positionX, panningY: positionY, repositionCanvas: true });
  1335. }
  1336. }
  1337. setCurrentFrame(direction: number) {
  1338. this.setState({
  1339. currentFrame: this.state.currentFrame + direction,
  1340. });
  1341. }
  1342. changeAnimationLimit(limit: number) {
  1343. const doubleLimit = limit * 2;
  1344. this.setState({
  1345. animationLimit: limit,
  1346. canvasLength: doubleLimit,
  1347. frameAxisLength: this.setFrameAxis(doubleLimit),
  1348. });
  1349. }
  1350. updateFrameInKeyFrame(frame: number, index: number) {
  1351. if (this.state && this.state.selected) {
  1352. let animation = this.state.selected;
  1353. let keys = [...animation.getKeys()];
  1354. keys[index].frame = frame;
  1355. animation.setKeys(keys);
  1356. this.selectAnimation(animation);
  1357. }
  1358. }
  1359. playPause(direction: number) {
  1360. this.registerObs();
  1361. if (this.state.selected) {
  1362. let target = this.props.entity;
  1363. if (this.props.entity instanceof TargetedAnimation) {
  1364. target = this.props.entity.target;
  1365. }
  1366. if (this.state.isPlaying && direction === 0) {
  1367. this.props.scene.stopAnimation(target);
  1368. this.setState({ isPlaying: false });
  1369. this._isPlaying = false;
  1370. this.forceUpdate();
  1371. } else {
  1372. if (this.state.isPlaying) {
  1373. this.props.scene.stopAnimation(target);
  1374. }
  1375. this.props.scene.stopAllAnimations();
  1376. let keys = this.state.selected.getKeys();
  1377. if (keys.length !== 0) {
  1378. let firstFrame = keys[0].frame;
  1379. let LastFrame = this.state.selected.getHighestFrame();
  1380. if (direction === 1) {
  1381. this._mainAnimatable = this.props.scene.beginAnimation(target, firstFrame, LastFrame, this.state.isLooping);
  1382. }
  1383. if (direction === -1) {
  1384. this._mainAnimatable = this.props.scene.beginAnimation(target, LastFrame, firstFrame, this.state.isLooping);
  1385. }
  1386. if (!this.state.isLooping && this._mainAnimatable) {
  1387. this._mainAnimatable.onAnimationEnd = () => this.playPause(0);
  1388. }
  1389. }
  1390. this._isPlaying = true;
  1391. this.setState({ isPlaying: true });
  1392. this.forceUpdate();
  1393. }
  1394. }
  1395. }
  1396. moveFrameTo(e: React.MouseEvent<SVGRectElement, MouseEvent>) {
  1397. this.stopAnimation();
  1398. var svg = e.currentTarget as SVGRectElement;
  1399. var CTM = svg.getScreenCTM();
  1400. let position;
  1401. if (CTM) {
  1402. position = new Vector2((e.clientX - CTM.e) / CTM.a, (e.clientY - CTM.f) / CTM.d);
  1403. let selectedFrame = Math.round(position.x / this._pixelFrameUnit);
  1404. this.setState({ currentFrame: selectedFrame, isPlaying: false });
  1405. }
  1406. }
  1407. registerObs() {
  1408. if (this._onBeforeRenderObserver) {
  1409. this.props.scene.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  1410. this._onBeforeRenderObserver = null;
  1411. }
  1412. this._onBeforeRenderObserver = this.props.scene.onBeforeRenderObservable.add(() => {
  1413. if (!this._isPlaying || !this._mainAnimatable) {
  1414. return;
  1415. }
  1416. this.setState({
  1417. currentFrame: Math.round(this._mainAnimatable.masterFrame),
  1418. });
  1419. });
  1420. }
  1421. componentWillUnmount() {
  1422. this.playPause(0);
  1423. if (this._onBeforeRenderObserver) {
  1424. this.props.scene.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  1425. this._onBeforeRenderObserver = null;
  1426. }
  1427. }
  1428. isCurrentFrame(frame: number) {
  1429. return this.state.currentFrame === frame;
  1430. }
  1431. render() {
  1432. return (
  1433. <div ref={this._editor} id="animation-curve-editor">
  1434. <Notification message={this.state.notification} open={this.state.notification !== "" ? true : false} close={() => this.clearNotification()} />
  1435. <GraphActionsBar
  1436. setKeyframeValue={this.setKeyframeValueFromInput}
  1437. enabled={this.state.selected === null || this.state.selected === undefined ? false : true}
  1438. title={this._entityName}
  1439. actionableKeyframe={this.state.actionableKeyframe}
  1440. handleFrameChange={(e) => this.handleFrameChange(e)}
  1441. handleValueChange={(e) => this.handleValueChange(e)}
  1442. addKeyframe={() => this.addKeyframeClick()}
  1443. removeKeyframe={() => this.removeKeyframeClick()}
  1444. brokenMode={this.state.isBrokenMode}
  1445. brokeTangents={() => this.setBrokenMode()}
  1446. lerpMode={this.state.lerpMode}
  1447. setLerpToActiveControlPoint={() => this.setLerpToActiveControlPoint()}
  1448. flatTangent={() => this.setFlatTangent()}
  1449. />
  1450. <div className="content">
  1451. <div className="row">
  1452. <EditorControls
  1453. deselectAnimation={() => this.deselectAnimation()}
  1454. selectAnimation={(animation: Animation, axis?: SelectedCoordinate) => this.selectAnimation(animation, axis)}
  1455. isTargetedAnimation={this._isTargetedAnimation}
  1456. entity={this.props.entity}
  1457. selected={this.state.selected}
  1458. lockObject={this.props.lockObject}
  1459. setNotificationMessage={(message: string) => {
  1460. this.setState({ notification: message });
  1461. }}
  1462. globalState={this.props.globalState}
  1463. snippetServer={this._snippetUrl}
  1464. fps={this.state.fps}
  1465. setFps={(fps: number) => this.setFramesPerSecond(fps)}
  1466. setIsLooping={() => this.setIsLooping()}
  1467. />
  1468. <div ref={this._graphCanvas} className="graph-chart" onWheel={(e) => this.zoom(e)}>
  1469. {this.state.svgKeyframes && (
  1470. <SvgDraggableArea
  1471. ref={this._svgCanvas}
  1472. selectKeyframe={(id: string, multiselect: boolean) => this.selectKeyframe(id, multiselect)}
  1473. viewBoxScale={this.state.frameAxisLength.length}
  1474. scale={this.state.scale}
  1475. keyframeSvgPoints={this.state.svgKeyframes}
  1476. removeSelectedKeyframes={(points: IKeyframeSvgPoint[]) => this.removeKeyframes(points)}
  1477. selectedControlPoint={(type: string, id: string) => this.selectedControlPoint(type, id)}
  1478. deselectKeyframes={() => this.deselectKeyframes()}
  1479. updatePosition={(updatedSvgKeyFrame: IKeyframeSvgPoint, id: string) => this.renderPoints(updatedSvgKeyFrame, id)}
  1480. panningY={(panningY: number) => {
  1481. this.setState({ panningY: panningY });
  1482. }}
  1483. panningX={(panningX: number) => {
  1484. this.setState({ panningX: panningX });
  1485. }}
  1486. setCurrentFrame={(direction: number) => this.setCurrentFrame(direction)}
  1487. positionCanvas={new Vector2(this.state.panningX, this.state.panningY)}
  1488. repositionCanvas={this.state.repositionCanvas}
  1489. canvasPositionEnded={() => this.setState({ repositionCanvas: false })}
  1490. resetActionableKeyframe={() => this.resetActionableKeyframe()}
  1491. >
  1492. {/* Multiple Curves */}
  1493. {this.state.selectedPathData?.map((curve, i) => (
  1494. <path
  1495. key={i}
  1496. ref={curve.domCurve}
  1497. pathLength={curve.pathLength}
  1498. id="curve"
  1499. d={curve.pathData}
  1500. style={{
  1501. stroke: curve.color,
  1502. fill: "none",
  1503. strokeWidth: "0.5",
  1504. }}
  1505. ></path>
  1506. ))}
  1507. {this.setValueLines(this.state.valueScale).map((line, i) => {
  1508. return (
  1509. <text
  1510. key={`value_inline_${i}`}
  1511. x={this.state.panningX - 5}
  1512. y={line.value}
  1513. dx="2"
  1514. textAnchor="middle"
  1515. dy="-1"
  1516. style={{
  1517. fontSize: `${0.18 * this.state.scale}em`,
  1518. fontWeight: "bold",
  1519. textAlign: "center",
  1520. }}
  1521. >
  1522. {line.label}
  1523. </text>
  1524. );
  1525. })}
  1526. {this.setValueLines(this.state.valueScale).map((line, i) => {
  1527. return <line key={i} x1={-((this.state.frameAxisLength.length * 10) / 2)} y1={line.value} x2={this.state.frameAxisLength.length * 10} y2={line.value}></line>;
  1528. })}
  1529. <rect onClick={(e) => this.moveFrameTo(e)} x={-((this.state.frameAxisLength.length * 10) / 2)} y={91 + this.state.panningY + "%"} width={this.state.frameAxisLength.length * 10} height="9%" fill="#222" style={{ cursor: "pointer" }}></rect>
  1530. {this.state.frameAxisLength.map((f, i) => (
  1531. <svg key={i} x="0" y={96 + this.state.panningY + "%"} className="frame-contain">
  1532. <text x={f.value} y="1px" dx="2px" style={{ fontSize: `${0.2 * this.state.scale}em` }}>
  1533. {f.label}
  1534. </text>
  1535. <line x1={f.value} y1="0" x2={f.value} y2="5%"></line>
  1536. {f.value % this.state.fps === 0 && f.value !== 0 ? <line x1={f.value} y1="-100%" x2={f.value} y2="5%"></line> : null}
  1537. {this.state.selected && this.isCurrentFrame(f.label) ? (
  1538. <svg>
  1539. <line
  1540. x1={f.value}
  1541. y1="0"
  1542. x2={f.value}
  1543. y2="-100%"
  1544. style={{
  1545. stroke: "white",
  1546. strokeWidth: 0.4,
  1547. }}
  1548. />
  1549. <svg x={f.value} y="-1">
  1550. <circle className="svg-playhead" cx="0" cy="0" r="2%" fill="white" />
  1551. <text
  1552. x="0"
  1553. y="1%"
  1554. textAnchor="middle"
  1555. style={{
  1556. fontSize: `${0.17 * this.state.scale}em`,
  1557. pointerEvents: "none",
  1558. fontWeight: 600,
  1559. }}
  1560. >
  1561. {f.label}
  1562. </text>
  1563. </svg>
  1564. </svg>
  1565. ) : null}
  1566. </svg>
  1567. ))}
  1568. </SvgDraggableArea>
  1569. )}
  1570. <ScaleLabel current={this.state.valueScale} />
  1571. </div>
  1572. </div>
  1573. <div className="row-bottom">
  1574. <Timeline
  1575. currentFrame={this.state.currentFrame}
  1576. playPause={(direction: number) => this.playPause(direction)}
  1577. isPlaying={this.state.isPlaying}
  1578. dragKeyframe={(frame: number, index: number) => this.updateFrameInKeyFrame(frame, index)}
  1579. onCurrentFrameChange={(frame: number) => this.changeCurrentFrame(frame)}
  1580. onAnimationLimitChange={(limit: number) => this.changeAnimationLimit(limit)}
  1581. animationLimit={this.state.animationLimit}
  1582. keyframes={this.state.selected && this.state.selected.getKeys()}
  1583. selected={this.state.selected && this.state.selected.getKeys()[0]}
  1584. fps={this.state.fps}
  1585. repositionCanvas={(keyframe: IAnimationKey) => this.setCanvasPosition(keyframe)}
  1586. ></Timeline>
  1587. </div>
  1588. </div>
  1589. </div>
  1590. );
  1591. }
  1592. }