Skip to content

Commit

Permalink
Clean up interpolate
Browse files Browse the repository at this point in the history
* Don't glom properties onto a function; flow doesn't handle this well.
* Remove unused vec2 function.
  • Loading branch information
jfirebaugh committed Nov 3, 2017
1 parent 1ef3cdb commit 05f5d8c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const LngLat = require('./lng_lat'),
Point = require('@mapbox/point-geometry'),
Coordinate = require('./coordinate'),
util = require('../util/util'),
interp = require('../style-spec/util/interpolate'),
interp = require('../style-spec/util/interpolate').number,
TileCoord = require('../source/tile_coord'),
EXTENT = require('../data/extent'),
glmatrix = require('@mapbox/gl-matrix');
Expand Down
35 changes: 15 additions & 20 deletions src/style-spec/util/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,27 @@

const Color = require('./color');

module.exports = interpolate;
module.exports = {
number,
color,
array
};

function interpolate(a: number, b: number, t: number) {
function number(a: number, b: number, t: number) {
return (a * (1 - t)) + (b * t);
}

interpolate.number = interpolate;

interpolate.vec2 = function(from: [number, number], to: [number, number], t: number) {
return [
interpolate(from[0], to[0], t),
interpolate(from[1], to[1], t)
];
};

interpolate.color = function(from: Color, to: Color, t: number) {
function color(from: Color, to: Color, t: number) {
return new Color(
interpolate(from.r, to.r, t),
interpolate(from.g, to.g, t),
interpolate(from.b, to.b, t),
interpolate(from.a, to.a, t)
number(from.r, to.r, t),
number(from.g, to.g, t),
number(from.b, to.b, t),
number(from.a, to.a, t)
);
};
}

interpolate.array = function(from: Array<number>, to: Array<number>, t: number) {
function array(from: Array<number>, to: Array<number>, t: number) {
return from.map((d, i) => {
return interpolate(d, to[i], t);
return number(d, to[i], t);
});
};
}
2 changes: 1 addition & 1 deletion src/style/style_transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class StyleTransition {

const timeFraction = Math.min((Date.now() - this.zoomHistory.lastIntegerZoomTime) / this.duration, 1);
const zoomFraction = Math.abs(z - lastIntegerZoom);
const t = interpolate(timeFraction, 1, zoomFraction);
const t = interpolate.number(timeFraction, 1, zoomFraction);

if (from === undefined || to === undefined)
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/symbol/get_anchors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

const interpolate = require('../style-spec/util/interpolate');
const interpolate = require('../style-spec/util/interpolate').number;
const Anchor = require('../symbol/anchor');
const checkMaxAngle = require('./check_max_angle');

Expand Down
2 changes: 1 addition & 1 deletion src/ui/camera.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow

const util = require('../util/util');
const interpolate = require('../style-spec/util/interpolate');
const interpolate = require('../style-spec/util/interpolate').number;
const browser = require('../util/browser');
const LngLat = require('../geo/lng_lat');
const LngLatBounds = require('../geo/lng_lat_bounds');
Expand Down
10 changes: 0 additions & 10 deletions test/unit/style-spec/interpolate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@ const test = require('mapbox-gl-js-test').test;
const interpolate = require('../../../src/style-spec/util/interpolate');
const Color = require('../../../src/style-spec/util/color');

test('interpolate.number', (t) => {
t.equal(interpolate(0, 1, 0.5), 0.5);
t.end();
});

test('interpolate.number', (t) => {
t.equal(interpolate.number(0, 1, 0.5), 0.5);
t.end();
});

test('interpolate.vec2', (t) => {
t.deepEqual(interpolate.vec2([0, 0], [1, 2], 0.5), [0.5, 1]);
t.end();
});

test('interpolate.color', (t) => {
t.deepEqual(interpolate.color(new Color(0, 0, 0, 0), new Color(1, 2, 3, 4), 0.5), new Color(0.5, 1, 3 / 2, 2));
t.end();
Expand Down

0 comments on commit 05f5d8c

Please sign in to comment.