API¶
thorvg.new(path, options)¶
Loads an SVG or Lottie asset through love.filesystem and returns a Document.
Options:
width,height: raster target size; defaults to the asset's intrinsic size.autoplay: start playback immediately; defaults totrue.loop: wrap playback; defaults totrue.speed: playback multiplier; defaults to1.progress: initial normalized progress; defaults to0.filter: LÖVE texture filter (linearornearest); defaults tolinear.frameCache: maximum number of rendered frames retained as GPU textures; defaults to0(disabled). Use a small value for interfaces that revisit frames while scrubbing.threads: ThorVG worker count. The first engine initialization fixes this value for the process;0lets rendering remain on the caller thread.mimeType: override format detection.resourcePath: native resource directory for external file references.assetRoot: virtual-filesystem directory used for relative image and font references; defaults to the document's directory.fonts: table mapping font-family names to virtual-filesystem paths. For desktop SVGs, mapped fonts are preloaded before parsing (requirescapabilities.svgFonts).assetResolver(source, kind): optional custom resolver. It receives the source string and either"picture"or"font", and returns a string or LÖVEDataobject containing the asset bytes. Returningnildeclines it.
Public metadata fields are duration, totalFrames, intrinsicWidth, and
intrinsicHeight.
Document methods¶
update(dt): advances playback and marks a changed frame dirty.play()/pause(): controls automatic playback.setProgress(value): selects normalized animation progress.seek(seconds): selects animation time.resize(width, height): recreates the reusable target only when dimensions change.render(): rasterizes and uploads only when dirty; returns whether work occurred.clearCache(): releases all cached frame textures while keeping the document usable.getCacheStats(): returnsenabled,size,limit,hits,misses, andevictions.getRenderStats(): returns render counts plus the last, total, and average rasterization and upload times in seconds.lastCacheHitidentifies a cache lookup that skipped both stages.resetRenderStats(): resets the render timing counters.draw(x, y, rotation, sx, sy, ox, oy): renders if needed, then draws the texture.getImage(): renders if needed and returns the reusable LÖVEImage.getImageData(): renders if needed and returns the reusable CPU-side LÖVEImageData. This is useful for color picking and other occasional pixel queries. It requiresframeCache = 0; retain neither it nor its pointer after releasing the document.release(): immediately releases native and LÖVE resources; it is idempotent.
Document also has a finalizer, but explicit release() is recommended.
Frame cache¶
The optional cache is a least-recently-used cache of completed LÖVE textures.
It avoids both ThorVG rasterization and texture upload when a previously visited
animation position is requested. Cache keys preserve progress to one thousandth
of an animation frame. Resizing or calling clearCache() releases every cached
texture.
Each entry uses approximately width * height * 4 bytes of GPU memory, plus
driver overhead. For example, eight 256-by-256 frames require about 2 MiB. Keep
the default disabled for animations that move forward without revisiting frames.
Pixel contract¶
The C ABI renders straight-alpha RGBA8 bytes directly into the ImageData
allocation. Image:replacePixels performs the CPU-to-GPU upload. The native API
is declared in include/love_thorvg.h; love_thorvg_abi_version() currently
returns 2.
External assets¶
Relative Lottie images are resolved from the animation's directory through
love.filesystem:
For fonts referenced only by family name, provide an explicit mapping:
local animation = thorvg.new('ui/success.json', {
fonts = {
Inter = 'fonts/Inter-Regular.ttf',
},
})
Use assetResolver for aliases, generated data, archives, or another virtual
asset source. Resolver data is copied by the native layer before the callback
returns.
Desktop SVG element editing (0.3.0)¶
Check thorvg.capabilities.svgElements before using these methods. They allow
an application to run its own animation timeline; they do not interpret CSS.
local x, y, width, height = document:getElementBounds('head')
document:setElement('head', {1, 0, 0, 1, 12, 0}, 0.8)
document:draw(0, 0)
getElementBounds(id) returns the first matching element's axis-aligned bounds
in its parent's coordinate system, including its current local transform but
excluding stroke width (SVG fill-box geometry). Measure resting bounds before editing or transforming the element.
setElement(id, matrix, opacity) replaces the local transform with the SVG
{a,b,c,d,e,f} matrix and sets opacity in [0,1] (default 1). It updates all
matching IDs present in the loaded scene, invalidates cached frames, and marks
the document dirty. The normal uncached path reuses its ImageData and texture.
Missing IDs, nonfinite values, and released documents report errors.
Use a named <g> wrapper for each animated part. ThorVG may discard IDs within
<use> clones or prune initially hidden elements, so inline animated content,
or put the ID on a wrapper around the <use>. Load content visibly and apply
its starting opacity after loading if it must animate from fully transparent.
The experimental web backend reports svgElements = false; these methods are
desktop-only. Lottie playback remains unchanged.