type Curve = Array<[number, number, number]>;
type Vec4 = [number, number, number, number];
function compileInterpolateExpr (expr: any): Curve {
const [ tag, [curve, exp], [param], ...steps] = expr;
if (tag !== 'interpolate') {
return null;
}
const base = curve === 'linear' ? 1 : exp;
if (param !== 'zoom') {
return null;
}
const result = [];
for (let i = 0; i < steps.length; i += 2) {
const matchValue = steps[i],
matchResult = steps[i + 1];
result.push([matchValue, matchResult, base])
}
return result;
}
/**
* Получает значение из переданной кривой для переданного зума
*/
function sample(curve: Curve, styleZoom: number): number {
const segNumber = getCurveSegment(curve, styleZoom);
if (segNumber === 0) {
return curve[0][1];
}
if (segNumber === curve.length) {
return sampleLastPoint(curve, styleZoom);
}
const ratio = getRatio(curve, styleZoom, segNumber);
return (1 - ratio) * curve[segNumber - 1][1] + ratio * curve[segNumber][1];
}
/**
* Экстраполирует значение кривой для случая, когда зум больше её последней точки
*/
function sampleLastPoint(curve: Curve, styleZoom: number): number {
const lastPoint = curve[curve.length - 1];
const base = lastPoint[2];
if (base === 1) {
return lastPoint[1];
}
return lastPoint[1] * Math.pow(base, styleZoom - lastPoint[0]);
}
/**
* Возвращает индекс сегмента кривой, которому принадлежит переданный зум
* Индекс сегмента = индексу его правой точки
*/
function getCurveSegment(curve: Curve, styleZoom: number): number {
let i = 0;
while (i < curve.length) {
if (styleZoom < curve[i][0]) {
break;
} else {
i++;
}
}
return i;
}
/**
* Возвращает соотношение для интерполяции между левой и правой точками сегмента
*/
function getRatio(curve: Curve, styleZoom: number, segNumber: number): number {
const...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.