-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
92 lines (81 loc) · 2.17 KB
/
demo.js
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
import transit from './src/index.ts';
const obj = document.getElementById('obj');
const jsonInput = document.getElementById('json');
const colorsInput = document.getElementById('colors');
const colorsSubmit = document.getElementById('colorsSubmit');
const resultInput = document.getElementById('result');
const gradient = document.getElementById('grad');
const applyStyle = style => {
obj.style.transform = `translate(${style.left}px, 40px) scale(${
style.scale
})`;
obj.style.color = style.color || '#000';
obj.style.background = `linear-gradient(${style.bg1}, ${style.bg2})`;
obj.innerText = Math.round(style.count);
resultInput.value = JSON.stringify(style, undefined, 2);
};
const initial = JSON.parse(jsonInput.value);
const blockStyle = transit(initial, applyStyle);
applyStyle(initial);
console.log(blockStyle);
const transitOptions = {
duration: 300,
fps: 60,
easing: t => (t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t)
};
let prevValue = jsonInput.value;
jsonInput.addEventListener('keyup', () => {
if (prevValue !== jsonInput.value) {
blockStyle.to(JSON.parse(jsonInput.value), transitOptions);
prevValue = jsonInput.value;
}
});
function gradientDemo(colors) {
let curColor = -1;
let stopped = false;
const gradColors = transit(
{ c1: colors[colors.length - 1], c2: colors[0] },
({ c1, c2 }) => {
gradient.style.backgroundImage = `linear-gradient(${c1}, ${c2})`;
}
);
const updateColors = () => {
if (stopped) {
return;
}
curColor = (curColor + 1) % colors.length;
const nextColor = (curColor + 1) % colors.length;
gradColors.to(
{
c1: colors[curColor],
c2: colors[nextColor]
},
{
end: updateColors,
duration: 2000
}
);
};
updateColors();
return () => {
gradColors.stop();
stopped = true;
};
}
let currGradientDemo = gradientDemo(
colorsInput.value
.trim()
.split('\n')
.map(s => s.trim())
);
colorsSubmit.addEventListener('click', () => {
if (currGradientDemo) {
currGradientDemo();
}
currGradientDemo = gradientDemo(
colorsInput.value
.trim()
.split('\n')
.map(s => s.trim())
);
});