feat: navigation + keyboard, gamepad, camera tests

This commit is contained in:
Tomáš Mládek 2024-02-22 23:04:48 +01:00
parent 2ac8cf0e7c
commit 2d44469d0e
17 changed files with 782 additions and 23 deletions

View file

@ -16,6 +16,7 @@
},
"devDependencies": {
"@tsconfig/svelte": "^5.0.2",
"@types/debug": "^4.1.12",
"@types/eslint": "8.56.0",
"@types/lodash": "^4.14.202",
"@typescript-eslint/eslint-plugin": "^6.0.0",
@ -39,6 +40,8 @@
"@sveltejs/adapter-static": "^3.0.1",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@tabler/icons-webfont": "^2.47.0",
"debug": "^4.3.4",
"lodash": "^4.17.21",
"normalize.css": "^8.0.1",
"svelte": "^4.2.7",

View file

@ -20,6 +20,12 @@ dependencies:
'@sveltejs/vite-plugin-svelte':
specifier: ^3.0.0
version: 3.0.2(svelte@4.2.9)(vite@5.0.12)
'@tabler/icons-webfont':
specifier: ^2.47.0
version: 2.47.0
debug:
specifier: ^4.3.4
version: 4.3.4
lodash:
specifier: ^4.17.21
version: 4.17.21
@ -43,6 +49,9 @@ devDependencies:
'@tsconfig/svelte':
specifier: ^5.0.2
version: 5.0.2
'@types/debug':
specifier: ^4.1.12
version: 4.1.12
'@types/eslint':
specifier: 8.56.0
version: 8.56.0
@ -87,7 +96,7 @@ devDependencies:
version: 3.6.3(postcss@8.4.33)(svelte@4.2.9)
wait-on:
specifier: ^7.2.0
version: 7.2.0
version: 7.2.0(debug@4.3.4)
packages:
@ -674,6 +683,16 @@ packages:
- supports-color
dev: false
/@tabler/icons-webfont@2.47.0:
resolution: {integrity: sha512-yfV9zDal0iYDmyGz4BS9IlhaaMydtLdyOrY2UAZToP65sVWj7AFIi6symNzsoBaX867xAZWVHdKcocah0BfSog==}
dependencies:
'@tabler/icons': 2.47.0
dev: false
/@tabler/icons@2.47.0:
resolution: {integrity: sha512-4w5evLh+7FUUiA1GucvGj2ReX2TvOjEr4ejXdwL/bsjoSkof6r1gQmzqI+VHrE2CpJpB3al7bCTulOkFa/RcyA==}
dev: false
/@tootallnate/quickjs-emscripten@0.23.0:
resolution: {integrity: sha512-C5Mc6rdnsaJDjO3UpGW/CQTHtCKaYlScZTly4JIu97Jxo/odCiH0ITnDXSJPTOrEKk/ycSZ0AOgTmkDtkOsvIA==}
dev: true
@ -686,6 +705,12 @@ packages:
resolution: {integrity: sha512-4Kh9a6B2bQciAhf7FSuMRRkUWecJgJu9nPnx3yzpsfXX/c50REIqpHY4C82bXP90qrLtXtkDxTZosYO3UpOwlA==}
dev: false
/@types/debug@4.1.12:
resolution: {integrity: sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==}
dependencies:
'@types/ms': 0.7.34
dev: true
/@types/eslint@8.56.0:
resolution: {integrity: sha512-FlsN0p4FhuYRjIxpbdXovvHQhtlG05O1GG/RNWvdAxTboR438IOTwmrY/vLA+Xfgg06BTkP045M3vpFwTMv1dg==}
dependencies:
@ -704,6 +729,10 @@ packages:
resolution: {integrity: sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ==}
dev: true
/@types/ms@0.7.34:
resolution: {integrity: sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g==}
dev: true
/@types/node@20.11.19:
resolution: {integrity: sha512-7xMnVEcZFu0DikYjWOlRq7NTPETrm7teqUT2WkQjrTIkEgUyyGdWsj/Zg8bEJt5TNklzbPD1X3fqfsHw3SpapQ==}
requiresBuild: true
@ -947,10 +976,10 @@ packages:
resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==}
dev: true
/axios@1.6.7:
/axios@1.6.7(debug@4.3.4):
resolution: {integrity: sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==}
dependencies:
follow-redirects: 1.15.5
follow-redirects: 1.15.5(debug@4.3.4)
form-data: 4.0.0
proxy-from-env: 1.1.0
transitivePeerDependencies:
@ -1618,7 +1647,7 @@ packages:
resolution: {integrity: sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==}
dev: true
/follow-redirects@1.15.5:
/follow-redirects@1.15.5(debug@4.3.4):
resolution: {integrity: sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==}
engines: {node: '>=4.0'}
peerDependencies:
@ -1626,6 +1655,8 @@ packages:
peerDependenciesMeta:
debug:
optional: true
dependencies:
debug: 4.3.4
dev: true
/form-data@4.0.0:
@ -2880,12 +2911,12 @@ packages:
vite: 5.0.12
dev: false
/wait-on@7.2.0:
/wait-on@7.2.0(debug@4.3.4):
resolution: {integrity: sha512-wCQcHkRazgjG5XoAq9jbTMLpNIjoSlZslrJ2+N9MxDsGEv1HnFoVjOCexL0ESva7Y9cu350j+DWADdk54s4AFQ==}
engines: {node: '>=12.0.0'}
hasBin: true
dependencies:
axios: 1.6.7
axios: 1.6.7(debug@4.3.4)
joi: 17.12.1
lodash: 4.17.21
minimist: 1.2.8

