-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcanvasImageSaver.js
55 lines (49 loc) · 1.79 KB
/
canvasImageSaver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
if (typeof exports === 'object' && typeof module !== 'undefined') {
module.exports = CanvasImageSaver;
} else {
global.CanvasImageSaver = CanvasImageSaver;
}
function CanvasImageSaver (canvas, cropOptions, successCallback, errorCallback, callbackContext) {
var noop = function () {};
this.canvas = canvas;
this.cropOptions = cropOptions;
this.successCallback = successCallback || noop;
this.errorCallback = errorCallback || noop;
this.callbackContext = callbackContext || this;
if (window.cordova) {
this.saverImplementator = new CordovaCanvasSaver();
} else {
this.saverImplementator = new BrowserCanvasSaver();
}
};
CanvasImageSaver.prototype = {
save: function (filename, directory) {
var canvas;
if (this.cropOptions) {
// Sets default crop options
this.cropOptions.xCropOffset = this.cropOptions.xCropOffset || 0;
this.cropOptions.yCropOffset = this.cropOptions.yCropOffset || 0;
this.cropOptions.width = this.cropOptions.width || this.canvas.width - this.cropOptions.xCropOffset;
this.cropOptions.height = this.cropOptions.height || this.canvas.height - this.cropOptions.yCropOffset;
// Creates temporal canvas to draw cropped image
canvas = document.createElement('canvas');
canvas.width = this.cropOptions.width;
canvas.height = this.cropOptions.height;
canvas.getContext('2d').drawImage(this.canvas,
this.cropOptions.xCropOffset, this.cropOptions.yCropOffset,
canvas.width, canvas.height,
0, 0,
canvas.width, canvas.height
);
} else {
canvas = this.canvas;
}
return this.saverImplementator.save(
canvas,
this.successCallback.bind(this.callbackContext),
this.errorCallback.bind(this.callbackContext),
filename,
directory
);
}
};