> For the complete documentation index, see [llms.txt](https://antonshevchuk.gitbook.io/jquery-for-beginners/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://antonshevchuk.gitbook.io/jquery-for-beginners/uk/90_create_plugin/easing.md). Agents can also search and read this documentation through its [MCP server](https://antonshevchuk.gitbook.io/jquery-for-beginners/~gitbook/mcp).

# Easing

Це не easy

Тепер знову звернемося до `easing` – наведу приклад довільної функції, якій слідуватиме анімація. Щоб особливо не фантазувати, я взяв приклад зі статті на колись популярному Хабрі про анімацію у MooTools фреймворку – наочний приклад із серцебиттям, яке описується наступними функціями:

<figure><img src="https://1484847317-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPapTvBcCpmxaFFXJLlyw%2Fuploads%2Fgit-blob-6e0cb439d0122dc48b97a1420a70dc91d6a11058%2Feasing.svg?alt=media" alt=""><figcaption></figcaption></figure>

У розширенні функціоналу easing немає нічого воєнного:

```javascript
$.extend($.easing, {
    /**
     * Heart Beat
     *
     * @param {Float} x progress
     */
    heart:function(x) {
        if (x < 0.3) return Math.pow(x, 4) * 49.4;
        if (x < 0.4) return 9 * x - 2.3;
        if (x < 0.5) return -13 * x + 6.5;
        if (x < 0.6) return 4 * x - 2;
        if (x < 0.7) return 0.4;
        if (x < 0.75) return 4 * x - 2.4;
        if (x < 0.8) return -4 * x + 3.6;
        if (x >= 0.8) return 1 - Math.sin(Math.acos(x));
    }
});
```

Трішки пояснень, конструкція `$.extend({}, {})` «змішує» об'єкти:

```javascript
$.extend({name:"Anton"}, {location:"Kharkiv"});
// >>>
{
  "name": "Anton",
  "location": "Kharkiv"
};

$.extend({name:"Anton", location:"Kharkiv"}, {location:"Kyiv"});
// >>>
{
  "name": "Anton",
  "location": "Kyiv"
}
```

Таким чином ми «вмішуємо» новий метод до наявного об'єкта `$.easing`; згідно з кодом, наш метод приймає як параметр лише одне значення:

`x` – коефіцієнт проходження анімації, змінюється від 0 до 1, дробове

Результат, звісно, цікавий, але його можна ще трішки розширити додатковими функціями (розгорнемо та скомбінуємо):

```javascript
$.extend(jQuery.easing, {
    heartIn: function (x) {
        return $.easing.heart(x);
    },
    heartOut: function (x) {
        return $.easing.heart(1 - x);
    },
    heartInOut: function (x) {
        if (x < 0.5) return $.easing.heartIn(x);
        return $.easing.heartOut(x);
    }
});
```

Отримаємо наступні похідні функції:

|                                                                                                  **heartIn**                                                                                                  |                                                                                                   **heartOut**                                                                                                  |                                                                                                    **heartInOut**                                                                                                   |
| :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| ![heartIn](https://1484847317-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPapTvBcCpmxaFFXJLlyw%2Fuploads%2Fgit-blob-87aa6033009c65a12f16a894f441d7a4d7f10cb5%2FheartIn.png?alt=media) | ![heartOut](https://1484847317-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPapTvBcCpmxaFFXJLlyw%2Fuploads%2Fgit-blob-db5de6cb8fa8b3f2dd3eba2302a94ce67095eb89%2FheartOut.png?alt=media) | ![heartInOut](https://1484847317-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPapTvBcCpmxaFFXJLlyw%2Fuploads%2Fgit-blob-022c4b604b59acd044d393c786f1a5643ed3fda6%2FheartInOut.png?alt=media) |

Працювати із цим витвором треба наступним чином:

```javascript
$("#my").animate({height:"+200px"}, 2000, "heartIn"); // ось воно
```

Приклад роботи цієї функції можна помацати на відповідній сторінці [animate.easing.html](https://anton.shevchuk.name/book/code/animate.easing.html):

{% embed url="<https://anton.shevchuk.name/book/code/animate.easing.html>" %}
