···12121313Check the `package.path` documentation for more information about it.
14141515+### normal usage to draw a complete svg
1616+1517```lua
1618mon_dessin = svg:create()
1717-mon_dessin:add(mon_dessin:Rect())
1919+mon_dessin:addRect()
1820mon_dessin:draw()
1921```
2022···2628</svg>
2729```
28303131+You can modify an existing element and add it several times in the same drawing:
3232+3333+```lua
3434+mon_dessin = svg:create()
3535+rectangle = mon_dessin.Rect()
3636+mon_dessin:add(rectangle)
3737+rectangle.x = 20
3838+rectangle.y = 20
3939+mon_dessin.add(rectangle)
4040+mon_dessin.draw()
4141+```
4242+4343+result for `print(mon_dessin:draw())` :
4444+4545+```html
4646+<svg width="100" height="100" fill="transparent" stroke="#000000" version="1.1" xmlns="http://www.w3.org/2000/svg">
4747+ <rect y="10" x="10" stroke-width="1" height="10" width="10" rx="0" ry="0" />
4848+ <rect y="20" x="20" stroke-width="1" height="10" width="10" rx="0" ry="0" />
4949+</svg>
5050+```
5151+5252+### using the lib to generate only elements, without the global `<svg>`
5353+5454+all svg elements are composed with these properties:
5555+5656+- name : the type of element (rect, text, polyline, ...)
5757+- content : if the elements have a content (eg: the text content for `<text></text>`)
5858+- strElement : the complete element, with all the parmaters, rendered as a svg string
5959+- all other option : all the element options that are present in svg (eg: x, y, height, width, ...)
6060+6161+you can use the strElement option to get the svg formated string and use it as you want.
6262+6363+```lua
6464+mon_dessin = svg:create()
6565+rectangle = mon_dessin:Rect()
6666+```
6767+6868+result for `print(rectangle.strElement)` :
6969+7070+```html
7171+<rect y="10" x="10" stroke-width="1" height="10" width="10" rx="0" ry="0" />
7272+```
7373+2974### testing
30753176can be tested [here](https://www.jdoodle.com/execute-lua-online)
···58103 - stroke: string => lines color, default to "#000000"
59104 - fill: string => fill color, default to "transparent"
60105106106+**svg:addText(text, x, y, style, transform)** *add text*
107107+108108+ - text: string => the text to write
109109+ - x: number => Horizontal position, default to 10
110110+ - y: number => Vertical position, default to 50
111111+ - style: string => style of the text (css with svg params), default to:
112112+ * `font-family: Verdana`
113113+ * `font-size: 10`
114114+ * `stroke: self.stroke` svg:create() parameter ("#000000" by default)
115115+ * `fill: self.fill` svg:create() parameter ("transparent" by default)
116116+ - tranform: string => transformation options (eg: rotation)
117117+118118+**svg:addRect(x, y, width, height, stroke, strokeWidth, fill, rx, ry, transform)** *add a rectangle*
119119+120120+ - x: number => Horizontal position from top left corner, default to 10
121121+ - y: number => Vertical position from top left corner, default to 10
122122+ - width: number => Width of the rectangle, default to 10
123123+ - height: number => Height of the rectangle, default to 10
124124+ - stroke: string => lines color, default to svg:create() parameter ("#000000" by default)
125125+ - strokeWidth: string => lines width, default to 1
126126+ - fill: string => fill color, default to svg:create() parameter ("transparent" by default)
127127+ - rx: number => Horizontal raduis of the corners, default to 0
128128+ - ry: number => Vertical radius of the corners, default to 0
129129+ - tranform: string => transformation options (eg: rotation)
130130+131131+**svg:addCircle(r, cx, cy, stroke, strokeWidth, fill, transform)** *add a circle*
132132+133133+ - r: number => circle radius, default to 25
134134+ - cx: number => Horizontal position of the center, default to 30
135135+ - cy: number => Vertical position of the center, default to 30
136136+ - stroke: string => lines color, default to svg:create() parameter ("#000000" by default)
137137+ - strokeWidth: string => lines width, default to 1
138138+ - fill: string => fill color, default to svg:create() parameter ("transparent" by default)
139139+ - tranform: string => transformation options (eg: rotation)
140140+141141+**svg:addEllipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform)** *add an ellipse*
142142+143143+ - cx: number => Horizontal position of the center, default to 30
144144+ - cy: number => Vertical position of the center, default to 30
145145+ - rx: number => Horizontal radius of the ellipse, default to 25
146146+ - ry: number => Vertical radius of the ellipse, default to 15
147147+ - stroke: string => lines color, default to svg:create() parameter ("#000000" by default)
148148+ - strokeWidth: string => lines width, default to 1
149149+ - fill: string => fill color, default to svg:create() parameter ("transparent" by default)
150150+ - tranform: string => transformation options (eg: rotation)
151151+152152+**svg:addLine(x1, y1, x2, y2, stroke, strokeWidth, fill)** *add a line*
153153+154154+ - x1: number => Horizontal position of the center, default to 30
155155+ - y1: number => Vertical position of the center, default to 30
156156+ - x2: number => Horizontal radius of the ellipse, default to 25
157157+ - y2: number => Vertical radius of the ellipse, default to 15
158158+ - stroke: string => lines color, default to svg:create() parameter ("#000000" by default)
159159+ - strokeWidth: string => lines width, default to 1
160160+ - fill: string => fill color, default to svg:create() parameter ("transparent" by default)
161161+162162+**svg:addPolyline(points, stroke, strokeWidth, fill, transform)** *add a polyline*
163163+164164+ - points: string => all the points of the polyline with the standard svg format "x1,y1 x2,y2 x3,y3 ..."
165165+ - stroke: string => lines color
166166+ - strokeWidth: string => lines width
167167+ - fill: string => fill color
168168+ - tranform: string => transformation options (eg: rotation)
169169+170170+**svg:addPath(d, stroke, strokeWidth, fill, transform)** *add a path*
171171+172172+ - d: string => standard svg parameters
173173+ - stroke: string => lines color
174174+ - strokeWidth: string => lines width
175175+ - fill: string => fill color
176176+ - tranform: string => transformation options (eg: rotation)
177177+61178**svg:add(element)** *add an element to the drawing*
6217963180 - element: svg.Element => see farther for element list
6418165182**svg:draw()** *return the svg formated string*
661836767-### Elements for svg:add(element)
184184+### Elements for svg:add(element) or elements rendering only
6818569186**svg:Text(text, x, y, style, transform)** *add text*
70187···128245 - stroke: string => lines color
129246 - strokeWidth: string => lines width
130247 - fill: string => fill color
248248+ - tranform: string => transformation options (eg: rotation)
131249132250**svg:Path(d, stroke, strokeWidth, fill, transform)** *add a path*
133251134252 - d: string => standard svg parameters
135253 - stroke: string => lines color
136254 - strokeWidth: string => lines width
137137- - fill: string => fill color255255+ - fill: string => fill color
256256+ - tranform: string => transformation options (eg: rotation)
···11-svg={}svg.__index=svg;function svg:create(a,b,c,d)local e={}setmetatable(e,svg)e.width=a or 100;e.height=height or 100;e.stroke=c or"#000000"e.fill=d or"transparent"e.elements={}e.Element={}e.Element.__index=svg.Element;function e.Element:create(f,g,h)local i={}setmetatable(i,e.Element)i.name=f;i.strElement="<"..f.." "for j,k in pairs(g)do i[j]=k;i.strElement=i.strElement..j.."=\""..k.."\" "end;if h~=nil then i.strElement=i.strElement..">"..h.."</"..f..">"else i.strElement=i.strElement.."/>"end;return i end;return e end;function svg:add(l)table.insert(self.elements,l)end;function svg:draw()local m="<svg width=\""..self.width.."\" height=\""..self.height.."\" fill=\""..self.fill.."\" stroke=\""..self.stroke.."\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">"for n,o in pairs(self.elements)do m=m..o.strElement end;m=m.."</svg>"return m end;function svg:Text(p,q,s,t,u)return self.Element:create("text",{x=q or 10,y=s or 50,style=t or"font-family: Verdana; font-size: 10; stroke: "..self.stroke.."; fill: "..self.fill..";",transform=u or""},p)end;function svg:Rect(q,s,a,height,c,v,d,w,x,u)return self.Element:create("rect",{x=q or 10,y=s or 10,width=a or 10,height=height or 10,stroke=c or self.stroke,fill=d or self.fill,["stroke-width"]=v or 1,rx=w or 0,ry=x or 0,transform=u or""})end;function svg:Circle(r,cx,cy,c,v,d,u)return self.Element:create("circle",{r=r or 25,cx=cx or 30,cy=cy or 30,stroke=c or self.stroke,["stroke-width"]=v or 1,fill=d or self.fill,transform=u or""})end;function svg:Ellipse(cx,cy,w,x,c,v,d,u)return self.Element:create("ellipse",{rx=r or 25,ry=r or 15,cx=cx or 30,cy=cy or 30,stroke=c or self.stroke,["stroke-width"]=v or 1,fill=d or self.fill,transform=u or""})end;function svg:Line(y,z,A,B,c,v,d)return self.Element:create("line",{x1=r or 15,y1=r or 15,x2=cx or 50,y2=cy or 45,stroke=c or self.stroke,["stroke-width"]=v or 1,fill=d or self.fill})end;function svg:Polyline(C,c,v,d,u)return self.Element:create("polyline",{points=C or"10,10 50,50 50,25 75,25",stroke=c or self.stroke,["stroke-width"]=v or 1,fill=d or self.fill,transform=u or""})end;function svg:Path(D,c,v,d,u)return self.Element:create("path",{d=D or"M50 0 L75 100 L25 100 Z",stroke=c or self.stroke,["stroke-width"]=v or 1,fill=d or self.fill,transform=u or""})end
11+svg={}svg.__index=svg;function svg:create(a,b,c,d)local e={}setmetatable(e,svg)e.width=a or 100;e.height=height or 100;e.stroke=c or"#000000"e.fill=d or"transparent"e.elements={}e.Element={}e.Element.__index=svg.Element;function e.Element:create(f,g,h)local i={}setmetatable(i,e.Element)i.name=f;i.strElement="<"..f.." "for j,k in pairs(g)do i[j]=k;i.strElement=i.strElement..j.."=\""..k.."\" "end;if h~=nil then i.strElement=i.strElement..">"..h.."</"..f..">"else i.strElement=i.strElement.."/>"end;return i end;return e end;function svg:add(l)table.insert(self.elements,l)end;function svg:draw()local m="<svg width=\""..self.width.."\" height=\""..self.height.."\" fill=\""..self.fill.."\" stroke=\""..self.stroke.."\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">"for n,o in pairs(self.elements)do m=m..o.strElement end;m=m.."</svg>"return m end;function svg:Text(p,q,s,t,u)return self.Element:create("text",{x=q or 10,y=s or 50,style=t or"font-family: Verdana; font-size: 10; stroke: "..self.stroke.."; fill: "..self.fill..";",transform=u or""},p)end;function svg:addText(p,q,s,t,u)self:add(self:Text(p,q,s,t,u))end;function svg:Rect(q,s,a,height,c,v,d,w,x,u)return self.Element:create("rect",{x=q or 10,y=s or 10,width=a or 10,height=height or 10,stroke=c or self.stroke,fill=d or self.fill,["stroke-width"]=v or 1,rx=w or 0,ry=x or 0,transform=u or""})end;function svg:addRect(q,s,a,height,c,v,d,w,x,u)self:add(self:Rect(q,s,a,height,c,v,d,w,x,u))end;function svg:Circle(r,cx,cy,c,v,d,u)return self.Element:create("circle",{r=r or 25,cx=cx or 30,cy=cy or 30,stroke=c or self.stroke,["stroke-width"]=v or 1,fill=d or self.fill,transform=u or""})end;function svg:addCircle(r,cx,cy,c,v,d,u)self:add(self:Circle(r,cx,cy,c,v,d,u))end;function svg:Ellipse(cx,cy,w,x,c,v,d,u)return self.Element:create("ellipse",{rx=r or 25,ry=r or 15,cx=cx or 30,cy=cy or 30,stroke=c or self.stroke,["stroke-width"]=v or 1,fill=d or self.fill,transform=u or""})end;function svg:addEllipse(cx,cy,w,x,c,v,d,u)self:add(self:Ellipse(cx,cy,w,x,c,v,d,u))end;function svg:Line(y,z,A,B,c,v,d)return self.Element:create("line",{x1=r or 15,y1=r or 15,x2=cx or 50,y2=cy or 45,stroke=c or self.stroke,["stroke-width"]=v or 1,fill=d or self.fill})end;function svg:addLine(y,z,A,B,c,v,d)self:add(self:Line(y,z,A,B,c,v,d))end;function svg:Polyline(C,c,v,d,u)return self.Element:create("polyline",{points=C or"10,10 50,50 50,25 75,25",stroke=c or self.stroke,["stroke-width"]=v or 1,fill=d or self.fill,transform=u or""})end;function svg:addPolyline(C,c,v,d,u)self:add(self:Polyline(C,c,v,d,u))end;function svg:Path(D,c,v,d,u)return self.Element:create("path",{d=D or"M50 0 L75 100 L25 100 Z",stroke=c or self.stroke,["stroke-width"]=v or 1,fill=d or self.fill,transform=u or""})end;function svg:addPath(D,c,v,d,u)self:add(self:Path(D,c,v,d,u))end
+22
svg.lua
···7474 transform = transform or ""
7575 }, text)
7676end
7777+function svg:addText(text, x, y, style, transform)
7878+ self:add(self:Text(text, x, y, style, transform))
7979+end
77807881-- function to create a rectangle
7982-- x: number => Position du rectangle sur l'axe horizontal par rapport au coin supérieur gauche
···99102 ry = ry or 0,
100103 transform = transform or ""
101104 })
105105+end
106106+function svg:addRect(x, y, width, height, stroke, strokeWidth, fill, rx, ry, transform)
107107+ self:add(self:Rect(x, y, width, height, stroke, strokeWidth, fill, rx, ry, transform))
102108end
103109104110-- function to create a circle
···120126 transform = transform or ""
121127 })
122128end
129129+function svg:addCircle(r, cx, cy, stroke, strokeWidth, fill, transform)
130130+ self:add(self:Circle(r, cx, cy, stroke, strokeWidth, fill, transform))
131131+end
123132124133-- function to create an ellipse
125134-- cx: number => Horizontal position of the center
···142151 transform = transform or ""
143152 })
144153end
154154+function svg:addEllipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform)
155155+ self:add(self:Ellipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform))
156156+end
145157146158-- function ro create a line
147159-- c1: number => Horizontal postion for point 1
···162174 fill = fill or self.fill
163175 })
164176end
177177+function svg:addLine(x1, y1, x2, y2, stroke, strokeWidth, fill)
178178+ self:add(self:Line(x1, y1, x2, y2, stroke, strokeWidth, fill))
179179+end
180180+165181166182-- function ro create a polyline
167183-- points: string => all the points of the polyline with the standard svg format "x1,y1 x2,y2 x3,y3 ..."
···177193 transform = transform or ""
178194 })
179195end
196196+function svg:addPolyline(points, stroke, strokeWidth, fill, transform)
197197+ self:add(self:Polyline(points, stroke, strokeWidth, fill, transform))
198198+end
180199181200-- function ro create a polyline
182201-- points: string => all the points of the polyline with the standard svg format "x1,y1 x2,y2 x3,y3 ..."
···192211 transform = transform or ""
193212 })
194213end
214214+function svg:addPath(d, stroke, strokeWidth, fill, transform)
215215+ self:add(self:Path(d, stroke, strokeWidth, fill, transform))
216216+end
+7-7
test.lua
···77-- TEST IS STARTING HERE -- YOU CAN MODIFY IT --
8899mon_dessin = svg:create()
1010-mon_dessin:add(mon_dessin:Text("Hello World !!"))
1111-mon_dessin:add(mon_dessin:Rect())
1212-mon_dessin:add(mon_dessin:Circle())
1313-mon_dessin:add(mon_dessin:Ellipse())
1414-mon_dessin:add(mon_dessin:Line())
1515-mon_dessin:add(mon_dessin:Polyline())
1616-mon_dessin:add(mon_dessin:Path())
1010+mon_dessin:addText("Hello World !!")
1111+mon_dessin:addRect()
1212+mon_dessin:addCircle()
1313+mon_dessin:addEllipse()
1414+mon_dessin:addLine()
1515+mon_dessin:addPolyline()
1616+mon_dessin:addPath()
1717print(mon_dessin:draw())