-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvg-generator.js
139 lines (116 loc) · 2.98 KB
/
svg-generator.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
129
130
131
132
133
134
135
136
137
138
139
const fs = require('fs')
const path = require('path')
const singleTags = ['rect', 'circle', 'path']
class Element {
constructor(name) {
this.name = name
this.attributes = {}
this.isSingleTag = false
if (singleTags.indexOf(name) !== -1) {
this.isSingleTag = true
}
}
attr(obj) {
Object.assign(this.attributes, obj)
return this
}
render() {
const { isSingleTag, name } = this
const attrStr = Object.keys(this.attributes).map(key => `${key}="${this.attributes[key]}"`).join(' ')
let result = ''
if (isSingleTag) {
result = [`\<${name} ${attrStr}/>`]
} else {
result = [`\<${name} ${attrStr}>`, `\</${name}>`]
}
return result
}
}
class Node {
constructor(element) {
this.parent = null
this.childs = null
this.value = element
}
appendChild(node) {
if (!this.childs) this.childs = []
this.childs.push(node)
}
appendPath(data) {
const svgPath = new Element('path').attr({
d: data.map((coordinate, index) => {
let xy = ''
if (index === 0) {
xy = "M"
} else if (index === data.length - 1) {
xy = ''
} else {
xy = 'L'
}
xy += coordinate.join(' ')
return xy
}).join(' '),
fill: 'none',
stroke: 'black'
}
);
this.appendChild(new Node(svgPath))
}
}
class SVGGenerator {
createLayout(param1, param2) {
const svg = new Element('svg').attr({ xmlns: "http://www.w3.org/2000/svg" })
if (typeof param1 === 'number' && typeof param2 === 'number') {
svg.attr({ width, height });
}
if (typeof param1 === 'object' && param1 !== null) {
svg.attr(param1)
}
this.root = new Node(svg)
return this
}
getRoot() {
return this.root
}
appendChild(node) {
this.root.appendChild(node)
}
setChilds(nodes) {
this.root.childs = nodes
}
createGroup(nodes) {
const group = new Node(new Element('g'))
nodes.forEach(node => group.appendChild(node))
return group
}
render() {
function traversal(node) {
const currentElement = node.value
const [headTag, closeTag] = currentElement.render()
if (currentElement.isSingleTag) return `${headTag}\r`
let renderedStr = `${headTag}\r`
let childs = node.childs
if (childs) {
for (let i = 0; i < childs.length; i++) {
renderedStr += traversal(childs[i])
}
}
return renderedStr + closeTag + `\r`
}
return traversal(this.root)
}
export(name) {
const headStr = '<?xml version="1.0" encoding="UTF-8"?>\r'
const renderedSVG = headStr + this.render();
const filePath = `${__dirname}/output/${name}.svg`
if (!fs.existsSync(path.dirname(filePath))) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
}
fs.writeFile(filePath, renderedSVG, (err) => {
if (err) throw err;
})
}
}
module.exports = SVGGenerator
module.exports.Element = Element
module.exports.Node = Node