View file

@ -9,9 +9,45 @@ body, html {
background-color: black;
font-family: 'B612', 'IBM Plex Sans', 'Helvetica Neue', Arial, sans-serif;
font-size: 1.5vw;
font-size: min(1.5vw, 1.5vh);
}
* {
box-sizing: border-box;
}
a {
color: white;
}
h1, h2, h3 {
margin-top: 0;
}
button, .button {
display: inline-flex;
align-items: center;
gap: 0.25em;
text-decoration: none;
border: 1px solid white;
cursor: pointer;
padding: 0.25em 0.5em;
border-radius: 0.25em;
background: black;
color: white;
}
select {
background: black;
color: white;
padding: 0.25em 0.5em;
border-radius: 0.25em;
border: 1px solid white;
&:disabled {
opacity: 0.7;
}
}

View file

@ -16,7 +16,8 @@
let verticalMargin = MARGIN_SIZE;
let unloaded = true;
let transparent = false;
export let transparent = false;
export let subdued = false;
function updateCounts() {
const gridWidth = window.innerWidth - MARGIN_SIZE;
@ -72,6 +73,7 @@
class="background"
class:unloaded
class:transparent
class:subdued
class:even-vertical={verticalCount % 2 === 0}
style="--horizontal-count: {horizontalCount};
--vertical-count: {verticalCount};
@ -266,6 +268,13 @@
}
}
.background.subdued {
& .edge,
& .corner {
opacity: 0.33;
}
}
.grid {
display: grid;
grid-template-columns: repeat(var(--horizontal-count), var(--block-size));

22
src/lib/Spinner.svelte Normal file
View file

@ -0,0 +1,22 @@
<script>
import { IconSpiral } from '@tabler/icons-svelte';
export let size = 32;
</script>
<div class="spinner"><IconSpiral {size} class="spinner-icon" /></div>
<style>
.spinner {
text-align: center;
}
:global(.spinner-icon) {
animation: spin 1s linear infinite;
}
@keyframes spin {
100% {
transform: rotate(360deg);
}
}
</style>

View file

@ -4,7 +4,10 @@
import Axes from '$lib/Axes.svelte';
import ColorGradient from '$lib/ColorGradient.svelte';
import BrightnessGradient from '$lib/BrightnessGradient.svelte';
import { onMount } from 'svelte';
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher<{ focus: void }>();
export let full = false;
let sizes = {
blockSize: 64,
@ -20,16 +23,12 @@
$: circleBlocks =
2 * Math.floor((Math.min(sizes.horizontalCount, sizes.verticalCount) * 0.66) / 2) +
(sizes.horizontalCount % 2);
onMount(() => {
window.addEventListener('dblclick', () => {
document.body.requestFullscreen();
});
});
</script>
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="test-card"
class:full
style="--block-size: {sizes.blockSize}px;
--horizontal-margin: {sizes.horizontalMargin}px;
--vertical-margin: {sizes.verticalMargin}px;
@ -37,9 +36,13 @@
--column-width: {columnWidth};
--column-height: {columnHeight};
--left-column: {leftColumn};"
on:dblclick={() => dispatch('focus') && document.body.requestFullscreen()}
>
<BackgroundGrid on:change={(ev) => (sizes = ev.detail)} />
<BackgroundGrid on:change={(ev) => (sizes = ev.detail)} subdued={!full} />
<div class="axes">
<Axes />
</div>
<div class="outer"></div>
<div class="inner"></div>
@ -137,4 +140,13 @@
flex-grow: 1;
}
}
.test-card:not(.full) {
& .info,
& .column,
& .axes,
& .inner {
display: none;
}
}
</style>

