nanosvg.newImage()

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

Overview

Displays an image on the screen containing the rendered SVG.

Syntax

nanosvg.newImage( 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.

x (optional)

Number. The x coordinate of the image on screen.

y (optional)

Number. The y coordinate of the image on screen.

width (optional)

Number. The content width of the image, which is the width within the reference screen of the content.

height (optional)

Number. The content height of the image, which is the height within the reference screen of the content.

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.

Examples

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

local testSvg = nanosvg.newImage(
{
    filename = "test.svg",
    x = display.contentCenterX,
    y = display.contentCenterY,
    width = 100,
    height = 100
})
Create Image From SVG Data
local nanosvg = require( "plugin.nanosvg" )

local circle = nanosvg.newImage(
{
    data = "<svg viewBox='0 0 200 200'><circle cx='100' cy='100' r='100'/></svg>",
    x = display.contentCenterX,
    y = display.contentCenterY,
    width = 100,
    height = 100
})