Two.Path

Extends: Two.Shape

This is the primary primitive class for creating all drawable shapes in Two.js. Unless specified methods return their instance of Two.Path for the purpose of chaining.

Constructor

ArgumentDescription
verticesA list of Two.Anchors that represent the order and coordinates to construct the rendered shape.
closedDescribes whether the shape is closed or open.
curvedDescribes whether the shape automatically calculates bezier handles for each vertex.
manualDescribes whether the developer controls how vertices are plotted or if Two.js automatically plots coordinates based on closed and curved booleans.

Properties

A list of properties that are on every Two.Path.

fromObject

Returns: Two.Path

ArgumentDescription
objObject notation of a Two.Path to create a new instance

Create a new Two.Path from an object notation of a Two.Path.

nota-bene

Works in conjunction with Two.Path.toObject

closed

Determines whether a final line is drawn between the final point in the vertices array and the first point.

curved

When the path is automatic = true this boolean determines whether the lines between the points are curved or not.

beginning

Number between zero and one to state the beginning of where the path is rendered.

Two.Path.beginning is a percentage value that represents at what percentage into the path should the renderer start drawing.

nota-bene

This is great for animating in and out stroked paths in conjunction with Two.Path.ending.

ending

Number between zero and one to state the ending of where the path is rendered.

Two.Path.ending is a percentage value that represents at what percentage into the path the renderer should stop drawing.

nota-bene

This is great for animating in and out stroked paths in conjunction with Two.Path.beginning.

fill

The value of what the path should be filled in with.

See: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value for more information on CSS's colors as String.

stroke

The value of what the path should be outlined in with.

See: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value for more information on CSS's colors as String.

linewidth

The thickness in pixels of the stroke.

opacity

The opaqueness of the path.

nota-bene

Can be used in conjunction with CSS Colors that have an alpha value.

className

A class to be applied to the element to be compatible with CSS styling.

nota-bene

Only rendered to DOM in the SVG renderer.

visible

Display the path or not.

nota-bene

For Two.CanvasRenderer and Two.WebGLRenderer when set to false all updating is disabled improving performance dramatically with many objects in the scene.

vertices

An ordered list of anchor points for rendering the path.

A list of Two.Anchor objects that consist of what form the path takes.

nota-bene

The array when manipulating is actually a Two.Collection.

automatic

Determines whether or not Two.js should calculate curves, lines, and commands automatically for you or to let the developer manipulate them for themselves.

dashes

Array of numbers. Odd indices represent dash length. Even indices represent dash space.

A list of numbers that represent the repeated dash length and dash space applied to the stroke of the path.

See: https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray for more information on the SVG stroke-dasharray attribute.

copy

ArgumentDescription
pathThe reference Two.Path

Copy the properties of one Two.Path onto another.

clone

Returns: Two.Path

ArgumentDescription
parentThe parent group or scene to add the clone to.

Create a new instance of Two.Path with the same properties of the current path.

toObject

Returns: Object

Return a JSON compatible plain object that represents the path.

nota-bene

Works in conjunction with Two.Path.fromObject

dispose

Returns: Two.Path

Release the path's renderer resources and detach all events. This method cleans up vertices collection events, individual vertex events, control point events, and disposes fill/stroke effects (calling dispose() on Gradients and Textures for thorough cleanup) while preserving the renderer type for potential re-attachment to a new renderer.

noFill

Short hand method to set fill to none.

noStroke

Short hand method to set stroke to none.

corner

Orient the vertices of the shape to the upper left-hand corner of the path.

center

Orient the vertices of the shape to the center of the path.

getBoundingClientRect

Returns: Object

  • Returns object with top, left, right, bottom, width, height attributes.
ArgumentDescription
shallowDescribes whether to calculate off local matrix or world matrix.

Return an object with top, left, right, bottom, width, and height parameters of the path.

contains

Returns: Boolean

ArgumentDescription
xx coordinate to hit test against
yy coordinate to hit test against
optionsOptional options object
options.ignoreVisibilityIf true, hit test against path.visible = false shapes
options.tolerancePadding to hit test against in pixels

Check to see if coordinates are within a Two.Path's bounding rectangle

nota-bene

Expects world-space coordinates – the same pixel-space you get from the renderer (e.g., mouse clientX/clientY adjusted for the canvas’s offset and pixel ratio).

getPointAt

Returns: Object

ArgumentDescription
tPercentage value describing where on the Two.Path to estimate and assign coordinate values.
objObject to apply calculated x, y to. If none available returns new Object.

Given a float t from 0 to 1, return a point or assign a passed obj's coordinates to that percentage on this Two.Path's curve.

plot

Based on closed / curved and sorting of vertices plot where all points should be and where the respective handles should be too.

nota-bene

While this method is public it is internally called by Two.Path._update when automatic = true.

smooth

ArgumentDescription
optionsConfiguration for smoothing.
options.typeType of smoothing algorithm.
options.fromIndex of vertices to start smoothing
options.toIndex of vertices to terminate smoothing

Adjust vertex handles to generate smooth curves without toggling automatic.

subdivide

ArgumentDescription
limitHow many times to recurse subdivisions.

Insert a Two.Anchor at the midpoint between every item in Two.Path.vertices.

mask

The shape whose alpha property becomes a clipping area for the path.

nota-bene

This property is currently not working because of SVG spec issues found here {@link https://code.google.com/p/chromium/issues/detail?id=370951}.

clip

Tells Two.js renderer if this object represents a mask for another object (or not).

strokeAttenuation

When set to true, stroke width scales with transformations (default behavior). When false, stroke width remains constant in screen space.

When strokeAttenuation is false, the stroke width is automatically adjusted to compensate for the object's world transform scale, maintaining constant visual thickness regardless of zoom level. When true (default), stroke width scales normally with transformations.