77
src/routes/+layout.svelte Normal file
View file

@ -0,0 +1,77 @@
<script lang="ts">
import 'normalize.css/normalize.css';
import '@fontsource/b612';
import '@fontsource/b612/700.css';
import '@tabler/icons-webfont/tabler-icons.css';
import '../index.css';
import TestCard from '$lib/TestCard.svelte';
import { page } from '$app/stores';
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
let idleTimeout: NodeJS.Timeout | undefined;
onMount(() => {
window.addEventListener('mousemove', () => {
clearTimeout(idleTimeout);
document.body.classList.remove('idle');
idleTimeout = setTimeout(() => {
document.body.classList.add('idle');
}, 3000);
});
});
$: onlyCard = $page.url.pathname === '/card';
</script>
<TestCard full={onlyCard} on:focus={() => goto('/card')} />
<main class:content={!onlyCard} class:sub={$page.url.pathname !== '/' && !onlyCard}>
<a href="/" class="button button-back"><i class="ti ti-arrow-back" />Back</a>
<slot />
</main>
<style>
main.content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.8);
border-radius: 0.5rem;
border: 1px solid white;
padding: 1rem;
display: flex;
flex-direction: column;
}
main.sub {
height: 90vh;
width: 90vw;
}
.button-back {
position: absolute;
top: 1rem;
right: 1rem;
opacity: 0.66;
transition: opacity 0.3s;
&:hover {
opacity: 1;
}
}
main:not(.sub) .button-back {
display: none;
}
:global(.hide-idle) {
transition: opacity 1s;
opacity: 1;
}
:global(body.idle .hide-idle) {
opacity: 0;
}
</style>

View file

@ -1,9 +1,72 @@
<script>
import 'normalize.css/normalize.css';
import '@fontsource/b612';
import '@fontsource/b612/700.css';
import '../index.css';
import TestCard from '$lib/TestCard.svelte';
</script>
<TestCard />
<nav>
<h1>Universal Test Card</h1>
<div class="options">
<a href="card">
<i class="ti ti-device-desktop"></i>
Screen
</a>
<a href="audio" class="disabled">
<i class="ti ti-volume"></i>
Audio
</a>
<a href="keyboard">
<i class="ti ti-keyboard"></i>
Keyboard
</a>
<a href="mouse" class="disabled">
<i class="ti ti-mouse"></i>
Mouse
</a>
<a href="gamepad">
<i class="ti ti-device-gamepad"></i>
Gamepad
</a>
<a href="camera">
<i class="ti ti-camera"></i>
Camera
</a>
<a href="microphone" class="disabled">
<i class="ti ti-microphone"></i>
Microphone
</a>
<a href="sensors" class="disabled">
<i class="ti ti-cpu-2"></i>
Sensors
</a>
</div>
</nav>
<style>
h1 {
text-align: center;
font-size: 3rem;
margin: 1rem;
text-transform: uppercase;
}
.options {
display: flex;
justify-content: space-evenly;
align-items: center;
gap: 2em;
& a {
text-align: center;
text-decoration: none;
&.disabled {
pointer-events: none;
opacity: 0.5;
}
}
& .ti {
display: block;
font-size: 3rem;
}
}
</style>

View file

View file

