nanosvg.newTexture()

Type Function
Return value TextureResourceExternal
Revision Release 2022.3683
Keywords SVG, nanosvg, newTexture
See also nanosvg.newImage()
nanosvg.*

Overview

Creates a texture instance (TextureResourceExternal) containing the rasterized SVG.

Created textures are subject to manual texture memory management. See the Texture Loading/Management guide for more information.

Syntax

nanosvg.newTexture( params )
params (required)

Table. Table containing the required parameters for the new object — see the next section for details.

Parameter Reference

Important

At least one data source — filename, filePath, or data must be specified to render the SVG.

filename (optional)

String. Indicates the name of the SVG file to load, relative to baseDir.

baseDir (optional)

Constant. Specifies the base directory where filename is located. Options include system.ResourceDirectory, system.DocumentsDirectory, system.ApplicationSupportDirectory, system.TemporaryDirectory and system.CachesDirectory. Default is system.ResourceDirectory.

filePath (optional)

String. Indicates the full file path of the SVG file to load.

data (optional)

String. Contains XML data of the SVG to be rasterized.

pixelWidth (optional)

Number. Specifies the horizontal pixel dimensions of the texture that the SVG is rendered to. If this is omitted, the texture width will be deduced from the SVG width/height ratio.

pixelHeight (optional)

Number. Specifies the vertical pixel dimensions of the texture that the SVG is rendered to. If this is omitted, the texture height will be deduced from the SVG width/height ratio.

scale (optional)

String. Specifies how the rasterized SVG should fit into the texture. This only applies if both pixelWidth and pixelHeight are specified. Options include:

  • "letterbox" — This option (default) fits the entire rasterized SVG into the texture width/height, leaving letterbox regions transparent.
  • "zoomEven" — This option fills the texture width/height with the rasterized SVG so that its smaller dimension fits into the texture.
scaleOffset (optional)

Number. Specifies how a scaled rasterized SVG will be positioned within the texture width/height. A value of 0.5 (default) will position it in the middle of the texture; 0 to the top or left; 1 to the bottom or right.

dpi (optional)

Number. Number for transforming between different SVG units (pixels, points, cm, mm, etc.). Default value is 96.

Example

Create Texture and Image From SVG File
local nanosvg = require( "plugin.nanosvg" )

local tex = nanosvg.newTexture(
{
    filename = "test.svg",
})

if tex then
    local testSvg = display.newImage( tex.filename, tex.baseDir, display.contentCenterX, display.contentCenterY )
    tex:releaseSelf()
else
    print( "Error rendering SVG" )
end