1 | (function webpackUniversalModuleDefinition(root, factory) {
|
2 | if(typeof exports === 'object' && typeof module === 'object')
|
3 | module.exports = factory();
|
4 | else if(typeof define === 'function' && define.amd)
|
5 | define([], factory);
|
6 | else if(typeof exports === 'object')
|
7 | exports["daycaca"] = factory();
|
8 | else
|
9 | root["daycaca"] = factory();
|
10 | })(typeof self !== 'undefined' ? self : this, function() {
|
11 | return (function(modules) {
|
12 |
|
13 | var installedModules = {};
|
14 |
|
15 |
|
16 | function __webpack_require__(moduleId) {
|
17 |
|
18 |
|
19 | if(installedModules[moduleId]) {
|
20 | return installedModules[moduleId].exports;
|
21 | }
|
22 |
|
23 | var module = installedModules[moduleId] = {
|
24 | i: moduleId,
|
25 | l: false,
|
26 | exports: {}
|
27 | };
|
28 |
|
29 |
|
30 | modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
31 |
|
32 |
|
33 | module.l = true;
|
34 |
|
35 |
|
36 | return module.exports;
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 | __webpack_require__.m = modules;
|
42 |
|
43 |
|
44 | __webpack_require__.c = installedModules;
|
45 |
|
46 |
|
47 | __webpack_require__.d = function(exports, name, getter) {
|
48 | if(!__webpack_require__.o(exports, name)) {
|
49 | Object.defineProperty(exports, name, {
|
50 | configurable: false,
|
51 | enumerable: true,
|
52 | get: getter
|
53 | /******/ });
|
54 | /******/ }
|
55 | /******/ };
|
56 | /******/
|
57 | /******/ // getDefaultExport function for compatibility with non-harmony modules
|
58 | /******/ __webpack_require__.n = function(module) {
|
59 | var getter = module && module.__esModule ?
|
60 | function getDefault() { return module['default']; } :
|
61 | function getModuleExports() { return module; };
|
62 | __webpack_require__.d(getter, 'a', getter);
|
63 | return getter;
|
64 | };
|
65 |
|
66 |
|
67 | __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
68 |
|
69 |
|
70 | __webpack_require__.p = "";
|
71 |
|
72 |
|
73 | return __webpack_require__(__webpack_require__.s = 0);
|
74 | })
|
75 |
|
76 | ([
|
77 |
|
78 | (function(module, exports, __webpack_require__) {
|
79 |
|
80 | "use strict";
|
81 |
|
82 |
|
83 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
84 |
|
85 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
86 |
|
87 |
|
88 |
|
89 | var isNumber = function isNumber(num) {
|
90 | return typeof num === 'number';
|
91 | };
|
92 | var imageReg = /[./](png|jpeg|jpg|gif|bmp)/;
|
93 |
|
94 | var defaultConfig = {
|
95 | ratio: 1,
|
96 | compress: 80,
|
97 | enableWebWorker: false
|
98 | };
|
99 |
|
100 | module.exports = {
|
101 | setConfig: function setConfig(config) {
|
102 | this._config = _extends(defaultConfig, config);
|
103 | },
|
104 |
|
105 |
|
106 | |
107 |
|
108 |
|
109 | init: function init(src, callback) {
|
110 | var _this = this;
|
111 |
|
112 | var scrTypes = src.split(';');
|
113 | var srcType = null;
|
114 | var image = this._createImage(src);
|
115 | if (scrTypes.length > 1) {
|
116 | srcType = scrTypes[0].replace('data:', '');
|
117 | }
|
118 | image.onload = function () {
|
119 | var cvs = _this._getCanvas(image.naturalWidth, image.naturalHeight);
|
120 | var ctx = cvs.getContext('2d');
|
121 | ctx.drawImage(image, 0, 0);
|
122 | var newImageData = cvs.toDataURL(srcType);
|
123 | callback(newImageData);
|
124 | };
|
125 | },
|
126 |
|
127 |
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 | base64: function base64(el, callback) {
|
134 | var _getSrc = this._getSrc(el),
|
135 | src = _getSrc.src,
|
136 | type = _getSrc.type;
|
137 |
|
138 | if (type === 'file') {
|
139 | return this._readFile(src, callback);
|
140 | } else if (type === 'video') {
|
141 | var video = el;
|
142 | var cvs = this._getCanvas(video.videoWidth, video.videoHeight);
|
143 | var ctx = cvs.getContext('2d');
|
144 | ctx.drawImage(video, 0, 0);
|
145 | var newImageData = cvs.toDataURL();
|
146 | callback(newImageData, cvs);
|
147 | }
|
148 | return this.init(src, callback);
|
149 | },
|
150 |
|
151 |
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | compress: function compress(source, quality, callback) {
|
159 | var _this2 = this;
|
160 |
|
161 | var _getSrc2 = this._getSrc(source),
|
162 | src = _getSrc2.src,
|
163 | type = _getSrc2.type;
|
164 |
|
165 | if (type === 'file') {
|
166 | return this._readFile(src, function (data) {
|
167 | _this2._compress(data, source, quality, callback);
|
168 | });
|
169 | }
|
170 | this._compress(src, source, quality, callback);
|
171 | },
|
172 | _compress: function _compress(src, source, quality, callback) {
|
173 | var _this3 = this;
|
174 |
|
175 | this._loadImage(src, function (image) {
|
176 | var mimeType = _this3._getImageType(source);
|
177 | var cvs = _this3._getCanvas(image.naturalWidth, image.naturalHeight);
|
178 | var ctx = cvs.getContext('2d');
|
179 | ctx.drawImage(image, 0, 0);
|
180 | var newImageData = cvs.toDataURL(mimeType, quality / 100);
|
181 | callback(newImageData);
|
182 | });
|
183 | },
|
184 |
|
185 |
|
186 | |
187 |
|
188 |
|
189 | crop: function crop(source, options, callback) {
|
190 | var _this4 = this;
|
191 |
|
192 | var _getSrc3 = this._getSrc(source),
|
193 | src = _getSrc3.src,
|
194 | type = _getSrc3.type;
|
195 |
|
196 | if (type === 'file') {
|
197 | return this._readFile(src, function (data) {
|
198 | _this4._crop(data, source, options, callback);
|
199 | });
|
200 | }
|
201 | this._crop(src, source, options, callback);
|
202 | },
|
203 | _crop: function _crop(src, source, options, callback) {
|
204 | var _this5 = this;
|
205 |
|
206 | this._loadImage(src, function (image) {
|
207 |
|
208 | if (!options.w && options.width) {
|
209 | options.w = options.width;
|
210 | options.h = options.height;
|
211 | }
|
212 |
|
213 |
|
214 | if (isNumber(options.x) && isNumber(options.y) && options.w > 0 && options.h > 0) {
|
215 | var w = options.w,
|
216 | h = options.h;
|
217 |
|
218 | if (options.maxWidth && options.maxWidth < w) {
|
219 | w = options.maxWidth;
|
220 | h = options.h * w / options.w;
|
221 | }
|
222 | if (options.maxHeight && options.maxHeight < h) {
|
223 | h = options.maxHeight;
|
224 | }
|
225 | if (options.fixedWidth && options.fixedHeight) {
|
226 | w = options.fixedWidth;
|
227 | h = options.fixedHeight;
|
228 | }
|
229 | var cvs = _this5._getCanvas(w, h);
|
230 | cvs.getContext('2d').drawImage(image, options.x, options.y, options.w, options.h, 0, 0, w, h);
|
231 | var mimeType = _this5._getImageType(source);
|
232 | var data = cvs.toDataURL(mimeType, options.compress / 100);
|
233 | callback(data);
|
234 | }
|
235 | });
|
236 | },
|
237 | resize: function resize(source, ratio, callback) {
|
238 | var _this6 = this;
|
239 |
|
240 | var _getSrc4 = this._getSrc(source),
|
241 | src = _getSrc4.src,
|
242 | type = _getSrc4.type;
|
243 |
|
244 | var options = {};
|
245 | if (typeof ratio === 'number' || typeof ratio === 'string') {
|
246 | options = {
|
247 | ratio: ratio,
|
248 | compress: defaultConfig.compress
|
249 | };
|
250 | }
|
251 | if ((typeof ratio === 'undefined' ? 'undefined' : _typeof(ratio)) === 'object') {
|
252 | options = ratio;
|
253 | }
|
254 | if (type === 'file') {
|
255 | return this._readFile(src, function (data) {
|
256 | _this6._resize(data, source, options, callback);
|
257 | });
|
258 | }
|
259 | this._resize(src, source, options, callback);
|
260 | },
|
261 | _resize: function _resize(src, source, options, callback) {
|
262 | var _this7 = this;
|
263 |
|
264 | function isNeedCrop(w, h, ow, oh) {
|
265 | return w / h === ow / oh;
|
266 | }
|
267 | this._loadImage(src, function (image) {
|
268 | var w = image.naturalWidth;
|
269 | var h = image.naturalHeight;
|
270 | var aspect = w / h;
|
271 | if (options.ratio > 0) {
|
272 | w = Math.floor(image.naturalWidth * options.ratio);
|
273 | h = Math.floor(image.naturalHeight * options.ratio);
|
274 | } else if (typeof options.width === 'number' && typeof options.height === 'number') {
|
275 | if (!isNeedCrop(options.width, options.height, w, h)) {
|
276 | w = Math.floor(options.width);
|
277 | h = Math.floor(options.height);
|
278 | } else {
|
279 | if (w > options.width || h > options.height) {
|
280 | options.x = (w - options.width) / 2;
|
281 | options.y = (h - options.height) / 2;
|
282 | return _this7._crop(src, source, options, callback);
|
283 | } else if (w < options.width) {
|
284 | w = options.width;
|
285 | h = w / aspect;
|
286 | options.x = 0;
|
287 | options.y = (h - options.height) / 2;
|
288 | return _this7._crop(src, source, options, callback);
|
289 | } else if (h < options.height) {
|
290 | h = options.height;
|
291 | w = h * aspect;
|
292 | options.y = 0;
|
293 | options.x = (w - options.width) / 2;
|
294 | return _this7._crop(src, source, options, callback);
|
295 | }
|
296 | }
|
297 | }
|
298 | var cvs = _this7._getCanvas(w, h);
|
299 | cvs.getContext('2d').drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, 0, 0, w, h);
|
300 | var mimeType = _this7._getImageType(source);
|
301 | var data = cvs.toDataURL(mimeType, options.compress / 100);
|
302 | callback(data);
|
303 | });
|
304 | },
|
305 |
|
306 |
|
307 | |
308 |
|
309 |
|
310 | rotate: function rotate(source, degree, callback) {
|
311 | var _this8 = this;
|
312 |
|
313 | var _getSrc5 = this._getSrc(source),
|
314 | src = _getSrc5.src,
|
315 | type = _getSrc5.type;
|
316 |
|
317 | if (type === 'file') {
|
318 | return this._readFile(src, function () {
|
319 | _this8._rotate(src, source, degree, callback);
|
320 | });
|
321 | }
|
322 | if (degree % 360 === 0) {
|
323 | return callback(src);
|
324 | }
|
325 | this._rotate(src, source, degree, callback);
|
326 | },
|
327 | _rotate: function _rotate(src, source, degree, callback) {
|
328 | var _this9 = this;
|
329 |
|
330 | this._loadImage(src, function (image) {
|
331 | var w = image.naturalWidth;
|
332 | var h = image.naturalHeight;
|
333 | degree %= 360;
|
334 | if (degree === 90 || degree === 270) {
|
335 | w = image.naturalHeight;
|
336 | h = image.naturalWidth;
|
337 | }
|
338 | var cvs = _this9._getCanvas(w, h);
|
339 | var ctx = cvs.getContext('2d');
|
340 | ctx.clearRect(0, 0, w, h);
|
341 | ctx.fillStyle = 'white';
|
342 | ctx.fillRect(0, 0, w, h);
|
343 | ctx.translate(w / 2, h / 2);
|
344 | ctx.rotate(degree * Math.PI / 180);
|
345 | ctx.drawImage(image, -image.naturalWidth / 2, -image.naturalHeight / 2);
|
346 | var mimeType = _this9._getImageType(source);
|
347 | var data = cvs.toDataURL(mimeType, 1);
|
348 | callback(data, w, h);
|
349 | cvs = null;
|
350 | ctx = null;
|
351 | });
|
352 | },
|
353 | _loadImage: function _loadImage(src, callback) {
|
354 | var image = this._createImage(src);
|
355 | image.onload = function () {
|
356 | callback(image);
|
357 | };
|
358 | },
|
359 | _readFile: function _readFile(file, callback) {
|
360 | var reader = new FileReader();
|
361 | reader.onload = function (event) {
|
362 | var data = event.target.result;
|
363 | callback(data);
|
364 | };
|
365 | reader.readAsDataURL(file);
|
366 | },
|
367 | _getCanvas: function _getCanvas(width, height) {
|
368 | var canvas = document.createElement('canvas');
|
369 | canvas.width = width;
|
370 | canvas.height = height;
|
371 | return canvas;
|
372 | },
|
373 | _createImage: function _createImage(src) {
|
374 | var image = new Image();
|
375 | image.src = src;
|
376 | image.crossOrigin = 'anonymous';
|
377 | return image;
|
378 | },
|
379 | _getSrc: function _getSrc(source) {
|
380 | var src = source;
|
381 | var type = 'url';
|
382 | if (this._isImageElement(source)) {
|
383 | var imgSrc = source.src;
|
384 | if (!imgSrc) {
|
385 | throw new Error('Element must hava src');
|
386 | }
|
387 | src = imgSrc;
|
388 | type = 'element';
|
389 | } else if (this._isVideoElement(source)) {
|
390 | src = source;
|
391 | type = 'video';
|
392 | } else if (this._isFileObject(source)) {
|
393 | src = source;
|
394 | type = 'file';
|
395 | }
|
396 | return {
|
397 | src: src,
|
398 | type: type
|
399 | };
|
400 | },
|
401 | _isFileObject: function _isFileObject(file) {
|
402 | return (typeof file === 'undefined' ? 'undefined' : _typeof(file)) === 'object' && file.type && file.size > 0;
|
403 | },
|
404 | _isImageElement: function _isImageElement(el) {
|
405 | return (typeof el === 'undefined' ? 'undefined' : _typeof(el)) === 'object' && el.tagName === 'IMG';
|
406 | },
|
407 | _isVideoElement: function _isVideoElement(el) {
|
408 | return (typeof el === 'undefined' ? 'undefined' : _typeof(el)) === 'object' && el.tagName === 'VIDEO';
|
409 | },
|
410 | _getImageType: function _getImageType(source) {
|
411 | var _getSrc6 = this._getSrc(source),
|
412 | src = _getSrc6.src,
|
413 | type = _getSrc6.type;
|
414 |
|
415 | var mimeType = 'image/jpeg';
|
416 | if (type === 'file') {
|
417 | var fileType = source.type;
|
418 | var outputType = fileType.match(/(image\/[\w]+)\.*/)[0];
|
419 | if (typeof outputType !== 'undefined') {
|
420 | mimeType = outputType;
|
421 | }
|
422 | } else {
|
423 | var arr = imageReg.exec(src);
|
424 | if (arr && arr[1]) {
|
425 | mimeType = 'image/' + arr[1];
|
426 | }
|
427 | }
|
428 | return mimeType;
|
429 | }
|
430 | };
|
431 |
|
432 | })
|
433 | ]);
|
434 | }); |
\ | No newline at end of file |