Chris
Chris

Reputation: 771

How to define percentage canvas increase in photoshop javascript

I'm a javascript novice, I have this bit of code in a jsx script that I would like to resize the canvas +20 percent on both horizontal and vertical sides.

resizeCanvas = docRef.resizeCanvas(curWidth + 20, curHeight + 20, AnchorPosition.MIDDLECENTER);

20 refers to whatever unit the ruler is set to. (inches, pixels, centimeters, etc).

What is the proper method of resizing the canvas by 20 PERCENT?

I suspect that I may have to change the units before the resizeCanvas to Units.PERCENT and then back to the default after resizing. To me, that seems to be a lot of typing, is there a better method?

Upvotes: 2

Views: 1693

Answers (2)

Matthew Crumley
Matthew Crumley

Reputation: 102735

I would do it like this:

resizeCanvas = docRef.resizeCanvas(curWidth * 1.2, curHeight * 1.2, AnchorPosition.MIDDLECENTER);

You may need to use Math.round or Math.floor to force the width and height to be integers if the resizeCanvas function doesn't handle that automatically.

Upvotes: 3

Chris
Chris

Reputation: 771

This is what I've come up with, is it the proper method?

var strtRulerUnits = app.preferences.rulerUnits; // store default ruler units

function resizecanvas(){
   app.preferences.rulerUnits = Units.PERCENT; // change units to percent
   docRef.resizeCanvas(curWidth + 20, curHeight + 20, AnchorPosition.MIDDLECENTER, ); // add 20 % to the canvas
   app.preferences.rulerUnits = strtRulerUnits; // restore to default ruler units
   }

resizecanvas ();

Upvotes: 2

Related Questions