UNPKG

8.2 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.debounce = debounce;
7exports.applyTransform = applyTransform;
8exports.downScaleImage = downScaleImage;
9exports.getLocale = getLocale;
10
11var _zh_CN = require('./locale/zh_CN');
12
13var _zh_CN2 = _interopRequireDefault(_zh_CN);
14
15var _en_US = require('./locale/en_US');
16
17var _en_US2 = _interopRequireDefault(_en_US);
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
20
21function 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 // https://fb.me/react-event-pooling
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}
46function 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;
58function applyTransform(element, transformString) {
59 var transformProperty = getTransformProperty(element);
60 if (transformProperty) {
61 element.style[transformProperty] = transformString;
62 }
63}
64// pixel-perfect downsampling
65function 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/* tslint:disable */
77function downScaleCanvas(cv, scale) {
78 if (!(scale < 1) || !(scale > 0)) throw 'scale must be a positive number <1 ';
79 var sqScale = scale * scale; // square scale = area of source pixel within target
80 var sw = cv.width; // source image width
81 var sh = cv.height; // source image height
82 var tw = Math.floor(sw * scale); // target image width
83 var th = Math.floor(sh * scale); // target image height
84 var sx = 0,
85 sy = 0,
86 sIndex = 0; // source x,y, index within source array
87 var tx = 0,
88 ty = 0,
89 yIndex = 0,
90 tIndex = 0; // target x,y, x,y index within target array
91 var tX = 0,
92 tY = 0; // rounded tx, ty
93 var w = 0,
94 nw = 0,
95 wx = 0,
96 nwx = 0,
97 wy = 0,
98 nwy = 0; // weight / next weight x / y
99 // weight is weight of current source point within target.
100 // next weight is weight of current source point within next target's point.
101 var crossX = false; // does scaled px cross its current px right border ?
102 var crossY = false; // does scaled px cross its current px bottom border ?
103 var sBuffer = cv.getContext('2d').getImageData(0, 0, sw, sh).data; // source buffer 8 bit rgba
104 var tBuffer = new Float32Array(3 * tw * th); // target buffer Float32 rgb
105 var sR = 0,
106 sG = 0,
107 sB = 0; // source's current point r,g,b
108 /* untested !
109 var sA = 0; //source alpha */
110 for (sy = 0; sy < sh; sy++) {
111 ty = sy * scale; // y src position within target
112 tY = 0 | ty; // rounded : target pixel's y
113 yIndex = 3 * tY * tw; // line index within target array
114 crossY = tY != (0 | ty + scale);
115 if (crossY) {
116 wy = tY + 1 - ty; // weight of point within target pixel
117 nwy = ty + scale - tY - 1; // ... within y+1 target pixel
118 }
119 for (sx = 0; sx < sw; sx++, sIndex += 4) {
120 tx = sx * scale; // x src position within target
121 tX = 0 | tx; // rounded : target pixel's x
122 tIndex = yIndex + tX * 3; // target pixel index within target array
123 crossX = tX != (0 | tx + scale);
124 if (crossX) {
125 wx = tX + 1 - tx; // weight of point within target pixel
126 nwx = tx + scale - tX - 1; // ... within x+1 target pixel
127 }
128 sR = sBuffer[sIndex]; // retrieving r,g,b for curr src px.
129 sG = sBuffer[sIndex + 1];
130 sB = sBuffer[sIndex + 2];
131 /* !! untested : handling alpha !!
132 sA = sBuffer[sIndex + 3];
133 if (!sA) continue;
134 if (sA != 0xFF) {
135 sR = (sR * sA) >> 8; // or use /256 instead ??
136 sG = (sG * sA) >> 8;
137 sB = (sB * sA) >> 8;
138 }
139 */
140 if (!crossX && !crossY) {
141 // just add components weighted by squared scale.
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 // add weighted component for current px
148 tBuffer[tIndex] += sR * w;
149 tBuffer[tIndex + 1] += sG * w;
150 tBuffer[tIndex + 2] += sB * w;
151 // add weighted component for next (tX+1) px
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 // add weighted component for current px
159 tBuffer[tIndex] += sR * w;
160 tBuffer[tIndex + 1] += sG * w;
161 tBuffer[tIndex + 2] += sB * w;
162 // add weighted component for next (tY+1) px
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 // add weighted component for current px
169 w = wx * wy;
170 tBuffer[tIndex] += sR * w;
171 tBuffer[tIndex + 1] += sG * w;
172 tBuffer[tIndex + 2] += sB * w;
173 // for tX + 1; tY px
174 nw = nwx * wy;
175 tBuffer[tIndex + 3] += sR * nw;
176 tBuffer[tIndex + 4] += sG * nw;
177 tBuffer[tIndex + 5] += sB * nw;
178 // for tX ; tY + 1 px
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 // for tX + 1 ; tY +1 px
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 } // end for sx
190 } // end for sy
191 // create result canvas
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 // convert float32 array into a UInt8Clamped Array
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 // writing result to canvas.
207 resCtx.putImageData(imgRes, 0, 0);
208 return resCV;
209}
210/* tslint:enable */
211function 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