Hey Adam,
This gives you everything you need:
posterizeTime(0);
const l = thisComp.layer("Logo");
const w = l.width;
const h = l.height;
const heightLog2 = Math.ceil(Math.log(h)/Math.log(2));
const widthLog2 = Math.ceil(Math.log(w)/Math.log(2));
const threshold = 1; // on average 1 pixel in a row is opaque to count as the topEdge;
var topEdge = 0;
var bottomEdge = h;
for(i=heightLog2; i>-1; i--) {
let partition = 2 ** i;
let s = l.sampleImage([w / 2, topEdge+partition/2], [w / 2, partition/2], false, time);
if (s[3]<=threshold/(w*partition)) topEdge+=partition;
s = l.sampleImage([w / 2, bottomEdge-partition/2], [w / 2, partition/2], false, time);
if (s[3]<=threshold/(w*partition)) bottomEdge-=partition;
}
var leftEdge = 0;
var rightEdge = w;
for(i=widthLog2; i>-1; i--) {
let partition = 2 ** i;
let s = l.sampleImage([leftEdge+partition/2, h / 2], [partition/2, h / 2], false, time);
if (s[3]<=threshold/(h*partition)) leftEdge+=partition;
s = l.sampleImage([rightEdge-partition/2, h / 2], [partition/2, h / 2, ], false, time);
if (s[3]<=threshold/(h*partition)) rightEdge-=partition;
}
alphaRect= {
left: leftEdge,
top: topEdge,
right: rightEdge,
bottom: bottomEdge,
width: rightEdge-leftEdge,
height: bottomEdge - topEdge,
center: mul([leftEdge+rightEdge, topEdge+bottomEdge],0.5)
};
With this setup code you can use the properties of alphaRect on scale and anchorPoint to center and scale up like this:
anchorPoint:
alphaRect.center;
scale:
mul(
[100,100],
Math.min(thisComp.width/alphaRect.width, thisComp.height/alphaRect.height)
);
Now, because both scale and anchorPoint need to know the value of alphaRect, you will have to copy the whole first code in both, and only add that last bit for each.
and maybe position needs to be explicitly centered ?
[thisComp.width/2, thisComp.height/2];