UNPKG

react-image-filter

Version:

Lightweight React component, for applying color filters on images, works in all modern browsers plus IE10+ and Edge.

228 lines (184 loc) 8 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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; } var WRAPPER_STYLE = { position: 'relative' }; var IMAGE_STYLE = { display: 'block', visibility: 'hidden', width: '100%' }; var SVG_STYLE = { display: 'block', height: '100%', width: '100%', overflow: 'hidden' }; var SVG_ABSOLUTE_STYLE = { left: 0, position: 'absolute', top: 0 }; var NONE = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0]; var INVERT = [-1, 0, 0, 0, 1, 0, -1, 0, 0, 1, 0, 0, -1, 0, 1, 0, 0, 0, 1, 0]; var GRAYSCALE = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0]; var SEPIA = [0.3, 0.45, 0.1, 0, 0, 0.2, 0.45, 0.1, 0, 0, 0.1, 0.3, 0.1, 0, 0, 0, 0, 0, 1, 0]; function omit(object, keysToOmit) { var result = {}; Object.keys(object).forEach(function (key) { if (keysToOmit.indexOf(key) === -1) { result[key] = object[key]; } }); return result; } var types = { DUOTONE: 'duotone', INVERT: 'invert', GRAYSCALE: 'grayscale', SEPIA: 'sepia' }; var ImageFilter = function (_Component) { _inherits(ImageFilter, _Component); function ImageFilter(props) { _classCallCheck(this, ImageFilter); var _this = _possibleConstructorReturn(this, (ImageFilter.__proto__ || Object.getPrototypeOf(ImageFilter)).call(this, props)); _this.state = { id: ('' + new Date().getTime() + Math.random()).replace('.', ''), filter: _this.getMatrix(props) }; return _this; } _createClass(ImageFilter, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { var _props = this.props, filter = _props.filter, colorOne = _props.colorOne, colorTwo = _props.colorTwo; if (filter !== nextProps.filter || nextProps.filter === 'duotone' && (colorOne !== nextProps.colorOne || colorTwo !== nextProps.colorTwo)) { this.setState({ filter: this.getMatrix(nextProps, true) }); } } }, { key: 'getMatrix', value: function getMatrix(props) { var triggerCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var filter = props.filter; if (filter === types.GRAYSCALE) { filter = GRAYSCALE; } else if (filter === types.INVERT) { filter = INVERT; } else if (filter === types.SEPIA) { filter = SEPIA; } else if (filter === types.DUOTONE) { filter = this.convertToDueTone(props.colorOne, props.colorTwo); } if (triggerCallback && props.onChange && typeof props.onChange === 'function') { props.onChange(filter); } return filter; } }, { key: 'convertToDueTone', value: function convertToDueTone(color1, color2) { return [color2[0] / 256 - color1[0] / 256, 0, 0, 0, color1[0] / 256, color2[1] / 256 - color1[1] / 256, 0, 0, 0, color1[1] / 256, color2[2] / 256 - color1[2] / 256, 0, 0, 0, color1[2] / 256, 0, 0, 0, 1, 0]; } }, { key: 'render', value: function render() { var _props2 = this.props, image = _props2.image, preserveAspectRatio = _props2.preserveAspectRatio, className = _props2.className, style = _props2.style, svgStyle = _props2.svgStyle, svgProps = _props2.svgProps; var _state = this.state, id = _state.id, filter = _state.filter; var aspectRatio = preserveAspectRatio === 'cover' ? 'xMidYMid slice' : preserveAspectRatio; var renderImage = preserveAspectRatio === 'none'; var svgMergedStyle = renderImage ? _extends({}, SVG_STYLE, SVG_ABSOLUTE_STYLE, svgStyle) : _extends({}, SVG_STYLE, svgStyle); var otherProps = omit(this.props, ['image', 'filter', 'preserveAspectRatio', 'className', 'style', 'svgStyle', 'svgProps', 'colorOne', 'colorTwo']); return _react2.default.createElement( 'div', _extends({}, otherProps, { className: 'ImageFilter ' + className, style: _extends({}, WRAPPER_STYLE, style) }), renderImage && _react2.default.createElement('img', { alt: '', 'aria-hidden': true, style: IMAGE_STYLE, src: image, className: 'ImageFilter-image' }), _react2.default.createElement( 'svg', _extends({}, svgProps, { className: 'ImageFilter-svg', style: svgMergedStyle }), _react2.default.createElement( 'filter', { id: 'filter-image-' + id, colorInterpolationFilters: 'sRGB' }, _react2.default.createElement('feColorMatrix', { type: 'matrix', values: filter.join(' ') }) ), _react2.default.createElement('image', { filter: 'url(#filter-image-' + id + ')', preserveAspectRatio: aspectRatio, xlinkHref: image, x: '0', y: '0', width: '100%', height: '100%' }) ) ); } }]); return ImageFilter; }(_react.Component); exports.default = ImageFilter; ImageFilter.propTypes = { image: _propTypes2.default.string.isRequired, filter: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.array]).isRequired, // Check https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio preserveAspectRatio: _propTypes2.default.string.isRequired, className: _propTypes2.default.string, style: _propTypes2.default.object, svgStyle: _propTypes2.default.object, svgProps: _propTypes2.default.object, colorOne: _propTypes2.default.array, colorTwo: _propTypes2.default.array, onChange: _propTypes2.default.func // eslint-disable-line react/no-unused-prop-types }; ImageFilter.defaultProps = { filter: NONE, preserveAspectRatio: 'none', className: '', style: {}, svgStyle: {}, svgProps: {} };