svgSetSize | Multi Theft Auto: Wiki Skip to content

svgSetSize

Client-side
Server-side
Shared
Needs checking

This function was partially migrated from the old wiki. Please review manually:

  • Missing section: Contents

This page is incomplete! Help wanted!

Please finish this page using the corresponding Old Wiki article.
Go to Contribution guidelines for more information.


Sets the underlying XML document from an SVG element.

Important

Before r21155 ( 3157905 ) the provided callback wasn't stored on the SVG and was only fired once after the function had performed its task. This is no longer the case - each SVG can now store a single callback function (optional) which is fired every time the SVG texture has been changed/updated.

Syntax

svgSetSize ( )

Code Examples

client

This example creates ansvgelement including a keybind (F2) to resize the SVG randomly, with the use of callbacks to notify in debugscript when the SVG was updated.

-- This could also be a file, with the path provided to svgCreate instead
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 svgs = {}
local function render(svg)
if (not isElement(svg)) or (getElementType(svg) ~= "svg") then
removeEventHandler("onClientRender", root, svgs[svg].handler)
svgs[svg] = nil
end
local width, height = svgGetSize(svg)
dxDrawImage(0, 0, width, height, svg, 0, 0, 0, tocolor(255, 255, 255), false)
end
local function onUpdate(svg)
-- If this is the first update, add svg to our table and start drawing it
if (not svgs[svg]) then
svgs[svg] = {
state = true,
handler = function()
render(svg)
end
}
addEventHandler("onClientRender", root, svgs[svg].handler)
end
iprint("SVG texture updated", svg, getTickCount())
end
local function init()
-- Create an SVG containing a circle, using the raw XML data above
local mySvg = svgCreate(500, 500, rawSvgData, onUpdate)
-- Bind a key to set SVG to a random size, which will trigger the onUpdate callback
bindKey("F2", "down", function()
setRandomSVGSize(mySvg)
end)
end
addEventHandler("onClientResourceStart", resourceRoot, init)
function setRandomSVGSize(svg)
local width, height = svgGetSize(svg)
local diff = math.min(width, height) / math.max(width, height)
local size = math.random(100, 500)
svgSetSize(svg, size, size * diff)
end