react-webcamera
Version:
React webcamera component with direct access to the camera data
166 lines (138 loc) • 6.17 kB
JavaScript
'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; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
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"); } }
function _possibleConstructorReturn(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; }
function _inherits(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; }
CameraComponent.defaultProps = {
demo: false
};
CameraComponent.propTypes = {
demo: _propTypes2.default.bool,
width: _propTypes2.default.number.isRequired,
height: _propTypes2.default.number.isRequired,
filter: _propTypes2.default.func
};
var CameraComponent = function (_Component) {
_inherits(CameraComponent, _Component);
function CameraComponent(props) {
_classCallCheck(this, CameraComponent);
var _this = _possibleConstructorReturn(this, (CameraComponent.__proto__ || Object.getPrototypeOf(CameraComponent)).call(this, props));
_this.navigator = navigator;
_this.pause = true;
_this.draw = _this.draw.bind(_this);
_this.drawToIntermediateCanvas = _this.drawToIntermediateCanvas.bind(_this);
_this.drawToFinalCanvas = _this.drawToFinalCanvas.bind(_this);
return _this;
}
_createClass(CameraComponent, [{
key: 'setupGetMedia',
value: function setupGetMedia() {
this.navigator.getMedia = this.navigator.getUserMedia || this.navigator.webkitGetUserMedia || this.navigator.mozGetUserMedia || this.navigator.msGetUserMedia;
}
}, {
key: 'startVideo',
value: function startVideo(video) {
// Show error message if camera is not supported by the browser
if (!this.navigator.getMedia) {
displayErrorMessage("Your browser doesn't have support for the navigator.getUserMedia interface.");
} else {
// Request the camera.
this.navigator.getMedia({
video: true
},
// Success Callback
function (stream) {
video.src = window.URL.createObjectURL(stream);
video.play();
video.onplay = function () {
//showVideo();
};
},
// Error Callback
function (err) {
console.error("There was an error with accessing the camera stream: " + err.name, err);
});
}
}
}, {
key: 'setupCanvas',
value: function setupCanvas() {
this.canvas = this.refs.finalCanvas;
this.context = this.canvas.getContext('2d');
this.intermediateCanvas = this.refs.intermediateCanvas;
this.intermediateContext = this.intermediateCanvas.getContext('2d');
}
}, {
key: 'drawToIntermediateCanvas',
value: function drawToIntermediateCanvas() {
this.intermediateContext.drawImage(this.refs.cameraStream, 0, 0, this.props.width, this.props.height);
}
}, {
key: 'drawToFinalCanvas',
value: function drawToFinalCanvas(data) {
this.context.putImageData(data, 0, 0);
}
}, {
key: 'draw',
value: function draw() {
this.drawToIntermediateCanvas();
//Apply filters/functions on the extracted video data
var idata = this.getVideoData();
var data = idata.data;
var filteredData = void 0;
var filteredImageData = idata;
if (this.props.filter) {
filteredData = this.props.filter(data);
filteredImageData = new ImageData(filteredData, this.props.width, this.props.height);
}
this.drawToFinalCanvas(filteredImageData);
setTimeout(this.draw, 80);
}
}, {
key: 'getVideoData',
value: function getVideoData() {
var idata = this.intermediateContext.getImageData(0, 0, this.props.width, this.props.height);
return idata;
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var video = this.refs.cameraStream;
this.setupGetMedia();
this.startVideo(video);
this.setupCanvas();
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement(
'div',
{ className: 'camera' },
_react2.default.createElement('video', { width: +this.props.width,
height: +this.props.height,
muted: true,
hidden: true,
ref: 'cameraStream',
controls: true,
onPlay: this.draw }),
_react2.default.createElement('canvas', { width: +this.props.width,
height: +this.props.height,
hidden: !this.props.demo,
ref: 'intermediateCanvas' }),
_react2.default.createElement('canvas', { width: +this.props.width,
height: +this.props.height,
ref: 'finalCanvas' })
);
}
}]);
return CameraComponent;
}(_react.Component);
exports.default = CameraComponent;