Skip to content

Commit e6dfc02

Browse files
authored
js-animations
1 parent 01c5e49 commit e6dfc02

1 file changed

Lines changed: 28 additions & 27 deletions

File tree

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# JavaScript animations
1+
# JavaScript انیمیشن های
22

3-
JavaScript animations can handle things that CSS can't.
3+
انیمیشن‌های جاوا اسکریپت می‌توانند چیزهایی را که CSS قادر به انجام آن نیست، مدیریت کنند.
44

5-
For instance, moving along a complex path, with a timing function different from Bezier curves, or an animation on a canvas.
5+
به عنوان مثال، حرکت در امتداد یک مسیر پیچیده، با یک تابع زمان بندی متفاوت از منحنی های Bezier، یا یک انیمیشن روی بوم.
66

7-
## Using setInterval
7+
## بکار بردن setInterval
88

9-
An animation can be implemented as a sequence of frames -- usually small changes to HTML/CSS properties.
9+
یک انیمیشن را می توان به عنوان دنباله ای از فریم ها پیاده سازی کرد - معمولاً تغییرات کوچکی در ویژگی های HTML/CSS.
1010

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 فریم یا بیشتر در ثانیه برای صاف به نظر رسیدن کافی است.
1212

13-
The pseudo-code can look like this:
13+
شبه کد می تواند به شکل زیر باشد:
1414

1515
```js
1616
let timer = setInterval(function() {
@@ -19,7 +19,7 @@ let timer = setInterval(function() {
1919
}, 20); // change by 2px every 20ms, about 50 frames per second
2020
```
2121

22-
More complete example of the animation:
22+
نمونه کاملتر انیمیشن:
2323

2424
```js
2525
let start = Date.now(); // remember start time
@@ -45,19 +45,20 @@ function draw(timePassed) {
4545
}
4646
```
4747

48-
Click for the demo:
48+
برای دمو کلیک کنید:
4949

5050
[codetabs height=200 src="move"]
5151

52-
## Using requestAnimationFrame
52+
## بکار بردن requestAnimationFrame
5353

54-
Let's imagine we have several animations running simultaneously.
54+
بیایید تصور کنیم چندین انیمیشن به طور همزمان اجرا می شوند.
5555

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 میلی‌ثانیه` دوباره رنگ‌آمیزی کند.
5757

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 میلی ثانیه` خواهیم داشت.
59+
60+
به عبارت دیگر، این:
5961

60-
In other words, this:
6162

6263
```js
6364
setInterval(function() {
@@ -67,40 +68,40 @@ setInterval(function() {
6768
}, 20)
6869
```
6970

70-
...Is lighter than three independent calls:
71+
... سبکتر از سه تماس مستقل است:
7172

7273
```js
7374
setInterval(animate1, 20); // independent animations
7475
setInterval(animate2, 20); // in different places of the script
7576
setInterval(animate3, 20);
7677
```
7778

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 کمتری بارگیری شود و روان تر به نظر برسد.
7980

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 میلی ثانیه اجرا کنیم.
8182

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" را ارائه می دهد. به همه این مسائل و حتی بیشتر می پردازد.
8384

84-
The syntax:
85+
نحو:
8586
```js
8687
let requestId = requestAnimationFrame(callback)
8788
```
8889

89-
That schedules the `callback` function to run in the closest time when the browser wants to do animation.
90+
زمانی که مرورگر می‌خواهد انیمیشن انجام دهد، عملکرد `callback` را برنامه‌ریزی می‌کند تا در نزدیک‌ترین زمان اجرا شود.
9091

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 گروه‌بندی می‌شوند. بنابراین یک محاسبه مجدد هندسی و رنگ آمیزی مجدد به جای تعداد زیادی وجود خواهد داشت.
9293

93-
The returned value `requestId` can be used to cancel the call:
94+
مقدار بازگشتی درخواست Id می تواند برای لغو تماس استفاده شود:
9495
```js
9596
// cancel the scheduled execution of callback
9697
cancelAnimationFrame(requestId);
9798
```
9899

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

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 بیش از حد بارگیری شده باشد یا باتری لپ‌تاپ تقریباً خالی شده باشد یا دلیل دیگری وجود داشته باشد.
102103

103-
The code below shows the time between first 10 runs for `requestAnimationFrame`. Usually it's 10-20ms:
104+
کد زیر زمان بین 10 اجرای اول درخواست AnimationFrame را نشان می دهد. معمولاً 10-20 میلی ثانیه است:
104105

105106
```html run height=40 refresh
106107
<script>
@@ -116,9 +117,9 @@ The code below shows the time between first 10 runs for `requestAnimationFrame`.
116117
</script>
117118
```
118119

119-
## Structured animation
120+
## انیمیشن ساخت یافته
120121

121-
Now we can make a more universal animation function based on `requestAnimationFrame`:
122+
اکنون می‌توانیم یک تابع انیمیشن جهانی‌تر بر اساس `requestAnimationFrame` ایجاد کنیم:
122123

123124
```js
124125
function animate({timing, draw, duration}) {

0 commit comments

Comments
 (0)