Context 模块
以下方法仅适用于 JavaScript Utility、JavaScript Shape、JavaScript Deformer、JavaScript Emitter 和 JavaScript Modifier。此模块中的所有内容都在 ctx 命名空间中,因此方法需要加上 ctx. 前缀,例如 var index = ctx.index;
成员函数
标题为“成员函数”的章节index → int
标题为“index → int”的章节当前点索引。在使用 Duplicator(或其他生成索引的图层,如 Connect Shape 或 Trails)时可用。
count → int
标题为“count → int”的章节点的总数。在使用 Duplicator(或其他生成索引的图层,如 Connect Shape 或 Trails)时可用。
positionX → number
标题为“positionX → number”的章节当前点的 X 位置。在使用 Duplicator(或其他生成索引的图层,如 Connect Shape 或 Trails)时可用。
positionY → number
标题为“positionY → number”的章节当前点的 Y 位置。在使用 Duplicator(或其他生成索引的图层,如 Connect Shape 或 Trails)时可用。
attributeId → string
标题为“attributeId → string”的章节正在计算的属性的 ID。
layerId → string
标题为“layerId → string”的章节正在计算的图层的 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);}loadObject(name:string) → object
标题为“loadObject(name:string) → object”的章节加载已保存的对象。
ctx.saveObject("test", {"someKey": 10});var textObj = ctx.loadObject("test");console.log(textObj["someKey"])hasObject(name:string) → bool
标题为“hasObject(name:string) → bool”的章节如果已保存名为 name 的对象且可加载,则返回 true。
ctx.saveObject("test", {"someKey": 10});if (ctx.hasObject("test")) { /// do something}niceName() → string
标题为“niceName() → string”的章节获取请求 JavaScript Utility 提供值的图层的显示名称。
console.log(ctx.niceName);transformationMatrix() → matrix
标题为“transformationMatrix() → matrix”的章节返回上游本地 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);globalTransformationMatrix() → matrix
标题为“globalTransformationMatrix() → matrix”的章节返回上游全局 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);