1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.debounce = debounce;
|
7 | exports.applyTransform = applyTransform;
|
8 | exports.downScaleImage = downScaleImage;
|
9 | exports.getLocale = getLocale;
|
10 |
|
11 | var _zh_CN = require('./locale/zh_CN');
|
12 |
|
13 | var _zh_CN2 = _interopRequireDefault(_zh_CN);
|
14 |
|
15 | var _en_US = require('./locale/en_US');
|
16 |
|
17 | var _en_US2 = _interopRequireDefault(_en_US);
|
18 |
|
19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
20 |
|
21 | function debounce(func, wait) {
|
22 | var immediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
23 |
|
24 | var timeout = void 0;
|
25 | return function debounceFunc() {
|
26 | var context = this;
|
27 | var args = arguments;
|
28 |
|
29 | if (args[0] && args[0].persist) {
|
30 | args[0].persist();
|
31 | }
|
32 | var later = function later() {
|
33 | timeout = null;
|
34 | if (!immediate) {
|
35 | func.apply(context, args);
|
36 | }
|
37 | };
|
38 | var callNow = immediate && !timeout;
|
39 | clearTimeout(timeout);
|
40 | timeout = setTimeout(later, wait);
|
41 | if (callNow) {
|
42 | func.apply(context, args);
|
43 | }
|
44 | };
|
45 | }
|
46 | function getTransformProperty(node) {
|
47 | var properties = ['transform', 'WebkitTransform', 'msTransform', 'MozTransform', 'OTransform'];
|
48 | var p = properties.shift();
|
49 | while (p) {
|
50 | if (typeof node.style[p] !== 'undefined') {
|
51 | return p;
|
52 | }
|
53 | p = properties.shift();
|
54 | }
|
55 | return '';
|
56 | }
|
57 | ;
|
58 | function applyTransform(element, transformString) {
|
59 | var transformProperty = getTransformProperty(element);
|
60 | if (transformProperty) {
|
61 | element.style[transformProperty] = transformString;
|
62 | }
|
63 | }
|
64 |
|
65 | function downScaleImage(img, scale) {
|
66 | var imgCV = document.createElement('canvas');
|
67 | imgCV.width = img.width;
|
68 | imgCV.height = img.height;
|
69 | var imgCtx = imgCV.getContext('2d');
|
70 | imgCtx.drawImage(img, 0, 0);
|
71 | if (scale >= 1) {
|
72 | return imgCV;
|
73 | }
|
74 | return downScaleCanvas(imgCV, scale);
|
75 | }
|
76 |
|
77 | function downScaleCanvas(cv, scale) {
|
78 | if (!(scale < 1) || !(scale > 0)) throw 'scale must be a positive number <1 ';
|
79 | var sqScale = scale * scale;
|
80 | var sw = cv.width;
|
81 | var sh = cv.height;
|
82 | var tw = Math.floor(sw * scale);
|
83 | var th = Math.floor(sh * scale);
|
84 | var sx = 0,
|
85 | sy = 0,
|
86 | sIndex = 0;
|
87 | var tx = 0,
|
88 | ty = 0,
|
89 | yIndex = 0,
|
90 | tIndex = 0;
|
91 | var tX = 0,
|
92 | tY = 0;
|
93 | var w = 0,
|
94 | nw = 0,
|
95 | wx = 0,
|
96 | nwx = 0,
|
97 | wy = 0,
|
98 | nwy = 0;
|
99 |
|
100 |
|
101 | var crossX = false;
|
102 | var crossY = false;
|
103 | var sBuffer = cv.getContext('2d').getImageData(0, 0, sw, sh).data;
|
104 | var tBuffer = new Float32Array(3 * tw * th);
|
105 | var sR = 0,
|
106 | sG = 0,
|
107 | sB = 0;
|
108 | |
109 |
|
110 | for (sy = 0; sy < sh; sy++) {
|
111 | ty = sy * scale;
|
112 | tY = 0 | ty;
|
113 | yIndex = 3 * tY * tw;
|
114 | crossY = tY != (0 | ty + scale);
|
115 | if (crossY) {
|
116 | wy = tY + 1 - ty;
|
117 | nwy = ty + scale - tY - 1;
|
118 | }
|
119 | for (sx = 0; sx < sw; sx++, sIndex += 4) {
|
120 | tx = sx * scale;
|
121 | tX = 0 | tx;
|
122 | tIndex = yIndex + tX * 3;
|
123 | crossX = tX != (0 | tx + scale);
|
124 | if (crossX) {
|
125 | wx = tX + 1 - tx;
|
126 | nwx = tx + scale - tX - 1;
|
127 | }
|
128 | sR = sBuffer[sIndex];
|
129 | sG = sBuffer[sIndex + 1];
|
130 | sB = sBuffer[sIndex + 2];
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | if (!crossX && !crossY) {
|
141 |
|
142 | tBuffer[tIndex] += sR * sqScale;
|
143 | tBuffer[tIndex + 1] += sG * sqScale;
|
144 | tBuffer[tIndex + 2] += sB * sqScale;
|
145 | } else if (crossX && !crossY) {
|
146 | w = wx * scale;
|
147 |
|
148 | tBuffer[tIndex] += sR * w;
|
149 | tBuffer[tIndex + 1] += sG * w;
|
150 | tBuffer[tIndex + 2] += sB * w;
|
151 |
|
152 | nw = nwx * scale;
|
153 | tBuffer[tIndex + 3] += sR * nw;
|
154 | tBuffer[tIndex + 4] += sG * nw;
|
155 | tBuffer[tIndex + 5] += sB * nw;
|
156 | } else if (crossY && !crossX) {
|
157 | w = wy * scale;
|
158 |
|
159 | tBuffer[tIndex] += sR * w;
|
160 | tBuffer[tIndex + 1] += sG * w;
|
161 | tBuffer[tIndex + 2] += sB * w;
|
162 |
|
163 | nw = nwy * scale;
|
164 | tBuffer[tIndex + 3 * tw] += sR * nw;
|
165 | tBuffer[tIndex + 3 * tw + 1] += sG * nw;
|
166 | tBuffer[tIndex + 3 * tw + 2] += sB * nw;
|
167 | } else {
|
168 |
|
169 | w = wx * wy;
|
170 | tBuffer[tIndex] += sR * w;
|
171 | tBuffer[tIndex + 1] += sG * w;
|
172 | tBuffer[tIndex + 2] += sB * w;
|
173 |
|
174 | nw = nwx * wy;
|
175 | tBuffer[tIndex + 3] += sR * nw;
|
176 | tBuffer[tIndex + 4] += sG * nw;
|
177 | tBuffer[tIndex + 5] += sB * nw;
|
178 |
|
179 | nw = wx * nwy;
|
180 | tBuffer[tIndex + 3 * tw] += sR * nw;
|
181 | tBuffer[tIndex + 3 * tw + 1] += sG * nw;
|
182 | tBuffer[tIndex + 3 * tw + 2] += sB * nw;
|
183 |
|
184 | nw = nwx * nwy;
|
185 | tBuffer[tIndex + 3 * tw + 3] += sR * nw;
|
186 | tBuffer[tIndex + 3 * tw + 4] += sG * nw;
|
187 | tBuffer[tIndex + 3 * tw + 5] += sB * nw;
|
188 | }
|
189 | }
|
190 | }
|
191 |
|
192 | var resCV = document.createElement('canvas');
|
193 | resCV.width = tw;
|
194 | resCV.height = th;
|
195 | var resCtx = resCV.getContext('2d');
|
196 | var imgRes = resCtx.getImageData(0, 0, tw, th);
|
197 | var tByteBuffer = imgRes.data;
|
198 |
|
199 | var pxIndex = 0;
|
200 | for (sIndex = 0, tIndex = 0; pxIndex < tw * th; sIndex += 3, tIndex += 4, pxIndex++) {
|
201 | tByteBuffer[tIndex] = Math.ceil(tBuffer[sIndex]);
|
202 | tByteBuffer[tIndex + 1] = Math.ceil(tBuffer[sIndex + 1]);
|
203 | tByteBuffer[tIndex + 2] = Math.ceil(tBuffer[sIndex + 2]);
|
204 | tByteBuffer[tIndex + 3] = 255;
|
205 | }
|
206 |
|
207 | resCtx.putImageData(imgRes, 0, 0);
|
208 | return resCV;
|
209 | }
|
210 |
|
211 | function getLocale(text, locale) {
|
212 | var dict = locale === 'en-US' ? _en_US2["default"] : _zh_CN2["default"];
|
213 | if (dict.hasOwnProperty(text)) {
|
214 | return dict[text];
|
215 | }
|
216 | return text;
|
217 | } |
\ | No newline at end of file |