Skip to content

Visualize Your Ideas With Code

feat(player): improve accessibility

aarthificial0fc923576e7b
diff --git a/packages/player/src/main.ts b/packages/player/src/main.ts
index d4283eef..ca7d9237 100644
--- a/packages/player/src/main.ts
+++ b/packages/player/src/main.ts
@@ -23,6 +23,10 @@ class MotionCanvasPlayer extends HTMLElement {
return !!attr;
}
+ private get hover() {
+ return this.getAttribute('auto') === 'hover';
+ }
+
private get quality() {
const attr = this.getAttribute('quality');
return attr ? parseFloat(attr) : 1;
@@ -53,6 +57,7 @@ class MotionCanvasPlayer extends HTMLElement {
private renderTime = 0;
private finished = false;
private playing = false;
+ private connected = false;
public constructor() {
super();
@@ -65,6 +70,7 @@ class MotionCanvasPlayer extends HTMLElement {
this.overlay.addEventListener('click', this.handleClick);
this.overlay.addEventListener('mousemove', this.handleMouseMove);
this.overlay.addEventListener('mouseleave', this.handleMouseLeave);
+ this.button.addEventListener('mousedown', this.handleMouseDown);
this.setState(State.Initial);
}
@@ -72,6 +78,10 @@ class MotionCanvasPlayer extends HTMLElement {
if (this.mouseMoveId) {
clearTimeout(this.mouseMoveId);
}
+ if (this.hover && !this.playing) {
+ this.setPlaying(true);
+ }
+
this.mouseMoveId = window.setTimeout(() => {
this.mouseMoveId = null;
this.updateClass();
@@ -80,6 +90,9 @@ class MotionCanvasPlayer extends HTMLElement {
};
private handleMouseLeave = () => {
+ if (this.hover) {
+ this.setPlaying(false);
+ }
if (this.mouseMoveId) {
clearTimeout(this.mouseMoveId);
this.mouseMoveId = null;
@@ -87,7 +100,12 @@ class MotionCanvasPlayer extends HTMLElement {
}
};
+ private handleMouseDown = (e: MouseEvent) => {
+ e.preventDefault();
+ };
+
private handleClick = () => {
+ if (this.auto) return;
this.handleMouseMove();
this.setPlaying(!this.playing);
this.button.animate(
@@ -108,7 +126,7 @@ class MotionCanvasPlayer extends HTMLElement {
}
private setPlaying(value: boolean) {
- if (this.state === State.Ready && (value || this.auto)) {
+ if (this.state === State.Ready && (value || (this.auto && !this.hover))) {
this.playing = true;
this.request();
} else {
@@ -119,29 +137,39 @@ class MotionCanvasPlayer extends HTMLElement {
private updateClass() {
this.overlay.className = `overlay state-${this.state}`;
+ this.canvas.className = `canvas state-${this.state}`;
this.overlay.classList.toggle('playing', this.playing);
this.overlay.classList.toggle('auto', this.auto);
- this.overlay.classList.toggle(
- 'hover',
- this.mouseMoveId !== null && !this.auto,
- );
+ this.overlay.classList.toggle('hover', this.mouseMoveId !== null);
+
+ if (this.connected) {
+ if (this.mouseMoveId !== null || !this.playing) {
+ this.dataset.overlay = '';
+ } else {
+ delete this.dataset.overlay;
+ }
+ }
}
private shouldPlay() {
- return this.state === State.Ready && this.playing;
+ return this.state === State.Ready && this.playing && this.connected;
}
private async updateSource(source: string) {
- this.setState(State.Loading);
+ this.setState(State.Initial);
this.abortController?.abort();
this.abortController = new AbortController();
let project: Project;
try {
- project = (
- await import(/* webpackIgnore: true */ /* @vite-ignore */ source)
- ).default;
+ const promise = import(
+ /* webpackIgnore: true */ /* @vite-ignore */ source
+ );
+ const delay = new Promise(resolve => setTimeout(resolve, 200));
+ await Promise.any([delay, promise]);
+ this.setState(State.Loading);
+ project = (await promise).default;
} catch (e) {
this.setState(State.Error);
return;
@@ -183,10 +211,10 @@ class MotionCanvasPlayer extends HTMLElement {
private request() {
this.runId ??= requestAnimationFrame(async time => {
this.runId = null;
+ if (!this.shouldPlay()) return;
+
if (time - this.renderTime >= 990 / this.project.framerate) {
this.renderTime = time;
- if (!this.shouldPlay()) return;
-
try {
await this.run();
} catch (e) {
@@ -202,7 +230,7 @@ class MotionCanvasPlayer extends HTMLElement {
private attributeChangedCallback(name: string, oldValue: any, newValue: any) {
switch (name) {
case 'auto':
- this.setPlaying(true);
+ this.setPlaying(this.playing);
break;
case 'src':
this.updateSource(newValue);
@@ -222,9 +250,12 @@ class MotionCanvasPlayer extends HTMLElement {
}
private disconnectedCallback() {
- if (this.playing) {
- this.setPlaying(false);
- }
+ this.connected = false;
+ }
+
+ private connectedCallback() {
+ this.connected = true;
+ this.request();
}
}
diff --git a/packages/player/src/styles.scss b/packages/player/src/styles.scss
index 3dce05b0..6b2fc6b4 100644
--- a/packages/player/src/styles.scss
+++ b/packages/player/src/styles.scss
@@ -32,12 +32,13 @@ $states: ('initial' 'loading' 'ready' 'error');
cursor: pointer;
}
- &:not(.hover):not(.auto):hover {
+ &.playing:not(.hover):hover {
cursor: none;
}
&.hover,
- &:not(.playing) {
+ &.state-ready:focus-within,
+ &.state-ready:not(.playing) {
opacity: 1;
.button {
@@ -46,8 +47,14 @@ $states: ('initial' 'loading' 'ready' 'error');
}
}
- &.auto .button {
- display: none;
+ &.state-loading,
+ &.state-error {
+ opacity: 1;
+ transition: opacity 1s;
+ }
+
+ &.state-ready.auto {
+ opacity: 0;
}
}
@@ -56,21 +63,39 @@ $states: ('initial' 'loading' 'ready' 'error');
max-width: 96px;
aspect-ratio: 1;
scale: 0.5;
- transition: scale 0.1s ease-in;
+ transition: scale 0.1s ease-in, opacity 0.1s;
+ background-color: transparent;
+ border: none;
background-size: 100% 100%;
background-repeat: no-repeat;
- opacity: 0.87;
+ opacity: 0.54;
+ cursor: inherit;
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjRweCIgdmlld0JveD0iMCAwIDI0IDI0IiB3aWR0aD0iMjRweCIgZmlsbD0iI2ZmZmZmZiI+PHBhdGggZD0iTTAgMGgyNHYyNEgwVjB6IiBmaWxsPSJub25lIi8+PHBhdGggZD0iTTEyIDJDNi40OCAyIDIgNi40OCAyIDEyczQuNDggMTAgMTAgMTAgMTAtNC40OCAxMC0xMFMxNy41MiAyIDEyIDJ6bS0yIDE0LjV2LTlsNiA0LjUtNiA0LjV6Ii8+PC9zdmc+');
.playing & {
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDI0IDI0IiBoZWlnaHQ9IjI0cHgiIHZpZXdCb3g9IjAgMCAyNCAyNCIgd2lkdGg9IjI0cHgiIGZpbGw9IiNmZmZmZmYiPjxnPjxyZWN0IGZpbGw9Im5vbmUiIGhlaWdodD0iMjQiIHdpZHRoPSIyNCIvPjxyZWN0IGZpbGw9Im5vbmUiIGhlaWdodD0iMjQiIHdpZHRoPSIyNCIvPjxyZWN0IGZpbGw9Im5vbmUiIGhlaWdodD0iMjQiIHdpZHRoPSIyNCIvPjwvZz48Zz48Zy8+PHBhdGggZD0iTTEyLDJDNi40OCwyLDIsNi40OCwyLDEyczQuNDgsMTAsMTAsMTBzMTAtNC40OCwxMC0xMFMxNy41MiwyLDEyLDJ6IE0xMSwxNkg5VjhoMlYxNnogTTE1LDE2aC0yVjhoMlYxNnoiLz48L2c+PC9zdmc+');
}
+
+ &:focus,
+ .overlay:hover & {
+ opacity: 0.87;
+ }
+
+ .auto & {
+ display: none;
+ }
}
.canvas {
width: 100%;
display: block;
+ opacity: 0;
+ transition: opacity 0.1s;
+
+ &.state-ready {
+ opacity: 1;
+ }
}
.message {
diff --git a/packages/player/src/template.html b/packages/player/src/template.html
index 6d4de4bb..dd913cf3 100644
--- a/packages/player/src/template.html
+++ b/packages/player/src/template.html
@@ -1,13 +1,11 @@
<canvas class="canvas"></canvas>
<div class="overlay">
- <div class="button ready"></div>
- <div class="message initial">No video provided.</div>
+ <button title='Play / Pause' class="button ready" tabindex='0'> </button>
<div class="message error">
An error occurred while loading the animation.
</div>
<svg
class="loader loading"
- xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
stroke="#ffffff"
stroke-width="2"
diff --git a/packages/player/types/main.d.ts b/packages/player/types/main.d.ts
new file mode 100644
index 00000000..b8422619
--- /dev/null
+++ b/packages/player/types/main.d.ts
@@ -0,0 +1,5 @@
+export interface MotionCanvasPlayerProps {
+ src: string;
+ auto?: boolean;
+ quality?: number;
+}