Rob Colbert
2 years ago
7 changed files with 724 additions and 8 deletions
@ -0,0 +1,13 @@ |
|||
Copyright 2022 Rob Colbert @[email protected] |
|||
|
|||
Licensed under the Apache License, Version 2.0 (the "License"); |
|||
you may not use this file except in compliance with the License. |
|||
You may obtain a copy of the License at |
|||
|
|||
http://www.apache.org/licenses/LICENSE-2.0 |
|||
|
|||
Unless required by applicable law or agreed to in writing, software |
|||
distributed under the License is distributed on an "AS IS" BASIS, |
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
See the License for the specific language governing permissions and |
|||
limitations under the License. |
@ -0,0 +1,538 @@ |
|||
// lib/nice-easing.js
|
|||
// Copyright (C) 2022 Rob Colbert @[email protected]
|
|||
// License: Apache-2.0
|
|||
|
|||
/* |
|||
* NiceEasing is derived from: |
|||
* https://github.com/kaelzhang/easing-functions
|
|||
* https://raw.githubusercontent.com/kaelzhang/easing-functions/master/index.js
|
|||
* License: [unclear, MIT, acknowledges Phaser.io] |
|||
* |
|||
* Which is derived from: |
|||
* https://github.com/photonstorm/phaser
|
|||
* License: MIT (C) 2014 Phaser.io |
|||
*/ |
|||
|
|||
'use strict'; |
|||
|
|||
/** |
|||
* A collection of easing methods defining ease-in ease-out curves. |
|||
* |
|||
* @class Easing |
|||
*/ |
|||
class NiceEasing { } |
|||
|
|||
/** |
|||
* Linear easing. |
|||
* |
|||
* @class Easing.Linear |
|||
*/ |
|||
class Linear { |
|||
|
|||
/** |
|||
* Ease-in. |
|||
* |
|||
* @method Easing.Linear#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} Simply: k |
|||
*/ |
|||
static None (k) { |
|||
return k; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Quadratic easing. |
|||
* |
|||
* @class Easing.Quadratic |
|||
*/ |
|||
class Quadratic { |
|||
|
|||
/** |
|||
* Ease-in. |
|||
* |
|||
* @method Easing.Quadratic#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} k^2. |
|||
*/ |
|||
static In (k) { |
|||
return k * k; |
|||
} |
|||
|
|||
/** |
|||
* Ease-out. |
|||
* |
|||
* @method Easing.Quadratic#Out |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} k* (2-k). |
|||
*/ |
|||
static Out (k) { |
|||
return k * (2 - k); |
|||
} |
|||
|
|||
/** |
|||
* Ease-in/out. |
|||
* |
|||
* @method Easing.Quadratic#InOut |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static InOut (k) { |
|||
if ((k *= 2) < 1) { |
|||
return 0.5 * k * k; |
|||
} |
|||
return -0.5 * (--k * (k - 2) - 1); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Cubic easing. |
|||
* |
|||
* @class Easing.Cubic |
|||
*/ |
|||
class Cubic { |
|||
|
|||
/** |
|||
* Cubic ease-in. |
|||
* |
|||
* @method Easing.Cubic#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static In (k) { |
|||
return k * k * k; |
|||
} |
|||
|
|||
/** |
|||
* Cubic ease-out. |
|||
* |
|||
* @method Easing.Cubic#Out |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static Out (k) { |
|||
return --k * k * k + 1; |
|||
} |
|||
|
|||
/** |
|||
* Cubic ease-in/out. |
|||
* |
|||
* @method Easing.Cubic#InOut |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static InOut (k) { |
|||
if ((k *= 2) < 1) { |
|||
return 0.5 * k * k * k; |
|||
} |
|||
return 0.5 * ((k -= 2) * k * k + 2); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Quartic easing. |
|||
* |
|||
* @class Easing.Quartic |
|||
*/ |
|||
class Quartic { |
|||
|
|||
/** |
|||
* Quartic ease-in. |
|||
* |
|||
* @method Easing.Quartic#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static In (k) { |
|||
return k * k * k * k; |
|||
} |
|||
|
|||
/** |
|||
* Quartic ease-out. |
|||
* |
|||
* @method Easing.Quartic#Out |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static Out (k) { |
|||
return 1 - (--k * k * k * k); |
|||
} |
|||
|
|||
/** |
|||
* Quartic ease-in/out. |
|||
* |
|||
* @method Easing.Quartic#InOut |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static InOut (k) { |
|||
if ((k *= 2) < 1) { |
|||
return 0.5 * k * k * k * k; |
|||
} |
|||
return -0.5 * ((k -= 2) * k * k * k - 2); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Quintic easing. |
|||
* |
|||
* @class Easing.Quintic |
|||
*/ |
|||
class Quintic { |
|||
|
|||
/** |
|||
* Quintic ease-in. |
|||
* |
|||
* @method Easing.Quintic#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static In (k) { |
|||
return k * k * k * k * k; |
|||
} |
|||
|
|||
/** |
|||
* Quintic ease-out. |
|||
* |
|||
* @method Easing.Quintic#Out |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static Out (k) { |
|||
return --k * k * k * k * k + 1; |
|||
} |
|||
|
|||
/** |
|||
* Quintic ease-in/out. |
|||
* |
|||
* @method Easing.Quintic#InOut |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static InOut (k) { |
|||
if ((k *= 2) < 1) { |
|||
return 0.5 * k * k * k * k * k; |
|||
} |
|||
return 0.5 * ((k -= 2) * k * k * k * k + 2); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Sinusoidal easing. |
|||
* |
|||
* @class Easing.Sinusoidal |
|||
*/ |
|||
class Sinusoidal { |
|||
|
|||
/** |
|||
* Sinusoidal ease-in. |
|||
* |
|||
* @method Easing.Sinusoidal#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static In (k) { |
|||
return 1 - Math.cos(k * Math.PI / 2); |
|||
} |
|||
|
|||
/** |
|||
* Sinusoidal ease-out. |
|||
* |
|||
* @method Easing.Sinusoidal#Out |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static Out (k) { |
|||
return Math.sin(k * Math.PI / 2); |
|||
} |
|||
|
|||
/** |
|||
* Sinusoidal ease-in/out. |
|||
* |
|||
* @method Easing.Sinusoidal#InOut |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static InOut (k) { |
|||
return 0.5 * (1 - Math.cos(Math.PI * k)); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Exponential easing. |
|||
* |
|||
* @class Easing.Exponential |
|||
*/ |
|||
class Exponential { |
|||
|
|||
/** |
|||
* Exponential ease-in. |
|||
* |
|||
* @method Easing.Exponential#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static In (k) { |
|||
return k === 0 ? 0 : Math.pow(1024, k - 1); |
|||
} |
|||
|
|||
/** |
|||
* Exponential ease-out. |
|||
* |
|||
* @method Easing.Exponential#Out |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static Out (k) { |
|||
return k === 1 ? 1 : 1 - Math.pow(2, - 10 * k); |
|||
} |
|||
|
|||
/** |
|||
* Exponential ease-in/out. |
|||
* |
|||
* @method Easing.Exponential#InOut |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static InOut (k) { |
|||
if (k === 0) { return 0; } |
|||
if (k === 1) { return 1; } |
|||
if ((k *= 2) < 1) { |
|||
return 0.5 * Math.pow(1024, k - 1); |
|||
} |
|||
return 0.5 * (-Math.pow(2, - 10 * (k - 1)) + 2); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Circular easing. |
|||
* |
|||
* @class Easing.Circular |
|||
*/ |
|||
class Circular { |
|||
|
|||
/** |
|||
* Circular ease-in. |
|||
* |
|||
* @method Easing.Circular#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static In (k) { |
|||
return 1 - Math.sqrt(1 - k * k); |
|||
} |
|||
|
|||
/** |
|||
* Circular ease-out. |
|||
* |
|||
* @method Easing.Circular#Out |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static Out (k) { |
|||
return Math.sqrt(1 - (--k * k)); |
|||
} |
|||
|
|||
/** |
|||
* Circular ease-in/out. |
|||
* |
|||
* @method Easing.Circular#InOut |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static InOut (k) { |
|||
if ((k *= 2) < 1) { |
|||
return - 0.5 * (Math.sqrt(1 - k * k) - 1); |
|||
} |
|||
return 0.5 * (Math.sqrt(1 - (k -= 2) * k) + 1); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Elastic easing. |
|||
* |
|||
* @class Easing.Elastic |
|||
*/ |
|||
class Elastic { |
|||
|
|||
/** |
|||
* Elastic ease-in. |
|||
* |
|||
* @method Easing.Elastic#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static In (k) { |
|||
var s, a = 0.1, p = 0.4; |
|||
if (k === 0) { return 0; } |
|||
if (k === 1) { return 1; } |
|||
if (!a || a < 1) { |
|||
a = 1; s = p / 4; |
|||
} else { |
|||
s = p * Math.asin(1 / a) / (2 * Math.PI); |
|||
} |
|||
return -(a * Math.pow(2, 10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p)); |
|||
} |
|||
|
|||
/** |
|||
* Elastic ease-out. |
|||
* |
|||
* @method Easing.Elastic#Out |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static Out (k) { |
|||
var s, a = 0.1, p = 0.4; |
|||
if (k === 0) { return 0; } |
|||
if (k === 1) { return 1; } |
|||
if (!a || a < 1) { |
|||
a = 1; s = p / 4; |
|||
} else { |
|||
s = p * Math.asin(1 / a) / (2 * Math.PI); |
|||
} |
|||
return (a * Math.pow(2, - 10 * k) * Math.sin((k - s) * (2 * Math.PI) / p) + 1); |
|||
} |
|||
|
|||
/** |
|||
* Elastic ease-in/out. |
|||
* |
|||
* @method Easing.Elastic#InOut |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static InOut (k) { |
|||
var s, a = 0.1, p = 0.4; |
|||
if (k === 0) { return 0; } |
|||
if (k === 1) { return 1; } |
|||
if (!a || a < 1) { |
|||
a = 1; s = p / 4; |
|||
} |
|||
else { |
|||
s = p * Math.asin(1 / a) / (2 * Math.PI); |
|||
} |
|||
if ((k *= 2) < 1) { |
|||
return -0.5 * (a * Math.pow(2, 10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p)); |
|||
} |
|||
return a * Math.pow(2, -10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p) * 0.5 + 1; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Back easing. |
|||
* |
|||
* @class Easing.Back |
|||
*/ |
|||
class Back { |
|||
|
|||
/** |
|||
* Back ease-in. |
|||
* |
|||
* @method Easing.Back#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static In (k) { |
|||
var s = 1.70158; |
|||
return k * k * ((s + 1) * k - s); |
|||
} |
|||
|
|||
/** |
|||
* Back ease-out. |
|||
* |
|||
* @method Easing.Back#Out |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static Out (k) { |
|||
var s = 1.70158; |
|||
return --k * k * ((s + 1) * k + s) + 1; |
|||
} |
|||
|
|||
/** |
|||
* Back ease-in/out. |
|||
* |
|||
* @method Easing.Back#InOut |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static InOut (k) { |
|||
var s = 1.70158 * 1.525; |
|||
if ((k *= 2) < 1) { |
|||
return 0.5 * (k * k * ((s + 1) * k - s)); |
|||
} |
|||
return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Bounce easing. |
|||
* |
|||
* @class Easing.Bounce |
|||
*/ |
|||
class Bounce { |
|||
|
|||
/** |
|||
* Bounce ease-in. |
|||
* |
|||
* @method Easing.Bounce#In |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static In (k) { |
|||
return 1 - NiceEasing.Bounce.Out(1 - k); |
|||
} |
|||
|
|||
/** |
|||
* Bounce ease-out. |
|||
* |
|||
* @method Easing.Bounce#Out |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static Out (k) { |
|||
if (k < (1 / 2.75)) { |
|||
return 7.5625 * k * k; |
|||
} else if (k < (2 / 2.75)) { |
|||
return 7.5625 * (k -= (1.5 / 2.75)) * k + 0.75; |
|||
} else if (k < (2.5 / 2.75)) { |
|||
return 7.5625 * (k -= (2.25 / 2.75)) * k + 0.9375; |
|||
} else { |
|||
return 7.5625 * (k -= (2.625 / 2.75)) * k + 0.984375; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Bounce ease-in/out. |
|||
* |
|||
* @method Easing.Bounce#InOut |
|||
* @param {number} k - The value to be tweened. |
|||
* @returns {number} The tweened value. |
|||
*/ |
|||
static InOut (k) { |
|||
if (k < 0.5) { |
|||
return NiceEasing.Bounce.In(k * 2) * 0.5; |
|||
} |
|||
return NiceEasing.Bounce.Out(k * 2 - 1) * 0.5 + 0.5; |
|||
} |
|||
} |
|||
|
|||
/* |
|||
* Assign "child" classes to NiceEasing |
|||
*/ |
|||
NiceEasing.Linear = Linear; |
|||
NiceEasing.Quadratic = Quadratic; |
|||
NiceEasing.Cubic = Cubic; |
|||
NiceEasing.Quartic = Quartic; |
|||
NiceEasing.Quintic = Quintic; |
|||
NiceEasing.Sinusoidal = Sinusoidal; |
|||
NiceEasing.Exponential = Exponential; |
|||
NiceEasing.Circular = Circular; |
|||
NiceEasing.Elastic = Elastic; |
|||
NiceEasing.Back = Back; |
|||
NiceEasing.Bounce = Bounce; |
|||
|
|||
/* |
|||
* Export NiceEasing |
|||
*/ |
|||
|
|||
export default NiceEasing; |
@ -0,0 +1,50 @@ |
|||
// lib/nice-tween.js
|
|||
// Copyright (C) 2022 Rob Colbert @[email protected]
|
|||
// License: Apache-2.0
|
|||
|
|||
'use strict'; |
|||
|
|||
import NiceEasing from './nice-easing.js'; |
|||
|
|||
/** |
|||
* Animate a list of properties on a target object over time using a |
|||
* mathematical easing function. |
|||
*/ |
|||
export default class NiceTween { |
|||
|
|||
/** |
|||
* NiceTween will animate a liste of properties on a target object over time. |
|||
* @param {Object} target The object being influenced by the tween. |
|||
* @param {*} from An object describing the starting values of affected |
|||
* properties. |
|||
* @param {*} to An object describing the ending values of affected |
|||
* properties. |
|||
* @param {*} easing The easing function to apply to the tween |
|||
*/ |
|||
constructor ( |
|||
target, |
|||
from, |
|||
to, |
|||
easing = NiceEasing.Linear.None, |
|||
) { |
|||
this.target = target; |
|||
this.from = from; |
|||
this.to = to; |
|||
this.easing = easing; |
|||
|
|||
this.state = 'idle'; |
|||
this.start = null; |
|||
this.elapsed = 0; |
|||
this.duration = 0; |
|||
} |
|||
|
|||
start (duration) { |
|||
this.start = performance.now(); |
|||
this.duration = duration; |
|||
this.state = 'running'; |
|||
} |
|||
|
|||
update (elapsed) { |
|||
|
|||
} |
|||
} |
Loading…
Reference in new issue