-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas2image.js
68 lines (64 loc) · 2.11 KB
/
canvas2image.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
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @file convert canvas to image
* @auther https://github.com/7inspire/canvas2image
*/
export default {
/**
* canvas缩放
*
* @param {Object} canvas canvas数据
* @param {Number} width 缩放后的宽度
* @return {Number} height 缩放后的高度
*/
resizeCanvas(canvas, width, height) {
let w = canvas.width;
let h = canvas.height;
width = width || w;
height = height || w;
let retCanvas = document.createElement('canvas');
let retCtx = retCanvas.getContext('2d');
retCanvas.setAttribute('width', width);
retCanvas.setAttribute('height', height);
retCanvas.setAttribute('style', 'width: 100%');
retCtx.drawImage(canvas, 0, 0, w, h, 0, 0, width, height);
return retCanvas;
},
/**
* 触发保存文件
*
* @param {Object} data 图片data URI
* @param {String} fileName 文件名
*/
saveFile(data, fileName) {
let saveLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
saveLink.href = data;
saveLink.download = fileName;
let event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
saveLink.dispatchEvent(event);
},
getDataURL(canvas) {
return canvas.toDataURL(canvas);
},
fixType(type) {
type = type.toLowerCase().replace(/jpg/i, 'jpeg');
let r = type.match(/png|jpeg|bmp|gif/)[0];
return 'image/' + r;
},
/**
* 将mime-type改为image/octet-stream,强制让浏览器直接download
*
* @param {Object} data 图片data URI
* @param {String} 图片格式
*/
changeMimeType(data, type = 'png') {
data = data.replace(this.fixType(type), 'image/octet-stream');
return data;
},
download(canvas, type = 'png') {
let data = this.getDataURL(canvas);
data = this.changeMimeType(data, type);
let fileName = 'baidu_' + (new Date()).getTime() + '.' + type;
this.saveFile(data, fileName);
}
};