A very simple Svelte action to take over pointer management.
npm install @terrygonguet/svelte-pointerlock
<script lang="ts">
import { usePointerlock } from "$lib"
const { pointerlock, enter, exit, toggle } = usePointerlock()
function onClick() {
toggle()
}
</script>
<main use:pointerlock on:click="{onClick}">
<p>Click to toggle pointer lock</p>
</main>
Call the usePointerlock function in your component and use: the pointerlock property of the returned object on the element you want to lock the pointer to. The enter, exit and toggle functions do what their names imply and return nothing, same as the underlying Pointerlock API.
function usePointerlockfunction usePointerlock(): {
pointerlock: Action
enter(): void
exit(): void
toggle(): void
}