From 9ed59f34e981937e6b6734d50e87d832d477844f Mon Sep 17 00:00:00 2001 From: Thomas DAGES Date: Thu, 6 Dec 2018 16:12:30 +0100 Subject: [PATCH] add circle element --- README.md | 21 +++++++++++++++------ svg.lua | 18 ++++++++++++++++++ svg.min.lua | 2 +- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 70f621e..da92c52 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,8 @@ can be tested [here](https://www.lua.org/cgi-bin/demo) *(use the minified versio - width: number => width of the drawing, default to 100 - height: number => height of the drawing, default to 100 - - stroke: string => lines color, default to '#000000' - - fill: string => fill color, default to 'transparent' + - stroke: string => lines color, default to "#000000" + - fill: string => fill color, default to "transparent" **svg:add(element)** *add an element to the drawing* @@ -35,14 +35,23 @@ 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() -**svg:Rect()** *add a rectangle* +**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 '#000000' + - stroke: string => lines color, default to svg:create() parameter *("#000000")* - strokeWidth: string => lines width, default to 1 - - fill: string => fill color, default to transparent + - fill: string => fill color, default to svg:create() parameter *("transparent")* - rx: number => Horizontal raduis of the corners, default to 0 - - ry: number => Vertical radius of the corners, default to 0 \ No newline at end of file + - ry: number => Vertical radius of the corners, default to 0 + +**svg:Circle(r, cx, cy, stroke, strokeWidth, fill)** *add a circle* + + - 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")* + - strokeWidth: string => lines width, default to 1 + - fill: string => fill color, default to svg:create() parameter *("transparent")* \ No newline at end of file diff --git a/svg.lua b/svg.lua index 0a88480..e6d8adf 100644 --- a/svg.lua +++ b/svg.lua @@ -78,4 +78,22 @@ function svg:Rect(x, y, width, height, stroke, strokeWidth, fill, rx, ry) rx = rx or 0, ry = ry or 0 }) +end + +-- function to create a circle +-- r: number => circle radius +-- cx: number => Horizontal position of the center +-- cy: number => Vertical position of the center +-- stroke: string => lines color +-- strokeWidth: string => lines width +-- fill: string => fill color +function svg:Circle(r, cx, cy, stroke, strokeWidth, fill) + return self.Element.create("circle", { + r = r or 25, + 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 2cb51b7..c950f3f 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 \ 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,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 -- 2.51.2