@ -0,0 +1,259 @@
<script lang="ts">
import { onDestroy, onMount } from 'svelte';
import { browser } from '$app/environment';
import debug from 'debug';
const dbg = debug('app:camera');
let video: HTMLVideoElement;
let devices: MediaDeviceInfo[] = [];
let currentDevice: string | undefined;
let requestResolution: [number, number] | 'auto' = 'auto';
let requestFramerate: number | 'auto' = 'auto';
let deviceInfo: {
resolution?: string;
frameRate?: number;
} = {};
let snapshot: string | undefined;
let flipped = false;
$: dbg('devices %O', devices);
$: dbg('currentDevice %s', currentDevice);
onMount(() => {
refreshDevices();
video.addEventListener('playing', () => {
if (browser && video?.srcObject instanceof MediaStream) {
deviceInfo = {
resolution: `${video.videoWidth}x${video.videoHeight}`,
frameRate: video?.srcObject?.getVideoTracks()[0]?.getSettings().frameRate
};
}
});
});
onDestroy(() => {
if (browser && video?.srcObject instanceof MediaStream) {
video.srcObject.getTracks().forEach((t) => t.stop());
}
});
async function refreshDevices() {
devices = (await navigator.mediaDevices.enumerateDevices()).filter(
(d) => d.kind === 'videoinput'
);
if (!currentDevice) {
currentDevice = devices[0]?.deviceId;
}
}
$: if (currentDevice) {
navigator.mediaDevices
.getUserMedia({
video: {
deviceId: currentDevice,
width: requestResolution === 'auto' ? undefined : requestResolution[0],
height: requestResolution === 'auto' ? undefined : requestResolution[1],
frameRate: requestFramerate === 'auto' ? undefined : requestFramerate
}
})
.then((stream) => {
video.srcObject = stream;
refreshDevices();
});
}
async function takeSnapshot() {
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext('2d');
if (!ctx) return;
if (flipped) {
ctx.scale(-1, 1);
ctx.translate(-canvas.width, 0);
}
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
snapshot = canvas.toDataURL('image/png');
}
</script>
<h2><i class="ti ti-camera"></i> Camera test</h2>
<div class="controls">
<label>
Device
<select bind:value={currentDevice} disabled={!devices.length}>
{#each devices as device}
<option value={device.deviceId}>{device.label || '???'}</option>
{:else}
<option>No camera found</option>
{/each}
</select>
</label>
<button on:click={refreshDevices}>
<i class="ti ti-refresh"></i>
Refresh
</button>
<div class="separator"></div>
<label>
Resolution
<select bind:value={requestResolution}>
<option value="auto">Auto</option>
<option value={[4096, 2160]}>4096x2160</option>
<option value={[3840, 2160]}>3840x2160</option>
<option value={[1920, 1080]}>1920x1080</option>
<option value={[1280, 720]}>1280x720</option>
<option value={[640, 480]}>640x480</option>
<option value={[320, 240]}>320x240</option>
</select>
</label>
<label>
Frame rate
<select bind:value={requestFramerate}>
<option value="auto">Auto</option>
<option value={120}>120 fps</option>
<option value={60}>60 fps</option>
<option value={30}>30 fps</option>
<option value={15}>15 fps</option>
<option value={10}>10 fps</option>
<option value={5}>5 fps</option>
</select>
</label>
</div>
<div class="display" class:snapshot={Boolean(snapshot)}>
<!-- svelte-ignore a11y-media-has-caption -->
<video class:flipped bind:this={video} autoplay class:unloaded={!currentDevice}></video>
{#if snapshot}
<!-- svelte-ignore a11y-missing-attribute -->
<!--suppress HtmlRequiredAltAttribute -->
<img src={snapshot} />
<button on:click={() => (snapshot = undefined)}><i class="ti ti-x"></i></button>
{/if}
</div>
<footer>
{#if !currentDevice}
<span class="subdued">No camera selected</span>
{:else}
<ul>
{#key currentDevice}
<li>
Resolution: <strong>{deviceInfo.resolution || '???'}</strong>
</li>
<li>
Frame rate: <strong>{deviceInfo.frameRate || '???'}</strong>
</li>
{/key}
</ul>
<div class="controls">
<button on:click={takeSnapshot}>
<i class="ti ti-camera"></i>
Take picture
</button>
<button on:click={() => (flipped = !flipped)}>
<i class="ti ti-flip-vertical"></i>
{#if flipped}
Unflip image
{:else}
Flip image
{/if}
</button>
</div>
{/if}
</footer>
<style>
.controls {
display: flex;
align-items: end;
justify-content: stretch;
gap: 1em;
& label:first-child {
flex-grow: 1;
min-width: 0;
}
}
select {
background: black;
color: white;
padding: 0.25em 0.5em;
border-radius: 0.25em;
border: 1px solid white;
}
label {
display: flex;
flex-direction: column;
gap: 0.2em;
font-size: 0.8em;
& select {
font-size: initial;
}
}
.display {
position: relative;
display: flex;
flex-direction: column;
min-height: 0;
flex-grow: 1;
justify-content: center;
& img {
object-fit: contain;
}
& button {
position: absolute;
top: 1em;
right: 1em;
}
&.snapshot {
& video {
display: none;
}
}
}
video {
min-width: 0;
min-height: 0;
max-width: 100%;
max-height: 100%;
margin: 1em 0;
&.unloaded {
background: repeating-linear-gradient(45deg, gray, gray 20px, darkgray 20px, darkgray 40px);
flex-grow: 1;
}
&.flipped {
transform: scaleX(-1);
}
}
footer {
display: flex;
justify-content: space-between;
align-items: center;
}
ul {
list-style: none;
margin: 0;
padding: 0;
display: inline-flex;
gap: 1rem;
}
.subdued {
opacity: 0.8;
}
</style>

View file

@ -0,0 +1,25 @@
<script>
</script>
<a href="/" class="hide-idle"><i class="ti ti-arrow-back"></i> Back</a>
<style>
a {
position: absolute;
top: 2rem;
right: 2rem;
background: black;
border: 1px solid white;
border-radius: 0.2em;
padding: 0.5em 1em;
box-shadow: 0 0 0.5em rgba(255, 255, 255, 0.5);
display: flex;
gap: 0.5em;
align-items: center;
text-decoration: none;
}
</style>

View file

@ -0,0 +1,163 @@
<script lang="ts">
import { onMount } from 'svelte';
import { browser } from '$app/environment';
import debug from 'debug';
const dbg = debug('app:camera');
let gamepads: Gamepad[] = [];
let currentGamepad: Gamepad | undefined;
let buttons: GamepadButton[] = [];
let axes: number[] = [];
$: {
if (currentGamepad) {
function update() {
buttons = currentGamepad?.buttons.concat() || [];
axes = currentGamepad?.axes.concat() || [];
requestAnimationFrame(update);
}
update();
}
}
$: dbg('Gamepads %O', gamepads);
$: dbg('Current gamepad %s', currentGamepad);
$: currentGamepad?.vibrationActuator?.playEffect('dual-rumble', {
duration: 1000
});
onMount(() => {
refreshGamepads();
});
async function refreshGamepads() {
gamepads = browser ? (navigator.getGamepads().filter(Boolean) as Gamepad[]) : [];
currentGamepad = gamepads[0];
}
onMount(() => {
window.addEventListener('gamepadconnected', (e) => {
dbg('Gamepad connected', e);
refreshGamepads();
});
window.addEventListener('gamepaddisconnected', (e) => {
dbg('Gamepad disconnected', e);
refreshGamepads();
});
});
</script>
<h2><i class="ti ti-device-gamepad"></i> Gamepad & Joystick Tests</h2>
<div class="controls">
<label>
Device
<select disabled={!gamepads.length}>
{#each gamepads as gamepad}
<option value={gamepad.index}>{gamepad.id}</option>
{:else}
<option>No gamepads detected. (Try pressing a button)</option>
{/each}
</select>
</label>
<button on:click={refreshGamepads}>
<i class="ti ti-refresh"></i>
Refresh
</button>
</div>
{#if currentGamepad}
<section>
<h3>Buttons</h3>
<ul class="buttons">
{#each buttons as button, i}
<li class:pressed={button.pressed}>{i}</li>
{/each}
</ul>
</section>
<section>
<h3>Axes</h3>
<div class="axes">
{#each axes as axis, i (i)}
<div class="axis">
<div>
<span>{i}</span>
<progress value={axis + 1} max="2"></progress>
<span>{axis.toFixed(2)}</span>
</div>
</div>
{/each}
</div>
</section>
{/if}
<style>
.controls {
display: flex;
align-items: end;
justify-content: stretch;
gap: 1em;
& label:first-child {
flex-grow: 1;
min-width: 0;
}
}
label {
display: flex;
flex-direction: column;
gap: 0.2em;
font-size: 0.8em;
& select {
font-size: initial;
}
}
h3 {
margin-top: 2em;
text-align: center;
}
.buttons {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
& li {
display: block;
width: 2em;
height: 2em;
border: 1px solid white;
border-radius: 0.75em;
text-align: center;
line-height: 2em;
&.pressed {
background-color: darkred;
color: white;
}
}
}
.axes {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 0.5em 2em;
& .axis div {
display: flex;
gap: 0.25em;
& progress {
flex-grow: 1;
}
}
}
</style>

View file

@ -0,0 +1,59 @@
<script lang="ts">
import { onMount } from 'svelte';
let key: string;
let code: string;
let pressedKeys: string[] = [];
onMount(() => {
document.addEventListener('keydown', (event) => {
key = event.key;
code = event.code;
pressedKeys = [...pressedKeys, event.key];
pressedKeys = pressedKeys.slice(-50);
});
});
</script>
<h2>Keyboard testing</h2>
<p>Press a key on the keyboard to see the event object and the key code.</p>
<div class="current">
{#if key}
<span>{key}</span>
{/if}
{#if code}
<span class="code">({code})</span>
{/if}
</div>
<p>Pressed keys:</p>
<ul>
{#each pressedKeys as key}
<li>{key}</li>
{/each}
</ul>
<style>
.current {
display: flex;
}
.code {
margin-left: 1em;
opacity: 0.8;
}
ul {
list-style: none;
padding: 0;
display: flex;
flex-wrap: wrap;
gap: 0.2em;
}
li {
margin: 0;
padding: 0;
display: inline-block;
}
</style>

View file

View file

View file

View file