Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
1

Expression to calculate the sum of the lengths of trim paths

Community Beginner ,
Mar 20, 2024 Mar 20, 2024

On a shape layer I have a group that contains a stroked path with a trim path operator. I'd like to fill the whole path with stroke segments of different color and length by duplicating the group. The color part is working but I am getting confused how to calculate the starting and end points for each segment. Offset will be used to drive the animation. 

 

For the Start value this works fine (it sums the values of End):

 

 

var segmentId = thisProperty.propertyGroup(3).propertyIndex;
var numberOfSegments = thisLayer("Contents").numProperties;
 
var startPercentage = 0;
 
for(var i = numberOfSegments; i > segmentId; i--) {
    startPercentage += thisLayer("Contents")(segmentId).content("Trim Paths 1").end;
}
startPercentage;
 
 
But how do I calculate the end value for each. This is my attempt but it is not working. The logic is to find out the end value of the previous segment and add the new length to it. At the moment it just shoots both Start and End to 100%. There is some kind of unwated reference loop? 
 
 
var segmentId = thisProperty.propertyGroup(3).propertyIndex;
var numberOfSegments = thisLayer("Contents").numProperties;
var previousSegmentEnd = numberOfSegments < 2 ? 0 : thisLayer("Contents")(segmentId + 1).content("Trim Paths 1").end;
 
var segmentLengths = [6, 8, 10, 13, 16, 21];
seedRandom(segmentId, timeless=true);
var currentLength = segmentLengths[Math.floor(random(segmentLengths.length))];
 
previousSegmentEnd + currentLength;

 

 

Probably simple but I just seem to be stuck with this. Any help is much appreciated. 

 

 

TOPICS
Expressions
141
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 20, 2024 Mar 20, 2024

I see no provision to actually check whether the values are legally between 0 and 100, which could already be the issue.

 

Mylenium

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 20, 2024 Mar 20, 2024
LATEST

I think I figured it out. The code was waaay off. Probably should implement Mylenium's suggestion but here's the code:

For the Start value:

 

 

var segmentId = thisProperty.propertyGroup(3).propertyIndex;
var numberOfSegments = thisLayer("Contents").numProperties;
 
var startPercentage = 0;
 
// sum the lengths below this segment
if( numberOfSegments > 1 ){ 
for(var i = segmentId + 1; i <= numberOfSegments; i++) {
startPercentage += thisLayer("Contents")(i).content("Trim Paths 1").end - thisLayer("Contents")(i).content("Trim Paths 1").start;
}
}
 
startPercentage;
 
 
 
For the End value:
 
 
var segmentId = thisProperty.propertyGroup(3).propertyIndex;
var numberOfSegments = thisLayer("Contents").numProperties;
 
var previousSegmentEnd = 0;
 
if( segmentId + 1 <= numberOfSegments) {
previousSegmentEnd = thisLayer("Contents")(segmentId + 1).content("Trim Paths 1").end;
}
 
 
previousSegmentEnd + 10;

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines