Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to set three js canvas width 100%

body { margin: 0; }
canvas { width: 100vw; height: 100vh; display: block; }
Comment

how to set three js canvas width 100%

"use strict";

const renderer = new THREE.WebGLRenderer();
document.body.appendChild(renderer.domElement);

// There's no reason to set the aspect here because we're going
// to set it every frame anyway so we'll set it to 2 since 2
// is the the aspect for the canvas default size (300w/150h = 2)
const  camera = new THREE.PerspectiveCamera(70, 2, 1, 1000);
camera.position.z = 400;

const scene = new THREE.Scene();
const geometry = new THREE.BoxGeometry(200, 200, 200);
const material = new THREE.MeshPhongMaterial({
  color: 0x555555,
  specular: 0xffffff,
  shininess: 50,
  shading: THREE.SmoothShading
});

const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

const light1 = new THREE.PointLight(0xff80C0, 2, 0);
light1.position.set(200, 100, 300);
scene.add(light1);

function resizeCanvasToDisplaySize() {
  const canvas = renderer.domElement;
  const width = canvas.clientWidth;
  const height = canvas.clientHeight;
  if (canvas.width !== width ||canvas.height !== height) {
    // you must pass false here or three.js sadly fights the browser
    renderer.setSize(width, height, false);
    camera.aspect = width / height;
    camera.updateProjectionMatrix();

    // set render target sizes here
  }
}

function animate(time) {
  time *= 0.001;  // seconds

  resizeCanvasToDisplaySize();

  mesh.rotation.x = time * 0.5;
  mesh.rotation.y = time * 1;

  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}

requestAnimationFrame(animate);
Comment

PREVIOUS NEXT
Code Example
Javascript :: conditional rendering react 
Javascript :: selecting multiple feilds using populate in mongoose 
Javascript :: null vs undefined 
Javascript :: mongodb rename property 
Javascript :: javascript async 
Javascript :: javascript inheritance 
Javascript :: JavaScript Error Try Throw Catch 
Javascript :: react using object as prop 
Javascript :: how to use hash in javascript 
Javascript :: javascript list comprehension 
Javascript :: angular loop through array 
Javascript :: javascript static 
Javascript :: javascript spread syntax 
Javascript :: react native image slider 
Javascript :: javascript symbol 
Javascript :: clone an object javascript 
Javascript :: javascript format a date 
Javascript :: Angular 4 "Property does not exist on type component" 
Javascript :: add new element to existing json object 
Javascript :: ex:javascript 
Javascript :: append css file with javascript 
Javascript :: dynamic key in javascript object 
Javascript :: pure component in react 
Javascript :: convert number into string 
Javascript :: node red push to array 
Javascript :: react native get source maps 
Javascript :: rotas react com axios 
Javascript :: firstdata payment.js Form Validity 
Javascript :: When an aqueous solution of AgNO3 is mixed with an aqueous solution of (NH4)2CrO4, a precipitation reaction occurs. For this reaction, a) Write the molecular equation. 
Javascript :: document.elementFromPoint 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =