Skip to main content

VectorCompositeBuilder

Class

A mutable builder for a VectorComposite assembled from programmatic fills — each a path painted with a VectorPaint (a solid color or a gradient).

  • Snapchat: not supported
  • SPECS: fully supported
  • Camera Kit Web: not supported
  • Camera Kit Mobile: not supported
  • SnapchatNot supported — Available on SPECS only
  • SPECSFully supported
  • Camera Kit WebNot supported — Available on SPECS only
  • Camera Kit MobileNot supported — Available on SPECS only

Platform details

  • Snapchat — Not supported — Available on SPECS only.

  • SPECS — Fully supported.

  • Camera Kit Web — Not supported — Available on SPECS only.

  • Camera Kit Mobile — Not supported — Available on SPECS only.

Since Lens Scripting v370

A mutable builder for a VectorComposite assembled from programmatic fills — each a path painted with a VectorPaint (a solid color or a gradient). Create one with VectorCompositeBuilder.create, add filled paths with VectorCompositeBuilder#addPath, then call VectorCompositeBuilder#build to produce the composite. The builder remains usable after build(); subsequent addPath calls and another build() produce an independent composite.

Index

Example

var builder = VectorCompositeBuilder.create(256, 256);
var pathBuilder = VectorPathBuilder.create();
pathBuilder.moveTo(new vec2(0, 0));
pathBuilder.lineTo(new vec2(100, 0));
pathBuilder.lineTo(new vec2(50, 100));
var path = pathBuilder.build();
builder.addPath(path, VectorPaint.solid(new vec4(1, 0, 0, 1)), VectorComposite.FillRule.NonZero);
var composite = builder.build();
composite.ready().then(function () {
var renderer = new VectorRenderer();
var target = scene.createRenderTargetTexture();
target.control.outputResolution = RenderTargetProvider.OutputResolution.Custom;
target.control.resolution = new vec2(256, 256);
// render() is cheap to repeat — call it again (e.g. from an UpdateEvent)
// whenever the target size or placement changes.
renderer.render(target, composite, null);
});

Extends

Methods

addPath()

addPath(path, paint, fillRule): void

SPECS

Appends a filled path to the composite. path is an immutable VectorPath; paint is a VectorPaint — a solid color (VectorPaint.solid) or a gradient (VectorPaint.gradient); fillRule is one of VectorComposite.FillRule.NonZero (the SVG default winding rule) or VectorComposite.FillRule.EvenOdd. Both path and paint are referenced as-is — a single VectorPaint may be passed to multiple addPath calls to fill several paths with one paint.

Parameters

ParameterTypeDescription
pathVectorPathThe path geometry to fill.
paintVectorPaintThe paint (solid color or gradient) to fill it with.
fillRuleFillRuleThe fill rule for this path.

Returns

void

Since

Lens Scripting Version 370

Example

var builder = VectorCompositeBuilder.create(512, 512);

// Build a path.
var pathBuilder = VectorPathBuilder.create();
pathBuilder.moveTo(new vec2(0, 0));
pathBuilder.lineTo(new vec2(512, 0));
pathBuilder.lineTo(new vec2(256, 512));
var path = pathBuilder.build();

// Solid fill.
builder.addPath(path, VectorPaint.solid(new vec4(0.92, 0.26, 0.21, 1.0)),
VectorComposite.FillRule.NonZero);

// Gradient fill — build a reusable gradient, then wrap it in a paint.
var gb = VectorGradientBuilder.linear(new vec2(0, 0), new vec2(512, 0));
gb.addColorStop(0.0, new vec4(0.92, 0.26, 0.21, 1.0));
gb.addColorStop(1.0, new vec4(0.13, 0.59, 0.95, 1.0));
builder.addPath(path, VectorPaint.gradient(gb.build()),
VectorComposite.FillRule.NonZero);

var composite = builder.build();

addStroke()

addStroke(path, paint, stroke): void

SPECS

Appends a stroked path to the composite. path is an immutable VectorPath; paint is a VectorPaint — a solid color (VectorPaint.solid) or a gradient (VectorPaint.gradient); stroke is an immutable VectorStroke that carries the stroke style (width, line cap, line join, miter limit, and an optional dash pattern). Both path, paint, and stroke are referenced as-is — a single VectorStroke may be passed to multiple addStroke calls to stroke several paths with one style.

