diff --git a/README.md b/README.md index e1fff88..d983efe 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ mon_dessin:add(mon_dessin:Rect()) mon_dessin:draw() ``` -result for `print(mon_dessin:draw)` : +result for `print(mon_dessin:draw())` : ```html @@ -37,16 +37,16 @@ can be tested [here](https://www.lua.org/cgi-bin/demo) *(use the minified versio **svg:draw()** *return the svg formated string* -### Elements for svg:add() +### Elements for svg:add(element) **svg:Rect(x, y, width, height, stroke, strokeWidth, fill, rx, ry)** *add a rectangle* - x: number => Horizontal position from top left corner, default to 10 - y: number => Vertical position from top left corner, default to 10 - width: number => Width of the rectangle, default to 10 - height: number => Height of the rectangle, default to 10 - - stroke: string => lines color, default to svg:create() parameter *("#000000")* + - stroke: string => lines color, default to svg:create() parameter ("#000000" by default) - strokeWidth: string => lines width, default to 1 - - fill: string => fill color, default to svg:create() parameter *("transparent")* + - fill: string => fill color, default to svg:create() parameter ("transparent" by default) - rx: number => Horizontal raduis of the corners, default to 0 - ry: number => Vertical radius of the corners, default to 0 @@ -55,6 +55,6 @@ can be tested [here](https://www.lua.org/cgi-bin/demo) *(use the minified versio - r: number => circle radius, default to 25 - cx: number => Horizontal position of the center, default to 30 - cy: number => Vertical position of the center, default to 30 - - stroke: string => lines color, default to svg:create() parameter *("#000000")* + - stroke: string => lines color, default to svg:create() parameter ("#000000" by default) - strokeWidth: string => lines width, default to 1 - - fill: string => fill color, default to svg:create() parameter *("transparent")* \ No newline at end of file + - fill: string => fill color, default to svg:create() parameter ("transparent" by default) \ No newline at end of file diff --git a/svg.lua b/svg.lua index 57112d0..6d895cc 100644 --- a/svg.lua +++ b/svg.lua @@ -96,4 +96,24 @@ function svg:Circle(r, cx, cy, stroke, strokeWidth, fill) ["stroke-width"] = strokeWidth or 1, fill = fill or self.fill }) +end + +-- function to create an ellipse +-- cx: number => Horizontal position of the center +-- cy: number => Vertical position of the center +-- rx: number => Horizontal radius of the ellipse +-- ry: number => Vertical radius of the ellipse +-- stroke: string => lines color +-- strokeWidth: string => lines width +-- fill: string => fill color +function svg:Ellipse(cx, cy, rx, ry, stroke, strokeWidth, fill) + return self.Element:create("ellipse", { + rx = r or 25, + ry = r or 15, + cx = cx or 30, + cy = cy or 30, + stroke = stroke or self.stroke, + ["stroke-width"] = strokeWidth or 1, + fill = fill or self.fill + }) end \ No newline at end of file diff --git a/svg.min.lua b/svg.min.lua index fdc81bd..b923968 100644 --- a/svg.min.lua +++ b/svg.min.lua @@ -1 +1 @@ -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)local h={}setmetatable(h,e.Element)h.name=f;h.strElement="<"..f.." "for i,j in pairs(g)do h[i]=j;h.strElement=h.strElement..i.."=\""..j.."\" "end;h.strElement=h.strElement.."/>"return h end;return e end;function svg:add(k)table.insert(self.elements,k)end;function svg:draw()local l=""for m,n in pairs(self.elements)do l=l..n.strElement end;l=l..""return l end;function svg:Rect(o,p,a,height,c,q,d,r,s)return self.Element:create("rect",{x=o or 10,y=p or 10,width=a or 10,height=height or 10,stroke=c or self.stroke,fill=d or self.fill,["stroke-width"]=q or 1,rx=r or 0,ry=s or 0})end;function svg:Circle(t,u,v,c,q,d)return self.Element:create("circle",{r=t or 25,cx=u or 30,cy=v or 30,stroke=c or self.stroke,["stroke-width"]=q or 1,fill=d or self.fill})end \ No newline at end of file +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)local h={}setmetatable(h,e.Element)h.name=f;h.strElement="<"..f.." "for i,j in pairs(g)do h[i]=j;h.strElement=h.strElement..i.."=\""..j.."\" "end;h.strElement=h.strElement.."/>"return h end;return e end;function svg:add(k)table.insert(self.elements,k)end;function svg:draw()local l=""for m,n in pairs(self.elements)do l=l..n.strElement end;l=l..""return l end;function svg:Rect(o,p,a,height,c,q,d,s,t)return self.Element:create("rect",{x=o or 10,y=p or 10,width=a or 10,height=height or 10,stroke=c or self.stroke,fill=d or self.fill,["stroke-width"]=q or 1,rx=s or 0,ry=t or 0})end;function svg:Circle(r,u,v,c,q,d)return self.Element:create("circle",{r=r or 25,cx=u or 30,cy=v or 30,stroke=c or self.stroke,["stroke-width"]=q or 1,fill=d or self.fill})end;function svg:Ellipse(u,v,s,t,c,q,d)return self.Element:create("ellipse",{rx=r or 25,ry=r or 15,cx=u or 30,cy=v or 30,stroke=c or self.stroke,["stroke-width"]=q or 1,fill=d or self.fill})end \ No newline at end of file diff --git a/test.lua b/test.lua index 13b5b8f..f6dbce3 100644 --- a/test.lua +++ b/test.lua @@ -1,6 +1,7 @@ -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)local h={}setmetatable(h,e.Element)h.name=f;h.strElement="<"..f.." "for i,j in pairs(g)do h[i]=j;h.strElement=h.strElement..i.."=\""..j.."\" "end;h.strElement=h.strElement.."/>"return h end;return e end;function svg:add(k)table.insert(self.elements,k)end;function svg:draw()local l=""for m,n in pairs(self.elements)do l=l..n.strElement end;l=l..""return l end;function svg:Rect(o,p,a,height,c,q,d,r,s)return self.Element:create("rect",{x=o or 10,y=p or 10,width=a or 10,height=height or 10,stroke=c or self.stroke,fill=d or self.fill,["stroke-width"]=q or 1,rx=r or 0,ry=s or 0})end;function svg:Circle(t,u,v,c,q,d)return self.Element:create("circle",{r=t or 25,cx=u or 30,cy=v or 30,stroke=c or self.stroke,["stroke-width"]=q or 1,fill=d or self.fill})end +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)local h={}setmetatable(h,e.Element)h.name=f;h.strElement="<"..f.." "for i,j in pairs(g)do h[i]=j;h.strElement=h.strElement..i.."=\""..j.."\" "end;h.strElement=h.strElement.."/>"return h end;return e end;function svg:add(k)table.insert(self.elements,k)end;function svg:draw()local l=""for m,n in pairs(self.elements)do l=l..n.strElement end;l=l..""return l end;function svg:Rect(o,p,a,height,c,q,d,s,t)return self.Element:create("rect",{x=o or 10,y=p or 10,width=a or 10,height=height or 10,stroke=c or self.stroke,fill=d or self.fill,["stroke-width"]=q or 1,rx=s or 0,ry=t or 0})end;function svg:Circle(r,u,v,c,q,d)return self.Element:create("circle",{r=r or 25,cx=u or 30,cy=v or 30,stroke=c or self.stroke,["stroke-width"]=q or 1,fill=d or self.fill})end;function svg:Ellipse(u,v,s,t,c,q,d)return self.Element:create("ellipse",{rx=r or 25,ry=r or 15,cx=u or 30,cy=v or 30,stroke=c or self.stroke,["stroke-width"]=q or 1,fill=d or self.fill})end mon_dessin = svg:create() mon_dessin:add(mon_dessin:Rect()) mon_dessin:add(mon_dessin:Circle()) +mon_dessin:add(mon_dessin:Ellipse()) print(mon_dessin:draw()) \ No newline at end of file