paperjs / paper.js

The Swiss Army Knife of Vector Graphics Scripting – Scriptographer ported to JavaScript and the browser, using HTML5 Canvas. Created by @lehni & @puckey
http://paperjs.org
Other
14.5k stars 1.23k forks source link

Missing easing functions #1855

Open jonashw opened 4 years ago

jonashw commented 4 years ago

I would like to implement easeIn/easeOut/easeInOut variants for sine, exponential, and circular easing functions. Is there any reason they haven't already been implemented?

Current list per the docs: ‘linear’ ‘easeInQuad’ ‘easeOutQuad’ ‘easeInOutQuad’ ‘easeInCubic’ ‘easeOutCubic’ ‘easeInOutCubic’ ‘easeInQuart’ ‘easeOutQuart’ ‘easeInOutQuart’ ‘easeInQuint’ ‘easeOutQuint’ ‘easeInOutQuint’ — default: ‘linear’

lehni commented 4 years ago

Yeah we should probably add these. Until then, you can do:

import { Tween } from 'paper'

const { PI, sin, cos, sqrt, pow } = Math

// https://github.com/ai/easings.net/tree/master/src/easings/easingsFunctions.ts

Object.assign(Tween.easings, {
  easeInSine(x) {
    return 1 - cos((x * PI) / 2)
  },
  easeOutSine(x) {
    return sin((x * PI) / 2)
  },
  easeInOutSine(x) {
    return -(cos(PI * x) - 1) / 2
  },
  easeInExpo(x) {
    return x === 0 ? 0 : pow(2, 10 * x - 10)
  },
  easeOutExpo(x) {
    return x === 1 ? 1 : 1 - pow(2, -10 * x)
  },
  easeInOutExpo(x) {
    return x === 0
      ? 0
      : x === 1
        ? 1
        : x < 0.5
          ? pow(2, 20 * x - 10) / 2
          : (2 - pow(2, -20 * x + 10)) / 2
  },
  easeInCirc(x) {
    return 1 - sqrt(1 - pow(x, 2))
  },
  easeOutCirc(x) {
    return sqrt(1 - pow(x - 1, 2))
  },
  easeInOutCirc(x) {
    return x < 0.5
      ? (1 - sqrt(1 - pow(2 * x, 2))) / 2
      : (sqrt(1 - pow(-2 * x + 2, 2)) + 1) / 2
  }
})