To paint a path with both a fill and a stroke, call VectorCompositeBuilder#addPath followed by addStroke on the same path: the fill (added first) draws under the stroke.

Parameters

ParameterTypeDescription
pathVectorPathThe path geometry to stroke.
paintVectorPaintThe paint (solid color or gradient) to stroke with.
strokeVectorStrokeThe stroke style.

Returns

void

Since

Lens Scripting Version 370

Example

var builder = VectorCompositeBuilder.create(512, 512);

// Build a path.
var pathBuilder = VectorPathBuilder.create();
pathBuilder.moveTo(new vec2(0, 0));
pathBuilder.lineTo(new vec2(512, 0));
pathBuilder.lineTo(new vec2(256, 512));
var path = pathBuilder.build();

// Solid stroke.
var sb = VectorStrokeBuilder.create(6);
sb.setCap(VectorComposite.LineCap.Round);
var stroke = sb.build();
builder.addStroke(path, VectorPaint.solid(new vec4(0.92, 0.26, 0.21, 1.0)), stroke);

addText()

addText(text, paint, position, rotation): void

SPECS

Appends a text run to the composite. text is an immutable VectorText produced by VectorTextBuilder#build; paint is a VectorPaint — only a solid color is supported for text in this version (a gradient paint throws); position is the run's baseline anchor position, in the composite's coordinate space (where it falls relative to the run's glyphs depends on the text's anchor); rotation rotates the run about that position, in degrees, clockwise (0 = axis-aligned).

Parameters

ParameterTypeDescription
textVectorTextThe text run content and style.
paintVectorPaintThe paint to draw the run with (a solid color only).
positionvec2The baseline anchor position, in the composite's coordinate space.
rotationnumberRotation about the baseline position, in degrees, clockwise.

Returns

void

Since

Lens Scripting Version 374

Example

var builder = VectorCompositeBuilder.create(512, 128);

var tb = VectorTextBuilder.create("Hello", 48);
tb.setHorizontalAlignment(HorizontalAlignment.Center);
var text = tb.build();

builder.addText(text, VectorPaint.solid(new vec4(1, 1, 1, 1)), new vec2(256, 64), 0);
var composite = builder.build();

build()

build(): VectorComposite

SPECS

Snapshots the accumulated fills into a new, unregistered VectorComposite and returns it. The composite is not yet built (not ready); call VectorComposite#ready to tessellate it asynchronously, or — in a C++ unit test — call buildRetained() directly. The builder remains usable after build(); further addPath calls and another build() produce an independent composite.

Returns

VectorComposite

Since

Lens Scripting Version 370


create()

static create(width, height): VectorCompositeBuilder

SPECS

Creates a new, empty builder for a composite of the given pixel dimensions. width and height define the coordinate space of the composite and its intrinsic size once built.

Parameters

ParameterTypeDescription
widthnumberCanvas width in pixels (must be > 0).
heightnumberCanvas height in pixels (must be > 0).

Returns

VectorCompositeBuilder

Since

Lens Scripting Version 370


getTypeName()

static getTypeName(): TypeName<VectorCompositeBuilder>

SPECS

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<VectorCompositeBuilder>

Since

Lens Scripting Version 370

Overrides

ScriptObject.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

ScriptObject.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object matches or derives from the passed in type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

ScriptObject.isOfType


isSame()

isSame(other): boolean

Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

ScriptObject.isSame


setFontSource()

setFontSource(fontSource): void

SPECS

Assigns the font source used to resolve VectorCompositeBuilder#addText runs in the composite produced by VectorCompositeBuilder#build — a Font, FontFamily, or FontCollection asset, exactly like VectorComposite#fontSource.

Parameters

ParameterTypeDescription
fontSourceFont | FontFamily | FontCollectionThe font source (a Font, FontFamily, or FontCollection asset, or null).

Returns

void

Since

Lens Scripting Version 374

Example

//@input Asset.Font customFont
var builder = VectorCompositeBuilder.create(512, 128);
builder.setFontSource(script.customFont);