react360imgthree
Version:
A 360 view image gallery react component.
173 lines (140 loc) • 6.07 kB
JavaScript
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = require('react');
var React__default = _interopDefault(React);
require('react-dom');
var THREE = require('three');
var OrbitControls = require('three/examples/jsm/controls/OrbitControls');
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
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;
};
}();
var inherits = function (subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
};
var possibleConstructorReturn = function (self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
};
var View360Three = function (_Component) {
inherits(View360Three, _Component);
function View360Three() {
var _ref;
var _temp, _this, _ret;
classCallCheck(this, View360Three);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = possibleConstructorReturn(this, (_ref = View360Three.__proto__ || Object.getPrototypeOf(View360Three)).call.apply(_ref, [this].concat(args))), _this), _this.sceneSetup = function () {
// get container dimensions and use them for scene sizing
var width = _this.el.clientWidth;
var height = _this.el.clientHeight;
_this.scene = new THREE.Scene();
_this.camera = new THREE.PerspectiveCamera(75, // fov = field of view
width / height, // aspect ratio
0.1, // near plane
1000 // far plane
);
_this.camera.position.z = 5; // is used here to set some distance from a cube that is located at z = 0
// OrbitControls allow a camera to orbit around the object
// https://threejs.org/docs/#examples/controls/OrbitControls
_this.controls = new OrbitControls.OrbitControls(_this.camera, _this.el);
_this.renderer = new THREE.WebGLRenderer();
_this.renderer.setSize(width, height);
_this.el.appendChild(_this.renderer.domElement); // mount using React ref
}, _this.addCustomSceneObjects = function () {
var texture = new THREE.TextureLoader().load(_this.props.image);
var material = new THREE.MeshBasicMaterial({ map: texture });
var geometry = new THREE.SphereBufferGeometry(600, 60, 40);
// invert the geometry on the x-axis so that all of the faces point inward
geometry.scale(-1, 1, 1);
_this.cube = new THREE.Mesh(geometry, material);
//const mesh = new THREE.Mesh(geometry, material);
_this.scene.add(_this.cube);
}, _this.startAnimationLoop = function () {
//this.cube.rotation.x += 0.01;
if (_this.props.autoRotate == "true") {
_this.cube.rotation.y += 0.0009;
}
_this.renderer.render(_this.scene, _this.camera);
// The window.requestAnimationFrame() method tells the browser that you wish to perform
// an animation and requests that the browser call a specified function
// to update an animation before the next repaint
_this.requestID = window.requestAnimationFrame(_this.startAnimationLoop);
}, _this.handleWindowResize = function () {
var width = _this.el.clientWidth;
var height = _this.el.clientHeight;
_this.renderer.setSize(width, height);
_this.camera.aspect = width / height;
// Note that after making changes to most of camera properties you have to call
// .updateProjectionMatrix for the changes to take effect.
_this.camera.updateProjectionMatrix();
}, _temp), possibleConstructorReturn(_this, _ret);
}
createClass(View360Three, [{
key: "componentDidMount",
value: function componentDidMount() {
this.sceneSetup();
this.addCustomSceneObjects();
this.startAnimationLoop();
window.addEventListener("resize", this.handleWindowResize);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
window.removeEventListener("resize", this.handleWindowResize);
window.cancelAnimationFrame(this.requestID);
this.controls.dispose();
}
// Standard scene setup in Three.js. Check "Creating a scene" manual for more information
// https://threejs.org/docs/#manual/en/introduction/Creating-a-scene
// Here should come custom code.
}, {
key: "render",
value: function render() {
var _this2 = this;
return React__default.createElement("div", {
style: {
width: Number(this.props.width),
height: Number(this.props.height)
},
ref: function ref(_ref2) {
return _this2.el = _ref2;
}
});
}
}]);
return View360Three;
}(React.Component);
module.exports = View360Three;
//# sourceMappingURL=index.js.map
;