What is Custom Animation?
Custom animation refers to any visual movement that is created specifically for a project rather than using prepackaged motion. In web development this usually means writing your own CSS @keyframes, using the Web Animations API, or scripting controls with JavaScript libraries such as GSAP, Anime.js, or Three.js. The benefit is full control over timing, easing, interaction, and visual style.
Why Use Custom Animation?
- Brand identity unique motion can become a recognizable part of a brand.
- Performance handcrafted animations can be optimized for the target device.
- Interaction animation can respond to scroll, hover, or user input in ways static libraries cannot.
- Accessibility you can respect user preferences, reduce motion, or provide alternative cues.
Fundamental Concepts
Timing & Easing
Every animation has a duration and an easing curve that determines how speed changes over time. CSS provides linear, ease, easein, easeout, and cubicbezier(). The Web Animations API accepts an easing string with the same options plus steps() for framebyframe control.
Transformations
Transform properties (translate, rotate, scale, skew) are GPUaccelerated when used alone, making them ideal for smooth motion. Stacking transforms in a single transform declaration reduces layout thrashing.
Opacity & Filters
Changing opacity is cheap because it only affects compositing. Filters (blur, brightness, huerotate) are more expensive but can add depth when used sparingly.
CSSOnly Custom Animation
Below is a minimal example that creates a bouncing ball using @keyframes. Because everything is done in CSS, the animation works even when JavaScript is disabled.
Key points to remember:
- Use
transformfor movement whenever possible. - Keep the number of animated properties low.
- Set
animation-fill-modeif you need the element to retain a final state.
JavaScriptDriven Animation
When interaction is requiredscrolllinked effects, draganddrop, timeline controlJavaScript becomes essential. The Web Animations API (WAAPI) is now supported in all modern browsers and gives you the same declarative power as CSS while allowing runtime changes.
Simple WAAPI Example
Advantages of WAAPI:
- Control playback speed, direction, and current time.
- Combine multiple keyframe effects into a single
GroupEffect(future spec). - Easier to synchronize with other JS tasks.
Using Animation Libraries
Libraries abstract repetitive tasks and provide richer timelines. Two popular choices are:
GSAP (GreenSock Animation Platform)
- Highly performant, works across browsers.
- Timeline object lets you chain multiple tweens with precise offsets.
- Plugins for SVG, scroll, physics, and more.
Anime.js
- Lightweight and intuitive syntax.
- Supports CSS, SVG, DOM attributes, and JS objects.
anime({ targets: '.dot', translateX: 250, rotate: '1turn', backgroundColor: '#FF595E', duration: 1500, loop: true, easing: 'easeInOutSine'}); Both libraries handle vendor prefixes, requestAnimationFrame throttling, and provide useful callbacks such as onComplete and onUpdate.
Performance Tips
- Animate on the compositor thread stick to
transformandopacity. - Limit layoutthreatening properties avoid animating
width,height,top/leftunless necessary. - Use
will-changesparingly hint the browser but remove it after the animation ends. - Group related changes batch DOM reads/writes to reduce reflows.
- Respect
prefers-reduced-motionprovide a fallback or disable nonessential motion for users who request it.
Example of respecting user preference:
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) { // Skip nonessential animations document.querySelectorAll('.animated').forEach(el => el.style.animation = 'none');} Accessibility Considerations
Motion can be disorienting for some users. Follow these practices:
- Provide a reduce motion toggle that disables or simplifies animation.
- Pair visual animation with ARIA live regions if the motion conveys important information.
- Make sure interactive elements remain focusable and keyboardnavigable during animation.
Putting It All Together A Mini Project
Imagine a landing page where a headline fades in, an illustration slides up with a slight bounce, and a calltoaction button pulses when it enters the viewport. Below is a concise implementation that mixes CSS for simple fades, WAAPI for the slide, and IntersectionObserver to trigger the pulse.
Welcome to Our Site
Get StartedThis snippet demonstrates how a single page can blend declarative CSS, lowlevel JavaScript animation, and modern APIs to deliver a polished experience while keeping the code readable.
