react-progressive-image-blur
Version:
A progressive image loader with blur transition.
827 lines (693 loc) • 26.9 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("react"));
else if(typeof define === 'function' && define.amd)
define("ReactProgressiveImageBlur", ["react"], factory);
else if(typeof exports === 'object')
exports["ReactProgressiveImageBlur"] = factory(require("react"));
else
root["ReactProgressiveImageBlur"] = factory(root["react"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DeferImg = undefined;
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; }; }();
exports.DeferImgGlobalSettings = DeferImgGlobalSettings;
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var _superfastblur = __webpack_require__(2);
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 inlineStyleWrapper = {
position: 'relative',
overflow: 'hidden'
};
var inlineStyleCanvas = {
position: 'absolute',
top: 0,
left: 0,
zIndex: 2
// the first value always will be the thumbnail
// the last value will always be the original file
};var defaultImgResolution = {
50: '-w50',
200: '-w200',
630: '-w630',
1180: '-o'
};
var customImgResolutions = null;
/**
* React Lazy Progressive Image Loader
*
* @export
* @class DeferImg
* @extends {React.Component}
*/
var DeferImg = exports.DeferImg = function (_React$Component) {
_inherits(DeferImg, _React$Component);
function DeferImg(props) {
_classCallCheck(this, DeferImg);
var _this = _possibleConstructorReturn(this, (DeferImg.__proto__ || Object.getPrototypeOf(DeferImg)).call(this, props));
_this.state = {
resolutions: props.resolutions || customImgResolutions || defaultImgResolution,
fileinfo: {
ready: false,
filename: '',
urlParams: '',
filepath: '',
extention: ''
},
canLoad: false,
finished: false,
loadHd: false,
blurRelay: 60,
scrollEventListener: null
};
_this.eventListener = false;
_this.debounceTimer = null;
_this.debounceCalled = false;
_this.shouldLoadDetectDebounce = _this.shouldLoadDetectDebounce.bind(_this);
_this.debounceFinished = _this.debounceFinished.bind(_this);
return _this;
}
_createClass(DeferImg, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.setFileInfo(this.props.src);
this.setElementId();
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (this.eventListener) this.unregisterScrollListener();
if (this.debounceTimer) clearTimeout(this.debounceTimer);
}
}, {
key: 'registerScrollListener',
value: function registerScrollListener() {
this.eventListener = true;
window.addEventListener('scroll', this.shouldLoadDetectDebounce, true);
}
}, {
key: 'unregisterScrollListener',
value: function unregisterScrollListener() {
if (!this.eventListener) return;
window.removeEventListener('scroll', this.shouldLoadDetectDebounce, true);
this.eventListener = false;
}
}, {
key: 'debounceFinished',
value: function debounceFinished() {
clearTimeout(this.debounceTimer);
this.debounceTimer = null;
this.debounceCalled = false;
this.shouldLoad();
}
}, {
key: 'shouldLoadDetectDebounce',
value: function shouldLoadDetectDebounce() {
if (this.debounceTimer || this.debounceCalled) return;
this.debounceCalled = true;
this.debounceTimer = setTimeout(this.debounceFinished, 300);
}
/**
* Concat specific resolution image
*
* @param {any} [{ thumb, width }={}]
* @returns {string} Image source string
* @memberof DeferImg
*/
}, {
key: 'haveImage',
value: function haveImage() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
thumb = _ref.thumb;
if (thumb) {
return this.filenameConcat({
resolution: Math.min.apply(null, Object.keys(this.state.resolutions))
});
}
var width = document.getElementById(this.state.idImgHolder).offsetWidth;
// If the width is fit just right in
// use it immediatly
if (this.state.resolutions[width]) return this.filenameConcat({
resolution: this.state.resolutions[width]
});
// Or calculate the 'distant' list to find out
// which resolution is better for current width
var nearest = Object.keys(this.state.resolutions).map(function (r) {
return {
k: r,
dist: Math.abs(width - r)
};
}).sort(function (prev, next) {
return prev.dist - next.dist;
}).shift();
return this.filenameConcat({
resolution: nearest.k
});
}
/**
* Concat source string
*
* @param {any} { resolution }
* @returns
* @memberof DeferImg
*/
}, {
key: 'filenameConcat',
value: function filenameConcat(_ref2) {
var resolution = _ref2.resolution;
return this.state.fileinfo.filepath + this.state.fileinfo.filename + this.state.resolutions[resolution] + this.state.fileinfo.extention + this.state.fileinfo.urlParams;
}
/**
* Call boxBlurImage to blur the thumb/given image source
*
* @param {any} [{ radius, iteration = 1, source }={}]
* @memberof DeferImg
*/
}, {
key: 'blurImg',
value: function blurImg() {
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
radius = _ref3.radius,
_ref3$iteration = _ref3.iteration,
iteration = _ref3$iteration === undefined ? 1 : _ref3$iteration,
source = _ref3.source;
(0, _superfastblur.boxBlurImage)(source || this.state.idLowResImgHolder, this.state.idBlurCanvas, radius, false, iteration);
}
/**
* Image blur animation function
*
* @param {any} { start }
* @param {function} callback Callback function for animation
* @memberof DeferImg
*/
}, {
key: 'blurAnimation',
value: function blurAnimation() {
var _this2 = this;
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
amount = _ref4.amount,
source = _ref4.source;
var callback = arguments[1];
var relay = this.state.blurRelay;
var finishPoint = relay - amount <= 0 ? 0 : relay - amount;
var timer = setInterval(function () {
if (relay <= finishPoint) {
clearInterval(timer);
callback && callback({ finishPoint: finishPoint });
return;
}
_this2.blurImg({ radius: relay, source: source });
relay -= 10;
}, 10);
this.setState({ blurRelay: finishPoint });
}
/**
* Sets the ids for image andcavas
*
* @memberof DeferImg
*/
}, {
key: 'setElementId',
value: function setElementId() {
var _this3 = this;
var idSuffix = Date.now() + '.' + Math.random();
this.setState({
idLowResImgHolder: 'img-lr-' + idSuffix,
idBlurCanvas: 'canvas-blur-' + idSuffix,
idHdImgHolder: 'img-hd-' + idSuffix,
idImgHolder: 'img-container-' + idSuffix
}, function () {
if (!_this3.shouldLoad()) _this3.registerScrollListener();
});
}
/**
* Sets the image source info
*
* @param {any} fn Image source
* @memberof DeferImg
*/
}, {
key: 'setFileInfo',
value: function setFileInfo(fn) {
var regexUrlParams = regexUrlParams = /(\?|\&)([^=]+)\=([^&]+)/g;
var cleanUrl = fn.replace(regexUrlParams, '');
var urlParamsMatchResult = fn.match(regexUrlParams);
var urlParams = urlParamsMatchResult && urlParamsMatchResult.pop() || '';
var filename = cleanUrl.split('/').pop();
var filepath = fn.replace(filename, '');
filename = filename.split('.').shift();
var extention = '.' + cleanUrl.split('.').pop();
this.setState({
fileinfo: { ready: true, urlParams: urlParams, filename: filename, filepath: filepath, extention: extention }
});
}
/**
* Detect if current image element is in viewport
* and then start load it immediatly
*
* @memberof DeferImg
*/
}, {
key: 'shouldLoad',
value: function shouldLoad() {
var img = document.getElementById(this.state.idImgHolder);
var bound = img.getBoundingClientRect();
var docHeight = window.screen.height;
var canLoad = bound.top >= 0 && bound.top <= docHeight;
this.setState({ canLoad: canLoad });
return canLoad;
}
// After thumb loaded, start to load hd version
}, {
key: 'switchToHd',
value: function switchToHd() {
var _this4 = this;
this.blurAnimation({ amount: 40 }, function () {
setTimeout(function () {
_this4.setState({ loadHd: true });
}, 800);
});
}
// After hd version finish loading
// clear the canvas and thumb image
}, {
key: 'loadHdFinished',
value: function loadHdFinished() {
var _this5 = this;
this.blurAnimation({
amount: 20,
source: this.state.idHdImgHolder
}, function () {
_this5.setState({ finished: true });
});
}
}, {
key: 'render',
value: function render() {
var _this6 = this;
if (!this.state.fileinfo.ready || !this.state.canLoad) return _react2.default.createElement('div', { id: this.state.idImgHolder, style: inlineStyleWrapper });
return _react2.default.createElement(
'div',
{
className: this.props.className,
id: this.state.idImgHolder,
style: inlineStyleWrapper
},
this.state.loadHd ? _react2.default.createElement('img', {
alt: this.props.alt,
id: this.state.idHdImgHolder,
src: this.haveImage({ thumb: false }),
onLoad: function onLoad() {
_this6.loadHdFinished();
},
style: { opacity: this.state.finished ? 1 : 0 }
}) : _react2.default.createElement('img', {
alt: this.props.alt,
id: this.state.idLowResImgHolder,
src: this.haveImage({ thumb: true }),
onLoad: function onLoad() {
_this6.switchToHd();
_this6.unregisterScrollListener();
},
style: { opacity: this.state.loadHd ? 1 : 0 }
}),
this.state.finished ? null : _react2.default.createElement('canvas', { id: this.state.idBlurCanvas, style: inlineStyleCanvas }),
this.props.figcaption ? _react2.default.createElement(
'figcaption',
null,
this.props.figcaption
) : null
);
}
}]);
return DeferImg;
}(_react2.default.Component);
function DeferImgGlobalSettings(_ref5) {
var resolutions = _ref5.resolutions;
customImgResolutions = resolutions && Object.assign({}, resolutions) || null;
return true;
}
/***/ }),
/* 1 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_1__;
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.boxBlurImage = boxBlurImage;
exports.boxBlurCanvasRGBA = boxBlurCanvasRGBA;
exports.boxBlurCanvasRGB = boxBlurCanvasRGB;
/*
Superfast Blur - a fast Box Blur For Canvas
Version: 0.5
Author: Mario Klingemann
Contact: mario@quasimondo.com
Website: http://www.quasimondo.com/BoxBlurForCanvas
Twitter: @quasimondo
In case you find this class useful - especially in commercial projects -
I am not totally unhappy for a small donation to my PayPal account
mario@quasimondo.de
Or support me on flattr:
https://flattr.com/thing/140066/Superfast-Blur-a-pretty-fast-Box-Blur-Effect-for-CanvasJavascript
Copyright (c) 2011 Mario Klingemann
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
var mul_table = [1, 57, 41, 21, 203, 34, 97, 73, 227, 91, 149, 62, 105, 45, 39, 137, 241, 107, 3, 173, 39, 71, 65, 238, 219, 101, 187, 87, 81, 151, 141, 133, 249, 117, 221, 209, 197, 187, 177, 169, 5, 153, 73, 139, 133, 127, 243, 233, 223, 107, 103, 99, 191, 23, 177, 171, 165, 159, 77, 149, 9, 139, 135, 131, 253, 245, 119, 231, 224, 109, 211, 103, 25, 195, 189, 23, 45, 175, 171, 83, 81, 79, 155, 151, 147, 9, 141, 137, 67, 131, 129, 251, 123, 30, 235, 115, 113, 221, 217, 53, 13, 51, 50, 49, 193, 189, 185, 91, 179, 175, 43, 169, 83, 163, 5, 79, 155, 19, 75, 147, 145, 143, 35, 69, 17, 67, 33, 65, 255, 251, 247, 243, 239, 59, 29, 229, 113, 111, 219, 27, 213, 105, 207, 51, 201, 199, 49, 193, 191, 47, 93, 183, 181, 179, 11, 87, 43, 85, 167, 165, 163, 161, 159, 157, 155, 77, 19, 75, 37, 73, 145, 143, 141, 35, 138, 137, 135, 67, 33, 131, 129, 255, 63, 250, 247, 61, 121, 239, 237, 117, 29, 229, 227, 225, 111, 55, 109, 216, 213, 211, 209, 207, 205, 203, 201, 199, 197, 195, 193, 48, 190, 47, 93, 185, 183, 181, 179, 178, 176, 175, 173, 171, 85, 21, 167, 165, 41, 163, 161, 5, 79, 157, 78, 154, 153, 19, 75, 149, 74, 147, 73, 144, 143, 71, 141, 140, 139, 137, 17, 135, 134, 133, 66, 131, 65, 129, 1];
var shg_table = [0, 9, 10, 10, 14, 12, 14, 14, 16, 15, 16, 15, 16, 15, 15, 17, 18, 17, 12, 18, 16, 17, 17, 19, 19, 18, 19, 18, 18, 19, 19, 19, 20, 19, 20, 20, 20, 20, 20, 20, 15, 20, 19, 20, 20, 20, 21, 21, 21, 20, 20, 20, 21, 18, 21, 21, 21, 21, 20, 21, 17, 21, 21, 21, 22, 22, 21, 22, 22, 21, 22, 21, 19, 22, 22, 19, 20, 22, 22, 21, 21, 21, 22, 22, 22, 18, 22, 22, 21, 22, 22, 23, 22, 20, 23, 22, 22, 23, 23, 21, 19, 21, 21, 21, 23, 23, 23, 22, 23, 23, 21, 23, 22, 23, 18, 22, 23, 20, 22, 23, 23, 23, 21, 22, 20, 22, 21, 22, 24, 24, 24, 24, 24, 22, 21, 24, 23, 23, 24, 21, 24, 23, 24, 22, 24, 24, 22, 24, 24, 22, 23, 24, 24, 24, 20, 23, 22, 23, 24, 24, 24, 24, 24, 24, 24, 23, 21, 23, 22, 23, 24, 24, 24, 22, 24, 24, 24, 23, 22, 24, 24, 25, 23, 25, 25, 23, 24, 25, 25, 24, 22, 25, 25, 25, 24, 23, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 23, 25, 23, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 24, 22, 25, 25, 23, 25, 25, 20, 24, 25, 24, 25, 25, 22, 24, 25, 24, 25, 24, 25, 25, 24, 25, 25, 25, 25, 22, 25, 25, 25, 24, 25, 24, 25, 18];
function boxBlurImage(imageID, canvasID, radius, blurAlphaChannel, iterations) {
var img = document.getElementById(imageID);
var w = img.offsetWidth;
var h = img.offsetHeight;
var canvas = document.getElementById(canvasID);
canvas.style.width = w + 'px';
canvas.style.height = h + 'px';
canvas.width = w;
canvas.height = h;
var context = canvas.getContext('2d');
context.clearRect(0, 0, w, h);
context.drawImage(img, 0, 0, w, h);
if (isNaN(radius) || radius < 1) return;
if (blurAlphaChannel) {
boxBlurCanvasRGBA(canvasID, 0, 0, w, h, radius, iterations);
} else {
boxBlurCanvasRGB(canvasID, 0, 0, w, h, radius, iterations);
}
}
function boxBlurCanvasRGBA(id, top_x, top_y, width, height, radius, iterations) {
if (isNaN(radius) || radius < 1) return;
radius |= 0;
if (isNaN(iterations)) iterations = 1;
iterations |= 0;
if (iterations > 3) iterations = 3;
if (iterations < 1) iterations = 1;
var canvas = document.getElementById(id);
var context = canvas.getContext('2d');
var imageData;
try {
try {
imageData = context.getImageData(top_x, top_y, width, height);
} catch (e) {
// NOTE: this part is supposedly only needed if you want to work with local files
// so it might be okay to remove the whole try/catch block and just use
// imageData = context.getImageData( top_x, top_y, width, height );
try {
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
imageData = context.getImageData(top_x, top_y, width, height);
} catch (e) {
throw new Error('unable to access local image data: ' + e);
return;
}
}
} catch (e) {
throw new Error('unable to access image data: ' + e);
return;
}
var pixels = imageData.data;
var rsum, gsum, bsum, asum, x, y, i, p, p1, p2, yp, yi, yw, idx, pa;
var wm = width - 1;
var hm = height - 1;
var wh = width * height;
var rad1 = radius + 1;
var mul_sum = mul_table[radius];
var shg_sum = shg_table[radius];
var r = [];
var g = [];
var b = [];
var a = [];
var vmin = [];
var vmax = [];
while (iterations-- > 0) {
yw = yi = 0;
for (y = 0; y < height; y++) {
rsum = pixels[yw] * rad1;
gsum = pixels[yw + 1] * rad1;
bsum = pixels[yw + 2] * rad1;
asum = pixels[yw + 3] * rad1;
for (i = 1; i <= radius; i++) {
p = yw + ((i > wm ? wm : i) << 2);
rsum += pixels[p++];
gsum += pixels[p++];
bsum += pixels[p++];
asum += pixels[p];
}
for (x = 0; x < width; x++) {
r[yi] = rsum;
g[yi] = gsum;
b[yi] = bsum;
a[yi] = asum;
if (y == 0) {
vmin[x] = ((p = x + rad1) < wm ? p : wm) << 2;
vmax[x] = (p = x - radius) > 0 ? p << 2 : 0;
}
p1 = yw + vmin[x];
p2 = yw + vmax[x];
rsum += pixels[p1++] - pixels[p2++];
gsum += pixels[p1++] - pixels[p2++];
bsum += pixels[p1++] - pixels[p2++];
asum += pixels[p1] - pixels[p2];
yi++;
}
yw += width << 2;
}
for (x = 0; x < width; x++) {
yp = x;
rsum = r[yp] * rad1;
gsum = g[yp] * rad1;
bsum = b[yp] * rad1;
asum = a[yp] * rad1;
for (i = 1; i <= radius; i++) {
yp += i > hm ? 0 : width;
rsum += r[yp];
gsum += g[yp];
bsum += b[yp];
asum += a[yp];
}
yi = x << 2;
for (y = 0; y < height; y++) {
pixels[yi + 3] = pa = asum * mul_sum >>> shg_sum;
if (pa > 0) {
pa = 255 / pa;
pixels[yi] = (rsum * mul_sum >>> shg_sum) * pa;
pixels[yi + 1] = (gsum * mul_sum >>> shg_sum) * pa;
pixels[yi + 2] = (bsum * mul_sum >>> shg_sum) * pa;
} else {
pixels[yi] = pixels[yi + 1] = pixels[yi + 2] = 0;
}
if (x == 0) {
vmin[y] = ((p = y + rad1) < hm ? p : hm) * width;
vmax[y] = (p = y - radius) > 0 ? p * width : 0;
}
p1 = x + vmin[y];
p2 = x + vmax[y];
rsum += r[p1] - r[p2];
gsum += g[p1] - g[p2];
bsum += b[p1] - b[p2];
asum += a[p1] - a[p2];
yi += width << 2;
}
}
}
context.putImageData(imageData, top_x, top_y);
}
function boxBlurCanvasRGB(id, top_x, top_y, width, height, radius, iterations) {
if (isNaN(radius) || radius < 1) return;
radius |= 0;
if (isNaN(iterations)) iterations = 1;
iterations |= 0;
if (iterations > 3) iterations = 3;
if (iterations < 1) iterations = 1;
var canvas = document.getElementById(id);
var context = canvas.getContext('2d');
var imageData;
try {
try {
imageData = context.getImageData(top_x, top_y, width, height);
} catch (e) {
// NOTE: this part is supposedly only needed if you want to work with local files
// so it might be okay to remove the whole try/catch block and just use
// imageData = context.getImageData( top_x, top_y, width, height );
try {
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
imageData = context.getImageData(top_x, top_y, width, height);
} catch (e) {
throw new Error('unable to access local image data: ' + e);
return;
}
}
} catch (e) {
throw new Error('unable to access image data: ' + e);
return;
}
var pixels = imageData.data;
var rsum, gsum, bsum, asum, x, y, i, p, p1, p2, yp, yi, yw, idx;
var wm = width - 1;
var hm = height - 1;
var wh = width * height;
var rad1 = radius + 1;
var r = [];
var g = [];
var b = [];
var mul_sum = mul_table[radius];
var shg_sum = shg_table[radius];
var vmin = [];
var vmax = [];
while (iterations-- > 0) {
yw = yi = 0;
for (y = 0; y < height; y++) {
rsum = pixels[yw] * rad1;
gsum = pixels[yw + 1] * rad1;
bsum = pixels[yw + 2] * rad1;
for (i = 1; i <= radius; i++) {
p = yw + ((i > wm ? wm : i) << 2);
rsum += pixels[p++];
gsum += pixels[p++];
bsum += pixels[p++];
}
for (x = 0; x < width; x++) {
r[yi] = rsum;
g[yi] = gsum;
b[yi] = bsum;
if (y == 0) {
vmin[x] = ((p = x + rad1) < wm ? p : wm) << 2;
vmax[x] = (p = x - radius) > 0 ? p << 2 : 0;
}
p1 = yw + vmin[x];
p2 = yw + vmax[x];
rsum += pixels[p1++] - pixels[p2++];
gsum += pixels[p1++] - pixels[p2++];
bsum += pixels[p1++] - pixels[p2++];
yi++;
}
yw += width << 2;
}
for (x = 0; x < width; x++) {
yp = x;
rsum = r[yp] * rad1;
gsum = g[yp] * rad1;
bsum = b[yp] * rad1;
for (i = 1; i <= radius; i++) {
yp += i > hm ? 0 : width;
rsum += r[yp];
gsum += g[yp];
bsum += b[yp];
}
yi = x << 2;
for (y = 0; y < height; y++) {
pixels[yi] = rsum * mul_sum >>> shg_sum;
pixels[yi + 1] = gsum * mul_sum >>> shg_sum;
pixels[yi + 2] = bsum * mul_sum >>> shg_sum;
if (x == 0) {
vmin[y] = ((p = y + rad1) < hm ? p : hm) * width;
vmax[y] = (p = y - radius) > 0 ? p * width : 0;
}
p1 = x + vmin[y];
p2 = x + vmax[y];
rsum += r[p1] - r[p2];
gsum += g[p1] - g[p2];
bsum += b[p1] - b[p2];
yi += width << 2;
}
}
}
context.putImageData(imageData, top_x, top_y);
}
/***/ })
/******/ ]);
});
//# sourceMappingURL=ReactProgressiveImageBlur.js.map