Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
For loop where each value is distributed correctly

I have a for loop where I want to distribute a certain number of words with the same length across a timeline.

I have variables that give me the duration for each text, the start of where the words will spawn, and the end.

var wordArray = [];
for (t = 0; t < words.length; t++) {
//This generates the textbox
wordArray[wordArray.length] = currentComp.layers.addText(words[t]);
 if (t == 0) {
        //This makes the first textbox appear in the start
        wordArray[wordArray.length - 1].inPoint = start;
        //This makes the first textbox have the length with the value that divides it by the number of words
        wordArray[wordArray.length - 1].outPoint = wordArray[wordArray.length - 1].inPoint + textTime;
    //Here I am trying to make them all evenly distributed by multiplying the divided duration for the number of words that are already existing
    } else if (t < words.length - 2) {
        wordArray[wordArray.length - 1].inPoint = start + textTime * (wordArray.length - 1);
        wordArray[wordArray.length - 1].outPoint = end - textTime * (wordArray.length - 1);
    } else {
        //This is here to make the last word stop at the end of the text duration.
        wordArray[wordArray.length - 1].inPoint = start  + textTime * (wordArray.length - 1);
        wordArray[wordArray.length - 1].outPoint = wordArray[wordArray.length - 1].inPoint + textTime;
    }

}

The error comes in the end, as seen in the image.The word in the end (highlighted in white) has less length than the previous, and the second to last word has more length than all the other words.

I'm aware this isn't a syntax issue, rather a maths issue that I can't seem to figure out. Thanks in advance

EDIT: Found the answer and updated the syntax to match it.

The word in the end (highlighted in white) has less length than the previous, and the second to last word has more length than all the other words

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I updated the syntax to match the correct code.

Turns out that The issue was that the gap for each word wasn't being calculated correctly.

I got it to work by calculating the start and the end of each section by doing this :

var startDurationString = timeCalc[0].split(":");
    var finalStartDurationString = startDurationString[1] + ":" + startDurationString[2] + ":" + startDurationString[3];
    var startDuration = currentFormatToTime(finalStartDurationString, currentComp.frameRate);
    var endDurationString = timeCalc[1].split(":");
    var finalEndDurationString = endDurationString[1] + ":" + endDurationString[2] + ":" + endDurationString[3];
    var endDuration = currentFormatToTime(finalEndDurationString, currentComp.frameRate);

Then, I used the difference of the start and the end and divided by the length of the words.

durationForEachText = (endDuration-startDuration) / numberOfWords.length;

Hope it helps anyone out there.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!