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