Skip to main content

Context Module

Pro Feature

Pro features are only available with a Professional licence. To upgrade, visit cavalry.scenegroup.co.

Intro

The following methods are only available to the JavaScript Utility, JavaScript Shape, JavaScript Deformer, JavaScript Emitter and JavaScript Modifier. Everything in this module is in the ctx namespace and so methods need prefixing with ctx. e.g var index = ctx.index;

Member Functions

index

The current point index. Available when using a Duplicator (or other Layer that generates indices, such as the Connect Shape or Trails).

count

The number of points in total. Available when using a Duplicator (or other Layer that generates indices, such as the Connect Shape or Trails).

positionX

The X position of the current point. Available when using a Duplicator (or other Layer that generates indices, such as the Connect Shape or Trails).

positionY

The Y position of the current point. Available when using a Duplicator (or other Layer that generates indices, such as the Connect Shape or Trails).

saveObject(name:string, objectLiteralToSave:object)

Save an object for use later. The normal use case for this feature is when creating solvers – for example, a script that needs to know a previous value.

The value to be saved needs to be an object.

If writing a Path for use later, please use the path.toObject()/ path.fromObject(obj) methods when saving/loading them.

// Simple example
ctx.saveObject("test", {"someKey": 10});

// A path example
// Create a path
var path = new cavalry.Path();
path.moveTo(0.,0.);
path.lineTo(0.,-100.);
path.lineTo(300.,-100.);
// Save it
ctx.saveObject("path", path.toObject());
// Safety check that there is a saved value
if (ctx.hasObject("path")) {
// Load the object
let previous = ctx.loadObject("path");
// set the path from the object
path.fromObject(previous);
}

loadObject(name:string) → object

Load a saved object.

ctx.saveObject("test", {"someKey": 10});
var textObj = ctx.loadObject("test");
console.log(textObj["someKey"])

hasObject(name:string) → bool

Returns true if an object of name has been saved and can be loaded.

ctx.saveObject("test", {"someKey": 10});
if (ctx.hasObject("test")) {
/// do something
}

niceName() → string

Get the nice name of the Layer that requested the JavaScript Utility for a value.

console.log(ctx.niceName);

transformationMatrix() → matrix

Returns the upstream local 2D transformation matrix, or null if there isn't one.

/*
1. Create a Shape.
2. On the Shape's Deformers attribute, click the + button and choose JavaScript Deformer.
3. Copy the below in the JavaScript Deformer's Expression.
*/

var tm = ctx.transformationMatrix();
console.log(tm.position().x);

globalTransformationMatrix() → matrix

Returns the upstream global 2D transformation matrix, or null if there isn't one.

/*
1. Create a Shape.
2. On the Shape's Deformers attribute, click the + button and choose JavaScript Deformer.
3. Copy the below into the JavaScript Deformer's Expression.
4. Group the Shape.
*/

var tm = ctx.globalTransformationMatrix();
console.log(tm.position().x);