UNPKG

ami-cjs.js

Version:

<p align="center"> <img src="https://cloud.githubusercontent.com/assets/214063/23213764/78ade038-f90c-11e6-8208-4fcade5f3832.png" width="60%"> </p>

186 lines (160 loc) 7.43 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /** * @module helpers/x/renderer2d */ var _cameras = require('../../cameras/cameras.orthographic'); var _cameras2 = _interopRequireDefault(_cameras); var _controls = require('../../controls/controls.trackballortho'); var _controls2 = _interopRequireDefault(_controls); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var _class = function () { function _class() { var containerId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'r2d'; var orientation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default'; _classCallCheck(this, _class); this._container = null; this._renderer = null; this._camera = null; this._controls = null; this._orientation = orientation; this._scene = null; this._object = null; this._initRenderer(containerId); this._initCamera(); this._initScene(); this._initControls(); // setup event listeners this._onScroll = this._onScroll.bind(this); this._onWindowResize = this._onWindowResize.bind(this); this.addEventListeners(); } _createClass(_class, [{ key: 'add', value: function add(object) { this._object = object; this._scene.add(this._object); this._setupCamera(this._object.stack); this._orientCamera(this._object, this._orientation); this._object.canvasWidth = this._container.clientWidth; this._object.canvasHeight = this._container.clientHeight; } }, { key: 'addEventListeners', value: function addEventListeners() { this._controls.addEventListener('OnScroll', this._onScroll, false); window.addEventListener('resize', this._onWindowResize, false); } }, { key: 'removeEventListeners', value: function removeEventListeners() { this._controls.removeEventListener('OnScroll', this._onScroll, false); window.removeEventListener('resize', this._onWindowResize, false); } }, { key: 'animate', value: function animate() { this._controls.update(); this._renderer.render(this._scene, this._camera); // request new frame requestAnimationFrame(this.animate.bind(this)); } // private methods }, { key: '_initRenderer', value: function _initRenderer(containerId) { // renderer this._container = document.getElementById(containerId); this._renderer = new THREE.WebGLRenderer({ antialias: true }); this._renderer.setSize(this._container.clientWidth, this._container.clientHeight); this._renderer.setClearColor(0x212121, 1); this._renderer.setPixelRatio(window.devicePixelRatio); this._container.appendChild(this._renderer.domElement); } }, { key: '_initCamera', value: function _initCamera() { this._camera = new _cameras2.default(this._container.clientWidth / -2, this._container.clientWidth / 2, this._container.clientHeight / 2, this._container.clientHeight / -2, 1, 1000); } }, { key: '_initScene', value: function _initScene() { this._scene = new THREE.Scene(); } }, { key: '_initControls', value: function _initControls() { // controls this._controls = new _controls2.default(this._camera, this._container); this._controls.staticMoving = true; this._controls.noRotate = true; this._camera.controls = this._controls; } }, { key: '_setupCamera', value: function _setupCamera(stack) { // set camera var worldbb = stack.worldBoundingBox(); var lpsDims = new THREE.Vector3(worldbb[1] - worldbb[0], worldbb[3] - worldbb[2], worldbb[5] - worldbb[4]); // box: {halfDimensions, center} var box = { center: stack.worldCenter().clone(), halfDimensions: new THREE.Vector3(lpsDims.x + 10, lpsDims.y + 10, lpsDims.z + 10) }; // init and zoom var canvas = { width: this._container.clientWidth, height: this._container.clientHeight }; this._camera.directions = [stack.xCosine, stack.yCosine, stack.zCosine]; this._camera.box = box; this._camera.canvas = canvas; this._camera.update(); this._camera.fitBox(2); } }, { key: '_orientCamera', value: function _orientCamera(target) { var orientation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default'; this._camera.orientation = orientation; this._camera.update(); this._camera.fitBox(2); target.orientation = this._camera.stackOrientation; } }, { key: '_onWindowResize', value: function _onWindowResize() { this._camera.canvas = { width: this._container.clientWidth, height: this._container.clientHeight }; this._camera.fitBox(2); this._renderer.setSize(this._container.clientWidth, this._container.clientHeight); this._object.canvasWidth = this._container.clientWidth; this._object.canvasHeight = this._container.clientHeight; } }, { key: '_onScroll', value: function _onScroll(event) { if (event.delta > 0) { if (this._object.index >= this._object.orientationMaxIndex) { return false; } this._object.index += 1; } else { if (this._object.index <= 0) { return false; } this._object.index -= 1; } } }]); return _class; }(); exports.default = _class; module.exports = exports['default'];