Home Web Front-end JS Tutorial After Effects: The Modulo Operator (%)

After Effects: The Modulo Operator (%)

Jan 20, 2025 pm 08:40 PM

In-depth explanation of the modulo operator (%) in After Effects expressions

The modulo operator (%), also known as the remainder operator, is a very useful tool in expression construction, but it may not be easy for beginners to understand. This article explains its functionality and uses.

% is used to calculate the remainder of an equation. For example:

<code>10 % 3</code>
Copy after login
Copy after login

This expression returns 1 because the quotient of 10 divided by 3 is 3, the remainder is 1.

This is useful for creating loops when working with time variables.

Loop expression

Most designers new to After Effects expressions are familiar with the loopOut() expression. It allows us to use "cycle" (cycle from start to end), "pingpong" (cycle from start to end and back to the start), "offset" (repeat keyframes but each time with a offset of value to build the animation) or "continue" (to continue the motion using the speed of the last keyframe) to cycle through keyframe properties. This is very comprehensive and covers everything you need for keyframe animation.

However, if you want to loop an expression, loopOut is not a viable solution. There may be many reasons for not wanting to use keyframes, but the main one is if a value needs to be updated dynamically, and constantly. It's much easier to update an expression attached to a slider than it is to update a set of keyframes.

If the motion is continuous, then Linear or Ease will suffice. But for complex animations that require looping, we can use the time modulo operator to implement the loop.

To see how this works, copy and paste the following expression into the text layer’s Source Text property:

<code>Math.floor(time % 5)</code>
Copy after login

You will see the layer count from 0 to 4 every second, looping back to 0 every 5 seconds. This is because the remainder of the expression changes every second as time passes:

随时间变化的余数
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
The Math.floor function rounds the argument to an integer.

After Effects: The Modulo Operator (%)

From this, it's easy to see how to use it when you need to animate numbers between specific parameters.

Example: Digital Clock

Let’s use % to make a digital clock.

The seconds need to count between 0 and 60, and the minutes need to be incremented every 60 intervals. Let’s paste this into the text layer’s Source Text property again:

<code>10 % 3</code>
Copy after login
Copy after login

Breaking up the expression, our sec variable will count from 0 to 60, and the minute variable will be incremented on every multiple of 60 (again, we use Math.floor to round the numbers). The if statement that follows adds a 0 in front of the sec variable if it is less than 10, ensuring that our seconds variable always has two digits (you can repeat this for the minutes as well, if you want). Then just use time separators to combine them together.

After Effects: The Modulo Operator (%)

If you need the counter to work independently of time, you can achieve the same effect by replacing the time with a slider and setting its value.

You can also make the time separator blink using the modulo operator and the After Effects text expression selector.

Go to the text layer and add the Opacity animation option to the text layer (if you’re not sure how to do this, you can check out all about this in this article). Then add expression selector and remove range selector.

After Effects: The Modulo Operator (%)

Set the opacity in the animator to 0 and add this expression to the Amount property:

//Digital clock separator flashes //Add to expression selector

minute = Math.floor(time / 60);

minute = 10 && textIndex == 3 ? Math.floor(time2 % 1.5) 100 : 0;

I wrote a conditional statement based on the fact that the number of digits in the minutes variable is not fixed. First, I copy the minutes variable from the source text property. I then use this to calculate the textIndex value of the time separator. When there is only one digit in the minute display, it will equal 2. When the minutes display exceeds 10, it will be 3. Conditional statements can also be written as if statements, as shown below, to further explain what it is doing:

if (minute = 10 && textIndex == 3) Math.floor(time2 % 1.5) 100 else 0

If minutes is less than 10 and textIndex is equal to 2, then Math.floor(time2 % 1.5) 100 affects the second character in the text layer. This will make the letters flash (on/off ratio 2:1) thanks to the modulo operator. The Math.floor function rounds the number, and the entire expression is finally multiplied by 100 to toggle between 0 and 100, which is the range of the expression selector.

However, if minutes is equal to or greater than 10 and textIndex is equal to 3, the effect will be applied to the third character in the text layer. This explains the extra digits in the minute display. If your minutes display needs to be longer than 99, you will need to add another parameter to affect the display of the time separator when it is in the fourth position.

However, if your minute display is set to a constant number of digits, this statement becomes much simpler:

dividerIndex = 3; textIndex == dividerIndex ? Math.floor(time2 % 1.5) 100 : 0

After Effects: The Modulo Operator (%)

