UNPKG

react-xrplayer

Version:

An excellent xr player for react

262 lines (204 loc) 11.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); require("./style/EffectAlphaVideoPanel.less"); var _reactRedux = require("react-redux"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var EffectAlphaVideoPanel = /*#__PURE__*/function (_Component) { _inherits(EffectAlphaVideoPanel, _Component); var _super = _createSuper(EffectAlphaVideoPanel); function EffectAlphaVideoPanel(props) { var _this; _classCallCheck(this, EffectAlphaVideoPanel); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "loadMp4Video", function () { _this.video = document.getElementById("alpha-video"); _this.video.src = _this.props.videoUrl; _this.video.crossOrigin = "anonymous"; _this.showCanvas = document.getElementById("show"); _this.showCtx = _this.showCanvas.getContext("2d"); _this.bufferCanvas = document.getElementById("buffer"); _this.bufferCtx = _this.bufferCanvas.getContext("2d"); _this.updateCanvasSize(); _this.video.play(); _this.video.addEventListener('play', _this.startPlay, false); _this.video.addEventListener('ended', _this.endPlay, false); window.addEventListener('resize', _this.updateCanvasSize, false); }); _defineProperty(_assertThisInitialized(_this), "startPlay", function () { _this.isPlaying = true; _this.processFrame(); if (_this.props.onStartPlayHandler) { _this.props.onStartPlayHandler(); } }); _defineProperty(_assertThisInitialized(_this), "endPlay", function () { _this.isPlaying = false; if (_this.props.onDisplayEndedHandler) { _this.props.onDisplayEndedHandler(); } }); _defineProperty(_assertThisInitialized(_this), "updateCanvasSize", function () { var output = document.getElementById('output'); if (output) { _this.showCanvas.width = output.clientWidth; _this.showCanvas.height = output.clientHeight; _this.bufferCanvas.width = output.clientWidth; _this.bufferCanvas.height = output.clientHeight * 2; _this.width = _this.showCanvas.width; _this.height = _this.showCanvas.height; } else { _this.width = 10; _this.height = 10; } }); _defineProperty(_assertThisInitialized(_this), "processFrame", function () { if (_this.isPlaying === false) return; _this.bufferCtx.drawImage(_this.video, 0, 0, _this.width, _this.height * 2); _this.image = _this.bufferCtx.getImageData(0, 0, _this.width, _this.height); _this.image.crossOrigin = "Anonymous"; _this.alphaData = _this.bufferCtx.getImageData(0, _this.height, _this.width, _this.height).data; for (var i = 3, len = _this.image.data.length; i < len; i = i + 4) { _this.image.data[i] = _this.alphaData[i - 1]; } _this.showCtx.putImageData(_this.image, 0, 0, 0, 0, _this.width, _this.height); requestAnimationFrame(_this.processFrame); }); _defineProperty(_assertThisInitialized(_this), "onCloseClickListener", function (e) { e.preventDefault(); if (_this.props.onCloseClickHandler) { _this.props.onCloseClickHandler(); } }); _defineProperty(_assertThisInitialized(_this), "getPosition", function () { var tip = document.getElementById(_this.props.id); }); _this.video = null; _this.bufferCtx = null; _this.image = null; _this.alphaData = null; _this.isPlaying = false; return _this; } _createClass(EffectAlphaVideoPanel, [{ key: "componentDidMount", value: function componentDidMount() { var _this$props = this.props, muted = _this$props.muted, volume = _this$props.volume, videoMuted = _this$props.videoMuted; this.video = this.videoNode; this.video.volume = volume; this.video.muted = videoMuted || muted; this.video.setAttribute('webkit-playsinline', 'webkit-playsinline'); this.loadMp4Video(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.isPlaying = false; this.video.removeEventListener('play', this.startPlay); this.video.removeEventListener('ended', this.endPlay); window.removeEventListener('resize', this.updateCanvasSize); this.video = null; this.bufferCtx = null; this.image = null; this.alphaData = null; this.showCanvas = null; this.showCtx = null; this.bufferCanvas = null; this.bufferCtx = null; } }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, muted = _this$props2.muted, volume = _this$props2.volume, videoMuted = _this$props2.videoMuted; if (this.video) { this.video.volume = volume; this.video.muted = videoMuted || muted; } var _this$props3 = this.props, enableClose = _this$props3.enableClose, videoStyle = _this$props3.videoStyle, enableMask = _this$props3.enableMask; var overlayClassName = "alpha_video_overlay"; if (enableMask) { overlayClassName += " grep_overlay"; } return /*#__PURE__*/_react.default.createElement("div", { className: overlayClassName }, /*#__PURE__*/_react.default.createElement("video", { id: "alpha-video", className: "video", style: { display: "none" }, playsInline: true, ref: function ref(mount) { _this2.videoNode = mount; } }), /*#__PURE__*/_react.default.createElement("div", { id: "output", className: "alpha_video_overlay", style: videoStyle }, /*#__PURE__*/_react.default.createElement("canvas", { id: "show" }), /*#__PURE__*/_react.default.createElement("canvas", { id: "buffer", style: { display: "none" } })), enableClose ? /*#__PURE__*/_react.default.createElement("div", { className: "close", onClick: this.onCloseClickListener }) : ""); } }]); return EffectAlphaVideoPanel; }(_react.Component); EffectAlphaVideoPanel.propTypes = { videoUrl: _propTypes.default.string.isRequired, enableClose: _propTypes.default.bool, enableMask: _propTypes.default.bool, videoStyle: _propTypes.default.object, videoMuted: _propTypes.default.bool, onCloseClickHandler: _propTypes.default.func, onDisplayEndedHandler: _propTypes.default.func, onStartPlayHandler: _propTypes.default.func }; EffectAlphaVideoPanel.defaultProps = { enableClose: false, position: "right", videoMuted: false, enableMask: false, videoStyle: { width: 400, height: 400 } }; var _default = (0, _reactRedux.connect)(function (state) { return state.player; }, {})(EffectAlphaVideoPanel); exports.default = _default;