[READ-ONLY] Mirror of https://github.com/thoda-dev/svg-lua. svg lua library
Something went wrong. Try again.
svg-lua svg.lua
10 kB · 294 lines
Lua
at junie-init
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294-- INIT SVG Objectsvg = {}svg.__index = svg;
-- function to create a SVG drawing-- width: number => width of the drawing-- height: number => height of the drawing-- stroke: string => lines color-- fill: string => fill colorfunction svg:create(width, height, stroke, fill) local s = {} setmetatable(s,svg) s.width = width or 100 s.height = height or 100 s.stroke = stroke or "#000000" s.fill = fill or "transparent" s.elements = {}
-- INIT svg.Element Object s.Element = {} s.Element.__index = svg.Element
-- function to create svg.Element -- name: string => name of the element (standard svg element name) -- options: table => a table containing all the parameters of the element function s.Element:create(name, options, content) local e = {} setmetatable(e,s.Element) e.name = name e.strElement = "<" .. name .. " " for key, value in pairs(options) do e[key] = value e.strElement = e.strElement .. key .. "=\"" .. value .. "\" " end if content ~= nil then e.strElement = e.strElement .. ">" .. content .. "</" .. name .. ">" else e.strElement = e.strElement .. "/>" end return e; end
return send
-- function to add an element to the svg drawing-- element : svg.Element => the element to add to the drawingfunction svg:add(element) table.insert(self.elements, element)end
-- function to get the string formatted for the full svg drawingfunction svg:draw() local svgStr = "<svg width=\"" .. self.width .. "\" height=\"" .. self.height .. "\" fill=\"" .. self.fill .. "\" stroke=\"" .. self.stroke .. "\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">" for k,v in pairs(self.elements) do svgStr = svgStr .. v.strElement end svgStr = svgStr .. "</svg>" return svgStrend
-- function to get the string formatted for the svg elements whithout the outer <svg>function svg:innerdraw() local svgStr = "" for k,v in pairs(self.elements) do svgStr = svgStr .. v.strElement end return svgStrend
-- function to add text-- text: string => the text to write-- x: number => Horizontal position -- y: number => Vertical position-- anchor: string => the text-anchor attribute (start, middle or end)-- style: string => style of the text (css with svg params)-- tranform: string => transformation options (eg: rotation)function svg:Text(text, x, y, anchor, style, transform) local options = { x = x or 0, y = y or 0 } if not (anchor == nil) and not (anchor == "") then options["text-anchor"] = anchor end if not (style == nil) and not (style == "") then options.style = style end if not (transform == nil) and not (transform == "") then options.transform = transform end return self.Element:create("text", options, text)endfunction svg:addText(text, x, y, anchor, style, transform) self:add(self:Text(text, x, y, anchor, style, transform))end
-- function to create a rectangle-- x: number => Position du rectangle sur l'axe horizontal par rapport au coin supérieur gauche-- y: number => Position du rectangle sur l'axe vertical par rapport au coin supérieur gauche-- width: number => Largeur du rectangle-- height: number => Hauteur du rectangle.-- stroke: string => lines color-- strokeWidth: string => lines width-- fill: string => fill color-- rx: number => Rayon x des coins du rectangle-- ry: number => Rayon y des coins du rectangle-- transform: string => transformation options (eg: rotation)function svg:Rect(x, y, width, height, stroke, strokeWidth, fill, rx, ry, transform) local options = { x = x or 0, y = y or 0, width = width or 20, height = height or 10 } if not (stroke == nil) and not (stroke == "") then options.stroke = stroke end if not (fill == nil) and not (fill == "") then options.fill = fill end if not (strokeWidth == nil) and not (strokeWidth == "") then options["stroke-width"] = strokeWidth end if not (rx == nil) and not (rx == "") then options.rx = rx end if not (ry == nil) and not (ry == "") then options.ry = ry end if not (transform == nil) and not (transform == "") then options.transform = transform end return self.Element:create("rect", options)endfunction svg:addRect(x, y, width, height, stroke, strokeWidth, fill, rx, ry, transform) self:add(self:Rect(x, y, width, height, stroke, strokeWidth, fill, rx, ry, transform))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-- tranform: string => transformation options (eg: rotation)function svg:Circle(r, cx, cy, stroke, strokeWidth, fill, transform) local options = { r = r or 25, cx = cx or 30, cy = cy or 30 } if not (stroke == nil) and not (stroke == "") then options.stroke = stroke end if not (strokeWidth == nil) and not (strokeWidth == "") then options["stroke-width"] = strokeWidth end if not (fill == nil) and not (fill == "") then options.fill = fill end if not (transform == nil) and not (transform == "") then options.transform = transform end return self.Element:create("circle", options)endfunction svg:addCircle(r, cx, cy, stroke, strokeWidth, fill, transform) self:add(self:Circle(r, cx, cy, stroke, strokeWidth, fill, transform))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-- transform: string => transformation options (eg: rotation)function svg:Ellipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform) local options = { rx = rx or 25, ry = ry or 15, cx = cx or 30, cy = cy or 30 } if not (stroke == nil) and not (stroke == "") then options.stroke = stroke end if not (strokeWidth == nil) and not (strokeWidth == "") then options["stroke-width"] = strokeWidth end if not (fill == nil) and not (fill == "") then options.fill = fill end if not (transform == nil) and not (transform == "") then options.transform = transform end return self.Element:create("ellipse", options)endfunction svg:addEllipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform) self:add(self:Ellipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform))end
-- function ro create a line-- c1: number => Horizontal postion for point 1-- y1: number => Vertical position for point 1-- x2: number => Horizontal position for point 2-- y2: number => Vertical position for point 2-- stroke: string => lines color-- strokeWidth: string => lines width-- fill: string => fill colorfunction svg:Line(x1, y1, x2, y2, stroke, strokeWidth, fill) local options = { x1 = x1 or 15, y1 = y1 or 15, x2 = x2 or 50, y2 = y2 or 45 } if not (stroke == nil) and not (stroke == "") then options.stroke = stroke end if not (strokeWidth == nil) and not (strokeWidth == "") then options["stroke-width"] = strokeWidth end if not (fill == nil) and not (fill == "") then options.fill = fill end return self.Element:create("line", options)endfunction svg:addLine(x1, y1, x2, y2, stroke, strokeWidth, fill) self:add(self:Line(x1, y1, x2, y2, stroke, strokeWidth, fill))end
-- function ro create a polyline-- points: string => all the points of the polyline with the standard svg format "x1,y1 x2,y2 x3,y3 ..."-- stroke: string => lines color-- strokeWidth: string => lines width-- fill: string => fill colorfunction svg:Polyline(points, stroke, strokeWidth, fill, transform) local options = { points = points or "10,10 50,50 50,25 75,25" } if not (stroke == nil) and not (stroke == "") then options.stroke = stroke end if not (strokeWidth == nil) and not (strokeWidth == "") then options["stroke-width"] = strokeWidth end if not (fill == nil) and not (fill == "") then options.fill = fill end if not (transform == nil) and not (transform == "") then options.transform = transform end return self.Element:create("polyline", options)endfunction svg:addPolyline(points, stroke, strokeWidth, fill, transform) self:add(self:Polyline(points, stroke, strokeWidth, fill, transform))end
-- function ro create a polyline-- points: string => all the points of the polyline with the standard svg format "x1,y1 x2,y2 x3,y3 ..."-- stroke: string => lines color-- strokeWidth: string => lines width-- fill: string => fill colorfunction svg:Path(d, stroke, strokeWidth, fill, transform) local options = { d = d or "M50 0 L75 100 L25 100 Z" } if not (stroke == nil) and not (stroke == "") then options.stroke = stroke end if not (strokeWidth == nil) and not (strokeWidth == "") then options["stroke-width"] = strokeWidth end if not (fill == nil) and not (fill == "") then options.fill = fill end if not (transform == nil) and not (transform == "") then options.transform = transform end return self.Element:create("path", options)endfunction svg:addPath(d, stroke, strokeWidth, fill, transform) self:add(self:Path(d, stroke, strokeWidth, fill, transform))end