-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScale To Pixel.jsx
More file actions
75 lines (71 loc) · 2.84 KB
/
Copy pathScale To Pixel.jsx
File metadata and controls
75 lines (71 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Scale layer(s) to a specific pixel width and height
* through a simple UI.
*
* @author github.com/ilovedoumiao
* @version 1.0
* @version 1.1 - Improved aspect-ratio-aware sizing, exact width/height scaling, keyed and 3D layer support + safer layer handling.
*/
function showui() {
var e = app.project.activeItem;
if (!(e instanceof CompItem) || 0 === e.selectedLayers.length) {
alert("Select layer(s) first.");
return
}
var t = new Window("dialog", "Scale Layers"),
a = t.add("group");
a.orientation = "row", a.alignment = "left", a.add("statictext", void 0, "Enter 0 for no change");
var i = t.add("group");
i.orientation = "row", i.alignment = "left", i.add("statictext", void 0, "Width (px):");
var $ = i.add("edittext", void 0, "0");
$.characters = 10;
var l = t.add("group");
l.orientation = "row", l.alignment = "left", l.add("statictext", void 0, "Height (px):");
var r = l.add("edittext", void 0, "0");
r.characters = 10;
var o = t.add("group");
o.alignment = "center";
var c = o.add("button", void 0, "OK"),
n = o.add("button", void 0, "Cancel");
c.onClick = function() {
var e = parseFloat($.text),
a = parseFloat(r.text);
scalelayers(e, a), t.close()
}, n.onClick = function() {
t.close()
}, t.center(), t.show()
}
function calculatePixelScale(e, t, a, i, $) {
for (var l = [], r = 0; r < $.length; r++) l[r] = $[r];
return a > 0 && i > 0 ? (l[0] = a / e * 100, l[1] = i / t * 100) : a > 0 ? (l[0] = a / e * 100, l[1] = l[0]) : i > 0 && (l[1] = i / t * 100, l[0] = l[1]), l
}
function verifyScaleToPixelCalculation() {
var e = calculatePixelScale(200, 100, 100, 0, [100, 100, 75]),
t = calculatePixelScale(200, 100, 100, 100, [100, 100, 75]);
if (50 !== e[0] || 50 !== e[1] || 75 !== e[2] || 50 !== t[0] || 100 !== t[1] || 75 !== t[2]) throw Error("Scale To Pixel calculation self-check failed.")
}
function scaleLayerToPixels(e, t, a, i) {
var $ = e.property("ADBE Transform Group"),
l = $ && $.property("ADBE Scale");
if (e.sourceRectAtTime && l) {
var r = e.sourceRectAtTime(t, !1);
if (!(r.width <= 0) && !(r.height <= 0)) {
var o = l.valueAtTime(t, !1),
c = calculatePixelScale(r.width, r.height, a, i, o);
l.numKeys > 0 ? l.setValueAtTime(t, c) : l.setValue(c)
}
}
}
function scalelayers(e, t) {
if (!(isNaN(e) || isNaN(t)) && !(e < 0) && !(t < 0) && (0 !== e || 0 !== t)) {
var a = app.project.activeItem,
i = a.selectedLayers;
app.beginUndoGroup("Scale Layers To Pixels");
try {
for (var $ = 0; $ < i.length; $++) scaleLayerToPixels(i[$], a.time, e, t)
} finally {
app.endUndoGroup()
}
}
}
verifyScaleToPixelCalculation(), showui();