Search code examples
javascriptimage-processingphotoshop-script

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?


Solution

  • 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.