Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump #2

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pointer-lock-movement/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pointer-lock-movement",
"version": "0.1.6",
"version": "0.1.7",
"author": "Zheeeng <hi@zheeeng.me>",
"description": "A pointer lock movement manager for customizing your own creative UI.",
"keywords": [
Expand Down
46 changes: 24 additions & 22 deletions packages/pointer-lock-movement/src/pointer-lock-movement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const pointerLockMovement = (
...customOption
}: PointerLockMovementOption = {}
) => {
const option = { ...customOption, loopBehavior, trigger, zIndex }
const options = { ...customOption, loopBehavior, trigger, zIndex }

function requestPointerLock () {
(element.requestPointerLock ?? element.mozRequestPointerLock ?? element.webkitRequestPointerLock).call(element)
Expand Down Expand Up @@ -89,7 +89,7 @@ export const pointerLockMovement = (
context.y += context.movementY
context.status = 'moving'

if (option.loopBehavior === 'loop') {
if (options.loopBehavior === 'loop') {
if (context.x > context.maxWidth) {
context.x -= context.maxWidth
} else if (context.x < 0) {
Expand All @@ -101,7 +101,7 @@ export const pointerLockMovement = (
} else if (context.y < 0) {
context.y += context.maxHeight
}
} else if (option.loopBehavior === 'stop') {
} else if (options.loopBehavior === 'stop') {
if (context.x > context.maxWidth) {
context.x = context.maxWidth
context.status = 'stopped'
Expand Down Expand Up @@ -135,13 +135,13 @@ export const pointerLockMovement = (
}

function detectMoveOffset (event: Event) {
if (!(event instanceof PointerEvent) || !event.buttons || !option.dragOffset) {
if (!(event instanceof PointerEvent) || !event.buttons || !options.dragOffset) {
return
}

const offset = Math.sqrt(Math.pow(event.clientX - localState.startX, 2) + Math.pow(event.clientY - localState.startY, 2))

if (offset < option.dragOffset) {
if (offset < options.dragOffset) {
return
}

Expand All @@ -154,7 +154,7 @@ export const pointerLockMovement = (
function deActive () {
exitPointerLock()

option.onLock?.(false)
options.onLock?.(false)
document.removeEventListener('pointermove', handlePointerMove)

nextFn = undefined
Expand All @@ -163,17 +163,17 @@ export const pointerLockMovement = (
}

function handlePointerMove (event: PointerEvent) {
if (option.trigger === 'drag' && !event.buttons) {
if (options.trigger === 'drag' && !event.buttons) {
deActive()
} else {
handleContinueMove(event)
}
}

function active (pointerEvent: PointerEvent) {
const virtualScreen = requestScreen(option.screen, { zIndex: option.zIndex })
const virtualScreen = requestScreen(options.screen, { zIndex: options.zIndex })

const virtualCursor = requestCursor(option.cursor, { zIndex: option.zIndex })
const virtualCursor = requestCursor(options.cursor, { zIndex: options.zIndex })

nextFn = move(
{
Expand All @@ -189,9 +189,7 @@ export const pointerLockMovement = (
maxHeight: virtualScreen.height,
},
({ event, status, x, y, startX, startY, movementX, movementY }) => {
virtualCursor.style.transform = `translate3D(${x}px, ${y}px, 0px)`

option.onMove?.(
options.onMove?.(
event,
{
status,
Expand All @@ -201,6 +199,10 @@ export const pointerLockMovement = (
movementY,
}
)

if (!event.defaultPrevented) {
virtualCursor.style.transform = `translate3D(${x}px, ${y}px, 0px)`
}
}
)(pointerEvent)

Expand All @@ -215,7 +217,7 @@ export const pointerLockMovement = (
deActive()
})

option.onLock?.(true)
options.onLock?.(true)
requestPointerLock()
}

Expand Down Expand Up @@ -244,7 +246,7 @@ export const pointerLockMovement = (
return
}

if (localState.targetOnActiveElement && option.disableOnActiveElement) {
if (localState.targetOnActiveElement && options.disableOnActiveElement) {
return
}

Expand All @@ -264,19 +266,19 @@ export const pointerLockMovement = (
}

function handleDragOffsetActive (event: Event) {
if (!(event instanceof PointerEvent) || event.button !== 0 || !option.dragOffset) {
if (!(event instanceof PointerEvent) || event.button !== 0 || !options.dragOffset) {
return
}

if (localState.targetOnActiveElement && option.disableOnActiveElement) {
if (localState.targetOnActiveElement && options.disableOnActiveElement) {
return
}

if (isLocked()) {
return
}

option?.onPrepareLock?.(event)
options?.onPrepareLock?.(event)

localState.isDetecting = true
localState.startX = event.clientX
Expand All @@ -288,7 +290,7 @@ export const pointerLockMovement = (
if (localState.isDetecting) {

if (event instanceof PointerEvent) {
option?.onCancelPrepareLock?.(event)
options?.onCancelPrepareLock?.(event)
}
}

Expand All @@ -299,7 +301,7 @@ export const pointerLockMovement = (
}

function markElementIsActiveElement (event: Event) {
if (!(event instanceof PointerEvent) || event.button !== 0 || !option.disableOnActiveElement) {
if (!(event instanceof PointerEvent) || event.button !== 0 || !options.disableOnActiveElement) {
return
}

Expand All @@ -312,12 +314,12 @@ export const pointerLockMovement = (

assertSupportPointerLock()

if (option.disableOnActiveElement) {
if (options.disableOnActiveElement) {
element.addEventListener('pointerdown', markElementIsActiveElement)
}

if (option.trigger === 'drag') {
if (option.dragOffset) {
if (options.trigger === 'drag') {
if (options.dragOffset) {
element.addEventListener('pointerdown', handleDragOffsetActive)
document.addEventListener('pointerup', handleDeActive)

Expand Down
Loading