-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathparse.ts
225 lines (202 loc) · 5.94 KB
/
parse.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { decimalsLength, getMapTypeAndValue } from './helpers';
import { extractValues } from './directives';
import { getPrefix } from './prefix';
import { FilterValue } from './directives/tg-filter';
import { EdgeOptions } from './directives/tg-edge';
import { TgElement } from './type';
import { easePercentage as ease } from './ease';
import { gradientColors } from './color';
import rgbcolor from 'rgb-color';
/**
* This function will be called in observe stage,
* caching those values into an object for ease of use in scroll event.
*/
export function parseAttributes(element: HTMLElement): TgElement {
const prefix = getPrefix();
const follow: HTMLElement = extractValues(element, `${prefix}follow`);
const actualElement = follow || element;
const style = getComputedStyle(actualElement);
const top = +style.getPropertyValue(`--${prefix}top`);
const height = +style.getPropertyValue(`--${prefix}height`);
const name: string = extractValues(element, `${prefix}name`);
const from: number = extractValues(actualElement, `${prefix}from`);
const to: number = extractValues(actualElement, `${prefix}to`);
const steps: number = extractValues(actualElement, `${prefix}steps`);
const step: number = extractValues(actualElement, `${prefix}step`);
const bezier: string | Array<number> = extractValues(
actualElement,
`${prefix}bezier`
);
const filter: FilterValue = extractValues(element, `${prefix}filter`);
const edge: EdgeOptions = extractValues(actualElement, `${prefix}edge`);
const range = Math.abs(to - from);
const increment = step === 0 ? range / steps : step;
const segments = range / increment;
const decimals = decimalsLength(increment);
const multiplier = from > to ? -1 : 1;
const mapping: Record<string, string> = extractValues(
element,
`${prefix}map`,
{
increment,
decimals,
}
);
return {
el: element,
top,
height,
name,
from,
to,
steps,
step,
mapping,
filter,
edge,
range,
increment,
segments,
decimals,
multiplier,
lastValue: null,
bezier,
};
}
/**
* Calculation happens here,
* this function is called when scroll event happens.
* So keep this as light as possible.
*/
export function parseValues(elements: TgElement[]) {
const { scrollTop: scrolled, clientHeight } = document.documentElement;
elements.forEach((element) => {
const {
el,
top,
height,
increment,
segments,
decimals,
multiplier,
name,
from,
// currently unused
// to,
mapping,
filter,
edge,
lastValue,
bezier,
} = element;
// If the name is equal to '_' (--_), skip
if (name === '--_') {
return;
}
// edge is 'cover' by default
let percentage =
edge === 'cover'
? Math.min(
Math.max(
(scrolled + clientHeight - top) / (clientHeight + height),
0
),
1
)
: Math.min(Math.max((scrolled - top) / (height - clientHeight), 0), 1);
// Calculation result value of bezier
percentage = bezier ? ease(bezier, percentage) : percentage;
let value: string | number;
const mappingValue = (
from +
Math.floor((segments + 1) * percentage) * increment * multiplier
).toFixed(decimals);
value = +mappingValue;
if (filter.values.length > 0 && !filter.values.includes(value)) {
// If the mode is 'exact', remove the CSS property
// Setting the lastValue to null to ensure correct comparison below
if (filter.mode === 'smooth') {
let region = calcKeyFrameRegion(mapping, value);
// console.log(region);
if (region.length) {
value = calcKeyFrameValue(mapping, value, region);
} else {
return;
}
} else {
if (filter.mode === 'exact') {
element.lastValue = null;
el.style.removeProperty(name);
}
return;
}
}
if (typeof mapping[value] !== 'undefined') {
value = mapping[value];
}
if (lastValue != value) {
el.style.setProperty(name, `${value}`);
el.dispatchEvent(
new CustomEvent('tg', {
// @ts-ignore
target: el,
detail: {
value,
},
})
);
element.lastValue = value;
}
});
}
// Calculate the result value of the keyframe corresponding to the current value
const calcKeyFrameValue = (
mapping: Record<string, string>,
value: number,
region: Array<any>
) => {
const [lkey, rkey] = region;
const lValue = getMapTypeAndValue(mapping[lkey]);
const rValue = getMapTypeAndValue(mapping[rkey]);
const curKeyRange = Math.abs(value - +lkey);
const percentage = curKeyRange / (rkey - lkey);
// If the value is a color value
if (rValue.type === 'color' && lValue.type === 'color') {
// TODO: Link the steps here with user settings……
const steps = 100;
const graColor = gradientColors(lValue.val, rValue.val, steps);
const key = (percentage * steps).toFixed(0);
return graColor[key];
} else {
const range = Math.abs(rValue.val - lValue.val);
const multiplier = lValue.val > rValue.val ? -1 : 1;
return lValue.val + range * percentage * multiplier;
}
};
// Calculate which range the current value is in the mapping
const calcKeyFrameRegion = (mapping: Record<string, string>, value: number) => {
if (Object.keys(mapping).length === 1) {
return [];
} else {
let lastKey = '',
currentKey = '';
for (const key of Object.getOwnPropertyNames(mapping).sort((a, b) => {
return +a - +b;
})) {
if (lastKey === null) {
lastKey = key;
continue;
} else {
currentKey = key;
if (
(value >= +lastKey && value <= +currentKey) ||
(value >= +currentKey && value <= +lastKey)
) {
return [lastKey, currentKey];
}
}
lastKey = key;
}
}
return [];
};