Fix zoom to not jump anymore (#272)

* Remove scroll handling, honor zoom on drag + ctrl

* Add back ability to zoom with mouse wheel, but properly

* Add TODO for 'any' removal

* Update kittycad lib to remove 'any'
This commit is contained in:
Frank Noirot
2023-08-21 10:52:41 -04:00
committed by GitHub
parent 43b1272538
commit 25392824cb
4 changed files with 14 additions and 23 deletions

View File

@ -8,7 +8,7 @@
"@fortawesome/free-solid-svg-icons": "^6.4.2", "@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/react-fontawesome": "^0.2.0", "@fortawesome/react-fontawesome": "^0.2.0",
"@headlessui/react": "^1.7.13", "@headlessui/react": "^1.7.13",
"@kittycad/lib": "^0.0.28", "@kittycad/lib": "^0.0.29",
"@react-hook/resize-observer": "^1.2.6", "@react-hook/resize-observer": "^1.2.6",
"@tauri-apps/api": "^1.3.0", "@tauri-apps/api": "^1.3.0",
"@testing-library/jest-dom": "^5.14.1", "@testing-library/jest-dom": "^5.14.1",

View File

@ -380,8 +380,11 @@ export function App() {
clientX, clientX,
clientY, clientY,
ctrlKey, ctrlKey,
shiftKey,
currentTarget, currentTarget,
nativeEvent,
}) => { }) => {
nativeEvent.preventDefault()
if (isMouseDownInStream) { if (isMouseDownInStream) {
setDidDragInStream(true) setDidDragInStream(true)
} }
@ -393,7 +396,7 @@ export function App() {
...streamDimensions, ...streamDimensions,
}) })
const interaction = ctrlKey ? 'pan' : 'rotate' const interaction = ctrlKey ? 'zoom' : shiftKey ? 'pan' : 'rotate'
const newCmdId = uuidv4() const newCmdId = uuidv4()
setCmdId(newCmdId) setCmdId(newCmdId)
@ -432,7 +435,7 @@ export function App() {
return ( return (
<div <div
className="h-screen overflow-hidden relative flex flex-col" className="h-screen overflow-hidden relative flex flex-col cursor-pointer select-none"
onMouseMove={handleMouseMove} onMouseMove={handleMouseMove}
ref={streamRef} ref={streamRef}
> >

View File

@ -7,14 +7,11 @@ import {
} from 'react' } from 'react'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { useStore } from '../useStore' import { useStore } from '../useStore'
import { throttle } from '../lib/utils'
import { EngineCommand } from '../lang/std/engineConnection'
import { getNormalisedCoordinates } from '../lib/utils' import { getNormalisedCoordinates } from '../lib/utils'
import Loading from './Loading' import Loading from './Loading'
export const Stream = ({ className = '' }) => { export const Stream = ({ className = '' }) => {
const [isLoading, setIsLoading] = useState(true) const [isLoading, setIsLoading] = useState(true)
const [zoom, setZoom] = useState(0)
const videoRef = useRef<HTMLVideoElement>(null) const videoRef = useRef<HTMLVideoElement>(null)
const { const {
mediaStream, mediaStream,
@ -45,7 +42,6 @@ export const Stream = ({ className = '' }) => {
if (!videoRef.current) return if (!videoRef.current) return
if (!mediaStream) return if (!mediaStream) return
videoRef.current.srcObject = mediaStream videoRef.current.srcObject = mediaStream
setZoom(videoRef.current.getBoundingClientRect().height / 2)
}, [mediaStream, engineCommandManager]) }, [mediaStream, engineCommandManager])
const handleMouseDown: MouseEventHandler<HTMLVideoElement> = ({ const handleMouseDown: MouseEventHandler<HTMLVideoElement> = ({
@ -80,24 +76,16 @@ export const Stream = ({ className = '' }) => {
setIsMouseDownInStream(true) setIsMouseDownInStream(true)
} }
// TODO: consolidate this with the same function in App.tsx
const debounceSocketSend = throttle<EngineCommand>((message) => {
engineCommandManager?.sendSceneCommand(message)
}, 16)
const handleScroll: WheelEventHandler<HTMLVideoElement> = (e) => { const handleScroll: WheelEventHandler<HTMLVideoElement> = (e) => {
e.preventDefault() e.preventDefault()
debounceSocketSend({ engineCommandManager?.sendSceneCommand({
type: 'modeling_cmd_req', type: 'modeling_cmd_req',
cmd: { cmd: {
type: 'camera_drag_move', type: 'default_camera_zoom',
interaction: 'zoom', magnitude: e.deltaY * 0.4,
window: { x: 0, y: zoom + e.deltaY },
}, },
cmd_id: uuidv4(), cmd_id: uuidv4(),
}) })
setZoom(zoom + e.deltaY)
} }
const handleMouseUp: MouseEventHandler<HTMLVideoElement> = ({ const handleMouseUp: MouseEventHandler<HTMLVideoElement> = ({
@ -152,7 +140,7 @@ export const Stream = ({ className = '' }) => {
onMouseUp={handleMouseUp} onMouseUp={handleMouseUp}
onContextMenu={(e) => e.preventDefault()} onContextMenu={(e) => e.preventDefault()}
onContextMenuCapture={(e) => e.preventDefault()} onContextMenuCapture={(e) => e.preventDefault()}
onWheelCapture={handleScroll} onWheel={handleScroll}
onPlay={() => setIsLoading(false)} onPlay={() => setIsLoading(false)}
className="w-full h-full" className="w-full h-full"
/> />

View File

@ -1747,10 +1747,10 @@
resolved "https://registry.yarnpkg.com/@juggle/resize-observer/-/resize-observer-3.4.0.tgz#08d6c5e20cf7e4cc02fd181c4b0c225cd31dbb60" resolved "https://registry.yarnpkg.com/@juggle/resize-observer/-/resize-observer-3.4.0.tgz#08d6c5e20cf7e4cc02fd181c4b0c225cd31dbb60"
integrity sha512-dfLbk+PwWvFzSxwk3n5ySL0hfBog779o8h68wK/7/APo/7cgyWp5jcXockbxdk5kFRkbeXWm4Fbi9FrdN381sA== integrity sha512-dfLbk+PwWvFzSxwk3n5ySL0hfBog779o8h68wK/7/APo/7cgyWp5jcXockbxdk5kFRkbeXWm4Fbi9FrdN381sA==
"@kittycad/lib@^0.0.28": "@kittycad/lib@^0.0.29":
version "0.0.28" version "0.0.29"
resolved "https://registry.yarnpkg.com/@kittycad/lib/-/lib-0.0.28.tgz#a40d67544bf2eb5571855114a75d8cb0eb9ec189" resolved "https://registry.yarnpkg.com/@kittycad/lib/-/lib-0.0.29.tgz#e0c0751fc124dd0136f9731c6bb962cd8b8202f6"
integrity sha512-T5Lnu7qoB3bc4OMD3s4khPas+VkNKrMllpkvCzKrx7XXrelDCZOd21xALwEzbzOPMUdtp2SBd6nuJKMH/N2aOA== integrity sha512-YpyXyOfoUBItJk71AP8M9i4QGvNnNHiSO35dMDx2EsDKqEGwTLDHOBniwPEq+iJqrGcZf2CfBSOvAOnZCH790A==
dependencies: dependencies:
node-fetch "3.3.2" node-fetch "3.3.2"
openapi-types "^12.0.0" openapi-types "^12.0.0"