-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (60 loc) · 2.02 KB
/
index.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
69
70
71
72
73
74
75
76
'use strict';
var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits;
var hat = require('hat');
// leave in for node webkit context
var Image = window.Image;
var Blob = window.Blob;
function Exporter(opts) {
EventEmitter.call(this);
this.exportFormat = opts.format || 'png';
this.exportHeight = opts.height || 150;
this.exportWidth = opts.width || 300;
this.canvasContainer = opts.canvasContainer;
this.canvas = opts.canvas;
this.canvasEl = this.canvasContainer.appendChild(this.canvas);
this.canvasEl.id = hat();
this.canvasEl.width = this.exportWidth;
this.canvasEl.height = this.exportHeight;
this.ctx = this.canvas.getContext('2d');
}
module.exports = Exporter;
inherits(Exporter, EventEmitter);
Exporter.prototype.encode = function (svgString) {
var self = this;
var img = new Image();
var format = this.exportFormat;
var DOMURL = window.URL || window.webkitURL;
var svgBlob = new Blob([svgString], {type: 'image/svg+xml;charset=utf-8'});
var url = DOMURL.createObjectURL(svgBlob);
self.on('ready', function () {
var imgData;
switch (format) {
case 'jpeg':
imgData = self.canvas.toDataURL('image/jpeg');
break;
case 'png':
imgData = self.canvas.toDataURL('image/png');
break;
case 'webp':
imgData = self.canvas.toDataURL('image/webp');
break;
case 'svg':
imgData = svgString;
break;
default:
return self.emit('error', 'Image format is not jpeg, png or svg');
}
return self.emit('success', imgData);
});
img.onload = function() {
DOMURL.revokeObjectURL(url);
self.ctx.drawImage(img, 0, 0);
self.emit('ready');
};
img.onerror = function() {
DOMURL.revokeObjectURL(url);
return self.emit('error', 'Image failed on load');
};
img.src = url;
};