Skip to content

Commit

Permalink
enableTransition prop (for initial animation)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruucm committed Mar 31, 2024
1 parent e2d3445 commit e9b6ed3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function ShaderGradient({
rotSpringOption,
posSpringOption,
isFigmaPlugin = false,
enableTransition = true,
...props
}: GradientT) {
let controlProps = { ...defaultProps, ...props }
Expand Down Expand Up @@ -45,7 +46,11 @@ export function ShaderGradient({
posSpringOption={posSpringOption}
/>
{toggleAxis && <Axis isFigmaPlugin={isFigmaPlugin} />}
<CameraControl dampingFactor={dampingFactor} {...others} />
<CameraControl
dampingFactor={dampingFactor}
enableTransition={enableTransition}
{...others}
/>
</>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function useCameraAnimation({
cDistance,
cameraZoom,
zoomOut,
enableTransition,
}) {
const ref: any = useRef()
const control = ref.current
Expand All @@ -23,28 +24,28 @@ export function useCameraAnimation({

// rorate the camera
useEffect(() => {
control?.rotateTo(dToR(cAzimuthAngle), dToR(cPolarAngle), true)
control?.rotateTo(dToR(cAzimuthAngle), dToR(cPolarAngle), enableTransition)
}, [control, cAzimuthAngle, cPolarAngle])

// zoom-out tool
useEffect(() => {
if (zoomOut) {
// fixed distance & zoom
if (type === 'sphere') {
control?.dollyTo(zoomOutSphere.distance, true)
control?.zoomTo(zoomOutSphere.zoom, true)
control?.dollyTo(zoomOutSphere.distance, enableTransition)
control?.zoomTo(zoomOutSphere.zoom, enableTransition)
} else {
control?.dollyTo(zoomOutPlanes.distance, true)
control?.zoomTo(zoomOutPlanes.zoom, true)
control?.dollyTo(zoomOutPlanes.distance, enableTransition)
control?.zoomTo(zoomOutPlanes.zoom, enableTransition)
}
} else {
// control distance for planes & zoom for sphere
if (type === 'sphere') {
control?.zoomTo(cameraZoom, true)
control?.dollyTo(defaultSphereDistance, true)
control?.zoomTo(cameraZoom, enableTransition)
control?.dollyTo(defaultSphereDistance, enableTransition)
} else {
control?.dollyTo(cDistance, true)
control?.zoomTo(defaultPlanesZoom, true)
control?.dollyTo(cDistance, enableTransition)
control?.zoomTo(defaultPlanesZoom, enableTransition)
}
}
}, [control, zoomOut, type, cameraZoom, cDistance])
Expand Down
2 changes: 2 additions & 0 deletions packages/shadergradient-v2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ export type GradientT = MeshT & {
zoomOut?: boolean
toggleAxis?: boolean
hoverState?: string

enableTransition?: boolean
}
7 changes: 6 additions & 1 deletion packages/shadergradient/src/Gradient/ShaderGradient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function ShaderGradient({
rotSpringOption,
posSpringOption,
isFigmaPlugin = false,
enableTransition = true,
...props
}: GradientT) {
let controlProps = { ...defaultProps, ...props }
Expand Down Expand Up @@ -45,7 +46,11 @@ export function ShaderGradient({
posSpringOption={posSpringOption}
/>
{toggleAxis && <Axis isFigmaPlugin={isFigmaPlugin} />}
<CameraControl dampingFactor={dampingFactor} {...others} />
<CameraControl
dampingFactor={dampingFactor}
{...others}
enableTransition={enableTransition}
/>
</>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '@/consts'
import { dToR } from '@/utils'
import { useFrame } from '@react-three/fiber'
import { useCursorStore } from '@/store'

export function useCameraAnimation({
type,
Expand All @@ -16,6 +15,7 @@ export function useCameraAnimation({
cDistance,
cameraZoom,
zoomOut,
enableTransition,
}) {
const ref: any = useRef()
const control = ref.current
Expand All @@ -24,32 +24,31 @@ export function useCameraAnimation({

// rorate the camera
useEffect(() => {
control?.rotateTo(dToR(cAzimuthAngle), dToR(cPolarAngle), true)
control?.rotateTo(dToR(cAzimuthAngle), dToR(cPolarAngle), enableTransition)
}, [control, cAzimuthAngle, cPolarAngle])

// zoom-out tool
const hoverState = useCursorStore((state: any) => state.hoverState)
useEffect(() => {
if (zoomOut || hoverState !== 0) {
if (zoomOut) {
// fixed distance & zoom
if (type === 'sphere') {
control?.dollyTo(zoomOutSphere.distance, true)
control?.zoomTo(zoomOutSphere.zoom, true)
control?.dollyTo(zoomOutSphere.distance, enableTransition)
control?.zoomTo(zoomOutSphere.zoom, enableTransition)
} else {
control?.dollyTo(zoomOutPlanes.distance, true)
control?.zoomTo(zoomOutPlanes.zoom, true)
control?.dollyTo(zoomOutPlanes.distance, enableTransition)
control?.zoomTo(zoomOutPlanes.zoom, enableTransition)
}
} else {
// control distance for planes & zoom for sphere
if (type === 'sphere') {
control?.zoomTo(cameraZoom, true)
control?.dollyTo(defaultSphereDistance, true)
control?.zoomTo(cameraZoom, enableTransition)
control?.dollyTo(defaultSphereDistance, enableTransition)
} else {
control?.dollyTo(cDistance, true)
control?.zoomTo(defaultPlanesZoom, true)
control?.dollyTo(cDistance, enableTransition)
control?.zoomTo(defaultPlanesZoom, enableTransition)
}
}
}, [control, zoomOut, hoverState, type, cameraZoom, cDistance])
}, [control, zoomOut, type, cameraZoom, cDistance])

return ref
}
2 changes: 2 additions & 0 deletions packages/shadergradient/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ export type GradientT = MeshT & {
zoomOut?: boolean
toggleAxis?: boolean
hoverState?: string

enableTransition?: boolean
}

0 comments on commit e9b6ed3

Please sign in to comment.