dpzvc-ui 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dpzvc.esm.js +263 -268
- package/dist/dpzvc.esm.js.map +1 -1
- package/dist/dpzvc.esm.min.js +1 -1
- package/dist/dpzvc.esm.min.js.map +1 -1
- package/dist/dpzvc.js +250 -297
- package/dist/dpzvc.js.map +1 -1
- package/dist/dpzvc.min.js +1 -1
- package/dist/dpzvc.min.js.map +1 -1
- package/dist-prod/445.02e4c5b08288b0d23ede.js +3 -0
- package/dist-prod/445.02e4c5b08288b0d23ede.js.LICENSE.txt +5 -0
- package/dist-prod/445.02e4c5b08288b0d23ede.js.map +1 -0
- package/dist-prod/index.html +1 -1
- package/dist-prod/main.992de223adbd07b6b84b.js +2 -0
- package/dist-prod/main.992de223adbd07b6b84b.js.map +1 -0
- package/dist-prod/rater.2a701809bfde6325d1a2.chunk.js +2 -0
- package/dist-prod/rater.2a701809bfde6325d1a2.chunk.js.map +1 -0
- package/package.json +2 -2
- package/src/components/upload/upload.vue +1 -1
- package/src/lib/MegaPixImage.js +126 -122
- package/src/lib/MegaPixImageOld.js +157 -0
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
(self.webpackChunkdpzvc_ui=self.webpackChunkdpzvc_ui||[]).push([[109],{4142(n,e,t){var a=t(6795);a.__esModule&&(a=a.default),"string"==typeof a&&(a=[[n.id,a,""]]),a.locals&&(n.exports=a.locals);(0,t(534).A)("6ceb7c19",a,!0,{})},6795(n,e,t){"use strict";t.r(e),t.d(e,{default:()=>r});var a=t(1354),i=t.n(a),s=t(6314),A=t.n(s)()(i());A.push([n.id,".Rater[data-v-3763261e] {\n width: 100%;\n padding-top: 50px;\n display: flex;\n flex-direction: column;\n justify-content: center;\n align-items: center;\n}\n.Rater p[data-v-3763261e] {\n text-indent: 10px;\n font-weight: bold;\n font-size: 14px;\n margin: 10px 0;\n}\n/*# sourceMappingURL=Rater.css.map */","",{version:3,sources:["webpack://./Rater.vue","webpack://./src/views/Rater.vue"],names:[],mappings:"AACA;EACI,WAAA;EACA,iBAAA;EACA,aAAA;EACA,sBAAA;EACA,uBAAA;EACA,mBAAA;ACAJ;ADNA;EAQQ,iBAAA;EACA,iBAAA;EACA,eAAA;EACA,cAAA;ACCR;AACA,oCAAoC",sourcesContent:["\n.Rater {\n width: 100%;\n padding-top: 50px;\n display: flex;\n flex-direction: column;\n justify-content: center;\n align-items: center;\n p {\n text-indent: 10px;\n font-weight: bold;\n font-size: 14px;\n margin: 10px 0;\n }\n}\n",".Rater {\n width: 100%;\n padding-top: 50px;\n display: flex;\n flex-direction: column;\n justify-content: center;\n align-items: center;\n}\n.Rater p {\n text-indent: 10px;\n font-weight: bold;\n font-size: 14px;\n margin: 10px 0;\n}\n/*# sourceMappingURL=Rater.css.map */"],sourceRoot:""}]);const r=A},9123(n,e,t){"use strict";t.r(e),t.d(e,{default:()=>i});const a={name:"rater",data:function(){return{rate:0,disabledRate:4.5}}};t(4142);const i=(0,t(4486).A)(a,function(){var n=this,e=n._self._c;return e("div",{staticClass:"Rater"},[e("p",[n._v("可点击评分")]),n._v(" "),e("Rater",{model:{value:n.rate,callback:function(e){n.rate=e},expression:"rate"}}),n._v(" "),e("span",[n._v(n._s(n.rate))]),n._v(" "),e("p",[n._v("不可点击评分")]),n._v(" "),e("Rater",{attrs:{disabled:""},model:{value:n.disabledRate,callback:function(e){n.disabledRate=e},expression:"disabledRate"}}),n._v(" "),e("span",[n._v(n._s(n.disabledRate))])],1)},[],!1,null,"3763261e",null).exports}}]);
|
|
2
|
+
//# sourceMappingURL=rater.2a701809bfde6325d1a2.chunk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rater.2a701809bfde6325d1a2.chunk.js","mappings":"mFAGA,IAAIA,EAAU,EAAQ,MACnBA,EAAQC,aAAYD,EAAUA,EAAQE,SACnB,iBAAZF,IAAsBA,EAAU,CAAC,CAACG,EAAOC,GAAIJ,EAAS,MAC7DA,EAAQK,SAAQF,EAAOG,QAAUN,EAAQK,SAG/BE,EADH,UACO,WAAYP,GAAS,EAAM,CAAC,E,0FCNzCQ,E,MAA0B,GAA4B,KAE1DA,EAAwBC,KAAK,CAACN,EAAOC,GAAI,gUAcD,GAAG,CAAC,QAAU,EAAE,QAAU,CAAC,wBAAwB,mCAAmC,MAAQ,GAAG,SAAW,4IAA4I,eAAiB,CAAC,gSAAgS,+RAA+R,WAAa,MAE93B,S,yDCTA,MCZqK,EDYrK,CACAM,KAAA,QACAC,KAAA,WACA,OACAC,KAAA,EACAC,aAAA,IAEA,G,QEAA,SAXgB,E,QAAA,GACd,ECTW,WAAkB,IAAIC,EAAIC,KAAKC,EAAGF,EAAIG,MAAMD,GAAG,OAAOA,EAAG,MAAM,CAACE,YAAY,SAAS,CAACF,EAAG,IAAI,CAACF,EAAIK,GAAG,WAAWL,EAAIK,GAAG,KAAKH,EAAG,QAAQ,CAACI,MAAM,CAACC,MAAOP,EAAIF,KAAMU,SAAS,SAAUC,GAAMT,EAAIF,KAAKW,CAAG,EAAEC,WAAW,UAAUV,EAAIK,GAAG,KAAKH,EAAG,OAAO,CAACF,EAAIK,GAAGL,EAAIW,GAAGX,EAAIF,SAASE,EAAIK,GAAG,KAAKH,EAAG,IAAI,CAACF,EAAIK,GAAG,YAAYL,EAAIK,GAAG,KAAKH,EAAG,QAAQ,CAACU,MAAM,CAAC,SAAW,IAAIN,MAAM,CAACC,MAAOP,EAAID,aAAcS,SAAS,SAAUC,GAAMT,EAAID,aAAaU,CAAG,EAAEC,WAAW,kBAAkBV,EAAIK,GAAG,KAAKH,EAAG,OAAO,CAACF,EAAIK,GAAGL,EAAIW,GAAGX,EAAID,kBAAkB,EACvhB,EACsB,IDUpB,EACA,KACA,WACA,M","sources":["webpack://dpzvc-ui/./src/views/Rater.vue?5a04","webpack://dpzvc-ui/./src/views/Rater.vue?3465","webpack://dpzvc-ui/src/views/Rater.vue","webpack://dpzvc-ui/./src/views/Rater.vue?6ab8","webpack://dpzvc-ui/./src/views/Rater.vue?bbed","webpack://dpzvc-ui/./src/views/Rater.vue"],"sourcesContent":["// style-loader: Adds some css to the DOM by adding a <style> tag\n\n// load the styles\nvar content = require(\"!!../../node_modules/css-loader/dist/cjs.js??clonedRuleSet-3.use[1]!../../node_modules/vue-loader/lib/loaders/stylePostLoader.js!../../node_modules/postcss-loader/dist/cjs.js!../../node_modules/less-loader/dist/cjs.js!../../node_modules/vue-loader/lib/index.js??vue-loader-options!./Rater.vue?vue&type=style&index=0&id=3763261e&prod&lang=less&scoped=true\");\nif(content.__esModule) content = content.default;\nif(typeof content === 'string') content = [[module.id, content, '']];\nif(content.locals) module.exports = content.locals;\n// add the styles to the DOM\nvar add = require(\"!../../node_modules/vue-style-loader/lib/addStylesClient.js\").default\nvar update = add(\"6ceb7c19\", content, true, {});","// Imports\nimport ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \"../../node_modules/css-loader/dist/runtime/sourceMaps.js\";\nimport ___CSS_LOADER_API_IMPORT___ from \"../../node_modules/css-loader/dist/runtime/api.js\";\nvar ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_SOURCEMAP_IMPORT___);\n// Module\n___CSS_LOADER_EXPORT___.push([module.id, `.Rater[data-v-3763261e] {\n width: 100%;\n padding-top: 50px;\n display: flex;\n flex-direction: column;\n justify-content: center;\n align-items: center;\n}\n.Rater p[data-v-3763261e] {\n text-indent: 10px;\n font-weight: bold;\n font-size: 14px;\n margin: 10px 0;\n}\n/*# sourceMappingURL=Rater.css.map */`, \"\",{\"version\":3,\"sources\":[\"webpack://./Rater.vue\",\"webpack://./src/views/Rater.vue\"],\"names\":[],\"mappings\":\"AACA;EACI,WAAA;EACA,iBAAA;EACA,aAAA;EACA,sBAAA;EACA,uBAAA;EACA,mBAAA;ACAJ;ADNA;EAQQ,iBAAA;EACA,iBAAA;EACA,eAAA;EACA,cAAA;ACCR;AACA,oCAAoC\",\"sourcesContent\":[\"\\n.Rater {\\n width: 100%;\\n padding-top: 50px;\\n display: flex;\\n flex-direction: column;\\n justify-content: center;\\n align-items: center;\\n p {\\n text-indent: 10px;\\n font-weight: bold;\\n font-size: 14px;\\n margin: 10px 0;\\n }\\n}\\n\",\".Rater {\\n width: 100%;\\n padding-top: 50px;\\n display: flex;\\n flex-direction: column;\\n justify-content: center;\\n align-items: center;\\n}\\n.Rater p {\\n text-indent: 10px;\\n font-weight: bold;\\n font-size: 14px;\\n margin: 10px 0;\\n}\\n/*# sourceMappingURL=Rater.css.map */\"],\"sourceRoot\":\"\"}]);\n// Exports\nexport default ___CSS_LOADER_EXPORT___;\n","<template>\n <div class=\"Rater\">\n <p>可点击评分</p>\n <Rater v-model=\"rate\"></Rater>\n <span>{{rate}}</span>\n <p>不可点击评分</p>\n <Rater v-model=\"disabledRate\" disabled></Rater>\n <span>{{disabledRate}}</span>\n </div>\n</template>\n\n<script>\n export default {\n name: \"rater\",\n data(){\n return {\n rate:0,\n disabledRate:4.5\n }\n }\n }\n</script>\n\n<style lang=\"less\" scoped>\n .Rater {\n width: 100%;\n padding-top: 50px;\n display: flex;\n flex-direction: column;\n justify-content: center;\n align-items: center;\n p {\n text-indent: 10px;\n font-weight: bold;\n font-size: 14px;\n margin: 10px 0;\n }\n }\n</style>","import mod from \"-!../../node_modules/babel-loader/lib/index.js!../../node_modules/vue-loader/lib/index.js??vue-loader-options!./Rater.vue?vue&type=script&lang=js\"; export default mod; export * from \"-!../../node_modules/babel-loader/lib/index.js!../../node_modules/vue-loader/lib/index.js??vue-loader-options!./Rater.vue?vue&type=script&lang=js\"","import { render, staticRenderFns } from \"./Rater.vue?vue&type=template&id=3763261e&scoped=true\"\nimport script from \"./Rater.vue?vue&type=script&lang=js\"\nexport * from \"./Rater.vue?vue&type=script&lang=js\"\nimport style0 from \"./Rater.vue?vue&type=style&index=0&id=3763261e&prod&lang=less&scoped=true\"\n\n\n/* normalize component */\nimport normalizer from \"!../../node_modules/vue-loader/lib/runtime/componentNormalizer.js\"\nvar component = normalizer(\n script,\n render,\n staticRenderFns,\n false,\n null,\n \"3763261e\",\n null\n \n)\n\nexport default component.exports","var render = function render(){var _vm=this,_c=_vm._self._c;return _c('div',{staticClass:\"Rater\"},[_c('p',[_vm._v(\"可点击评分\")]),_vm._v(\" \"),_c('Rater',{model:{value:(_vm.rate),callback:function ($$v) {_vm.rate=$$v},expression:\"rate\"}}),_vm._v(\" \"),_c('span',[_vm._v(_vm._s(_vm.rate))]),_vm._v(\" \"),_c('p',[_vm._v(\"不可点击评分\")]),_vm._v(\" \"),_c('Rater',{attrs:{\"disabled\":\"\"},model:{value:(_vm.disabledRate),callback:function ($$v) {_vm.disabledRate=$$v},expression:\"disabledRate\"}}),_vm._v(\" \"),_c('span',[_vm._v(_vm._s(_vm.disabledRate))])],1)\n}\nvar staticRenderFns = []\n\nexport { render, staticRenderFns }"],"names":["content","__esModule","default","module","id","locals","exports","add","___CSS_LOADER_EXPORT___","push","name","data","rate","disabledRate","_vm","this","_c","_self","staticClass","_v","model","value","callback","$$v","expression","_s","attrs"],"ignoreList":[],"sourceRoot":""}
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import EXIF from 'exif-js'
|
|
13
13
|
// import MegaPixImage from '../../lib/MegaPixImage'
|
|
14
|
-
import
|
|
14
|
+
import MegaPixImage from '../../lib/MegaPixImage';
|
|
15
15
|
import { JPEG } from '../../utils/util'
|
|
16
16
|
const prefixCls = 'dpzvc-upload';
|
|
17
17
|
export default {
|
package/src/lib/MegaPixImage.js
CHANGED
|
@@ -1,158 +1,162 @@
|
|
|
1
1
|
// MegaPixImage.js
|
|
2
|
-
|
|
3
|
-
if (typeof module === 'object' && typeof module.exports === 'object') {
|
|
4
|
-
module.exports = factory(); // CommonJS
|
|
5
|
-
} else if (typeof define === 'function' && define.amd) {
|
|
6
|
-
define([], factory); // AMD
|
|
7
|
-
} else {
|
|
8
|
-
global.MegaPixImage = factory(); // Browser global
|
|
9
|
-
}
|
|
10
|
-
}(typeof window !== 'undefined' ? window : this, function () {
|
|
11
|
-
|
|
12
|
-
// -------------------------
|
|
13
|
-
// Helper functions
|
|
14
|
-
// -------------------------
|
|
15
|
-
function detectSubsampling(img) {
|
|
16
|
-
var iw = img.naturalWidth, ih = img.naturalHeight;
|
|
17
|
-
if (iw * ih > 1024 * 1024) {
|
|
18
|
-
var canvas = document.createElement('canvas');
|
|
19
|
-
canvas.width = canvas.height = 1;
|
|
20
|
-
var ctx = canvas.getContext('2d');
|
|
21
|
-
ctx.drawImage(img, -iw + 1, 0);
|
|
22
|
-
return ctx.getImageData(0, 0, 1, 1).data[3] === 0;
|
|
23
|
-
} else {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
2
|
+
// 将原先的 UMD/CommonJS 改为 ESM export default
|
|
27
3
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
py = (ey + sy) >> 1;
|
|
40
|
-
}
|
|
41
|
-
var ratio = py / ih;
|
|
42
|
-
return (ratio === 0) ? 1 : ratio;
|
|
4
|
+
// -------------------------
|
|
5
|
+
// Helper functions
|
|
6
|
+
// -------------------------
|
|
7
|
+
function detectSubsampling(img) {
|
|
8
|
+
const iw = img.naturalWidth, ih = img.naturalHeight;
|
|
9
|
+
if (iw * ih > 1024 * 1024) {
|
|
10
|
+
const canvas = document.createElement('canvas');
|
|
11
|
+
canvas.width = canvas.height = 1;
|
|
12
|
+
const ctx = canvas.getContext('2d');
|
|
13
|
+
ctx.drawImage(img, -iw + 1, 0);
|
|
14
|
+
return ctx.getImageData(0, 0, 1, 1).data[3] === 0;
|
|
43
15
|
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
44
18
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
19
|
+
function detectVerticalSquash(img, iw, ih) {
|
|
20
|
+
const canvas = document.createElement('canvas');
|
|
21
|
+
canvas.width = 1;
|
|
22
|
+
canvas.height = ih;
|
|
23
|
+
const ctx = canvas.getContext('2d');
|
|
24
|
+
ctx.drawImage(img, 0, 0);
|
|
25
|
+
const data = ctx.getImageData(0, 0, 1, ih).data;
|
|
26
|
+
let sy = 0, ey = ih, py = ih;
|
|
27
|
+
while (py > sy) {
|
|
28
|
+
const alpha = data[(py - 1) * 4 + 3];
|
|
29
|
+
if (alpha === 0) ey = py;
|
|
30
|
+
else sy = py;
|
|
31
|
+
py = (ey + sy) >> 1;
|
|
49
32
|
}
|
|
33
|
+
const ratio = py / ih;
|
|
34
|
+
return ratio === 0 ? 1 : ratio;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function renderImageToDataURL(img, options, doSquash) {
|
|
38
|
+
const canvas = document.createElement('canvas');
|
|
39
|
+
renderImageToCanvas(img, canvas, options, doSquash);
|
|
40
|
+
return canvas.toDataURL("image/jpeg", options.quality || 0.8);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function renderImageToCanvas(img, canvas, options, doSquash) {
|
|
44
|
+
let iw = img.naturalWidth, ih = img.naturalHeight;
|
|
45
|
+
if (!(iw + ih)) return;
|
|
50
46
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
47
|
+
const width = options.width, height = options.height;
|
|
48
|
+
const ctx = canvas.getContext('2d');
|
|
49
|
+
ctx.save();
|
|
50
|
+
transformCoordinate(canvas, ctx, width, height, options.orientation);
|
|
51
|
+
|
|
52
|
+
const subsampled = detectSubsampling(img);
|
|
53
|
+
if (subsampled) { iw /= 2; ih /= 2; }
|
|
54
|
+
|
|
55
|
+
const d = 1024;
|
|
56
|
+
const tmpCanvas = document.createElement('canvas');
|
|
57
|
+
tmpCanvas.width = tmpCanvas.height = d;
|
|
58
|
+
const tmpCtx = tmpCanvas.getContext('2d');
|
|
59
|
+
|
|
60
|
+
const vertSquashRatio = doSquash ? detectVerticalSquash(img, iw, ih) : 1;
|
|
61
|
+
const dw = Math.ceil(d * width / iw);
|
|
62
|
+
const dh = Math.ceil(d * height / ih / vertSquashRatio);
|
|
63
|
+
|
|
64
|
+
let sy = 0, dy = 0;
|
|
65
|
+
while (sy < ih) {
|
|
66
|
+
let sx = 0, dx = 0;
|
|
67
|
+
while (sx < iw) {
|
|
68
|
+
tmpCtx.clearRect(0, 0, d, d);
|
|
69
|
+
tmpCtx.drawImage(img, -sx, -sy);
|
|
70
|
+
ctx.drawImage(tmpCanvas, 0, 0, d, d, dx, dy, dw, dh);
|
|
71
|
+
sx += d;
|
|
72
|
+
dx += dw;
|
|
77
73
|
}
|
|
78
|
-
|
|
79
|
-
|
|
74
|
+
sy += d;
|
|
75
|
+
dy += dh;
|
|
80
76
|
}
|
|
77
|
+
ctx.restore();
|
|
78
|
+
}
|
|
81
79
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
80
|
+
function transformCoordinate(canvas, ctx, width, height, orientation) {
|
|
81
|
+
switch (orientation) {
|
|
82
|
+
case 5: case 6: case 7: case 8:
|
|
83
|
+
canvas.width = height;
|
|
84
|
+
canvas.height = width;
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
canvas.width = width;
|
|
88
|
+
canvas.height = height;
|
|
89
|
+
}
|
|
90
|
+
switch (orientation) {
|
|
91
|
+
case 2: ctx.translate(width, 0); ctx.scale(-1, 1); break;
|
|
92
|
+
case 3: ctx.translate(width, height); ctx.rotate(Math.PI); break;
|
|
93
|
+
case 4: ctx.translate(0, height); ctx.scale(1, -1); break;
|
|
94
|
+
case 5: ctx.rotate(0.5 * Math.PI); ctx.scale(1, -1); break;
|
|
95
|
+
case 6: ctx.rotate(0.5 * Math.PI); ctx.translate(0, -height); break;
|
|
96
|
+
case 7: ctx.rotate(0.5 * Math.PI); ctx.translate(width, -height); ctx.scale(-1, 1); break;
|
|
97
|
+
case 8: ctx.rotate(-0.5 * Math.PI); ctx.translate(-width, 0); break;
|
|
96
98
|
}
|
|
99
|
+
}
|
|
97
100
|
|
|
98
|
-
|
|
99
|
-
|
|
101
|
+
const URL = window.URL && window.URL.createObjectURL ? window.URL :
|
|
102
|
+
window.webkitURL && window.webkitURL.createObjectURL ? window.webkitURL : null;
|
|
100
103
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
104
|
+
// -------------------------
|
|
105
|
+
// MegaPixImage class
|
|
106
|
+
// -------------------------
|
|
107
|
+
class MegaPixImage {
|
|
108
|
+
constructor(srcImage) {
|
|
105
109
|
if (window.Blob && srcImage instanceof Blob) {
|
|
106
|
-
if (!URL)
|
|
107
|
-
|
|
110
|
+
if (!URL) throw Error("No createObjectURL function found");
|
|
111
|
+
const img = new Image();
|
|
108
112
|
img.src = URL.createObjectURL(srcImage);
|
|
109
113
|
this.blob = srcImage;
|
|
110
114
|
srcImage = img;
|
|
111
115
|
}
|
|
112
116
|
if (!srcImage.naturalWidth && !srcImage.naturalHeight) {
|
|
113
|
-
|
|
117
|
+
this.imageLoadListeners = [];
|
|
118
|
+
const _this = this;
|
|
114
119
|
srcImage.onload = srcImage.onerror = function () {
|
|
115
|
-
|
|
120
|
+
const listeners = _this.imageLoadListeners;
|
|
116
121
|
if (listeners) {
|
|
117
122
|
_this.imageLoadListeners = null;
|
|
118
|
-
|
|
123
|
+
listeners.forEach(fn => fn());
|
|
119
124
|
}
|
|
120
125
|
};
|
|
121
|
-
this.imageLoadListeners = [];
|
|
122
126
|
}
|
|
123
127
|
this.srcImage = srcImage;
|
|
124
128
|
}
|
|
125
129
|
|
|
126
|
-
|
|
130
|
+
render(target, options, callback) {
|
|
127
131
|
if (this.imageLoadListeners) {
|
|
128
|
-
|
|
129
|
-
this.imageLoadListeners.push(function () { _this.render(target, options, callback); });
|
|
132
|
+
this.imageLoadListeners.push(() => { this.render(target, options, callback); });
|
|
130
133
|
return;
|
|
131
134
|
}
|
|
132
135
|
options = options || {};
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
136
|
+
const imgWidth = this.srcImage.naturalWidth, imgHeight = this.srcImage.naturalHeight;
|
|
137
|
+
let width = options.width, height = options.height;
|
|
138
|
+
const maxWidth = options.maxWidth, maxHeight = options.maxHeight;
|
|
139
|
+
const doSquash = !this.blob || this.blob.type === 'image/jpeg';
|
|
140
|
+
|
|
141
|
+
if (width && !height) height = (imgHeight * width / imgWidth) << 0;
|
|
142
|
+
else if (height && !width) width = (imgWidth * height / imgHeight) << 0;
|
|
139
143
|
else { width = imgWidth; height = imgHeight; }
|
|
144
|
+
|
|
140
145
|
if (maxWidth && width > maxWidth) { width = maxWidth; height = (imgHeight * width / imgWidth) << 0; }
|
|
141
146
|
if (maxHeight && height > maxHeight) { height = maxHeight; width = (imgWidth * height / imgHeight) << 0; }
|
|
142
|
-
var opt = { width: width, height: height };
|
|
143
|
-
for (var k in options) opt[k] = options[k];
|
|
144
|
-
|
|
145
|
-
var tagName = target.tagName.toLowerCase();
|
|
146
|
-
if (tagName === 'img') { target.src = renderImageToDataURL(this.srcImage, opt, doSquash); }
|
|
147
|
-
else if (tagName === 'canvas') { renderImageToCanvas(this.srcImage, target, opt, doSquash); }
|
|
148
|
-
if (typeof this.onrender === 'function') { this.onrender(target); }
|
|
149
|
-
if (callback) { callback(); }
|
|
150
|
-
if (this.blob) { this.blob = null; URL.revokeObjectURL(this.srcImage.src); }
|
|
151
|
-
};
|
|
152
147
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
148
|
+
const opt = { width, height, ...options };
|
|
149
|
+
const tagName = target.tagName.toLowerCase();
|
|
150
|
+
if (tagName === 'img') target.src = renderImageToDataURL(this.srcImage, opt, doSquash);
|
|
151
|
+
else if (tagName === 'canvas') renderImageToCanvas(this.srcImage, target, opt, doSquash);
|
|
152
|
+
|
|
153
|
+
if (typeof this.onrender === 'function') this.onrender(target);
|
|
154
|
+
if (callback) callback();
|
|
155
|
+
if (this.blob) { this.blob = null; URL.revokeObjectURL(this.srcImage.src); }
|
|
156
|
+
}
|
|
157
|
+
}
|
|
157
158
|
|
|
158
|
-
|
|
159
|
+
// -------------------------
|
|
160
|
+
// ESM export default
|
|
161
|
+
// -------------------------
|
|
162
|
+
export default MegaPixImage;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
if (typeof module === 'object' && typeof module.exports === 'object') {
|
|
3
|
+
module.exports = factory(); // CommonJS
|
|
4
|
+
} else if (typeof define === 'function' && define.amd) {
|
|
5
|
+
define([], factory); // AMD
|
|
6
|
+
} else {
|
|
7
|
+
global.MegaPixImage = factory(); // Browser global
|
|
8
|
+
}
|
|
9
|
+
}(typeof window !== 'undefined' ? window : this, function () {
|
|
10
|
+
|
|
11
|
+
// -------------------------
|
|
12
|
+
// Helper functions
|
|
13
|
+
// -------------------------
|
|
14
|
+
function detectSubsampling(img) {
|
|
15
|
+
var iw = img.naturalWidth, ih = img.naturalHeight;
|
|
16
|
+
if (iw * ih > 1024 * 1024) {
|
|
17
|
+
var canvas = document.createElement('canvas');
|
|
18
|
+
canvas.width = canvas.height = 1;
|
|
19
|
+
var ctx = canvas.getContext('2d');
|
|
20
|
+
ctx.drawImage(img, -iw + 1, 0);
|
|
21
|
+
return ctx.getImageData(0, 0, 1, 1).data[3] === 0;
|
|
22
|
+
} else {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function detectVerticalSquash(img, iw, ih) {
|
|
28
|
+
var canvas = document.createElement('canvas');
|
|
29
|
+
canvas.width = 1;
|
|
30
|
+
canvas.height = ih;
|
|
31
|
+
var ctx = canvas.getContext('2d');
|
|
32
|
+
ctx.drawImage(img, 0, 0);
|
|
33
|
+
var data = ctx.getImageData(0, 0, 1, ih).data;
|
|
34
|
+
var sy = 0, ey = ih, py = ih;
|
|
35
|
+
while (py > sy) {
|
|
36
|
+
var alpha = data[(py - 1) * 4 + 3];
|
|
37
|
+
if (alpha === 0) { ey = py; } else { sy = py; }
|
|
38
|
+
py = (ey + sy) >> 1;
|
|
39
|
+
}
|
|
40
|
+
var ratio = py / ih;
|
|
41
|
+
return (ratio === 0) ? 1 : ratio;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function renderImageToDataURL(img, options, doSquash) {
|
|
45
|
+
var canvas = document.createElement('canvas');
|
|
46
|
+
renderImageToCanvas(img, canvas, options, doSquash);
|
|
47
|
+
return canvas.toDataURL("image/jpeg", options.quality || 0.8);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function renderImageToCanvas(img, canvas, options, doSquash) {
|
|
51
|
+
var iw = img.naturalWidth, ih = img.naturalHeight;
|
|
52
|
+
if (!(iw + ih)) return;
|
|
53
|
+
var width = options.width, height = options.height;
|
|
54
|
+
var ctx = canvas.getContext('2d');
|
|
55
|
+
ctx.save();
|
|
56
|
+
transformCoordinate(canvas, ctx, width, height, options.orientation);
|
|
57
|
+
var subsampled = detectSubsampling(img);
|
|
58
|
+
if (subsampled) { iw /= 2; ih /= 2; }
|
|
59
|
+
var d = 1024;
|
|
60
|
+
var tmpCanvas = document.createElement('canvas');
|
|
61
|
+
tmpCanvas.width = tmpCanvas.height = d;
|
|
62
|
+
var tmpCtx = tmpCanvas.getContext('2d');
|
|
63
|
+
var vertSquashRatio = doSquash ? detectVerticalSquash(img, iw, ih) : 1;
|
|
64
|
+
var dw = Math.ceil(d * width / iw);
|
|
65
|
+
var dh = Math.ceil(d * height / ih / vertSquashRatio);
|
|
66
|
+
var sy = 0, dy = 0;
|
|
67
|
+
while (sy < ih) {
|
|
68
|
+
var sx = 0, dx = 0;
|
|
69
|
+
while (sx < iw) {
|
|
70
|
+
tmpCtx.clearRect(0, 0, d, d);
|
|
71
|
+
tmpCtx.drawImage(img, -sx, -sy);
|
|
72
|
+
ctx.drawImage(tmpCanvas, 0, 0, d, d, dx, dy, dw, dh);
|
|
73
|
+
sx += d; dx += dw;
|
|
74
|
+
}
|
|
75
|
+
sy += d; dy += dh;
|
|
76
|
+
}
|
|
77
|
+
ctx.restore();
|
|
78
|
+
tmpCanvas = tmpCtx = null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function transformCoordinate(canvas, ctx, width, height, orientation) {
|
|
82
|
+
switch (orientation) {
|
|
83
|
+
case 5: case 6: case 7: case 8: canvas.width = height; canvas.height = width; break;
|
|
84
|
+
default: canvas.width = width; canvas.height = height;
|
|
85
|
+
}
|
|
86
|
+
switch (orientation) {
|
|
87
|
+
case 2: ctx.translate(width, 0); ctx.scale(-1, 1); break;
|
|
88
|
+
case 3: ctx.translate(width, height); ctx.rotate(Math.PI); break;
|
|
89
|
+
case 4: ctx.translate(0, height); ctx.scale(1, -1); break;
|
|
90
|
+
case 5: ctx.rotate(0.5 * Math.PI); ctx.scale(1, -1); break;
|
|
91
|
+
case 6: ctx.rotate(0.5 * Math.PI); ctx.translate(0, -height); break;
|
|
92
|
+
case 7: ctx.rotate(0.5 * Math.PI); ctx.translate(width, -height); ctx.scale(-1, 1); break;
|
|
93
|
+
case 8: ctx.rotate(-0.5 * Math.PI); ctx.translate(-width, 0); break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
var URL = window.URL && window.URL.createObjectURL ? window.URL :
|
|
98
|
+
window.webkitURL && window.webkitURL.createObjectURL ? window.webkitURL : null;
|
|
99
|
+
|
|
100
|
+
// -------------------------
|
|
101
|
+
// MegaPixImage class
|
|
102
|
+
// -------------------------
|
|
103
|
+
function MegaPixImage(srcImage) {
|
|
104
|
+
if (window.Blob && srcImage instanceof Blob) {
|
|
105
|
+
if (!URL) { throw Error("No createObjectURL function found"); }
|
|
106
|
+
var img = new Image();
|
|
107
|
+
img.src = URL.createObjectURL(srcImage);
|
|
108
|
+
this.blob = srcImage;
|
|
109
|
+
srcImage = img;
|
|
110
|
+
}
|
|
111
|
+
if (!srcImage.naturalWidth && !srcImage.naturalHeight) {
|
|
112
|
+
var _this = this;
|
|
113
|
+
srcImage.onload = srcImage.onerror = function () {
|
|
114
|
+
var listeners = _this.imageLoadListeners;
|
|
115
|
+
if (listeners) {
|
|
116
|
+
_this.imageLoadListeners = null;
|
|
117
|
+
for (var i = 0; i < listeners.length; i++) { listeners[i](); }
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
this.imageLoadListeners = [];
|
|
121
|
+
}
|
|
122
|
+
this.srcImage = srcImage;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
MegaPixImage.prototype.render = function (target, options, callback) {
|
|
126
|
+
if (this.imageLoadListeners) {
|
|
127
|
+
var _this = this;
|
|
128
|
+
this.imageLoadListeners.push(function () { _this.render(target, options, callback); });
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
options = options || {};
|
|
132
|
+
var imgWidth = this.srcImage.naturalWidth, imgHeight = this.srcImage.naturalHeight,
|
|
133
|
+
width = options.width, height = options.height,
|
|
134
|
+
maxWidth = options.maxWidth, maxHeight = options.maxHeight,
|
|
135
|
+
doSquash = !this.blob || this.blob.type === 'image/jpeg';
|
|
136
|
+
if (width && !height) { height = (imgHeight * width / imgWidth) << 0; }
|
|
137
|
+
else if (height && !width) { width = (imgWidth * height / imgHeight) << 0; }
|
|
138
|
+
else { width = imgWidth; height = imgHeight; }
|
|
139
|
+
if (maxWidth && width > maxWidth) { width = maxWidth; height = (imgHeight * width / imgWidth) << 0; }
|
|
140
|
+
if (maxHeight && height > maxHeight) { height = maxHeight; width = (imgWidth * height / imgHeight) << 0; }
|
|
141
|
+
var opt = { width: width, height: height };
|
|
142
|
+
for (var k in options) opt[k] = options[k];
|
|
143
|
+
|
|
144
|
+
var tagName = target.tagName.toLowerCase();
|
|
145
|
+
if (tagName === 'img') { target.src = renderImageToDataURL(this.srcImage, opt, doSquash); }
|
|
146
|
+
else if (tagName === 'canvas') { renderImageToCanvas(this.srcImage, target, opt, doSquash); }
|
|
147
|
+
if (typeof this.onrender === 'function') { this.onrender(target); }
|
|
148
|
+
if (callback) { callback(); }
|
|
149
|
+
if (this.blob) { this.blob = null; URL.revokeObjectURL(this.srcImage.src); }
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// -------------------------
|
|
153
|
+
// Export as ESM default
|
|
154
|
+
// -------------------------
|
|
155
|
+
return MegaPixImage;
|
|
156
|
+
|
|
157
|
+
}));
|