-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilepin-styles.js
More file actions
128 lines (120 loc) · 3.7 KB
/
Copy pathtilepin-styles.js
File metadata and controls
128 lines (120 loc) · 3.7 KB
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
var _ = require('underscore');
function Styles(values) {
values = values || {};
function styles() {
var result = {};
_.each(arguments, function(obj) {
if (!obj)
return;
_.each(obj, function(val, key) {
var oldValue = result[key];
if (_.isObject(oldValue) && _.isObject(val)) {
val = styles(oldValue, val);
}
result[key] = val;
})
})
return result;
}
styles.clone = function() {
var newValues = JSON.parse(JSON.stringify(values));
return Styles(newValues);
}
styles.add = function() {
var args = [ values ].concat(_.toArray(arguments));
values = styles.apply(styles, args);
return styles;
}
styles.set = function() {
values = styles.apply(styles, arguments);
return styles;
}
styles.range = function(fromZoom, toZoom) {
var functions = _.toArray(arguments);
functions.splice(0, 2);
var delta = 1;
if (fromZoom > toZoom) {
delta = -1;
}
_.each(functions, function(f) {
for (var z = fromZoom; true; z += delta) {
var obj = f(z, fromZoom, toZoom);
if (obj) {
values = styles(values, obj);
}
if (z == toZoom)
break;
}
})
return styles;
}
styles.get = function() {
return values;
}
return styles;
}
Styles._linear = function(fromVal, toVal, f, getk) {
return function(zoom, fromZoom, toZoom) {
var val;
if (fromZoom == toZoom) {
val = fromVal;
} else {
var k = getk(zoom, fromZoom, toZoom);
val = (fromVal + k * (toVal - fromVal));
}
return f(zoom, val, fromZoom, toZoom);
}
}
function max(zoom, fromZoom, toZoom) {
return Math.abs(Math.max(fromZoom, toZoom) - zoom);
}
function min(zoom, fromZoom, toZoom) {
return Math.abs(Math.min(fromZoom, toZoom) - zoom);
}
// Linear increasing changes
Styles.linear = Styles.linearInc = Styles.inc = function(fromVal, toVal, f) {
return Styles._linear(fromVal, toVal, f, function(zoom, fromZoom, toZoom) {
return (zoom - fromZoom) / (toZoom - fromZoom);
})
}
// Linear decreasing changes
Styles.linearDec = Styles.dec = function(fromVal, toVal, f) {
return Styles._linear(fromVal, toVal, f, function(zoom, fromZoom, toZoom) {
return (toZoom - zoom) / (toZoom - fromZoom);
})
}
Styles._fib = function(pos) {
var array = Styles._fibArray = Styles._fibArray || [];
while (array.length <= pos) {
var b = array.length < 2 ? 0 : array[array.length - 2];
var a = array.length < 1 ? 1 : array[array.length - 1];
array.push(a + b);
}
return array[pos];
}
// Fibonacci sequence
Styles.fibInc = Styles.fib = function(f) {
return function(zoom, fromZoom, toZoom) {
var val = Styles._fib(min(zoom, fromZoom, toZoom));
return f(zoom, val, fromZoom, toZoom);
}
}
Styles.fibDec = function(f) {
return function(zoom, fromZoom, toZoom) {
var val = Styles._fib(max(zoom, fromZoom, toZoom));
return f(zoom, val, fromZoom, toZoom);
}
}
Styles.expInc = Styles.exp = function(n, f) {
return function(zoom, fromZoom, toZoom) {
var val = Math.pow(n, max(zoom, fromZoom, toZoom));
return f(zoom, val, fromZoom, toZoom);
}
}
Styles.expDec = function(n, f) {
return function(zoom, fromZoom, toZoom) {
var val = Math.pow(n, min(zoom, fromZoom, toZoom));
return f(zoom, val, fromZoom, toZoom);
}
}
module.exports = Styles;