Skip to content

Commit

Permalink
Merge pull request #927 from vorth/cancel-tweens
Browse files Browse the repository at this point in the history
Tweens canceled on mouse interaction
  • Loading branch information
vorth authored Oct 22, 2024
2 parents 19178a3 + 680047c commit 827bea3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
32 changes: 18 additions & 14 deletions online/src/viewer/context/camera.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ const CameraProvider = ( props ) =>
setState( 'outlines', props.outlines );
}

const setCamera = loadedCamera =>
{
setState( 'camera', loadedCamera );
// important: update trackballCamera synchronously, NOT as an effect.
injectCameraState( state.camera, trackballCamera );
}

if ( !!props.context ) {
// Sync background from the context
createEffect( () => {
Expand All @@ -136,8 +143,7 @@ const CameraProvider = ( props ) =>
// Sync rotation from the context
createEffect( () => {
const { up, lookDir } = props.context.state.camera;
setState( 'camera', { up, lookDir } );
injectCameraState( state.camera, trackballCamera );
setCamera( { up, lookDir } );
});
}

Expand All @@ -160,6 +166,7 @@ const CameraProvider = ( props ) =>
// This gets hooked up to TrackballControls changes, and updates the main camera state
// from the captive trackballCamera in response.
const extractedCamera = extractCameraState( trackballCamera, target );
// console.log( `trackball sync ${name}, distance=${extractedCamera.distance}` );
const { up, lookDir } = extractedCamera;
if ( !! props.context ) {
props.context.setCamera( { up, lookDir } );
Expand All @@ -182,14 +189,6 @@ const CameraProvider = ( props ) =>
return [ vec.x, vec.y, vec.z ];
}

const setCamera = loadedCamera =>
{
setState( 'camera', loadedCamera );
// console.log( `setCamera ${loadedCamera.distance}` );
// important: update trackballCamera synchronously, NOT as an effect.
injectCameraState( state.camera, trackballCamera );
}

const setDistance = distance =>
{
setCamera( fixedFrustum( distance ) );
Expand All @@ -213,6 +212,12 @@ const CameraProvider = ( props ) =>
}
animate();

const cancelTweens = () =>
{
tweens .getAll() .map( tween => tween.stop() );
tweens .removeAll();
}

const tweenCamera = ( goalCamera ) =>
{
const { duration=0 } = state.tweening;
Expand All @@ -222,8 +227,7 @@ const CameraProvider = ( props ) =>
return;
}

tweens .getAll() .map( tween => tween.stop() );
tweens .removeAll();
cancelTweens();

const { distance, lookAt: [x,y,z] } = goalCamera; // target values

Expand Down Expand Up @@ -268,11 +272,11 @@ const CameraProvider = ( props ) =>
// code in the caller finishes.
requestAnimationFrame( () => tweens .getAll() .map( tween => tween.start() ) );
}

const providerValue = {
name: props.name,
perspectiveProps, trackballProps, state,
resetCamera, setCamera, tweenCamera, setLighting, togglePerspective, toggleOutlines, setDistance, mapViewToWorld,
resetCamera, setCamera, tweenCamera, cancelTweens, setLighting, togglePerspective, toggleOutlines, setDistance, mapViewToWorld,
};

// The perspectiveProps is used to initialize PerspectiveCamera in clients.
Expand Down
6 changes: 4 additions & 2 deletions online/src/viewer/ltcanvas.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ const Lighting = () =>

const LightedCameraControls = (props) =>
{
const { perspectiveProps, trackballProps, name, state } = useCamera();
const { perspectiveProps, trackballProps, name, state, cancelTweens } = useCamera();
const [ tool ] = useInteractionTool();
const enableTrackball = () => ( tool === undefined ) || tool().allowTrackball;
props = mergeProps( { rotateSpeed: 4.5, zoomSpeed: 3, panSpeed: 1 }, props );
const halfWidth = () => perspectiveProps.width / 2;

const onDragStart = () => cancelTweens();
const onDragEnd = () => ( tool !== undefined ) && tool() .onTrackballEnd();

return (
Expand All @@ -57,7 +58,8 @@ const LightedCameraControls = (props) =>
</PerspectiveCamera>
</Show>
<TrackballControls enabled={enableTrackball()} rotationOnly={props.rotationOnly} name={name}
camera={trackballProps.camera} target={perspectiveProps.target} sync={trackballProps.sync} trackballEnd={onDragEnd}
camera={trackballProps.camera} target={perspectiveProps.target} sync={trackballProps.sync}
trackballStart={onDragStart} trackballEnd={onDragEnd}
rotateSpeed={props.rotateSpeed} zoomSpeed={props.zoomSpeed} panSpeed={props.panSpeed} />
</>
);
Expand Down
4 changes: 4 additions & 0 deletions online/src/viewer/trackballcontrols.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export const TrackballControls = (props) =>

controls.connect( gl().domElement );

const onStart = () => {
props.trackballStart && props.trackballStart( controls .target, props.name );
}
controls .addEventListener( "start", onStart );
const onChange = () => props.sync( controls .target, props.name );
controls .addEventListener( "change", onChange );
const onEnd = () => {
Expand Down

0 comments on commit 827bea3

Please sign in to comment.