dpzvc-ui 1.2.2 → 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.
@@ -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
+ }));