1
1
import { loopAccess } from "./util" ;
2
2
import { Point , interpolate } from "./point" ;
3
+ import { Xml } from "../xml" ;
3
4
4
5
export interface RenderOptions {
5
6
// Viewport size.
@@ -65,17 +66,50 @@ export const render = (p: Point[], opt: RenderOptions): string => {
65
66
path += `C${ hands . x1 } ,${ hands . y1 } ,${ hands . x2 } ,${ hands . y2 } ,${ point . x } ,${ point . y } ` ;
66
67
}
67
68
69
+ const stroke = opt . stroke || ( opt . guides ? "black" : "none" ) ;
70
+ const strokeWidth = opt . strokeWidth || ( opt . guides ? 1 : 0 ) ;
71
+
72
+ const xmlRoot = new Xml ( "svg" , {
73
+ width : opt . width ,
74
+ height : opt . height ,
75
+ viewBox : `0 0 ${ opt . width } ${ opt . height } ` ,
76
+ xmlns : "http://www.w3.org/2000/svg" ,
77
+ } ) ;
78
+
79
+ const xmlContent = new Xml ( "g" , {
80
+ transform : opt . transform || "" ,
81
+ } ) ;
82
+
83
+ xmlRoot . children . push ( xmlContent ) ;
84
+
85
+ xmlContent . children . push (
86
+ new Xml ( "path" , {
87
+ stroke,
88
+ "stroke-width" : strokeWidth ,
89
+ fill : opt . fill || "none" ,
90
+ d : path ,
91
+ } ) ,
92
+ ) ;
93
+
68
94
// Render guides if configured to do so.
69
- let guides = "" ;
70
95
if ( opt . guides ) {
71
96
const color = opt . stroke || "black" ;
72
97
const size = opt . strokeWidth || 1 ;
73
98
74
99
// Bounding box.
75
100
if ( opt . boundingBox ) {
76
- guides += `
77
- <rect x="0" y="0" width="${ opt . width } " height="${ opt . height } " fill="none"
78
- stroke="${ color } " stroke-width="${ 2 * size } " stroke-dasharray="${ 2 * size } " />` ;
101
+ xmlContent . children . push (
102
+ new Xml ( "rect" , {
103
+ x : 0 ,
104
+ y : 0 ,
105
+ width : opt . width ,
106
+ height : opt . height ,
107
+ fill : "none" ,
108
+ stroke : color ,
109
+ "stroke-width" : 2 * size ,
110
+ "stroke-dasharray" : 2 * size ,
111
+ } ) ,
112
+ ) ;
79
113
}
80
114
81
115
// Points and handles.
@@ -84,38 +118,45 @@ export const render = (p: Point[], opt: RenderOptions): string => {
84
118
const hands = loopAccess ( handles ) ( i ) ;
85
119
const nextPoint = loopAccess ( points ) ( i + 1 ) ;
86
120
87
- guides += `
88
- <line x1="${ x } " y1="${ y } " x2="${ hands . x1 } " y2="${ hands . y1 } "
89
- stroke-width="${ size } " stroke="${ color } " />
90
- <line x1="${ nextPoint . x } " y1="${ nextPoint . y } " x2="${ hands . x2 } " y2="${ hands . y2 } "
91
- stroke-width="${ size } " stroke="${ color } " stroke-dasharray="${ 2 * size } " />
92
- <circle cx="${ hands . x1 } " cy="${ hands . y1 } " r="${ size } "
93
- fill="${ color } " />
94
- <circle cx="${ hands . x2 } " cy="${ hands . y2 } " r="${ size } "
95
- fill="${ color } " />
96
- <circle cx="${ x } " cy="${ y } " r="${ 2 * size } " fill="${ color } " />` ;
121
+ xmlContent . children . push (
122
+ new Xml ( "line" , {
123
+ x1 : x ,
124
+ y1 : y ,
125
+ x2 : hands . x1 ,
126
+ y2 : hands . y1 ,
127
+ "stroke-width" : size ,
128
+ stroke : color ,
129
+ } ) ,
130
+ new Xml ( "line" , {
131
+ x1 : nextPoint . x ,
132
+ y1 : nextPoint . y ,
133
+ x2 : hands . x2 ,
134
+ y2 : hands . y2 ,
135
+ "stroke-width" : size ,
136
+ stroke : color ,
137
+ "stroke-dasharray" : 2 * size ,
138
+ } ) ,
139
+ new Xml ( "circle" , {
140
+ cx : hands . x1 ,
141
+ cy : hands . y1 ,
142
+ r : size ,
143
+ fill : color ,
144
+ } ) ,
145
+ new Xml ( "circle" , {
146
+ cx : hands . x2 ,
147
+ cy : hands . y2 ,
148
+ r : size ,
149
+ fill : color ,
150
+ } ) ,
151
+ new Xml ( "circle" , {
152
+ cx : x ,
153
+ cy : y ,
154
+ r : 2 * size ,
155
+ fill : color ,
156
+ } ) ,
157
+ ) ;
97
158
}
98
159
}
99
160
100
- const stroke = opt . stroke || ( opt . guides ? "black" : "none" ) ;
101
- const strokeWidth = opt . strokeWidth || ( opt . guides ? 1 : 0 ) ;
102
-
103
- return `
104
- <svg
105
- width="${ opt . width } "
106
- height="${ opt . height } "
107
- viewBox="0 0 ${ opt . width } ${ opt . height } "
108
- xmlns="http://www.w3.org/2000/svg"
109
- >
110
- <g transform="${ opt . transform || "" } ">
111
- <path
112
- stroke="${ stroke } "
113
- stroke-width="${ strokeWidth } "
114
- fill="${ opt . fill || "none" } "
115
- d="${ path } "
116
- />
117
- ${ guides }
118
- </g>
119
- </svg>
120
- ` . replace ( / \s + / g, " " ) ;
161
+ return xmlRoot . render ( ) ;
121
162
} ;
0 commit comments