From c3258f43f230be87d103cfa1c6720fc0b858fa40 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 21 May 2021 15:58:29 +0200 Subject: timeline scrubber moving (still buggy) --- pages/editor.tsx | 56 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) (limited to 'pages') diff --git a/pages/editor.tsx b/pages/editor.tsx index cb68601..56becaf 100644 --- a/pages/editor.tsx +++ b/pages/editor.tsx @@ -1,4 +1,6 @@ -import { CSSProperties, ReactNode, useEffect, useState } from 'react'; +import { CSSProperties, ReactNode, useEffect, useRef, useState } from 'react'; +import { animated, useSpring } from 'react-spring'; +import { useGesture } from 'react-use-gesture'; import create from 'zustand'; import { loopSlide } from '../timeline'; import { TimedVideoPlayer } from './present'; @@ -30,6 +32,13 @@ var getTimelineZoom = create(set => ({ var zoomToPx = (zoom: number) => (12 - 0.5) * zoom ** (1 / 0.4) + 0.5; +function getFrameAtOffset(offset: number, timelineZoom: number) { + var timeline = document.querySelector('.timeline .timelineInner'); + var currentOffset = timeline.scrollLeft; + var frame = (offset + currentOffset) / zoomToPx(timelineZoom); + return frame; +} + var useTimelineLabels = create(set => ({ labels: [], setLabels: (newLabels: Array) => set(() => ({ labels: newLabels })), @@ -59,6 +68,8 @@ function TimelineEditor(props: { var frame = useFrame((st: any) => st.currentFrame); var setFrame = useFrame((st: any) => st.setFrame); + var timelineZoom = getTimelineZoom((st: any) => st.zoom); + useEffect(() => { var canvas = document.getElementById('timeScaleCanvas') as HTMLCanvasElement; var ctx = canvas.getContext('2d'); @@ -141,17 +152,39 @@ function TimelineEditor(props: { window.addEventListener('resize', onresize); }, []); + var scrubberRef = useRef(null); + var scrubberDragRef = useRef(null); + + var [scrubberPos, scrubberSpring] = useSpring( + () => ({ + x: 0, + config: { mass: 0.5, tension: 500, friction: 20 }, + }), + ); + + useGesture( + { + onDrag: ({ offset: [x, _y] }) => scrubberSpring.start({ x: Math.round(getFrameAtOffset(x, timelineZoom)) }), + }, + { domTarget: scrubberDragRef, eventOptions: { passive: false } }, + ); + return <>
{timelineLabels}
-
+
-
+
setDummy(!dummy); - var [player, setPlayer] = useState(new TimedVideoPlayer()); + var [player, _setPlayer] = useState(new TimedVideoPlayer()); var timelineZoom = getTimelineZoom((st: any) => st.zoom); var setTimelineZoom = getTimelineZoom((st: any) => st.setZoom); @@ -181,7 +214,7 @@ export default function Index() { var frame = useFrame((st: any) => st.currentFrame); var mouseX = 0; - var mouseY = 0; + // var mouseY = 0; useEffect(() => { var videoEL = document.getElementById('player') as HTMLVideoElement; @@ -190,8 +223,7 @@ export default function Index() { function zoomAroundPoint(newZoom: number, pivot: number) { var timeline = document.querySelector('.timeline .timelineInner'); - var currentOffset = timeline.scrollLeft; - var frame = (pivot + currentOffset) / zoomToPx(timelineZoom); + var frame = getFrameAtOffset(pivot, timelineZoom); var newOffset = (frame * zoomToPx(newZoom)) - pivot; timeline.scrollLeft = newOffset; @@ -214,10 +246,16 @@ export default function Index() { window.addEventListener('mousemove', e => { var rect = canvas.getBoundingClientRect(); mouseX = e.clientX - rect.x; - mouseY = e.clientY - rect.y; + // mouseY = e.clientY - rect.y; }); }, []); + useEffect(() => { + var preventDefault = (e: Event) => e.preventDefault(); + document.addEventListener('gesturestart', preventDefault); + document.addEventListener('gesturechange', preventDefault); + }, []); + return <>
@@ -320,7 +358,7 @@ export default function Index() {
{ + onChange={(_event: any, newValue: number | number[]) => { var center = document.querySelector('.timeline .timelineInner').clientWidth / 2; zoomAroundPoint(newValue as number, center); }} -- cgit v1.2.3