Forums › Adobe After Effects Expressions › SOURCERECTATTIME FOR STRING
Tagged: Basic
-
SOURCERECTATTIME FOR STRING
-
Mackie john
March 9, 2022 at 5:43 pmI need to use sourcerectattime().width for only a particular string in a layer source text.
E.g. if the text is Thisismytext**Here you go. So i want to be able measure the width of “Thisismytext” and “Here you go” separately within the same source text. How can i achieve this.
-
Tomas Bumbulevičius
March 9, 2022 at 8:26 pmOnly possible if you put that XXX phrase in a temporary text layer and get the width from there. Side note – font family & style must be identical.
-
Mackie john
March 10, 2022 at 4:46 amMany thanks for your response Tomas. That is not an option for me. Is there any alternative to sourcerectattime. I want be able to acheive with expressions within the same layer
-
Andrei Popa
March 10, 2022 at 7:29 amHi.
I think this could work. You still need to do some testing.
Add a scale animator to your text. Set units to index, set mode to subtract and scale to 0.
Then on start and end add these expressions.
txt = text.sourceText.replace(/\r/g,"");
var parts = ["testing", "is a text"];
if(time<0){
val = txt.indexOf(parts[-Math.ceil(time+1)]);
}else{
val = value}
valtxt = text.sourceText.replace(/\r/g,"");
var parts = ["testing", "is a text"];
if(time<0){
val = text.animator("Animator 1").selector("Range Selector 1").start+parts[-Math.ceil(time+1)].length;
}else{
val = txt.length}
valAnd when you want to see the text width for some specific word, run:
thisComp.layer("Text Layer").sourceRectAtTime(-1,false).width
-1 means the first word from the array, -2 means the second and so on.
-
Filip Vandueren
March 10, 2022 at 8:40 amI have made an animation preset which helps with the process Andrei showcases.
Also working on a second version which is even compatible with text-animators.
here’s V1: https://we.tl/t-gI5DRRI0b9
demo of what v2 can do:
https://imgur.com/gallery/I7uVARU
-
Tomas Bumbulevičius
March 10, 2022 at 3:08 pmThis looks amazing Filip, well done!!! And the good thing about it is that you made it as a preset – definitely something a lot easier for others to apply than understanding all the tiny pieces in a whole picture (less prone to errors when using – thus, less questions caused!)
-
René Pedersen
December 14, 2022 at 10:25 amAmazing preset. I was told again and again it was impossible to get the size of individual lines in a text box since sourceRectAtTime uses the boundary of the entire text box.
I am having problems utilizing the full potential though since I cant figure out what the Left – Top value is relative to.
I am trying to create a coloured box for just one of the lines in a text box that automatically resizes when I change the text. Therefore I changed the sourceSubRect Helper to be based on Lines instead of characters.
And I have added a shape layer with a path (and a fill) and added this code from the mask path that the preset generates to the path.
pos=thisComp.layer("Text").effect("Left - Top")("Point"); //I have pickwhipped the effect to the text-layer.
dim=thisComp.layer("Text").effect("Width - Height")("Point");
pts=[
pos,
pos+[dim[0],0],
pos+dim,
pos+[0,dim[1]]
];
createPath(pts,[],[],true);
It creates a perfect sized box for my line, but I cant really figure out how to position it.
When I have marked the Text layer I can see that the mask created is positioned correct, but if I try to use the points from “Left – Top” in the position of the TextBox/Shape Layer it is placed outside of the comps boundaries.
My text layer is positioned at x = 0 and y = 0 and the anchor point on both my Text layer and Shape layer is placed top left with this expression
sourceSize = thisLayer.sourceRectAtTime(time, false);
T = sourceSize.top;
L = sourceSize.left;
[L , T]
The left value always ends up around -515 and -519 (depending on the text) but I have no idea how to use these values.
-
Filip Vandueren
December 19, 2022 at 9:13 amHi René,
The coordinates are relative to the anchorPoint of the text-layer.
If the text is right- or center-aligned, it makes sense that some of the coordinates will be negative (to the left of the anchorPoint).
The easiest way to implement it in a shape, is to parent the shape to your text-layer, and make sure all positions/anchorpoints of the shape layer and its groups are at [0,0].
If you now use the coordinates in a createPath() the vertices should line up.
if you don’t want to parent, you need to use (pseudocode:)
shapeLayer.fromComp(textLayer.toComp( vertex ))
Log in to reply.