UNPKG

s2maps-gpu

Version:

S2 Maps GPU - An open source, high-performance, and GPU-accelerated map engine for rendering large-scale, interactive maps.

95 lines (94 loc) 2.71 kB
import { Color, interpolate } from './color/index.js'; /** * Convert a string to a function that will return an interpolation * between two values or colors. * @param easeType - ease type * @param base - base value * @returns an easing function */ export default function getEasingFunction(easeType = 'lin', base = 1) { const func = easeType === 'lin' ? linear : easeType === 'expo' ? exponential : easeType === 'quad' ? quad : easeType === 'cubic' ? cubic : step; return (zoom, start, end, startValue, endValue) => { const t = func(zoom, start, end, base); if (typeof startValue === 'number' && typeof endValue === 'number') return (startValue + t * (endValue - startValue)); else if (startValue instanceof Color && endValue instanceof Color) return interpolate(startValue, endValue, t); else return startValue; }; } /** * Linear ease function: y = mx * @param input - input value * @param start - start value * @param end - end value * @returns the interpolated value */ function linear(input, start, end) { return (input - start) / (end - start); } /** * Exponential ease function: y = e^x OR y = Math.pow(2, 10 * x) * @param input - input value * @param start - start value * @param end - end value * @param base - base value * @returns the interpolated value */ function exponential(input, start, end, base) { // grab change const diff = end - start; if (diff === 0) return 0; // refine base value if (base <= 0) base = 0.1; else if (base > 2) base = 2; // grab diff const progress = input - start; // linear case if (base === 1) return progress / diff; // solve return (Math.pow(base, progress) - 1) / (Math.pow(base, diff) - 1); } /** * Quadratic ease function: y = x^2 * @param input - input value * @param start - start value * @param end - end value * @returns the interpolated value */ function quad(input, start, end) { return Math.pow(input - start, 2) / Math.pow(end - start, 2); } /** * Cubic ease function: y = x^3 * @param input - input value * @param start - start value * @param end - end value * @returns the interpolated value */ function cubic(input, start, end) { return Math.pow(input - start, 3) / Math.pow(end - start, 3); } /** * Stepping ease function: y = 1 or 0 * @param input - input value * @param _start - start value * @param end - end value * @returns the interpolated value */ function step(input, _start, end) { return input >= end ? 1 : 0; }