ThreeJS-Webpack-ES6-Boilerp.../src/js/app/components/camera.js

30 lines
1003 B
JavaScript
Raw Normal View History

import * as THREE from 'three';
2016-09-12 19:54:07 +00:00
import Config from '../../data/config';
2016-09-12 19:54:07 +00:00
2016-10-07 18:55:31 +00:00
// Class that creates and updates the main camera
2016-09-12 19:54:07 +00:00
export default class Camera {
constructor(renderer) {
const width = renderer.domElement.width;
const height = renderer.domElement.height;
2016-10-07 18:55:31 +00:00
// Create and position a Perspective Camera
2016-09-12 19:54:07 +00:00
this.threeCamera = new THREE.PerspectiveCamera(Config.camera.fov, width / height, Config.camera.near, Config.camera.far);
this.threeCamera.position.set(Config.camera.posX, Config.camera.posY, Config.camera.posZ);
2016-10-07 18:55:31 +00:00
// Initial sizing
2016-09-12 19:54:07 +00:00
this.updateSize(renderer);
2016-10-07 08:42:23 +00:00
// Listeners
2016-09-12 19:54:07 +00:00
window.addEventListener('resize', () => this.updateSize(renderer), false);
}
updateSize(renderer) {
2016-10-07 08:42:23 +00:00
// Multiply by dpr in case it is retina device
this.threeCamera.aspect = renderer.domElement.width * Config.dpr / renderer.domElement.height * Config.dpr;
2016-10-07 18:55:31 +00:00
// Always call updateProjectionMatrix on camera change
2016-09-12 19:54:07 +00:00
this.threeCamera.updateProjectionMatrix();
}
}