Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import THREE from 'three'
- export default class Scene {
- constructor($container) {
- this._$container = $container
- const { offsetWidth: width, offsetHeight: height } = this._$container
- this._scene = this._getScene()
- this._camera = this._getCamera(width, height)
- this._renderer = this._getRenderer(width, height)
- this._handleResizeBound = this._handleResize.bind(this)
- this._drawBound = this._draw.bind(this)
- this._$container.appendChild(this._renderer.domElement)
- this._frameId = null
- this._addListeners()
- this._draw()
- }
- _getScene() {
- return new THREE.Scene()
- }
- _getCamera(width, height) {
- const camera = new THREE.PerspectiveCamera(45, width / height, 1, 100)
- camera.position.z = 5
- return camera
- }
- _getRenderer(width, height) {
- const renderer = new THREE.WebGLRenderer({
- alpha: true,
- antialias: false
- })
- renderer.setSize(width, height)
- renderer.setClearColor(0x000000, 0)
- return renderer
- }
- _addListeners() {
- window.addEventListener('resize', this._handleResizeBound)
- }
- _removeListeners() {
- window.removeEventListener('resize', this._handleResizeBound)
- }
- _handleResize() {
- const { offsetWidth: width, offsetHeight: height } = this._$container
- this._camera.aspect = width / height
- this._camera.updateProjectionMatrix()
- this._renderer.setSize(width, height)
- }
- _draw() {
- this._frameId = requestAnimationFrame(this._drawBound)
- this._renderer.render(this._scene, this._camera)
- }
- add(...children) {
- for(let i = 0; i < children.length; ++i) {
- this._scene.add(children[i])
- }
- }
- remove(...children) {
- for(let i = 0; i < children.length; ++i) {
- this._scene.remove(children[i])
- }
- }
- dispose() {
- this._removeListeners()
- cancelAnimationFrame(this._frameId)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement