[READ-ONLY] Mirror of https://github.com/thoda-dev/svg-lua. svg lua library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119-- 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, heigth, 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) 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 e.strElement = e.strElement .. "/>" 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 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 rectanglefunction svg:Rect(x, y, width, height, stroke, strokeWidth, fill, rx, ry) return self.Element:create("rect", { x = x or 10, y = y or 10, width = width or 10, height = height or 10, stroke = stroke or self.stroke, fill = fill or self.fill, ["stroke-width"] = strokeWidth or 1, 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 colorfunction 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
-- 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 colorfunction 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