This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc-chenillard.js
122 lines (101 loc) · 2.97 KB
/
sc-chenillard.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
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
import { css, html, svg, nothing } from 'lit';
import ScElement from './ScElement.js';
import { theme } from './styles.js';
import { getTime } from '@ircam/sc-gettime';
class ScChenillard extends ScElement {
static get properties() {
return {
width: { type: Number },
height: { type: Number },
size: { type: Number }, // handler size
}
}
static get styles() {
return css`
:host {
vertical-align: top;
display: inline-block;
box-sizing: border-box;
vertical-align: top;
font-size: 0;
}
:host > div {
box-sizing: border-box;
background-color: rgb(106, 106, 105);
border: 1px solid rgb(106, 106, 105);
border-radius: 2px;
position: relative
}
rect.foreground {
fill: ${theme['--color-primary-4']};
}
rect.background {
fill: ${theme['--color-primary-1']};
}
sc-number {
position: absolute;
bottom: 0;
left: 0
}
`;
}
constructor() {
super();
this.getProgressFunction = getTime;
this.width = 400;
this.height = 30;
this.size = 0.1; // size of the moving part
this._progress;
}
render() {
const size = this.size;
const amplitude = 1 - size;
const handlerWidth = this.width * this.size;
const height = this.height - 2;
const width = this.width - 2;
// progress 0 is on the left
// progress 1 is on the right
const posX = Math.round(this._progress * (width - handlerWidth));
// const pos = (this._progress % 2) * amplitude; // [0-2]
// let progressWidth;
// if (pos < amplitude) {
// progressWidth = Math.round(pos * width); // [0-2]*width
// } else {
// progressWidth = Math.round(Math.abs((2 * amplitude) - pos) * width); //[0-2]*width
// }
// const progressWidthStart = (progressWidth - chenillardWidth) - (1 - chenillardWidth); //[0-2]W - 0.2W
// console.log(progressWidthStart);
return html`
<div style="width: ${this.width}px; height: ${this.height}px">
<svg
style="width: ${width}px; height: ${height}px"
viewport="0 0 ${width} ${height}"
>
<rect class="background" width="${width}" height="${height}"></rect>
<rect class="foreground" x="${posX}" width="${handlerWidth}" height="${height}"></rect>
</svg>
</div>
`;
}
_render() {
const progress = this.getProgressFunction();
if (Number.isFinite(progress)) {
if (progress !== this._progress) {
this._progress = progress;
this.requestUpdate();
}
}
this._rafId = requestAnimationFrame(() => this._render());
}
connectedCallback() {
super.connectedCallback();
this._render();
}
disconnectedCallback() {
cancelAnimationFrame(this._timeoutInterval);
super.disconnectedCallback();
}
}
if (customElements.get('sc-chenillard') === undefined) {
customElements.define('sc-chenillard', ScChenillard);
}