feat: replaced the J/K navigation with good old HTML buttons
This commit is contained in:
parent
6287298e57
commit
8799edf99b
3 changed files with 11 additions and 26 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
# Examples/Web — raylib-cs in the browser (WebAssembly)
|
# Examples/Web — raylib-cs in the browser (WebAssembly)
|
||||||
|
|
||||||
Runs the raylib examples in the browser via WebAssembly, with a dropdown to switch between them.
|
Runs the raylib examples in the browser via WebAssembly, with a dropdown and Prev/Next buttons
|
||||||
|
to switch between them.
|
||||||
It proves the `Raylib-cs` NuGet package's `browser-wasm` support end-to-end: the package's
|
It proves the `Raylib-cs` NuGet package's `browser-wasm` support end-to-end: the package's
|
||||||
`buildTransitive` targets link the shipped `raylib.a` into the .NET wasm runtime.
|
`buildTransitive` targets link the shipped `raylib.a` into the .NET wasm runtime.
|
||||||
|
|
||||||
|
|
@ -37,7 +38,7 @@ dotnet serve -d Examples/bin/Release/net10.0/browser-wasm/AppBundle # dotnet
|
||||||
# or: npx http-server Examples/bin/Release/net10.0/browser-wasm/AppBundle
|
# or: npx http-server Examples/bin/Release/net10.0/browser-wasm/AppBundle
|
||||||
```
|
```
|
||||||
|
|
||||||
Open the printed URL and use the **Example** dropdown to switch examples.
|
Open the printed URL and use the **Example** dropdown or **Prev**/**Next** buttons to switch examples.
|
||||||
|
|
||||||
## Canvas scaling modes
|
## Canvas scaling modes
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@
|
||||||
h1 { font-size: 1.1rem; font-weight: 600; }
|
h1 { font-size: 1.1rem; font-weight: 600; }
|
||||||
#toolbar { display: flex; align-items: center; flex-wrap: wrap; gap: 0.25rem; }
|
#toolbar { display: flex; align-items: center; flex-wrap: wrap; gap: 0.25rem; }
|
||||||
#examples { font-size: 1rem; padding: .3rem .5rem; }
|
#examples { font-size: 1rem; padding: .3rem .5rem; }
|
||||||
|
#previousExample, #nextExample { font-size: .9rem; padding: .3rem .6rem; }
|
||||||
#scaleMode { font-size: 0.95rem; padding: .2rem .4rem; }
|
#scaleMode { font-size: 0.95rem; padding: .2rem .4rem; }
|
||||||
#viewport {
|
#viewport {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
|
|
@ -46,14 +47,6 @@
|
||||||
background: #000;
|
background: #000;
|
||||||
}
|
}
|
||||||
#status { color: #888; font-size: .85rem; margin: 0; }
|
#status { color: #888; font-size: .85rem; margin: 0; }
|
||||||
#hint { color: #888; font-size: .8rem; margin-left: .5rem; }
|
|
||||||
#hint kbd {
|
|
||||||
font-family: inherit;
|
|
||||||
background: #2c2c2c;
|
|
||||||
border: 1px solid #444;
|
|
||||||
border-radius: 3px;
|
|
||||||
padding: 0 .3rem;
|
|
||||||
}
|
|
||||||
#error {
|
#error {
|
||||||
color: #ff6b6b;
|
color: #ff6b6b;
|
||||||
background: #3a1d1d;
|
background: #3a1d1d;
|
||||||
|
|
@ -69,13 +62,14 @@
|
||||||
<div id="toolbar">
|
<div id="toolbar">
|
||||||
<label for="examples">Example: </label>
|
<label for="examples">Example: </label>
|
||||||
<select id="examples"><option>loading…</option></select>
|
<select id="examples"><option>loading…</option></select>
|
||||||
|
<button id="previousExample" type="button">Prev</button>
|
||||||
|
<button id="nextExample" type="button">Next</button>
|
||||||
<label for="scaleMode"> Scale: </label>
|
<label for="scaleMode"> Scale: </label>
|
||||||
<select id="scaleMode">
|
<select id="scaleMode">
|
||||||
<option value="integer">Integer</option>
|
<option value="integer">Integer</option>
|
||||||
<option value="native">Native (1x)</option>
|
<option value="native">Native (1x)</option>
|
||||||
<option value="fit">Fit</option>
|
<option value="fit">Fit</option>
|
||||||
</select>
|
</select>
|
||||||
<span id="hint"><kbd>J</kbd>/<kbd>K</kbd> next/previous example</span>
|
|
||||||
</div>
|
</div>
|
||||||
<div id="error" hidden>This example crashed — see the browser console for details. Select another example to continue.</div>
|
<div id="error" hidden>This example crashed — see the browser console for details. Select another example to continue.</div>
|
||||||
<div id="viewport">
|
<div id="viewport">
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ import {
|
||||||
const status = document.getElementById('status');
|
const status = document.getElementById('status');
|
||||||
const errorBanner = document.getElementById('error');
|
const errorBanner = document.getElementById('error');
|
||||||
const select = document.getElementById('examples');
|
const select = document.getElementById('examples');
|
||||||
|
const previousExampleButton = document.getElementById('previousExample');
|
||||||
|
const nextExampleButton = document.getElementById('nextExample');
|
||||||
const canvas = document.getElementById('canvas');
|
const canvas = document.getElementById('canvas');
|
||||||
const viewport = document.getElementById('viewport');
|
const viewport = document.getElementById('viewport');
|
||||||
const scaleModeSelect = document.getElementById('scaleMode');
|
const scaleModeSelect = document.getElementById('scaleMode');
|
||||||
|
|
@ -142,6 +144,7 @@ function selectExample(name) {
|
||||||
const ok = Host.SetExample(name);
|
const ok = Host.SetExample(name);
|
||||||
targetFps = Host.GetCurrentTargetFps();
|
targetFps = Host.GetCurrentTargetFps();
|
||||||
errorBanner.hidden = ok;
|
errorBanner.hidden = ok;
|
||||||
|
canvas.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
select.innerHTML = '';
|
select.innerHTML = '';
|
||||||
|
|
@ -153,12 +156,9 @@ for (const name of Host.GetExampleNames().split('\n').filter(n => n.length > 0))
|
||||||
}
|
}
|
||||||
select.addEventListener('change', () => {
|
select.addEventListener('change', () => {
|
||||||
selectExample(select.value);
|
selectExample(select.value);
|
||||||
canvas.focus();
|
|
||||||
});
|
});
|
||||||
selectExample(select.value);
|
selectExample(select.value);
|
||||||
|
|
||||||
// J/K step through examples instead of arrow keys on the focused dropdown,
|
|
||||||
// which would otherwise fight with examples that read the arrow keys.
|
|
||||||
function stepExample(delta) {
|
function stepExample(delta) {
|
||||||
const count = select.options.length;
|
const count = select.options.length;
|
||||||
if (count === 0) {
|
if (count === 0) {
|
||||||
|
|
@ -167,20 +167,10 @@ function stepExample(delta) {
|
||||||
|
|
||||||
select.selectedIndex = (select.selectedIndex + delta + count) % count;
|
select.selectedIndex = (select.selectedIndex + delta + count) % count;
|
||||||
selectExample(select.value);
|
selectExample(select.value);
|
||||||
canvas.focus();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('keydown', (event) => {
|
previousExampleButton.addEventListener('click', () => stepExample(-1));
|
||||||
if (event.repeat || event.target instanceof HTMLSelectElement) {
|
nextExampleButton.addEventListener('click', () => stepExample(1));
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.code === 'KeyJ') {
|
|
||||||
stepExample(1);
|
|
||||||
} else if (event.code === 'KeyK') {
|
|
||||||
stepExample(-1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
setScaleMode(scaleMode, false);
|
setScaleMode(scaleMode, false);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue