Say you’re drawing a set of sprites for a game. They look fine on a blank canvas, but they’re hard to see against the backgrounds in the game. You start outlining each sprite by hand and realize you’re doing the same edit over and over.
A custom JavaScript action can do that work for you. In this guide, you’ll write an action that adds a one-pixel outline around the sprite on your Pixen canvas. The outline goes on its own layer so you can turn it on and off as you work.
- Open a sprite in Pixen with at least one pixel of transparent space around the artwork. If a solid background layer is visible, hide it first.
- Choose File → Actions → Show Script Editor from the menu bar. Click New Script in the toolbar and paste the example below.
- Click the sprite window to make it the active document. Return to the editor and click Run Script in its toolbar.
- Look for the Outline layer in the Layers panel. Toggle its visibility to see the difference.
- Right-click the script in the editor’s sidebar and choose Rename. Give it a name such as Outline Sprite. Choose Edit Keyboard Shortcut from the same menu if you want to run it with a shortcut.
function main(document) {
const canvas = document.canvas;
const { width, height } = canvas.size;
const neighbors = [
[-1, 0], [1, 0], [0, -1], [0, 1]
];
const outlinePixels = [];
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (canvas.getPixel(x, y).a > 0) continue;
const touchesArt = neighbors.some(([dx, dy]) => {
const pixel = canvas.getPixel(x + dx, y + dy);
return pixel?.a > 0;
});
if (touchesArt) outlinePixels.push([x, y]);
}
}
if (outlinePixels.length === 0) return;
const outline = canvas.addLayer();
outline.name = "Outline";
const color = Color.fromHex("#000000");
for (const [x, y] of outlinePixels) {
outline.setPixel(x, y, color);
}
}
The script finds transparent pixels that share an edge with the artwork, then colors them on the new layer. To try a different outline color on the same sprite, choose Edit → Undo, change the hex code in Color.fromHex, and run the action again. See the scripting reference for other edits you can automate.