canvas-compress
Advanced tools
Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "canvas-compress", | ||
"version": "1.1.0", | ||
"description": "image compress with canvas", | ||
@@ -4,0 +5,0 @@ "main": "dist/canvas-compress.js", |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"function"==typeof define&&define.amd?define(["exif-js"],t):"object"==typeof exports?module.exports=t(require("exif-js")):e.CanvasCompress=t(e.EXIF)}(this,function(e){"use strict";function t(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){var r=e.width,i=e.height;switch(t){case 1:return{width:r,height:i,matrix:[1,0,0,1,0,0]};case 2:return{width:r,height:i,matrix:[-1,0,0,1,r,0]};case 3:return{width:r,height:i,matrix:[-1,0,0,-1,r,i]};case 4:return{width:r,height:i,matrix:[1,0,0,-1,0,i]};case 5:return{width:i,height:r,matrix:[0,1,1,0,0,0]};case 6:return{width:i,height:r,matrix:[0,1,-1,0,i,0]};case 7:return{width:i,height:r,matrix:[0,-1,-1,0,i,r]};case 8:return{width:i,height:r,matrix:[0,-1,1,0,0,r]}}}var n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var i in r)Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i])}return e},a=function(){function e(e,t){for(var r=0;r<t.length;r++){var i=t[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,r,i){return r&&e(t.prototype,r),i&&e(t,i),t}}(),o=window.URL||window.webkitURL,h=Math.log(2),u=4,s=.9,c="image/jpeg",g={width:1e3,height:618},d=function f(){var e=this;r(this,f),this.promise=new Promise(function(t,r){e.resolve=t,e.reject=r})},l=function(){function l(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],t=e.type,i=void 0===t?c:t,n=e.width,a=void 0===n?g.width:n,o=e.height,h=void 0===o?g.height:o,u=e.quality,d=void 0===u?s:u;r(this,l),d=parseFloat(d);var f={width:parseFloat(a),height:parseFloat(h)};Object.defineProperties(this,{outputType:{get:function(){return i}},outputSize:{get:function(){return f}},outputQuality:{get:function(){return d}}})}return a(l,[{key:"_getOriginalImage",value:function(e){var t=o.createObjectURL(e),r=new Image,i=new d;return r.onload=function(){i.resolve(r)},r.onerror=function(){i.reject("image load error")},r.src=t,i.promise}},{key:"_drawOriginalImage",value:function(r){var n=document.createElement("canvas"),a=n.getContext("2d"),h=new d;return e.getData(r,function(){var u=e.getTag(r,"Orientation")||1,s=i(r,u),c=s.width,g=s.height,d=s.matrix;n.width=c,n.height=g,a.save(),a.transform.apply(a,t(d)),a.drawImage(r,0,0),a.restore(),o.revokeObjectURL(r.src),h.resolve(n)}),h.promise}},{key:"_resizeImage",value:function(e){var t=this.outputSize,r=e.width,i=e.height,n=Math.min(1,t.width/r,t.height/i);return Promise.resolve({source:e,scale:n})}},{key:"_drawImage",value:function(e){var t=e.source,r=e.scale,i=t.getContext("2d"),n=Math.min(u,Math.ceil(1/r/h));r=Math.pow(r,1/n);for(var a=t.width,o=t.height;n--;){var s=a*r|0,c=o*r|0;i.drawImage(t,0,0,a,o,0,0,s,c),a=s,o=c}var g=document.createElement("canvas"),d=g.getContext("2d");return g.width=a,g.height=o,d.drawImage(t,0,0,a,o,0,0,a,o),Promise.resolve(g)}},{key:"_compress",value:function(e){var t=this.outputType,r=this.outputQuality,i=e.width,n=e.height,a=e.toDataURL(t,r),o=atob(a.split(",")[1]).split("").map(function(e){return e.charCodeAt(0)}),h=new Blob([new Uint8Array(o)],{type:t});return Promise.resolve({blob:h,width:i,height:n})}},{key:"process",value:function(e){if(!e)return Promise.reject(new ReferenceError("file blob is required"));if(!e.type.match(/^image/))return Promise.reject(new TypeError("unsupport file type: "+e.type));var t={};return this._getOriginalImage(e).then(function(e){return t.width=e.width,t.height=e.height,e}).then(this._drawOriginalImage.bind(this)).then(this._resizeImage.bind(this)).then(this._drawImage.bind(this)).then(this._compress.bind(this)).then(function(r){return{source:n({blob:e},t),result:n({},r)}})}}]),l}();return l}); | ||
!function(e,t){"function"==typeof define&&define.amd?define(["exif-js"],t):"object"==typeof exports?module.exports=t(require("exif-js")):e.CanvasCompress=t(e.EXIF)}(this,function(e){"use strict";function t(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){var r=e.width,i=e.height;switch(t){case 1:return{width:r,height:i,matrix:[1,0,0,1,0,0]};case 2:return{width:r,height:i,matrix:[-1,0,0,1,r,0]};case 3:return{width:r,height:i,matrix:[-1,0,0,-1,r,i]};case 4:return{width:r,height:i,matrix:[1,0,0,-1,0,i]};case 5:return{width:i,height:r,matrix:[0,1,1,0,0,0]};case 6:return{width:i,height:r,matrix:[0,1,-1,0,i,0]};case 7:return{width:i,height:r,matrix:[0,-1,-1,0,i,r]};case 8:return{width:i,height:r,matrix:[0,-1,1,0,0,r]}}}var n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var i in r)Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i])}return e},o=function(){function e(e,t){for(var r=0;r<t.length;r++){var i=t[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,r,i){return r&&e(t.prototype,r),i&&e(t,i),t}}(),a=window.URL||window.webkitURL,h=Math.log(2),u=4,s=.9,c="image/jpeg",f={width:1e3,height:618},g={_Promise:window.Promise,get Promise(){if("function"!=typeof this._Promise)throw new Error("canvas-compress requires Promise");return this._Promise},set Promise(e){if("function"!=typeof e)throw new TypeError("Promise should be a function");this._Promise=e}},l=function d(){var e=this;r(this,d),this.promise=new g.Promise(function(t,r){e.resolve=t,e.reject=r})},m=function(){function m(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],t=e.type,i=void 0===t?c:t,n=e.width,o=void 0===n?f.width:n,a=e.height,h=void 0===a?f.height:a,u=e.quality,g=void 0===u?s:u;r(this,m),g=parseFloat(g);var l={width:parseFloat(o),height:parseFloat(h)};Object.defineProperties(this,{outputType:{get:function(){return i}},outputSize:{get:function(){return l}},outputQuality:{get:function(){return g}}})}return o(m,null,[{key:"usePromise",value:function(e){g.Promise=e}}]),o(m,[{key:"_getOriginalImage",value:function(e){var t=a.createObjectURL(e),r=new Image,i=new l;return r.onload=function(){i.resolve(r)},r.onerror=function(){i.reject("image load error")},r.src=t,i.promise}},{key:"_drawOriginalImage",value:function(r){var n=document.createElement("canvas"),o=n.getContext("2d"),h=new l;return e.getData(r,function(){var u=e.getTag(r,"Orientation")||1,s=i(r,u),c=s.width,f=s.height,g=s.matrix;n.width=c,n.height=f,o.save(),o.transform.apply(o,t(g)),o.drawImage(r,0,0),o.restore(),a.revokeObjectURL(r.src),h.resolve(n)}),h.promise}},{key:"_resizeImage",value:function(e){var t=this.outputSize,r=e.width,i=e.height,n=Math.min(1,t.width/r,t.height/i);return Promise.resolve({source:e,scale:n})}},{key:"_drawImage",value:function(e){var t=e.source,r=e.scale,i=t.getContext("2d"),n=Math.min(u,Math.ceil(1/r/h));r=Math.pow(r,1/n);for(var o=t.width,a=t.height;n--;){var s=o*r|0,c=a*r|0;i.drawImage(t,0,0,o,a,0,0,s,c),o=s,a=c}var f=document.createElement("canvas"),g=f.getContext("2d");return f.width=o,f.height=a,g.drawImage(t,0,0,o,a,0,0,o,a),Promise.resolve(f)}},{key:"_compress",value:function(e){var t=this.outputType,r=this.outputQuality,i=e.width,n=e.height,o=e.toDataURL(t,r),a=atob(o.split(",")[1]).split("").map(function(e){return e.charCodeAt(0)}),h=new Blob([new Uint8Array(a)],{type:t});return Promise.resolve({blob:h,width:i,height:n})}},{key:"process",value:function(e){if(!e)return Promise.reject(new ReferenceError("file blob is required"));if(!e.type.match(/^image/))return Promise.reject(new TypeError("unsupport file type: "+e.type));var t={};return this._getOriginalImage(e).then(function(e){return t.width=e.width,t.height=e.height,e}).then(this._drawOriginalImage.bind(this)).then(this._resizeImage.bind(this)).then(this._drawImage.bind(this)).then(this._compress.bind(this)).then(function(r){return{source:n({blob:e},t),result:n({},r)}})}}]),m}();return m}); |
{ | ||
"name": "canvas-compress", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "image compress with canvas", | ||
@@ -5,0 +5,0 @@ "main": "dist/canvas-compress.js", |
@@ -62,5 +62,10 @@ # CanvasCompress | ||
## Use third-party Promise | ||
```javascript | ||
CanvasCompress.usePromise(require('bluebird')); | ||
``` | ||
## License | ||
MIT. | ||
MIT. |
@@ -13,2 +13,22 @@ const URL = window.URL || window.webkitURL; | ||
const GLOBAL_ENV = { | ||
_Promise: window.Promise, | ||
get Promise() { | ||
if (typeof this._Promise !== 'function') { | ||
throw new Error('canvas-compress requires Promise'); | ||
} | ||
return this._Promise; | ||
}, | ||
set Promise(Constructor) { | ||
if (typeof Constructor !== 'function') { | ||
throw new TypeError('Promise should be a function'); | ||
} | ||
this._Promise = Constructor; | ||
} | ||
}; | ||
function getTransform(image, orientation) { | ||
@@ -98,3 +118,3 @@ const { width, height } = image; | ||
constructor() { | ||
this.promise = new Promise((resolve, reject) => { | ||
this.promise = new GLOBAL_ENV.Promise((resolve, reject) => { | ||
this.resolve = resolve; | ||
@@ -107,4 +127,13 @@ this.reject = reject; | ||
/* export */ class CanvasCompress { | ||
constructor({ type = DEFAULT_TYPE, width = DEFAULT_SIZE.width, height = DEFAULT_SIZE.height, quality = DEFAULT_QUALITY } = {}) { | ||
static usePromise(Constructor) { | ||
GLOBAL_ENV.Promise = Constructor; | ||
} | ||
constructor({ | ||
type = DEFAULT_TYPE, | ||
width = DEFAULT_SIZE.width, | ||
height = DEFAULT_SIZE.height, | ||
quality = DEFAULT_QUALITY | ||
} = {}) { | ||
quality = parseFloat(quality); | ||
@@ -111,0 +140,0 @@ |
22029
363
71