-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Intergrate EventProxy for OffscreenCanvas
- Loading branch information
Showing
14 changed files
with
297 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { AssociatedArrayType, WorkerTask, WorkerTaskMessage } from 'wtd-core'; | ||
import { OffscreenPayload } from './OffscreenPayload.js'; | ||
|
||
export const wheelEventHandler = (event: Event, sendFn: (data: OffscreenPayload) => void) => { | ||
event.preventDefault(); | ||
wheelEventHandlerImpl(event, sendFn); | ||
}; | ||
|
||
export const preventDefaultHandler = (event: Event) => { | ||
event.preventDefault(); | ||
}; | ||
|
||
export const copyProperties = (src: Event, properties: string[], dst: AssociatedArrayType<unknown>) => { | ||
for (const name of properties) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
dst[name] = (src as any)[name]; | ||
} | ||
}; | ||
|
||
export const makeSendPropertiesHandler = (properties: string[]) => { | ||
return function sendProperties(event: Event, sendFn: (data: OffscreenPayload) => void) { | ||
const eventTarget = { | ||
type: event.type, | ||
}; | ||
copyProperties(event, properties, eventTarget); | ||
const payload = new OffscreenPayload({ | ||
event: eventTarget | ||
}); | ||
|
||
sendFn(payload); | ||
}; | ||
}; | ||
|
||
export const touchEventHandler = (event: TouchEvent, sendFn: (data: OffscreenPayload) => void) => { | ||
const touches = []; | ||
|
||
// eslint-disable-next-line @typescript-eslint/prefer-for-of | ||
for (let i = 0; i < event.touches.length; ++i) { | ||
const touch = event.touches[i]; | ||
touches.push({ | ||
pageX: touch.pageX, | ||
pageY: touch.pageY, | ||
}); | ||
} | ||
const payload = new OffscreenPayload({ | ||
event: { | ||
type: event.type, | ||
touches | ||
} | ||
}); | ||
sendFn(payload); | ||
}; | ||
|
||
// The four arrow keys | ||
export const orbitKeys: AssociatedArrayType<boolean> = { | ||
'37': true, // left | ||
'38': true, // up | ||
'39': true, // right | ||
'40': true, // down | ||
}; | ||
|
||
export const filteredKeydownEventHandler = (event: KeyboardEvent, sendFn: (data: OffscreenPayload) => void) => { | ||
const { code } = event; | ||
if (orbitKeys[code]) { | ||
event.preventDefault(); | ||
keydownEventHandler(event, sendFn); | ||
} | ||
}; | ||
|
||
export const mouseEventHandler = makeSendPropertiesHandler([ | ||
'ctrlKey', | ||
'metaKey', | ||
'shiftKey', | ||
'button', | ||
'clientX', | ||
'clientY', | ||
'pageX', | ||
'pageY', | ||
]); | ||
|
||
export const wheelEventHandlerImpl = makeSendPropertiesHandler([ | ||
'deltaX', | ||
'deltaY', | ||
]); | ||
|
||
export const keydownEventHandler = makeSendPropertiesHandler([ | ||
'ctrlKey', | ||
'metaKey', | ||
'shiftKey', | ||
'keyCode', | ||
]); | ||
|
||
// function sendSize() { | ||
// const rect = element.getBoundingClientRect(); | ||
// sendEvent({ | ||
// type: 'size', | ||
// left: rect.left, | ||
// top: rect.top, | ||
// width: element.clientWidth, | ||
// height: element.clientHeight, | ||
// }); | ||
// } | ||
|
||
// // really need to use ResizeObserver | ||
// window.addEventListener('resize', sendSize); | ||
// } | ||
|
||
export const registerCanvas = (canvas: HTMLCanvasElement, workerTask: WorkerTask) => { | ||
canvas.focus(); | ||
|
||
const offscreenPayload = new OffscreenPayload({ | ||
event: { | ||
type: 'init' | ||
} | ||
}); | ||
workerTask.sentMessage(WorkerTaskMessage.fromPayload(offscreenPayload), 'proxyStart'); | ||
|
||
const eventHandlers = { | ||
contextmenu: preventDefaultHandler, | ||
mousedown: mouseEventHandler, | ||
mousemove: mouseEventHandler, | ||
mouseup: mouseEventHandler, | ||
touchstart: touchEventHandler, | ||
touchmove: touchEventHandler, | ||
touchend: touchEventHandler, | ||
wheel: wheelEventHandler, | ||
keydown: filteredKeydownEventHandler, | ||
}; | ||
|
||
for (const [eventName, handler] of Object.entries(eventHandlers)) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
canvas.addEventListener(eventName, function(event: any) { | ||
handler(event, (offscreenPayload: OffscreenPayload) => { | ||
workerTask.sentMessage(WorkerTaskMessage.fromPayload(offscreenPayload), 'proxyEvent'); | ||
}); | ||
}); | ||
} | ||
}; |
Oops, something went wrong.