Skip to content

Commit

Permalink
feat: disable zoom via props
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-rpm committed Dec 17, 2024
1 parent eb70101 commit ab6eb66
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/App/components/Nova.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const AvatarNova: FC = () => {
))}
</SettingsPanel>
<Avatar
disableZoom
modelSrc={modelSrc}
emotion={emotions.smile}
animations={animations}
Expand Down
8 changes: 6 additions & 2 deletions src/components/Avatar/Avatar.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export interface AvatarProps extends LightingProps, EnvironmentProps, Omit<BaseM
*/
materialConfig?: MaterialConfiguration;
onAnimationEnd?: (action: AnimationAction) => void;
disableZoom?: boolean;
}

/**
Expand Down Expand Up @@ -205,7 +206,8 @@ const Avatar: FC<AvatarProps> = ({
lightTarget,
fov = 50,
onAnimationEnd,
materialConfig
materialConfig,
disableZoom
}) => {
const setSpawnState = useSetAtom(spawnState);

Expand All @@ -228,6 +230,7 @@ const Avatar: FC<AvatarProps> = ({
scale={scale}
onLoaded={onLoaded}
bloom={effects?.bloom}
disableZoom={disableZoom}
onAnimationEnd={onAnimationEnd}
materialConfig={materialConfig}
/>
Expand Down Expand Up @@ -303,7 +306,8 @@ const Avatar: FC<AvatarProps> = ({
materialConfig,
onAnimationEnd,
idleRotation,
headMovement
headMovement,
disableZoom
]);

useEffect(() => triggerCallback(onLoading), [modelSrc, animationSrc, onLoading]);
Expand Down
19 changes: 18 additions & 1 deletion src/components/Models/Model/Model.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ interface ModelProps extends BaseModelProps {
scene: Group;
modelRef?: Ref<Group>;
scale?: number;
disableZoom?: boolean;
onSpawnAnimationFinish?: () => void;
}

const ROTATION_STEP = 0.005;

export const Model: FC<ModelProps> = ({
scene,
disableZoom,
scale = 1,
modelRef,
onLoaded,
Expand Down Expand Up @@ -77,6 +79,13 @@ export const Model: FC<ModelProps> = ({
useEffect(() => triggerCallback(onLoaded), [scene, onLoaded]);

useEffect(() => {
const disableZoomHandler = (e: WheelEvent) => {
if (disableZoom) {
e.preventDefault();
e.stopPropagation();
}
};

gl.domElement.addEventListener('mousedown', setTouchingOn);
gl.domElement.addEventListener('touchstart', setTouchingOn, { passive: true });
window.addEventListener('mouseup', setTouchingOff);
Expand All @@ -86,6 +95,10 @@ export const Model: FC<ModelProps> = ({
window.addEventListener('mousemove', onTouchMove);
gl.domElement.addEventListener('touchmove', onTouchMove, { passive: true });

if (disableZoom) {
gl.domElement.addEventListener('wheel', disableZoomHandler, { passive: false });
}

return () => {
gl.domElement.removeEventListener('mousedown', setTouchingOn);
gl.domElement.removeEventListener('touchstart', setTouchingOn);
Expand All @@ -95,8 +108,12 @@ export const Model: FC<ModelProps> = ({

window.removeEventListener('mousemove', onTouchMove);
gl.domElement.removeEventListener('touchmove', onTouchMove);

if (disableZoom) {
gl.domElement.removeEventListener('wheel', disableZoomHandler);
}
};
});
}, [disableZoom, gl.domElement, onTouchMove]);

const spawnComponent = useMemo(
() => <Spawn avatar={scene} onSpawnFinish={onSpawnAnimationFinish} />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export interface MultipleAnimationModelProps extends BaseModelProps {
activeAnimation: string;
scale?: number;
emotion?: Emotion;
disableZoom?: boolean;
onAnimationEnd?: (action: AnimationAction) => void;
}

export const MultipleAnimationModel: FC<MultipleAnimationModelProps> = ({
modelSrc,
disableZoom,
animations,
activeAnimation,
scale = 1,
Expand Down Expand Up @@ -106,5 +108,14 @@ export const MultipleAnimationModel: FC<MultipleAnimationModelProps> = ({
useIdleExpression('blink', scene);
useFallbackScene(scene, setModelFallback);

return <Model scene={scene} scale={scale} onLoaded={onLoaded} bloom={bloom} materialConfig={materialConfig} />;
return (
<Model
scene={scene}
scale={scale}
disableZoom={disableZoom}
onLoaded={onLoaded}
bloom={bloom}
materialConfig={materialConfig}
/>
);
};

0 comments on commit ab6eb66

Please sign in to comment.