Skip to content

Commit

Permalink
fix: center should be able to disable specific axes
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed Oct 16, 2022
1 parent c046d08 commit bc78f69
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -2399,6 +2399,12 @@ export type Props = JSX.IntrinsicElements['group'] & {
left?: boolean
front?: boolean
back?: boolean
/** Disable x-axis centering */
disableX?: boolean
/** Disable y-axis centering */
disableY?: boolean
/** Disable z-axis centering */
disableZ?: boolean
/** Precision, defaults to true, see https://threejs.org/docs/index.html?q=box3#api/en/math/Box3.setFromObject */
precise?: boolean
/** Callback, fires in the useLayoutEffect phase, after measurement */
Expand Down
28 changes: 26 additions & 2 deletions src/core/Center.tsx
Expand Up @@ -24,14 +24,34 @@ type CenterProps = JSX.IntrinsicElements['group'] & {
left?: boolean
front?: boolean
back?: boolean
/** Disable x-axis centering */
disableX?: boolean
/** Disable y-axis centering */
disableY?: boolean
/** Disable z-axis centering */
disableZ?: boolean
/** See https://threejs.org/docs/index.html?q=box3#api/en/math/Box3.setFromObject */
precise?: boolean
/** Callback, fires in the useLayoutEffect phase, after measurement */
onCentered?: (props: OnCenterCallbackProps) => void
}

export const Center = React.forwardRef<Group, CenterProps>(function Center(
{ children, left, right, top, bottom, front, back, onCentered, precise = true, ...props },
{
children,
disableX,
disableY,
disableZ,
left,
right,
top,
bottom,
front,
back,
onCentered,
precise = true,
...props
},
fRef
) {
const ref = React.useRef<Group>(null!)
Expand All @@ -50,7 +70,11 @@ export const Center = React.forwardRef<Group, CenterProps>(function Center(
const vAlign = top ? height / 2 : bottom ? -height / 2 : 0
const hAlign = left ? -width / 2 : right ? width / 2 : 0
const dAlign = front ? depth / 2 : back ? -depth / 2 : 0
outer.current.position.set(-center.x + hAlign, -center.y + vAlign, -center.z + dAlign)
outer.current.position.set(
disableX ? 0 : -center.x + hAlign,
disableY ? 0 : -center.y + vAlign,
disableZ ? 0 : -center.z + dAlign
)

if (typeof onCentered !== 'undefined') {
onCentered({
Expand Down

1 comment on commit bc78f69

@vercel
Copy link

@vercel vercel bot commented on bc78f69 Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.