That’s it, you get a digital clock!

After showing how the modulo operator helps create loops, we can now consider how it can be applied to other properties.

Example: Analog Clock

Now let’s make an analog clock. When the pointer ticks, it's usually not a continuous movement but one that stops and starts suddenly. This is the type of loop that the modulo operator can help solve.

Let’s take a look at the following expression that can be pasted into the rotation property of the clock hands layer:

//Second hand rotates frames = thisComp.frameDuration;

loopTime = 1; dur = frames * 6; strength = 6;

counter = Math.floor(time/loopTime); t = time % loopTime;

ease(t, 0, dur, strength counter, strength (counter 1))

First, we set some variables. frames is the duration of a frame in the composition, allowing it to work across multiple frame rates.

Set loopTime to the time you want to loop. I want the loop to last one second, so I set it to 1. dur is the duration of the animation within the loop, so I set it to frames * 6, making it last 6 frames. strength is the change in animation value, since I'm animating the clock hands I set it to 6 so the clock hands will complete one rotation in 60 ticks.

Next, I create a counter variable that will help offset my value. I created it using Math.floor(time/loopTime) , using Math.floor to round the numbers and setting the speed of the counter to match the loop. Finally, t is a variable we can use to time expression-driven animations. This is time % loopTime, so when time reaches the number stored in loopTime, time loops.

After that, we can animate. In this example, I use the ease expression. By setting the first parameter to t, we remap the rotation value to our loop time variable. The next two parameters are 0 and dur, the start and end points of the animation. The last two parameters are strength counter and strength (counter 1), which is the value of the rotation property. By multiplying strength by counter, we can offset the value of each loop, ending at strength * (counter 1), ready for the next loop.

After Effects: The Modulo Operator (%)

The advantage of driving motion via expressions rather than keyframes in this case is if you need to build a clock template for changing times. The static value of the expression can be connected to the slider, making it easier to update continuously.

You can use more advanced expressions or build your own functions to create more customized animations:

//Second hand rotates frames = thisComp.frameDuration;

loopTime = 1; dur = frames * 6; change = 6;

counter = Math.floor(time/loopTime); t = time % loopTime;

function easeInOutBack (t, b, c, d, s) { if (s == undefined) s = 1.70158; if ((t/=d/2) < 1) return c/2(tt(((s=(1.525))) 1)t - s )) b; return c/2((t-=2)t(((s=(1.525)) 1)t s) 2) b; }

easeInOutBack(t, 0, change, dur, 1.70158)

After Effects: The Modulo Operator (%)

Finally, you can create a variable to set the starting value and use an if statement to skip the first iteration of the minute hand (and possibly hour hand) animation:

//Minute hand rotation frames = thisComp.frameDuration;

loopTime = 60; dur = frames * 6; strength = 6; startValue = 180;

counter = Math.floor(time/loopTime); t = time % loopTime;

function easeInOutBack (t, b, c, d, s) { if (s == undefined) s = 1.70158; if ((t/=d/2) < 1) return c/2(tt(((s=(1.525))) 1)t - s )) b; return c/2((t-=2)t(((s=(1.525)) 1)t s) 2) b; }

if (counter > 0) { easeInOutBack(t, startValue strength * counter, strength, dur, 1.70158) } else { startValue }

After Effects: The Modulo Operator (%)

From here, just connect the slider to our startValue variable. This way you have an analog clock that can be updated by simply changing the value in the slider.

Conclusion

The modulo operator is useful for creating loops to aid in dynamic expressions where other methods are not suitable for the needs of the project.

Try testing it in your own project!

Any comments? Is there anything unclear? Please leave a comment below.

The above is the detailed content of After Effects: The Modulo Operator (%). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

From C/C   to JavaScript: How It All Works From C/C to JavaScript: How It All Works Apr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript and the Web: Core Functionality and Use Cases JavaScript and the Web: Core Functionality and Use Cases Apr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript in Action: Real-World Examples and Projects JavaScript in Action: Real-World Examples and Projects Apr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding the JavaScript Engine: Implementation Details Understanding the JavaScript Engine: Implementation Details Apr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: Community, Libraries, and Resources Python vs. JavaScript: Community, Libraries, and Resources Apr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Python vs. JavaScript: Development Environments and Tools Python vs. JavaScript: Development Environments and Tools Apr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

The Role of C/C   in JavaScript Interpreters and Compilers The Role of C/C in JavaScript Interpreters and Compilers Apr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

See all articles