SvgSetSize

From Multi Theft Auto: Wiki
Revision as of 10:33, 28 September 2021 by LopSided (talk | contribs) (Add callback note to example)
Jump to navigation Jump to search

Sets the underlying XML document from an SVG element.

Syntax

bool svgSetSize( svg svgElement, int width, int height [, function callback ( bool didLoad ) ] )

Required Arguments

  • svgElement: The svg element you want to set the size of.
  • width: Desired width, preferably power of two (16, 32, 64 etc.), maximum is 4096
  • height : Desired height, preferably power of two (16, 32, 64 etc.), maximum is 4096

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • callback: A callback function which is invoked upon SVG document and texture creation (after resizing), useful for knowing when the SVG element is ready

Returns

  • Returns true if successful, false otherwise

Example

This example creates an svg element (with callbacks to initialize drawing) including a keybind to resize the svg randomly (see bindKey).

IMPORTANT: Depending on your implementation, callback usage may be necessary to ensure the SVG texture and XML document are ready.

-- 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
function onSvgLoad(svg)
    addEventHandler("onClientRender", root, render)
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