SvgSetDocumentXML: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Initial page creation)
 
(Update example)
Line 12: Line 12:


==Example==
==Example==
See the example for [[svgCreate]]
<syntaxhighlight lang="lua">
-- This could also be a file, with the path provided to svgCreate
local rawSvgData = [[
    <svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
        <circle cx="250" cy="250" r="250" fill="#0fc0fc" />
    </svg>
]]
 
local svg
 
function init()
    -- Create an SVG containing a circle, using the raw XML data above
    svg = svgCreate(500, 500, rawSvgData, function(didLoad)
        if (not didLoad) then -- SVG failed to load, so don't continue
            return
        end
 
        onSvgLoad(svg)
    end)
end
addEventHandler("onClientResourceStart", resourceRoot, init)
 
 
-- Implement a callback for SVG load, to manipulate the underlying XML document
function onSvgLoad(svg)
    -- Get the XML document from our SVG
    local svgXML = svgGetDocumentXML(svg)
   
    -- Add a rect SVG node, positioned center of the document
    local rect = xmlCreateChild(svgXML, "rect")
    xmlNodeSetAttribute(rect, "x", "25%")
    xmlNodeSetAttribute(rect, "y", "25%")
    xmlNodeSetAttribute(rect, "width", "50%")
    xmlNodeSetAttribute(rect, "height", "50%")
    xmlNodeSetAttribute(rect, "fill", "#00749b")
   
    -- Apply our XML to the SVG, and start drawing via callback
    return svgSetDocumentXML(svg, svgXML, function()
        addEventHandler("onClientRender", root, render)
    end)
end
 
function render()
    local width, height = svgGetSize(svg)
    dxDrawImage(0, 0, width, height, svg, 0, 0, 0, tocolor(255, 255, 255), false)
end
 
-- Add a keybind which sets the SVG to a random size
bindKey("r", "down", function()
    if (not isElement(svg)) or (getElementType(svg) ~= "svg") then
        return false
    end
 
    local size = math.random(100, 500)
    svgSetSize(svg, size, size)
end)
</syntaxhighlight>


==Requirements==
==Requirements==

Revision as of 18:03, 26 September 2021

Sets the underlying XML document of an SVG element.

Syntax

bool svgSetDocumentXML ( svg svgElement, xmlnode xmlDocument [, function callback ( bool didLoad ) ] )

Required Arguments

  • svgElement: The svg element you want to set the XML document of
  • xmlDocument: An xmlnode containing the data to be set on the SVG document

Returns

  • Returns true if successful, false otherwise

Example

-- This could also be a file, with the path provided to svgCreate
local rawSvgData = [[
    <svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
        <circle cx="250" cy="250" r="250" fill="#0fc0fc" />
    </svg>
]]

local svg

function init()
    -- Create an SVG containing a circle, using the raw XML data above
    svg = svgCreate(500, 500, rawSvgData, function(didLoad)
        if (not didLoad) then -- SVG failed to load, so don't continue
            return 
        end

        onSvgLoad(svg)
    end)
end
addEventHandler("onClientResourceStart", resourceRoot, init)


-- Implement a callback for SVG load, to manipulate the underlying XML document
function onSvgLoad(svg)
    -- Get the XML document from our SVG
    local svgXML = svgGetDocumentXML(svg)
    
    -- Add a rect SVG node, positioned center of the document
    local rect = xmlCreateChild(svgXML, "rect")
    xmlNodeSetAttribute(rect, "x", "25%")
    xmlNodeSetAttribute(rect, "y", "25%")
    xmlNodeSetAttribute(rect, "width", "50%")
    xmlNodeSetAttribute(rect, "height", "50%")
    xmlNodeSetAttribute(rect, "fill", "#00749b")
    
    -- Apply our XML to the SVG, and start drawing via callback
    return svgSetDocumentXML(svg, svgXML, function()
        addEventHandler("onClientRender", root, render)
    end)
end

function render()
    local width, height = svgGetSize(svg)
    dxDrawImage(0, 0, width, height, svg, 0, 0, 0, tocolor(255, 255, 255), false)
end

-- Add a keybind which sets the SVG to a random size
bindKey("r", "down", function()
    if (not isElement(svg)) or (getElementType(svg) ~= "svg") then
        return false
    end

    local size = math.random(100, 500)
    svgSetSize(svg, size, size)
end)

Requirements

This template will be deleted.

See Also