forked from fabricjs/fabric.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabric_node.js
128 lines (107 loc) · 3.55 KB
/
fabric_node.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
(function() {
if (typeof document != 'undefined' && typeof window != 'undefined') {
return;
}
var DOMParser = new require('xmldom').DOMParser,
URL = require('url'),
HTTP = require('http'),
Canvas = require('canvas'),
Image = require('canvas').Image;
function request(url, encoding, callback) {
var oURL = URL.parse(url),
client = HTTP.createClient(80, oURL.hostname),
request = client.request('GET', oURL.pathname, { 'host': oURL.hostname });
client.addListener('error', function(err) {
if (err.errno === process.ECONNREFUSED) {
fabric.log('ECONNREFUSED: connection refused to ' + client.host + ':' + client.port);
}
else {
fabric.log(err.message);
}
});
request.end();
request.on('response', function (response) {
var body = "";
if (encoding) {
response.setEncoding(encoding);
}
response.on('end', function () {
callback(body);
});
response.on('data', function (chunk) {
if (response.statusCode == 200) {
body += chunk;
}
});
});
}
fabric.util.loadImage = function(url, callback) {
request(url, 'binary', function(body) {
var img = new Image();
img.src = new Buffer(body, 'binary');
// preserving original url, which seems to be lost in node-canvas
img._src = url;
callback(img);
});
};
fabric.loadSVGFromURL = function(url, callback) {
url = url.replace(/^\n\s*/, '').replace(/\?.*$/, '').trim();
request(url, '', function(body) {
var doc = new DOMParser().parseFromString(body);
fabric.parseSVGDocument(doc.documentElement, function(results, options) {
callback(results, options);
});
});
};
fabric.util.getScript = function(url, callback) {
request(url, '', function(body) {
eval(body);
callback && callback();
});
};
fabric.Image.fromObject = function(object, callback) {
fabric.util.loadImage(object.src, function(img) {
var oImg = new fabric.Image(img);
oImg._initConfig(object);
oImg._initFilters(object);
callback(oImg);
});
};
fabric.createCanvasForNode = function(width, height) {
var canvasEl = fabric.document.createElement('canvas'),
nodeCanvas = new Canvas(width || 600, height || 600);
// jsdom doesn't create style on canvas element, so here be temp. workaround
canvasEl.style = { };
canvasEl.width = nodeCanvas.width;
canvasEl.height = nodeCanvas.height;
var canvas = fabric.Canvas || fabric.StaticCanvas;
var fabricCanvas = new canvas(canvasEl);
fabricCanvas.contextContainer = nodeCanvas.getContext('2d');
fabricCanvas.nodeCanvas = nodeCanvas;
return fabricCanvas;
};
fabric.StaticCanvas.prototype.createPNGStream = function() {
return this.nodeCanvas.createPNGStream();
};
if (fabric.Canvas) {
fabric.Canvas.prototype.createPNGStream
}
var origSetWidth = fabric.StaticCanvas.prototype.setWidth;
fabric.StaticCanvas.prototype.setWidth = function(width) {
origSetWidth.call(this);
this.nodeCanvas.width = width;
return this;
};
if (fabric.Canvas) {
fabric.Canvas.prototype.setWidth = fabric.StaticCanvas.prototype.setWidth;
}
var origSetHeight = fabric.StaticCanvas.prototype.setHeight;
fabric.StaticCanvas.prototype.setHeight = function(height) {
origSetHeight.call(this);
this.nodeCanvas.height = height;
return this;
};
if (fabric.Canvas) {
fabric.Canvas.prototype.setHeight = fabric.StaticCanvas.prototype.setHeight;
}
})();