Sin Waves with paths in Photoshop

By admin on 17 Apr | 0 comments

Here something that I did ages back as a substitute for not getting python to communicate. I went ahead I did it in JavaScript.

I had been wondering how I might do a sin wave path in Photoshop – obviously I had to write some code so after a bit of experimentation this what I came up with in the end, Warning not the prettiest code in the world but it but it works.

// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits
var startTypeUnits = app.preferences.typeUnits
var startDisplayDialogs = app.displayDialogs
// Set Adobe Photoshop CS2 to use pixels and display no dialogs
app.preferences.rulerUnits = Units.PIXELS
app.preferences.typeUnits = TypeUnits.PIXELS
app.displayDialogs = DialogModes.NO
// first close all the open documents
while (app.documents.length) {
app.activeDocument.close()
}
// create a document to work with
 
var Height =800 
var Width = 800
var phase = 100
 
var docRef = app.documents.add(Width, Height, 72, "Simple Line")
//line #1--it’s a straight line so the coordinates for anchor, left, and //right
//for each point have the same coordinates
var ArraySET = new Array()
 
 
for (i=0; i<Width; i++)
{
    var PI = Math.PI;
    var BaseY = 200 
    var AmplitudeY = 100
    
    //Before the y value is calculated the values need to be tranformed to radians and then back
    // in y co-ordinated from -1 to 1
    
    var Co_X = i
    var Co_Y = BaseY+(AmplitudeY*(Math.sin((PI*i)/phase)))
    
    
    ArraySET[i] = new PathPointInfo
    ArraySET[i].kind = PointKind.SMOOTHPOINT
    ArraySET[i].anchor = Array(Co_X, Co_Y)
    ArraySET[i].leftDirection = ArraySET[i].anchor
    ArraySET[i].rightDirection = ArraySET[i].anchor
 
}
 
 
// The data point for the path are define using the ArraySET array and place in the sub path below
var lineSubPathArray = new Array()
lineSubPathArray[0] = new SubPathInfo()
lineSubPathArray[0].operation = ShapeOperation.SHAPEADD
lineSubPathArray[0].closed = false
lineSubPathArray[0].entireSubPath = ArraySET
 
 
 
var myPathItem = docRef.pathItems.add("A Line", lineSubPathArray);
// stroke it so we can see something
myPathItem.strokePath(ToolType.BRUSH)
// Reset the application preferences
preferences.rulerUnits = startRulerUnits;
preferences.typeUnits = startTypeUnits;
displayDialogs = startDisplayDialogs

sinwavesinwave



It may not look all that impressive but now its the easiest way of getting a sine wave in photoshop, I'd image that the same techniques coulds be used for outputting graphs for presentations - hint use linear regression.

Gaussian curve

// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits
var startTypeUnits = app.preferences.typeUnits
var startDisplayDialogs = app.displayDialogs
// Set Adobe Photoshop CS2 to use pixels and display no dialogs
app.preferences.rulerUnits = Units.PIXELS
app.preferences.typeUnits = TypeUnits.PIXELS
app.displayDialogs = DialogModes.NO
// first close all the open documents
while (app.documents.length) {
app.activeDocument.close()
}
// create a document to work with
 
var Height =800 
var Width = 800
var phase = 100
 
var docRef = app.documents.add(Width, Height, 72, "Simple Line")
//line #1--it’s a straight line so the coordinates for anchor, left, and //right
//for each point have the same coordinates
var ArraySET = new Array()
 
 
for (i=0; i<Width; i++)
{
    var PI = Math.PI;
    var BaseY = 200 
    var AmplitudeY = 100
    var sigma = 90
    var average = 400
 
    //Before the y value is calculated the values need to be tranformed to radians and then back
    // in y co-ordinated from -1 to 1
    
    var Co_X = i
    //var Co_Y = BaseY+(AmplitudeY*(Math.sin((PI*i)/phase)))
    
    // Gaussian curve
    var termA =  1/Math.sqrt(2*PI*sigma*sigma)
    var termD = i-average
    var termC = termD*termD/(2*sigma*sigma)
    var termExp = -1*termC
    var termB = Math.exp(termExp)
     
     var termtotal = termA * termB
     
    var Co_Y = (Height-200)- (termA * termB * 50000)
    
    
   
    
    ArraySET[i] = new PathPointInfo
    ArraySET[i].kind = PointKind.SMOOTHPOINT
    ArraySET[i].anchor = Array(Co_X, Co_Y)
    ArraySET[i].leftDirection = ArraySET[i].anchor
    ArraySET[i].rightDirection = ArraySET[i].anchor
 
}
 
 
// The data point for the path are define using the ArraySET array and place in the sub path below
var lineSubPathArray = new Array()
lineSubPathArray[0] = new SubPathInfo()
lineSubPathArray[0].operation = ShapeOperation.SHAPEADD
lineSubPathArray[0].closed = false
lineSubPathArray[0].entireSubPath = ArraySET
 
 
 
var myPathItem = docRef.pathItems.add("A Line", lineSubPathArray);
// stroke it so we can see something
myPathItem.strokePath(ToolType.BRUSH)
// Reset the application preferences
preferences.rulerUnits = startRulerUnits;
preferences.typeUnits = startTypeUnits;
displayDialogs = startDisplayDialogs