michitaro / easing

easing functions for Typescript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

easing

Simple easing functions for TypeScript

Install

npm instasll --save @hscmap/easing

Example

import * as easing from "@hscmap/easing"


window.addEventListener('load', e => {
    const fs = [
        easing.linear,
        easing.fastStart2, easing.fastStart4,
        easing.slowStart2, easing.slowStart4,
        easing.slowStartStop2,
        easing.slowStartStop4,
    ]

    for (const f of fs) {
        const box = document.createElement('div')
        box.className = 'box'
        const title = document.createElement('h2')
        title.innerHTML = f.name
        const canvas = document.createElement('canvas')
        drawFunction(canvas, f)
        box.appendChild(title)
        box.appendChild(canvas)
        document.body.appendChild(box)
    }
})


function drawFunction(canvas: HTMLCanvasElement, f: (x: number) => number) {
    function canvasCoord(x: number, y: number) {
        return [canvas.width * x, canvas.height * (1 - y)]
    }
    const ctx = canvas.getContext('2d')!
    ctx.beginPath()
    for (let x = 0; x <= 1; x += 0.01) {
        const y = f(x)
        const [X, Y] = canvasCoord(x, y)
        ctx.lineTo(X, Y)
    }
    ctx.stroke()
}

About

easing functions for Typescript

License:MIT License


Languages

Language:TypeScript 56.2%Language:JavaScript 33.6%Language:HTML 10.2%