Skip to content

Commit 19c8e9c

Browse files
committed
fix(regl-renderer): added handling of element resize events
1 parent efd7e84 commit 19c8e9c

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

packages/utils/regl-renderer/demo.html

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title>Demo Application</title>
55
<style>
66
#jscad{
7-
width: 15cm;
7+
width: 100%;
88
height: 15cm;
99
margin: 0;
1010
outline: 1px solid black;
@@ -69,8 +69,11 @@
6969

7070
const containerElement = document.getElementById("jscad")
7171

72-
const width = containerElement.clientWidth
73-
const height = containerElement.clientHeight
72+
const pixelRatio = window.devicePixelRatio || 1
73+
const bounds = containerElement.getBoundingClientRect()
74+
75+
const width = (bounds.right - bounds.left) * pixelRatio
76+
const height = (bounds.bottom - bounds.top) * pixelRatio
7477

7578
const state = {}
7679

@@ -133,6 +136,27 @@
133136
// the heart of rendering, as themes, controls, etc change
134137
let updateView = true
135138

139+
const doResize = () => {
140+
const pixelRatio = window.devicePixelRatio || 1
141+
const bounds = containerElement.getBoundingClientRect()
142+
143+
const width = (bounds.right - bounds.left) * pixelRatio
144+
const height = (bounds.bottom - bounds.top) * pixelRatio
145+
146+
const prevWidth = containerElement.width
147+
const prevHeight = containerElement.height
148+
149+
if (prevWidth !== width || prevHeight !== height) {
150+
containerElement.width = width
151+
containerElement.height = height
152+
153+
perspectiveCamera.setProjection(state.camera, state.camera, { width, height })
154+
perspectiveCamera.update(state.camera, state.camera)
155+
}
156+
}
157+
158+
window.addEventListener('resize', (evt) => { updateView = true })
159+
136160
const doRotatePanZoom = () => {
137161

138162
if (rotateDelta[0] || rotateDelta[1]) {
@@ -170,6 +194,7 @@
170194
state.camera.position = updates.camera.position
171195
perspectiveCamera.update(state.camera)
172196

197+
doResize()
173198
renderer(renderOptions)
174199
}
175200
window.requestAnimationFrame(updateAndRender)

0 commit comments

Comments
 (0)