Last active
May 11, 2020 14:37
-
-
Save hiromorozumi/9129657a9e03fdbb240761e873bb2aeb to your computer and use it in GitHub Desktop.
A Photoshop script to automate drawing evenly-spaced spokes. Written in JavaScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use pixels for ruler units | |
var defaultRulerUnits = preferences.rulerUnits | |
preferences.rulerUnits = Units.PIXELS; | |
// get desired inputs from user | |
var spokeRadius = Number(prompt("Spoke radius (in pixels)?", 100)); | |
var colorCode = parseInt("0x" + prompt("Stroke color (in hex, rrggbb)?", "a0a0a0")); | |
var strokeWidth = Number(prompt("Stroke width?", 2)); | |
var nPoints = Number(prompt("How many spokes to draw?", 20)); | |
// create a new document with request size | |
var canvasSize = spokeRadius * 2; | |
if(strokeWidth % 2 == 1) | |
canvasSize += 1; | |
var docRef = app.documents.add(canvasSize, canvasSize, 72.0, "Wheel spoke", NewDocumentMode.RGB, DocumentFill.TRANSPARENT); | |
// set color with provided color code | |
var fillColor = new SolidColor; | |
fillColor.rgb.red = Math.floor(colorCode / 65536); | |
fillColor.rgb.green = Math.floor((colorCode % 65536) / 256); | |
fillColor.rgb.blue = Math.floor(colorCode % 256); | |
// create the first top-to-down "line" as original to copy from | |
var left = (canvasSize * 0.5) - (strokeWidth * 0.5); | |
var right = left + strokeWidth; | |
var top = 0; | |
var bottom = canvasSize * 0.5; | |
var bounds = [ [left, top],[right, top],[right, bottom],[left, bottom] ]; | |
// use "fill" to draw a line | |
docRef.selection.select(bounds, SelectionType.REPLACE, 0, false); | |
docRef.selection.fill(fillColor); | |
// repeatedly duplicate layer (copy line), then rotate by delta degrees | |
var delta = 360.0 / nPoints; | |
var nIterations = nPoints - 1; | |
var layerIndex = 1; | |
var currentLayer = docRef.layers[0]; | |
// first create duplicate layers | |
for(i=0; i<nPoints-1; i++) | |
docRef.layers[0].duplicate(); | |
// rotate each of the duplicated layers | |
for(i=1; i<nPoints; i++) | |
{ | |
currentLayer = docRef.layers[i]; | |
var degree = 360.0 * (i / nPoints); | |
docRef.selection.selectAll(); | |
currentLayer.rotate(degree); | |
} | |
// clear selection and merge all visible layers into one | |
docRef.selection.deselect(); | |
if(nPoints >= 1) | |
docRef.mergeVisibleLayers(); | |
// set units preference to original state | |
preferences.rulerUnits = defaultRulerUnits |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment