Three.js: with a window-sized canvas, resize canvas with the window.

pull/434/head
nabijaczleweli 2019-05-21 23:00:38 +02:00 committed by whitequark
parent ffef006b31
commit e243396c6f
1 changed files with 30 additions and 0 deletions

View File

@ -330,15 +330,18 @@ solvespace = function(obj, params) {
var geometry, controls, material, mesh, edges; var geometry, controls, material, mesh, edges;
var width, height, scale, offset, projRight, projUp; var width, height, scale, offset, projRight, projUp;
var directionalLightArray = []; var directionalLightArray = [];
var inheritedWidth = false, inheritedHeight = false;
if (typeof params === "undefined" || !("width" in params)) { if (typeof params === "undefined" || !("width" in params)) {
width = window.innerWidth; width = window.innerWidth;
inheritedWidth = true;
} else { } else {
width = params.width; width = params.width;
} }
if (typeof params === "undefined" || !("height" in params)) { if (typeof params === "undefined" || !("height" in params)) {
height = window.innerHeight; height = window.innerHeight;
inheritedHeight = true;
} else { } else {
height = params.height; height = params.height;
} }
@ -409,10 +412,37 @@ solvespace = function(obj, params) {
controls.addEventListener("change", render); controls.addEventListener("change", render);
controls.addEventListener("change", lightUpdate); controls.addEventListener("change", lightUpdate);
if(inheritedWidth || inheritedHeight) {
window.addEventListener("resize", resize);
}
animate(); animate();
return renderer.domElement; return renderer.domElement;
} }
function resize() {
scale = camera.zoomScale;
if(inheritedWidth) {
scale *= window.innerWidth / width;
width = window.innerWidth;
}
if(inheritedHeight) {
scale *= window.innerHeight / height;
height = window.innerHeight;
}
camera.renderWidth = width;
camera.renderHeight = height;
camera.zoomScale = scale;
renderer.setSize(width * window.devicePixelRatio, height * window.devicePixelRatio);
renderer.domElement.style =
"width: " + width + "px;" +
"height: " + height + "px;";
render();
}
function animate() { function animate() {
requestAnimationFrame(animate); requestAnimationFrame(animate);
controls.update(); controls.update();