-
Notifications
You must be signed in to change notification settings - Fork 0
/
svgcanvas.js
43 lines (36 loc) · 1.48 KB
/
svgcanvas.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
class SVGCanvas {
constructor(width, height, defaultFill, defaultStroke) {
this.width = width;
this.height = height;
this.context = new SVGContext(height, width, defaultFill, defaultStroke)
}
getContext() {
return this.context;
}
toBuffer() {
return this.context.outStr + "</svg>";
}
}
class SVGContext {
constructor(height, width, defaultFill, defaultStroke) {
this.outStr = `<svg version="1.1" font-size="11.5" stroke="${defaultStroke}" font-family="courier" fill="${defaultFill}" width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg"><style>*{white-space:pre}</style>`;
this.defaultStroke = defaultStroke;
this.defaultFill = defaultFill;
}
rect(x,y,width,height,fill,stroke,strokeWidth) {
this.outStr += `<rect x="${x}" y="${y}" width="${width}" height="${height}"`
if(fill && fill != this.defaultFill) { this.outStr += ` fill="${fill}"`; }
if(stroke && stroke != this.defaultStroke) { this.outStr += ` stroke="${stroke}"`; }
if(strokeWidth) { this.outStr += ` stroke-width="${strokeWidth}"` }
this.outStr += `/>`;
}
fillText(text, x, y, size, fill, stroke) {
text = text.replace(">", ">");
this.outStr += `<text x="${x}" y="${y}"`
if(size && size!=11.5) { this.outStr += ` font-size="${size}"`; }
if(fill && fill != this.defaultFill) { this.outStr += ` fill="${fill}"`; }
if(stroke && stroke != this.defaultStroke) { this.outStr += ` stroke="${stroke}"`; }
this.outStr += `>${text}</text>`;
}
}
exports.SVGCanvas = SVGCanvas;