You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 3-animation/1-bezier-curve/article.md
+15-13
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ For two points we have a linear curve (that's a straight line), for three points
33
33
34
34
Because of that last property, in computer graphics it's possible to optimize intersection tests. If convex hulls do not intersect, then curves do not either. So checking for the convex hulls intersection first can give a very fast "no intersection" result. Checking the intersection or convex hulls is much easier, because they are rectangles, triangles and so on (see the picture above), much simpler figures than the curve.
35
35
36
-
The main value of Bezier curves for drawing -- by moving the points the curve is changing *in intuitively obvious way*.
36
+
**The main value of Bezier curves for drawing -- by moving the points the curve is changing *in intuitively obvious way*.**
37
37
38
38
Try to move control points using a mouse in the example below:
39
39
@@ -56,7 +56,7 @@ First let's see the 3-points example.
56
56
57
57
Here's the demo, and the explanation follow.
58
58
59
-
Points can be moved by the mouse. Press the "play" button to run it.
59
+
Control points (1,2 and 3) can be moved by the mouse. Press the "play" button to run it.
As the algorithm is recursive, we can build Bezier curves of any order: using 5, 6 or more control points. But in practice many points are less useful. Usually we take 2-3 points, and for complex lines glue several curves together. That's simpler to develop and calculate.
132
+
```online
133
+
If there's anything unclear in the algorithm description, then live examples above show how
134
+
the curve is built.
135
+
```
136
+
137
+
As the algorithm is recursive, we can build Bezier curves of any order, that is: using 5, 6 or more control points. But in practice many points are less useful. Usually we take 2-3 points, and for complex lines glue several curves together. That's simpler to develop and calculate.
134
138
135
139
```smart header="How to draw a curve *through* given points?"
136
-
We use control points for a Bezier curve. As we can see, they are not on the curve. Or, to be precise, the first and the last ones do belong to curve, but others don't.
140
+
We use control points for a Bezier curve. As we can see, they are not on the curve, except the first and the last ones.
137
141
138
142
Sometimes we have another task: to draw a curve *through several points*, so that all of them are on a single smooth curve. That task is called [interpolation](https://en.wikipedia.org/wiki/Interpolation), and here we don't cover it.
139
143
140
-
There are mathematical formulas for such curves, for instance [Lagrange polynomial](https://en.wikipedia.org/wiki/Lagrange_polynomial).
141
-
142
-
In computer graphics [spline interpolation](https://en.wikipedia.org/wiki/Spline_interpolation) is often used to build smooth curves that connect many points.
144
+
There are mathematical formulas for such curves, for instance [Lagrange polynomial](https://en.wikipedia.org/wiki/Lagrange_polynomial). In computer graphics [spline interpolation](https://en.wikipedia.org/wiki/Spline_interpolation) is often used to build smooth curves that connect many points.
143
145
```
144
146
145
147
146
148
## Maths
147
149
148
150
A Bezier curve can be described using a mathematical formula.
149
151
150
-
As we saw -- there's actually no need to know it. But for completeness -- here it is.
152
+
As we saw -- there's actually no need to know it, most people just draw the curve by moving points with a mouse. But if you're into maths -- here it is.
151
153
152
154
Given the coordinates of control points <code>P<sub>i</sub></code>: the first control point has coordinates <code>P<sub>1</sub> = (x<sub>1</sub>, y<sub>1</sub>)</code>, the second: <code>P<sub>2</sub> = (x<sub>2</sub>, y<sub>2</sub>)</code>, and so on, the curve coordinates are described by the equation that depends on the parameter `t` from the segment `[0,1]`.
Copy file name to clipboardExpand all lines: 3-animation/2-css-animations/article.md
+12-6
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ Click the button below to animate the background:
40
40
</script>
41
41
```
42
42
43
-
There are 5 properties to describe CSS transitions:
43
+
There are 4 properties to describe CSS transitions:
44
44
45
45
-`transition-property`
46
46
-`transition-duration`
@@ -260,9 +260,15 @@ But how to make the Bezier curve for a specific task? There are many tools. For
260
260
261
261
Timing function `steps(number of steps[, start/end])` allows to split animation into steps.
262
262
263
-
Let's see that in an example with digits. We'll make the digits change not in a smooth, but in a discrete way.
263
+
Let's see that in an example with digits.
264
264
265
-
For that we split the animation into 9 steps:
265
+
Here's a list of digits, without any animations, just as a source:
266
+
267
+
[codetabs src="step-list"]
268
+
269
+
We'll make the digits appear in a discrete way by making the part of the list outside of the red "window" invisible and shifting the list to the left with each step.
270
+
271
+
There will be 9 steps, a step-move for each digit:
266
272
267
273
```css
268
274
#stripe.animate {
@@ -271,11 +277,11 @@ For that we split the animation into 9 steps:
271
277
}
272
278
```
273
279
274
-
In action`step(9, start)`:
280
+
In action:
275
281
276
282
[codetabs src="step"]
277
283
278
-
The first argument of `steps` is the number of steps. The transform will be split into 9 parts (10% each). The time interval is divided as well: 9 seconds split into 1 second intervals.
284
+
The first argument of `steps(9, start)` is the number of steps. The transform will be split into 9 parts (10% each). The time interval is automatically divided into 9 parts as well, so `transition: 9s` gives us 9 seconds for the whole animation – 1 second per digit.
279
285
280
286
The second argument is one of two words: `start` or `end`.
281
287
@@ -301,7 +307,7 @@ So the process would go like this:
301
307
- ...
302
308
-`9s` -- `-90%`
303
309
304
-
In action `step(9, end)`:
310
+
Here's `step(9, end)` in action (note the pause between the first digit change):
Copy file name to clipboardExpand all lines: 3-animation/3-js-animation/article.md
+15-18
Original file line number
Diff line number
Diff line change
@@ -4,20 +4,19 @@ JavaScript animations can handle things that CSS can't.
4
4
5
5
For instance, moving along a complex path, with a timing function different from Bezier curves, or an animation on a canvas.
6
6
7
-
## setInterval
7
+
## Using setInterval
8
8
9
-
From the HTML/CSS point of view, an animation is a gradual change of the style property. For instance, changing `style.left` from `0px`to `100px` moves the element.
9
+
An animation can be implemented as a sequence of frames -- usually small changes to HTML/CSS properties.
10
10
11
-
And if we increase it in `setInterval`, by making 50 small changes per second, then it looks smooth. That's the same principle as in the cinema: 24 or more frames per second is enough to make it look smooth.
11
+
For instance, changing `style.left` from `0px` to `100px` moves the element. And if we increase it in `setInterval`, changing by `2px` with a tiny delay, like 50 times per second, then it looks smooth. That's the same principle as in the cinema: 24 or more frames per second is enough to make it look smooth.
12
12
13
13
The pseudo-code can look like this:
14
14
15
15
```js
16
-
let delay =1000/50; // in 1 second 50 frames
17
16
let timer =setInterval(function() {
18
17
if (animation complete) clearInterval(timer);
19
-
else increase style.left
20
-
}, delay)
18
+
else increase style.left by 2px
19
+
}, 20); // change by 2px every 20ms, about 50 frames per second
21
20
```
22
21
23
22
More complete example of the animation:
@@ -50,15 +49,13 @@ Click for the demo:
50
49
51
50
[codetabs height=200 src="move"]
52
51
53
-
## requestAnimationFrame
52
+
## Using requestAnimationFrame
54
53
55
54
Let's imagine we have several animations running simultaneously.
56
55
57
-
If we run them separately, each one with its own`setInterval(..., 20)`, then the browser would have to repaint much more often than every `20ms`.
56
+
If we run them separately, then even though each one has`setInterval(..., 20)`, then the browser would have to repaint much more often than every `20ms`.
58
57
59
-
Each `setInterval` triggers once per `20ms`, but they are independent, so we have several independent runs within `20ms`.
60
-
61
-
These several independent redraws should be grouped together, to make it easier for the browser.
58
+
That's because they have different starting time, so "every 20ms" differs between different animations. The intervals are not alignned. So we'll have several independent runs within `20ms`.
setInterval(animate2, 20);// in different places of the script
78
75
setInterval(animate3, 20);
79
76
```
80
77
81
-
There's one more thing to keep in mind. Sometimes when CPU is overloaded, or there are other reasons to redraw less often. For instance, if the browser tab is hidden, then there's totally no point in drawing.
78
+
These several independent redraws should be grouped together, to make the redraw easier for the browser (and hence smoother for people).
82
79
83
-
There's a standard [Animation timing](http://www.w3.org/TR/animation-timing/) that provides the function `requestAnimationFrame`.
80
+
There's one more thing to keep in mind. Sometimes when CPU is overloaded, or there are other reasons to redraw less often (like when the browser tab is hidden), so we really shouldn't run it every `20ms`.
84
81
85
-
It addresses all these issues and even more.
82
+
But how do we know about that in JavaScript? There's a specification [Animation timing](http://www.w3.org/TR/animation-timing/) that provides the function `requestAnimationFrame`. It addresses all these issues and even more.
86
83
87
84
The syntax:
88
85
```js
@@ -416,7 +413,7 @@ Here's the animated "bouncing" text typing:
416
413
417
414
## Summary
418
415
419
-
JavaScript animation should be implemented via `requestAnimationFrame`. That built-in method allows to setup a callback function to run when the browser will be preparing a repaint. Usually that's very soon, but the exact time depends on the browser.
416
+
For animations that CSS can't handle well, or those that need tight control, JavaScript can help. JavaScript animations should be implemented via `requestAnimationFrame`. That built-in method allows to setup a callback function to run when the browser will be preparing a repaint. Usually that's very soon, but the exact time depends on the browser.
420
417
421
418
When a page is in the background, there are no repaints at all, so the callback won't run: the animation will be suspended and won't consume resources. That's great.
0 commit comments