Skip to content

Commit fb99525

Browse files
committed
up
1 parent 3340634 commit fb99525

File tree

5 files changed

+65
-37
lines changed

5 files changed

+65
-37
lines changed

3-animation/1-bezier-curve/article.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ For two points we have a linear curve (that's a straight line), for three points
3333

3434
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.
3535

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*.**
3737

3838
Try to move control points using a mouse in the example below:
3939

@@ -56,7 +56,7 @@ First let's see the 3-points example.
5656

5757
Here's the demo, and the explanation follow.
5858

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.
6060

6161
[iframe src="demo.svg?p=0,0,0.5,1,1,0&animate=1" height=370]
6262

@@ -117,47 +117,49 @@ A curve that looks like `y=1/t`:
117117

118118
[iframe src="demo.svg?p=0,0,0,0.75,0.25,1,1,1&animate=1" height=370]
119119

120-
121-
With zig-zag control points:
120+
Zig-zag control points also work fine:
122121

123122
[iframe src="demo.svg?p=0,0,1,0.5,0,0.5,1,1&animate=1" height=370]
124123

125-
Loop form:
124+
Making a loop is possible:
126125

127126
[iframe src="demo.svg?p=0,0,1,0.5,0,1,0.5,0&animate=1" height=370]
128127

129128
A non-smooth Bezier curve (yeah, that's possible too):
130129

131130
[iframe src="demo.svg?p=0,0,1,1,0,1,1,0&animate=1" height=370]
132131

133-
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.
134138

135139
```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.
137141
138142
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.
139143
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.
143145
```
144146

145147

146148
## Maths
147149

148150
A Bezier curve can be described using a mathematical formula.
149151

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.
151153

152154
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]`.
153155

154156
- The formula for a 2-points curve:
155157

156158
<code>P = (1-t)P<sub>1</sub> + tP<sub>2</sub></code>
157-
- For three points:
159+
- For 3 control points:
158160

159161
<code>P = (1−t)<sup>2</sup>P<sub>1</sub> + 2(1−t)tP<sub>2</sub> + t<sup>2</sup>P<sub>3</sub></code>
160-
- For four points:
162+
- For 4 control points:
161163

162164
<code>P = (1−t)<sup>3</sup>P<sub>1</sub> + 3(1−t)<sup>2</sup>tP<sub>2</sub> +3(1−t)t<sup>2</sup>P<sub>3</sub> + t<sup>3</sup>P<sub>4</sub></code>
163165

3-animation/2-css-animations/article.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Click the button below to animate the background:
4040
</script>
4141
```
4242

43-
There are 5 properties to describe CSS transitions:
43+
There are 4 properties to describe CSS transitions:
4444

4545
- `transition-property`
4646
- `transition-duration`
@@ -260,9 +260,15 @@ But how to make the Bezier curve for a specific task? There are many tools. For
260260

261261
Timing function `steps(number of steps[, start/end])` allows to split animation into steps.
262262

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.
264264

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:
266272

267273
```css
268274
#stripe.animate {
@@ -271,11 +277,11 @@ For that we split the animation into 9 steps:
271277
}
272278
```
273279

274-
In action `step(9, start)`:
280+
In action:
275281

276282
[codetabs src="step"]
277283

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.
279285

280286
The second argument is one of two words: `start` or `end`.
281287

@@ -301,7 +307,7 @@ So the process would go like this:
301307
- ...
302308
- `9s` -- `-90%`
303309

304-
In action `step(9, end)`:
310+
Here's `step(9, end)` in action (note the pause between the first digit change):
305311

306312
[codetabs src="step-end"]
307313

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
9+
<body>
10+
11+
<div id="digit"><div id="stripe">0123456789</div></div>
12+
13+
</body>
14+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#digit {
2+
border: 1px solid red;
3+
width: 1.2em;
4+
}
5+
6+
#stripe {
7+
display: inline-block;
8+
font: 32px monospace;
9+
}

3-animation/3-js-animation/article.md

+15-18
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@ JavaScript animations can handle things that CSS can't.
44

55
For instance, moving along a complex path, with a timing function different from Bezier curves, or an animation on a canvas.
66

7-
## setInterval
7+
## Using setInterval
88

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.
1010

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.
1212

1313
The pseudo-code can look like this:
1414

1515
```js
16-
let delay = 1000 / 50; // in 1 second 50 frames
1716
let timer = setInterval(function() {
1817
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
2120
```
2221

2322
More complete example of the animation:
@@ -50,15 +49,13 @@ Click for the demo:
5049

5150
[codetabs height=200 src="move"]
5251

53-
## requestAnimationFrame
52+
## Using requestAnimationFrame
5453

5554
Let's imagine we have several animations running simultaneously.
5655

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`.
5857

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`.
6259

6360
In other words, this:
6461

@@ -70,19 +67,19 @@ setInterval(function() {
7067
}, 20)
7168
```
7269

73-
...Is lighter than this:
70+
...Is lighter than three independent calls:
7471

7572
```js
76-
setInterval(animate1, 20);
77-
setInterval(animate2, 20);
73+
setInterval(animate1, 20); // independent animations
74+
setInterval(animate2, 20); // in different places of the script
7875
setInterval(animate3, 20);
7976
```
8077

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).
8279

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`.
8481

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.
8683

8784
The syntax:
8885
```js
@@ -416,7 +413,7 @@ Here's the animated "bouncing" text typing:
416413

417414
## Summary
418415

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.
420417

421418
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.
422419

0 commit comments

Comments
 (0)