|
4 | 4 | <title>Demo Application</title> |
5 | 5 | <style> |
6 | 6 | #jscad{ |
7 | | - width: 15cm; |
| 7 | + width: 100%; |
8 | 8 | height: 15cm; |
9 | 9 | margin: 0; |
10 | 10 | outline: 1px solid black; |
|
69 | 69 |
|
70 | 70 | const containerElement = document.getElementById("jscad") |
71 | 71 |
|
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 |
74 | 77 |
|
75 | 78 | const state = {} |
76 | 79 |
|
|
133 | 136 | // the heart of rendering, as themes, controls, etc change |
134 | 137 | let updateView = true |
135 | 138 |
|
| 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 | + |
136 | 160 | const doRotatePanZoom = () => { |
137 | 161 |
|
138 | 162 | if (rotateDelta[0] || rotateDelta[1]) { |
|
170 | 194 | state.camera.position = updates.camera.position |
171 | 195 | perspectiveCamera.update(state.camera) |
172 | 196 |
|
| 197 | + doResize() |
173 | 198 | renderer(renderOptions) |
174 | 199 | } |
175 | 200 | window.requestAnimationFrame(updateAndRender) |
|
0 commit comments