Skip to content

Visualize Your Ideas With Code

feature: display labels on timeline

aarthificial31239ef62062
diff --git a/public/index.html b/public/index.html
index 7b8f09b9..2ae5ed2a 100644
--- a/public/index.html
+++ b/public/index.html
@@ -85,7 +85,7 @@
}
.fill-start {
- background-color: #FF5F5D;
+ background-color: #FA6466;
}
.marker {
@@ -94,8 +94,47 @@
bottom: 12px;
width: 8px;
border-radius: 4px;
- background-color: #FF5F5D;
+ background-color: #FA6466;
}
+
+ .label {
+ position: absolute;
+ top: 0;
+ bottom: 0;
+ width: 16px;
+ padding: 12px;
+
+ display: flex;
+ justify-content: center;
+ }
+
+ .label::before {
+ content: '';
+ display: block;
+ width: 16px;
+ height: 16px;
+ border-radius: 8px;
+ background-color: #F5DC7C;
+ }
+
+ .label::after {
+ content: attr(data-title);
+ display: block;
+ position: absolute;
+ color: white;
+ background-color: rgba(0, 0, 0, 0.87);
+ bottom: 48px;
+ text-align: center;
+ padding: 4px 8px;
+ border-radius: 2px;
+ opacity: 0;
+ pointer-events: none ;
+ }
+
+ .label:hover::after {
+ opacity: 1;
+ }
+
</style>
</head>
<body>
diff --git a/src/player/Timeline.ts b/src/player/Timeline.ts
index cb8f0a8d..baf7c705 100644
--- a/src/player/Timeline.ts
+++ b/src/player/Timeline.ts
@@ -9,10 +9,12 @@ export class Timeline {
private readonly marker: HTMLElement;
private duration: number;
+ private labelElements: Record<string, HTMLElement> = {};
public constructor(
private readonly player: Player,
private readonly root: HTMLElement,
+ private readonly labels: Record<string, number>,
) {
this.fillTime = root.querySelector('.fill-time')!;
this.fillSeek = root.querySelector('.fill-seek')!;
@@ -24,7 +26,14 @@ export class Timeline {
this.player.StateChanged.sub(this.update);
this.root.addEventListener('click', e => {
- this.player.requestSeek(this.mousePositionToFrame(e.clientX));
+ const target = <HTMLElement>e.target;
+ this.player.requestSeek(
+ target.classList.contains('label')
+ ? this.player.project.secondsToFrames(
+ parseFloat(target.dataset.time!),
+ )
+ : this.mousePositionToFrame(e.clientX),
+ );
});
this.root.addEventListener('contextmenu', e => {
e.preventDefault();
@@ -43,6 +52,15 @@ export class Timeline {
this.root.addEventListener('mouseenter', () => {
this.fillSeek.style.opacity = '0.32';
});
+
+ for (const label in labels) {
+ const element = document.createElement('div');
+ element.classList.add('label');
+ element.dataset.title = label;
+ element.dataset.time = labels[label].toString();
+ root.appendChild(element);
+ this.labelElements[label] = element;
+ }
}
private mousePositionToFrame(position: number) {
@@ -68,5 +86,20 @@ export class Timeline {
this.marker.style.left = `${left}px`;
this.fillTime.style.width = `${fillWidth}px`;
this.fillStart.style.width = `${startWidth}px`;
+
+ for (const label in this.labels) {
+ const element = this.labelElements[label];
+ const elementFrame = this.player.project.secondsToFrames(
+ this.labels[label],
+ );
+ const elementLeft = clampRemap(
+ 1,
+ state.duration,
+ 0,
+ width - 8,
+ elementFrame,
+ );
+ element.style.left = `${elementLeft}px`;
+ }
};
}