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: 7-animation/3-js-animation/article.md
+28-27Lines changed: 28 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
-
# JavaScript animations
1
+
# JavaScript انیمیشن های
2
2
3
-
JavaScript animations can handle things that CSS can't.
3
+
انیمیشنهای جاوا اسکریپت میتوانند چیزهایی را که CSS قادر به انجام آن نیست، مدیریت کنند.
4
4
5
-
For instance, moving along a complex path, with a timing function different from Bezier curves, or an animation on a canvas.
5
+
به عنوان مثال، حرکت در امتداد یک مسیر پیچیده، با یک تابع زمان بندی متفاوت از منحنی های Bezier، یا یک انیمیشن روی بوم.
6
6
7
-
## Using setInterval
7
+
## بکار بردن setInterval
8
8
9
-
An animation can be implemented as a sequence of frames -- usually small changes to HTML/CSS properties.
9
+
یک انیمیشن را می توان به عنوان دنباله ای از فریم ها پیاده سازی کرد - معمولاً تغییرات کوچکی در ویژگی های HTML/CSS.
10
10
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.
11
+
برای مثال، تغییر «style.left» از`0px`به`100px`عنصر را جابهجا میکند. و اگر آن را در `setInterval` افزایش دهیم، با یک تاخیر کوچک، مانند 50 بار در ثانیه، `2px` تغییر کند، آنگاه صاف به نظر می رسد. این همان اصل در سینما است: 24 فریم یا بیشتر در ثانیه برای صاف به نظر رسیدن کافی است.
12
12
13
-
The pseudo-code can look like this:
13
+
شبه کد می تواند به شکل زیر باشد:
14
14
15
15
```js
16
16
let timer =setInterval(function() {
@@ -19,7 +19,7 @@ let timer = setInterval(function() {
19
19
}, 20); // change by 2px every 20ms, about 50 frames per second
20
20
```
21
21
22
-
More complete example of the animation:
22
+
نمونه کاملتر انیمیشن:
23
23
24
24
```js
25
25
let start =Date.now(); // remember start time
@@ -45,19 +45,20 @@ function draw(timePassed) {
45
45
}
46
46
```
47
47
48
-
Click for the demo:
48
+
برای دمو کلیک کنید:
49
49
50
50
[codetabs height=200 src="move"]
51
51
52
-
## Using requestAnimationFrame
52
+
## بکار بردن requestAnimationFrame
53
53
54
-
Let's imagine we have several animations running simultaneously.
54
+
بیایید تصور کنیم چندین انیمیشن به طور همزمان اجرا می شوند.
55
55
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`.
56
+
اگر آنها را جداگانه اجرا کنیم، حتی اگر هر کدام دارای`setInterval(...، 20)` باشند، مرورگر باید بیشتر از هر `20 میلیثانیه` دوباره رنگآمیزی کند.
57
57
58
-
That's because they have different starting time, so "every 20ms" differs between different animations. The intervals are not aligned. So we'll have several independent runs within `20ms`.
58
+
این به این دلیل است که آنها زمان شروع متفاوتی دارند، بنابراین "هر 20 میلی ثانیه" بین انیمیشن های مختلف متفاوت است. فواصل هم تراز نیستند. بنابراین ما چندین اجرا مستقل در عرض `20 میلی ثانیه` خواهیم داشت.
setInterval(animate2, 20); // in different places of the script
75
76
setInterval(animate3, 20);
76
77
```
77
78
78
-
These several independent redraws should be grouped together, to make the redraw easier for the browser and hence load less CPU load and look smoother.
79
+
این چندین تصویر مجدد مستقل باید با هم گروه بندی شوند تا ترسیم مجدد برای مرورگر آسان تر شود و در نتیجه بار CPU کمتری بارگیری شود و روان تر به نظر برسد.
79
80
80
-
There's one more thing to keep in mind. Sometimes 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`.
81
+
یک چیز دیگر را باید در نظر داشت. گاهی اوقات CPU بیش از حد بارگیری می شود، یا دلایل دیگری برای ترسیم مجدد کمتر وجود دارد (مانند زمانی که برگه مرورگر پنهان است)، بنابراین ما واقعاً نباید آن را هر 20 میلی ثانیه اجرا کنیم.
81
82
82
-
But how do we know about that in JavaScript? There's a specification [Animation timing](https://www.w3.org/TR/animation-timing/)that provides the function `requestAnimationFrame`. It addresses all these issues and even more.
83
+
اما چگونه در مورد آن در جاوا اسکریپت بدانیم؟ یک مشخصات [Animation timeing](https://www.w3.org/TR/animation-timing/)وجود دارد که تابع "requestAnimationFrame" را ارائه می دهد. به همه این مسائل و حتی بیشتر می پردازد.
83
84
84
-
The syntax:
85
+
نحو:
85
86
```js
86
87
let requestId =requestAnimationFrame(callback)
87
88
```
88
89
89
-
That schedules the `callback` function to run in the closest time when the browser wants to do animation.
90
+
زمانی که مرورگر میخواهد انیمیشن انجام دهد، عملکرد `callback` را برنامهریزی میکند تا در نزدیکترین زمان اجرا شود.
90
91
91
-
If we do changes in elements in `callback` then they will be grouped together with other`requestAnimationFrame`callbacks and with CSS animations. So there will be one geometry recalculation and repaint instead of many.
92
+
اگر تغییراتی را در عناصر در `بازگشت به تماس` انجام دهیم، آنها با دیگر تماسهای`requestAnimationFrame`و با انیمیشنهای CSS گروهبندی میشوند. بنابراین یک محاسبه مجدد هندسی و رنگ آمیزی مجدد به جای تعداد زیادی وجود خواهد داشت.
92
93
93
-
The returned value `requestId` can be used to cancel the call:
94
+
مقدار بازگشتی درخواست Id می تواند برای لغو تماس استفاده شود:
94
95
```js
95
96
// cancel the scheduled execution of callback
96
97
cancelAnimationFrame(requestId);
97
98
```
98
99
99
-
The `callback` gets one argument -- the time passed from the beginning of the page load in milliseconds. This time can also be obtained by calling [performance.now()](mdn:api/Performance/now).
100
+
`بازگشت به تماس` یک آرگومان دریافت می کند -- زمان سپری شده از آغاز بارگیری صفحه بر حسب میلی ثانیه. این زمان را نیز می توان از طریق تماس دریافت کرد[performance.now()](mdn:api/Performance/now).
100
101
101
-
Usually`callback`runs very soon, unless the CPU is overloaded or the laptop battery is almost discharged, or there's another reason.
102
+
معمولاً`callback`خیلی زود اجرا میشود، مگر اینکه CPU بیش از حد بارگیری شده باشد یا باتری لپتاپ تقریباً خالی شده باشد یا دلیل دیگری وجود داشته باشد.
102
103
103
-
The code below shows the time between first 10 runs for `requestAnimationFrame`. Usually it's 10-20ms:
104
+
کد زیر زمان بین 10 اجرای اول درخواست AnimationFrame را نشان می دهد. معمولاً 10-20 میلی ثانیه است:
104
105
105
106
```html run height=40 refresh
106
107
<script>
@@ -116,9 +117,9 @@ The code below shows the time between first 10 runs for `requestAnimationFrame`.
116
117
</script>
117
118
```
118
119
119
-
## Structured animation
120
+
## انیمیشن ساخت یافته
120
121
121
-
Now we can make a more universal animation function based on `requestAnimationFrame`:
122
+
اکنون میتوانیم یک تابع انیمیشن جهانیتر بر اساس `requestAnimationFrame` ایجاد کنیم:
0 commit comments