スクリプトチュートリアル:オブジェクトのスワップ
Affinity用の最初のスクリプト。2つのオブジェクトを選択し、スクリプトを実行すると、オブジェクトが配置されます。一度に座って読むには十分な短いですが、ほとんどすべてのスクリプトが行う4つのことについてタッチしています。
スクリプトは現在ベータ版です。Affinityの一部の部分はまだスクリプト化できず、今後のアップデートで追加機能が追加される予定です。
スクリプト全体
JavaScript
'use strict';
// Swaps the positions of two selected objects, keeping their centers.
const { CompoundCommandBuilder, DocumentCommand } = require('/commands.js');
const { Document } = require('/document.js');
const { Transform } = require('/geometry.js');
const { Selection } = require('/selections.js');
function main() {
const doc = Document.current;
if (!doc) {
alert("This script requires an open document");
return;
}
const nodes = doc.selection.nodes.toArray();
if (nodes.length != 2) {
alert("Please select exactly two objects");
return;
}
const [a, b] = nodes.map(n => ({ node: n, c: n.getSpreadBaseBox().centre }));
const builder = CompoundCommandBuilder.create()
.addCommand(
DocumentCommand.createTransform(Selection.create(doc, a.node),
Transform.createTranslate(b.c.x - a.c.x, b.c.y - a.c.y))
)
.addCommand(
DocumentCommand.createTransform(Selection.create(doc, b.node),
Transform.createTranslate(a.c.x - b.c.x, a.c.y - b.c.y))
);
doc.executeCommand(builder.createCommand());
}
main();ステップごと
1.必要なものをインポートする
JavaScript
const { CompoundCommandBuilder, DocumentCommand } = require('/commands.js');
const { Document } = require('/document.js');
const { Transform } = require('/geometry.js');
const { Selection } = require('/selections.js');Affinityのスクリプティングライブラリはモジュールに分割されており、使用する部分のみをプルできます。パスは内蔵ライブラリへ/解決するので、これらの行はどこにでもスクリプトから機能します。
- ドキュメントは、開いたドキュメントでどのように取得するかです。
- 選択範囲はオブジェクトのセットを回り込ませ、コマンドは何を処理するかを知ることができます。
- 変形は移動を構築する。
- DocumentCommandとCompoundCommandBuilderで変更が行われます。詳細については下記をご覧ください。
2.現行ドキュメントを入手
JavaScript
const doc = Document.current;
if (!doc) {
alert("This script requires an open document");
return;
}Document.currentは前面のドキュメントであり、何も開いていない場合はnullです。最初にチェックすると、スクリプトは例外ではなく、わかりやすいメッセージで失敗します。アラートはどこでも利用可能で、シンプルなメッセージボックスが表示されます。
3.選択範囲を読む
JavaScript
const nodes = doc.selection.nodes.toArray();
if (nodes.length != 2) {
alert("Please select exactly two objects");
return;
}ページ上のすべてがノードになります:シェイプ、カーブ、テキスト、グループ、画像。doc.selection.nodesは選択されたものをコレクションとして提供し、 toArray()はそれをプレーンなJavaScript配列に変換し、 length 、 map 、フレンズを使用できるようにします。スワップは2つのオブジェクトだけで意味があるので、他のオブジェクトはここで終わります。
4.各オブジェクトの中心を見つける
JavaScript
const [a, b] = nodes.map(n => ({ node: n, c: n.getSpreadBaseBox().centre }));getSpreadBaseBox()は、オブジェクトの回転やネストに関係なく、ページの共有座標空間であるスプレッド座標でオブジェクトの境界長方形を戻します。長方形には便利な中央ポイントがあります。次のステップで両方を読み取ることができるように、各ノードを中央に並べて配置します。
5. 移動をコマンドとして説明する
JavaScript
const builder = CompoundCommandBuilder.create()
.addCommand(
DocumentCommand.createTransform(Selection.create(doc, a.node),
Transform.createTranslate(b.c.x - a.c.x, b.c.y - a.c.y))
)
.addCommand(
DocumentCommand.createTransform(Selection.create(doc, b.node),
Transform.createTranslate(a.c.x - b.c.x, a.c.y - b.c.y))
);これがAffinityスクリプトの中心です。スクリプトはオブジェクトを直接刺激することはありません。変更を説明するコマンドを作成し、ドキュメントに与えて実行する。それにより、すべてのスクリプトを元に戻すことができ、プレビューが可能になります。
各線は次の3つのことを行います。
- Selection.create(doc, a.node)は1つのオブジェクトを回り込ませ、コマンドがそのターゲットを知るようにします。
- Transform.createTranslate(dx, dy)は2つの中心の差によって移動を作成します。オブジェクトAは「B減算A」に移動し、オブジェクトBは「A減算B」だけ移動します。
- DocumentCommand.createTransform(selection, transform)はこれらをコマンドに変換します。
2つのコマンドはCompoundCommandBuilderに移動し、そこで一つのコマンドにバンドルされます。そうでない場合、スワップは履歴パネルで2つの別々のステップになり、反転するには2つの取り消しが必要になります。
6.実行
JavaScript
doc.executeCommand(builder.createCommand());createCommand()は複合を最終化し、 executeCommandはそれを実行します。オブジェクトが移動し、履歴パネルにエントリが1つ得られ、 ⌘Z (Mac)/ Ctrl + Z (Windows)で元に戻ります。
7.スクリプトの実行
JavaScript
main();上記はすべて、メインの定義のみ。この最後の行がそれを呼び出しています。作業を最上位レベルに書き出すのではなく、関数で囲むことで、初期終了がシンプルになります。これは、ドキュメントがなかったり、オブジェクトの数が正しくない場合に、 メイン からの リターン でスクリプトがきれいに終了するためです。
スクリプトを試す
- 2つのシェイプを少し離れた場所に描画し、スワップが明確になるようにサイズを変更します。
- 両方を選択してスクリプトを実行します。それぞれのシェイプの中央は、他のシェイプが配置されていた場所に配置されます。
- ⌘Z (Mac)/ Ctrl + Z (Windows)を1回押す。複合コマンドのおかげで、両方のオブジェクトが1回のステップで元の位置に戻ります。
スクリプトを拡張するためのアイデア
- 2つ以上のスワップN オブジェクトのリングの周りに位置を回転します。それぞれは次のオブジェクトの中心に移動します。
- 中央の代わりにコーナーを置き換える。.centreを置換.topLeftでは、中央ではなく左上コーナーに揃えることができます。
- サイズも変更します。Transform.createScaleを使用して、各オブジェクトをオブジェクトの幅と高さの比率でスケーリングする。翻訳と結合された。
関連項目:
この記事は役に立ちましたか?