Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: configurable canvas config.onResize callback #84

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,11 @@ export function render(
if (state.frameloop !== 'never') animate()

// Handle resize
function onResize(state: RootState) {
const { width, height } = state.size
const projection = state.orthographic ? 'orthographic' : 'perspective'

if (state.renderer.width !== width || state.renderer.height !== height || state.camera.type !== projection) {
state.renderer.setSize(width, height)
state.camera[projection]({ aspect: width / height })
}
const onResize = config.onResize ?? onResizeDefault
if (onResize) {
store.subscribe(onResize)
onResize(state)
}
store.subscribe(onResize)
onResize(state)

// Report when an error was detected in a previous render
const logRecoverableError = typeof reportError === 'function' ? reportError : console.error
Expand Down Expand Up @@ -189,6 +183,19 @@ export function render(
return root.store
}

/**
* Default resize callback.
*/
function onResizeDefault(state: RootState) {
const { width, height } = state.size
const projection = state.orthographic ? 'orthographic' : 'perspective'

if (state.renderer.width !== width || state.renderer.height !== height || state.camera.type !== projection) {
state.renderer.setSize(width, height)
state.camera[projection]({ aspect: width / height })
}
}

/**
* Removes and cleans up internals on unmount.
*/
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export interface RenderProps {
camera?: OGL.Camera | OGLElement<typeof OGL.Camera> | Partial<OGL.CameraOptions>
scene?: OGL.Transform
events?: EventManager
onResize?: (state: RootState)=> void
onCreated?: (state: RootState) => any
}

Expand Down
Loading