Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
fix: timeline speed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 committed Sep 8, 2022
1 parent 6a6d98e commit 8aab3e9
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/components/molecules/Visualizer/Widget/Timeline/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useCallback, useEffect } from "react";
import { useState, useCallback, useEffect, useRef } from "react";

import { TimeEventHandler } from "@reearth/components/atoms/Timeline/types";

Expand All @@ -22,6 +22,7 @@ export const useTimeline = () => {
);
const [isOpened, setIsOpened] = useState(false);
const [currentTime, setCurrentTime] = useState(() => getOrNewDate(clock?.currentTime).getTime());
const isClockInitialized = useRef(false);
const clockCurrentTime = clock?.currentTime.getTime();
const clockStartTime = clock?.startTime.getTime();
const clockStopTime = clock?.stopTime.getTime();
Expand Down Expand Up @@ -75,19 +76,23 @@ export const useTimeline = () => {
[clock, speed],
);

const handleOnSpeedChange = useCallback((speed: number) => {
setSpeed(speed);
if (clock) {
const absSpeed = Math.abs(speed);
// Maybe we need to throttle changing speed.
clock.speed = clock.speed > 0 ? absSpeed : absSpeed * -1;
clock.tick();
}
}, []);
const handleOnSpeedChange = useCallback(
(speed: number) => {
setSpeed(speed);
if (clock) {
const absSpeed = Math.abs(speed);
// Maybe we need to throttle changing speed.
clock.speed = clock.speed > 0 ? absSpeed : absSpeed * -1;
clock.tick();
}
},
[clock],
);

// Initialize clock value
useEffect(() => {
if (clock) {
if (clock && !isClockInitialized.current) {
isClockInitialized.current = true;
queueMicrotask(() => {
clock.speed = 1;
clock.tick();
Expand Down

0 comments on commit 8aab3e9

Please sign in to comment.