Animations on web pages and how to add them
Animation is the quick replacement of a sequence of pictures (frames) that differ only slightly: the eye takes such frames as continuous movement. The number of frames shown in one second is the frame rate (fps): usually 24 for cinema and 30 or 60 for screens and games; so the total number of frames = fps · seconds. In animation tools the work is done on a timeline: layers separate the parts of the picture and frames are placed along time. The author sets only the keyframes (for example the first and last position), and the program calculates the in-between frames; this is called tweening: a motion tween changes the position of an object gradually and a shape tween changes its shape; Macromedia Flash added a Motion Guide layer for paths. In today's web pages the same idea is done in CSS: the @keyframes rule gives the key states and the animation property gives the duration, repeats and speed function; for complex cases JavaScript, SVG, or video, GIF and WebP animations are used. Safety: images flashing more than 3 times a second may trigger seizures in some people, so never make a rapidly flashing light effect; many users ask for less motion, which is respected with the prefers-reduced-motion rule.
In a cinema each picture on the film is still, but when they change quickly our eyes see continuous movement. Animation is built on this effect.
Cartoon artists, game designers and web designers plan movement with its main states.
Move a pencil tip steadily across a desk from one edge to the other in 5 seconds and mark its place each second. Did the gaps between the marks come out equal?
1<!DOCTYPE html> 2<html lang="uz"> 3<head> 4 <meta charset="utf-8"> 5 <title>Harakatlanuvchi shar</title> 6 <style> 7 .maydon { position: relative; width: 400px; height: 60px; background: #e8f1ff; } 8 .shar { position: absolute; top: 10px; left: 0; width: 40px; height: 40px; 9 border-radius: 50%; background: #d9480f; 10 animation: yur 3s linear infinite alternate; } 11 @keyframes yur { from { left: 0; } to { left: 360px; } } 12 @media (prefers-reduced-motion: reduce) { .shar { animation: none; } } 13 </style> 14</head> 15<body> 16 <div class="maydon"><div class="shar"></div></div> 17</body> 18</html>
| Frame | x |
|---|---|
| 1 | 0 |
| 11 | (11 – 1)·10 = 100 |
| 24 | 230 |
“Flip-book animation”: a “bouncing ball” of 12 frames is drawn on the edge of a notebook and flipped to show it; the author marks the keyframes and a partner draws the in-between frames.