Home > other >  For loop where each value is distributed correctly
For loop where each value is distributed correctly

Time:05-16

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

CodePudding user response:

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.

  • Related