跳转到内容

Context 模块

以下方法仅适用于 JavaScript UtilityJavaScript ShapeJavaScript DeformerJavaScript EmitterJavaScript Modifier。此模块中的所有内容都在 ctx 命名空间中,因此方法需要加上 ctx. 前缀,例如 var index = ctx.index;

当前点索引。在使用 Duplicator(或其他生成索引的图层,如 Connect ShapeTrails)时可用。

点的总数。在使用 Duplicator(或其他生成索引的图层,如 Connect ShapeTrails)时可用。

当前点的 X 位置。在使用 Duplicator(或其他生成索引的图层,如 Connect ShapeTrails)时可用。

当前点的 Y 位置。在使用 Duplicator(或其他生成索引的图层,如 Connect ShapeTrails)时可用。

正在计算的属性的 ID。

正在计算的图层的 ID。

saveObject(name:string, objectLiteralToSave:object)

标题为“saveObject(name:string, objectLiteralToSave:object)”的章节

保存对象以供后续使用。此功能的典型用例是创建求解器——例如,需要知道先前值的脚本。

要保存的值必须是对象。

如果要保存路径以供后续使用,请在保存/加载时使用 path.toObject()/ path.fromObject(obj) 方法。

// Simple examplectx.saveObject("test", {"someKey": 10});// A path example// Create a pathvar path = new cavalry.Path();path.moveTo(0.,0.);path.lineTo(0.,-100.);path.lineTo(300.,-100.);// Save itctx.saveObject("path", path.toObject());// Safety check that there is a saved valueif (ctx.hasObject("path")) { // Load the object let previous = ctx.loadObject("path"); // set the path from the object path.fromObject(previous);}

加载已保存的对象。

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

如果已保存名为 name 的对象且可加载,则返回 true。

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

获取请求 JavaScript Utility 提供值的图层的显示名称。

console.log(ctx.niceName);

返回上游本地 2D 变换矩阵,如果没有则返回 null。

/*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);

返回上游全局 2D 变换矩阵,如果没有则返回 null。

/*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);