Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 80x 80x 80x 80x 184x 184x 172x 12x 12x 12x 6x 6x 12x 6x 6x 184x | import * as is from '../is/is';
import type {ColorRGBA} from '../mathf/color';
import * as color from '../mathf/color';
import * as mathf from '../mathf/mathf';
import {CssUnitObjectTypes, cssUnit} from '../string/css-unit';
export interface interpolateConfig {
/**
* The value at which interperolation should begin. Also accepts
* css unit types (10px, rgba(255, 255, 255, 0.3))
* @type {number|string}
*/
from: number | string;
/**
* The value at which interperolation should begin. Also accepts css
* unit types (10px, rgba(255, 255, 255, 0.3)).
* @type {number|string}
*/
to: number | string;
/**
* The easing function to use.
* @type {Function}
*/
easeFunction: Function;
}
/**
* A class that interperolate between values. Interperolate is really a
* compositoin around mathf.ease.
*
* ```ts
* import { EASE, Interpolate } from '@blinkk/degu'
* let inter = new Interpolate({
* from: 0,
* to: 100,
* easeFunction: EASE.linear
* });
*
* inter.calculate(0.3); // return 30.
*
* inter.calculate(0.6); // return 60
*
* // Get the value from currentValue if needed.
* console.log(inter.currentValue); // 60
* ```
*
* This can be useful to create low level tweens.
* Here for example, we can manually, tween the x and y positions
* of a ball with separate easing.
*
* ```ts
* import { Raf, EASE, Interpolate } from '@blinkk/degu'
*
* this.progress = 0;
* let ballXInter = new Interpolate({
* from: 30,
* to: 100 ,
* easeFunction: EASE.easeOutSine
* })
* let ballYInter = new Interpolate({
* from: 30,
* to: 100 ,
* easeFunction: EASE.linear
* })
*
* // See RafTimer for how this works. Basically, here we are running raf for
* // 300ms and the progress is a value between 0-1 which we used to passed to the
* // interperolators.
* new RafTimer((progress)=> {
* // Update the positions of ball.
* ball.x = ballXInter.calculate(this.progress);
* ball.y = ballYInter.calculate(this.progress);
* }).start(300);
*
* See more examples in the /examples folder
*
* ```
*
*
* Unit and Color support.
*
* Interpolate also uses [[cssUnit]] to interprete string values (as best as it
* can). You can pass basic css unit values such as '10px' or '10vw' as
* from and to values. Color values of rgba and hex are also supported.
*
*
* It does NOT support cross unit interporalation at the moment such as
* from: '0px', to: '10vw'. (Here the units don't match.)
*
* ```ts
* let inter = new Interpolate({
* from: '0px',
* to: '100px',
* easeFunction: EASE.linear
* });
* inter.calculate(0.3); // return '30px'.
*
*
* inter = new Interpolate({
* from: 'rgba(255, 255, 255, 0)',
* to: 'rgba(0,0,0,1)',
* easeFunction: EASE.linear
* });
* inter.calculate(0.5) // 'rgba(127, 127, 127, 0.5)'
* ```
*
*
* @tested
*/
export class Interpolate {
/**
* The current interperolated value.
*/
public currentValue: number | string;
/**
* The current progress.
*/
public currentProgress: number;
constructor(private interpolateConfig: interpolateConfig) {
this.currentValue = 0;
this.currentProgress = 0;
// Assume we start on preogress 0.
this.calculate(0);
}
/**
* Modify the interpolate settings.
* @param interpolateConfig
*/
modify(interpolateConfig: interpolateConfig): Interpolate {
return new Interpolate(interpolateConfig);
}
calculate(progress: number) {
this.currentProgress = progress;
// If we are just interpolating a number just calculate a straight ease.
if (is.number(this.interpolateConfig.from)) {
this.currentValue = mathf.ease(
this.interpolateConfig.from as number,
this.interpolateConfig.to as number,
this.currentProgress,
this.interpolateConfig.easeFunction
);
} else {
// If we are interpolating a string.
// Use the from value to determine the type of cssUnit this is.
const from = cssUnit.parse(this.interpolateConfig.from as string);
const to = cssUnit.parse(this.interpolateConfig.to as string);
// If unit type, interpolate the values and append a unit.
if (from.valueType === CssUnitObjectTypes.number) {
this.currentValue = mathf.ease(
from.value as number,
to.value as number,
this.currentProgress,
this.interpolateConfig.easeFunction
);
this.currentValue = `${this.currentValue}${from.unit}`;
}
// If it's an rgba type inteporalate it.
if (from.valueType === CssUnitObjectTypes.rgba) {
const interpolatedRgba = color.rgbaEase(
from.value as ColorRGBA,
to.value as ColorRGBA,
this.currentProgress,
this.interpolateConfig.easeFunction
);
this.currentValue = color.rgbaToCss(interpolatedRgba);
}
}
return this.currentValue;
}
}
|