Skip to content

Instantly share code, notes, and snippets.

@kuy
Created January 19, 2019 08:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuy/e2ab2e4d9cad74d98c78f01b6dd4ba7b to your computer and use it in GitHub Desktop.
Save kuy/e2ab2e4d9cad74d98c78f01b6dd4ba7b to your computer and use it in GitHub Desktop.
// scatter-random-dots.jsx
// https://github.com/kuy
// Copyright(c) 2019 Yuki KODAMA / @kuy
// This script is distributed under the MIT License.
function precondition() {
if (documents.length === 0) {
alert("Please create a new document before running script.");
return false;
}
if (selection.length !== 1 || selection[0].typename !== 'PathItem') {
alert("Please select just 1 rectangle path.");
return false;
}
return true;
}
function createDot(x, y) {
var radius = 2;
var dot = activeDocument.activeLayer.pathItems.ellipse(y + radius, x - radius, radius, radius);
var black = new RGBColor();
black.red = black.green = black.blue = 0;
dot.fillColor = black;
dot.filled = true;
}
function createRandomDot(baseTop, baseLeft, rangeHeight, rangeWidth) {
var x = baseLeft + Math.floor(Math.random() * rangeWidth);
var y = baseTop - Math.floor(Math.random() * rangeHeight);
createDot(x, y);
}
function main() {
var n = 100;
var offset = 10;
var frame = selection[0];
for (var i = 0; i < n; i++) {
createRandomDot(frame.top, frame.left, frame.height - offset, frame.width - offset);
}
}
if (precondition()) {
main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment