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

refactor: types ARButton and VRButton #28

Closed
wants to merge 3 commits into from
Closed
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
50 changes: 32 additions & 18 deletions src/webxr/ARButton.js → src/webxr/ARButton.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import { WebGLRenderer, XRSession, XRSessionInit } from 'three'

export interface ARButtonSessionInit extends XRSessionInit {
domOverlay?: {
root: HTMLDivElement
}
}

class ARButton {
static createButton(renderer, sessionInit = {}) {
static createButton(
renderer: WebGLRenderer,
sessionInit: ARButtonSessionInit = {},
): HTMLButtonElement | HTMLAnchorElement {
const button = document.createElement('button')

function showStartAR(/*device*/) {
if (sessionInit.domOverlay === undefined) {
var overlay = document.createElement('div')
const overlay = document.createElement('div')
overlay.style.display = 'none'
document.body.appendChild(overlay)

var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
svg.setAttribute('width', 38)
svg.setAttribute('height', 38)
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
svg.setAttribute('width', '38')
svg.setAttribute('height', '38')
svg.style.position = 'absolute'
svg.style.right = '20px'
svg.style.top = '20px'
svg.addEventListener('click', function () {
currentSession.end()
})
svg.addEventListener('click', () => currentSession?.end())
overlay.appendChild(svg)

var path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
path.setAttribute('d', 'M 12,12 L 28,28 M 28,12 12,28')
path.setAttribute('stroke', '#fff')
path.setAttribute('stroke-width', 2)
path.setAttribute('stroke-width', '2')
svg.appendChild(path)

if (sessionInit.optionalFeatures === undefined) {
Expand All @@ -35,26 +44,26 @@ class ARButton {

//

let currentSession = null
let currentSession: XRSession | null

async function onSessionStarted(session) {
async function onSessionStarted(session: XRSession) {
session.addEventListener('end', onSessionEnded)

renderer.xr.setReferenceSpaceType('local')

await renderer.xr.setSession(session)

button.textContent = 'STOP AR'
sessionInit.domOverlay.root.style.display = ''
sessionInit.domOverlay!.root.style.display = ''

currentSession = session
}

function onSessionEnded(/*event*/) {
currentSession.removeEventListener('end', onSessionEnded)
currentSession?.removeEventListener('end', onSessionEnded)

button.textContent = 'START AR'
sessionInit.domOverlay.root.style.display = 'none'
sessionInit.domOverlay!.root.style.display = 'none'

currentSession = null
}
Expand All @@ -79,6 +88,9 @@ class ARButton {

button.onclick = function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could use addEventListener instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I'll convert it the morning 😃

if (currentSession === null) {
// @ts-expect-error waiting for TypeScript lib.dom.d.ts update
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/xr
// https://github.com/Microsoft/TypeScript/blob/master/lib/lib.dom.d.ts
navigator.xr.requestSession('immersive-ar', sessionInit).then(onSessionStarted)
} else {
currentSession.end()
Expand All @@ -101,11 +113,10 @@ class ARButton {

function showARNotSupported() {
disableButton()

button.textContent = 'AR NOT SUPPORTED'
}

function stylizeElement(element) {
function stylizeElement(element: HTMLElement) {
element.style.position = 'absolute'
element.style.bottom = '20px'
element.style.padding = '12px 6px'
Expand All @@ -126,9 +137,12 @@ class ARButton {

stylizeElement(button)

// @ts-expect-error waiting for TypeScript lib.dom.d.ts update
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/xr
// https://github.com/Microsoft/TypeScript/blob/master/lib/lib.dom.d.ts
navigator.xr
.isSessionSupported('immersive-ar')
.then(function (supported) {
.then((supported: boolean) => {
supported ? showStartAR() : showARNotSupported()
})
.catch(showARNotSupported)
Expand Down
49 changes: 19 additions & 30 deletions src/webxr/VRButton.js → src/webxr/VRButton.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
class VRButton {
static createButton(renderer, options) {
if (options) {
console.error(
'THREE.VRButton: The "options" parameter has been removed. Please set the reference space type via renderer.xr.setReferenceSpaceType() instead.',
)
}
import { WebGLRenderer, XRSession } from 'three'

class VRButton {
static createButton(renderer: WebGLRenderer): HTMLButtonElement | HTMLAnchorElement {
const button = document.createElement('button')

function showEnterVR(/*device*/) {
let currentSession = null
let currentSession: XRSession | null

async function onSessionStarted(session) {
async function onSessionStarted(session: XRSession) {
session.addEventListener('end', onSessionEnded)

await renderer.xr.setSession(session)
button.textContent = 'EXIT VR'

currentSession = session
}

function onSessionEnded(/*event*/) {
currentSession.removeEventListener('end', onSessionEnded)

currentSession?.removeEventListener('end', onSessionEnded)
button.textContent = 'ENTER VR'

currentSession = null
}

Expand All @@ -38,16 +30,13 @@ class VRButton {

button.textContent = 'ENTER VR'

button.onmouseenter = function () {
button.style.opacity = '1.0'
}

button.onmouseleave = function () {
button.style.opacity = '0.5'
}
button.onmouseenter = () => (button.style.opacity = '1.0')
joshuaellis marked this conversation as resolved.
Show resolved Hide resolved
button.onmouseleave = () => (button.style.opacity = '0.5')

button.onclick = function () {
if (currentSession === null) {
button.onclick = () => {
if (currentSession) {
currentSession.end()
} else {
// WebXR's requestReferenceSpace only works if the corresponding feature
// was requested at session creation time. For simplicity, just ask for
// the interesting ones as optional features, but be aware that the
Expand All @@ -58,14 +47,14 @@ class VRButton {
const sessionInit = {
optionalFeatures: ['local-floor', 'bounded-floor', 'hand-tracking'],
}
navigator.xr.requestSession('immersive-vr', sessionInit).then(onSessionStarted)
} else {
currentSession.end()

const xr: THREE.XR = (navigator as any).xr
xr.requestSession('immersive-vr', sessionInit).then(onSessionStarted)
}
}
}

function disableButton() {
function disableButton(): void {
button.style.display = ''

button.style.cursor = 'auto'
Expand All @@ -80,11 +69,10 @@ class VRButton {

function showWebXRNotFound() {
disableButton()

button.textContent = 'VR NOT SUPPORTED'
}

function stylizeElement(element) {
function stylizeElement(element: HTMLElement): void {
element.style.position = 'absolute'
element.style.bottom = '20px'
element.style.padding = '12px 6px'
Expand All @@ -105,7 +93,8 @@ class VRButton {

stylizeElement(button)

navigator.xr.isSessionSupported('immersive-vr').then(function (supported) {
const xr: THREE.XR = (navigator as any).xr
xr.isSessionSupported('immersive-vr').then((supported) => {
supported ? showEnterVR() : showWebXRNotFound()
})

Expand Down