Copy link to clipboard
Copied
Hello,
I am trying to find a way on how to scale an object horizontally without dependency after its rotation is done. For example, if the object is square and apply horizontal scale without rotation it will become a rectangle, and after applying rotation on the rectangle by 90°, the horizontal scaling now will become a vertical one and will continue to scale up or down the same sides of the rectangles independently from the rotation applied before. This is the same as when creating a shape and using the transform properties to change the width or height of that object, and no matter how the object is rotated, the x or y coordinate for scaling is the same.
Here is the code where the scaling and rotation is applied:
dialog = new Window("dialog");
dialog.text = "test";
group1 = dialog.add("group");
group1.orientation = "row";
group1.alignChildren = ["left","center"];
statictext1 = group1.add("statictext"); statictext1.text = "ScaleH:";
slider1 = group1.add("slider {minvalue: 0, maxvalue: 300, value: 100}");
slider1.preferredSize.width = 200;
ts = group1.add("edittext"); ts.text = "100";
ts.preferredSize.width = 40;
statictext2 = group1.add("statictext");statictext2.text = "%";
group2 = dialog.add("group");
statictext3 = group2.add("statictext"); statictext3.text = "Rotate:";
slider2 = group2.add("slider {minvalue: -180, maxvalue: 180, value: 0}");
slider2.preferredSize.width = 200;
tr = group2.add("edittext"); tr.text = "0";
tr.preferredSize.width = 40;
statictext4 = group2.add("statictext"); statictext4.text = "º";
buttonOK = dialog.add("button"); buttonOK.text = "Done";
buttonOK.preferredSize.width = 80;
buttonCancel = dialog.add("button"); buttonCancel.text = "Cancel";
buttonCancel.preferredSize.width = 80;
var doc = app.activeDocument;
var currentStatus = doc.activeHistoryState;
slider1.onChange = function () {
ts.text = Math.round(slider1.value );
doc.activeHistoryState = currentStatus;
app.activeDocument.suspendHistory ("scaled", "scaleH()");
app.refresh();
}
ts.onChange = function () {
slider1.value = Number(ts.text);
doc.activeHistoryState = currentStatus;
app.activeDocument.suspendHistory ("scaled", "scaleH()");
app.refresh();
}
slider2.onChange = function () {
tr.text = Math.round(slider2.value );
doc.activeHistoryState = currentStatus;
app.activeDocument.suspendHistory ("rotated", "rotate()");
app.refresh();
}
tr.onChange = function () {
slider2.value = Number(tr.text);
doc.activeHistoryState = currentStatus;
app.activeDocument.suspendHistory ("rotated", "rotate()");
app.refresh();
}
function scaleH(){
activeDocument.activeLayer.resize(slider1.value, undefined, AnchorPosition.MIDDLECENTER);
with (activeDocument.activeLayer) {rotate(slider2.value)};
}
function rotate(){
activeDocument.activeLayer.resize(slider1.value, slider1.value, AnchorPosition.MIDDLECENTER);
with (activeDocument.activeLayer) {rotate(slider2.value)};
}
buttonOK.onClick = function(){
doc.activeHistoryState = doc.historyStates['rotate'];
(ref1 = new ActionReference()).putProperty(charIDToTypeID('HstS'), charIDToTypeID('CrnH'));
(desc1 = new ActionDescriptor()).putReference(charIDToTypeID('null'), ref1);
executeAction(charIDToTypeID('Dlt '), desc1, DialogModes.NO);
app.activeDocument.suspendHistory ("rotated", "rotate()");
dialog.close();
}
buttonCancel.onClick = function(){
doc.activeHistoryState = doc.historyStates['rotate'];
(ref1 = new ActionReference()).putProperty(charIDToTypeID('HstS'), charIDToTypeID('CrnH'));
(desc1 = new ActionDescriptor()).putReference(charIDToTypeID('null'), ref1);
executeAction(charIDToTypeID('Dlt '), desc1, DialogModes.NO);
dialog.close();
}
dialog.show();
Thank you.
This can be done without a script. Convert the shape to a Smart Object. Next, call the transformation (Ctrl + T). Instead of sliders, use the W, H and Angle fields in the options bar. Use up / down arrows and in combination with Shift to smoothly change Width and Angle. These parameters will be remembered and can be adjusted during subsequent transformations.
It is difficult to do this with a script even for a smart object. The new Photoshop seems to have (I can't check) the ability to reset th
...Copy link to clipboard
Copied
Your code seems to work as it should. Clarify the question.
Copy link to clipboard
Copied
Hello r-bin,
For example if you rotate a rectangle you created by 45°, the horizontal scaling has to know that XY axis is also rotated 45° thus x coordinate of the object will be scaled accordinlgy 45° positive and negative.
Copy link to clipboard
Copied
But this is what happens. Or I didn’t understand something.
PS. In general, I translate English using Google.
Copy link to clipboard
Copied
For example, create a rectangular shape (not square) and using the script from my original post rotate the shape by 90°, and then scale the rectangle horizontally as you wish. You will notice that the horizontal scaling now becomes a vertical one. After executing the script for the second time, now try to scale it again without rotation, and you will notice that it scales it horizontally and not vertically because it does not know that rotation was applied previously. Is it possible to remember the previous rotation information so the horizontal scaling remains the same no matter the angle of rotation applied?
Copy link to clipboard
Copied
This can be done without a script. Convert the shape to a Smart Object. Next, call the transformation (Ctrl + T). Instead of sliders, use the W, H and Angle fields in the options bar. Use up / down arrows and in combination with Shift to smoothly change Width and Angle. These parameters will be remembered and can be adjusted during subsequent transformations.
It is difficult to do this with a script even for a smart object. The new Photoshop seems to have (I can't check) the ability to reset the transformation for a smart object. Therefore, in theory, you can find out the changes for the smart object, the size and angle, reset them, then perform the transformation according to the sliders.
If this is not a smart object, then you can create invisible rectangular vector mask for it, calculate the size and angle from the positions of the points of the mask, and carry out the transformation knowing both the angle and size. This method is not as accurate as in the case of a smart object and is also complicated.
Question. Why do you need such a script?
Copy link to clipboard
Copied
You are right about this as I have experimented in many ways and found no way how to do it with a script. Was trying to do it by undoing the last state of the angle, resize it and then again rotate it by a different angle without success.
I do like to experiment with new eventualy unusual possibilities by scripting. It is my nature to learn that way if you don't mind.
Thank you again r-bin.
Copy link to clipboard
Copied
function rotate(){
activeDocument.activeLayer.resize(slider1.value, undefined, AnchorPosition.MIDDLECENTER);
with (activeDocument.activeLayer) {rotate(slider2.value)};
}