Skip to content
Open
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
9 changes: 6 additions & 3 deletions classes/class03/lib/shared/camera.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
export const extraHorizontalCaptureAreaInPixels = 600

export default class Camera {
constructor() {
this.video = document.createElement('video')
}

static async init() {
if(!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
throw new Error(
`Browser API navigator.mediaDevices.getUserMedia not available`
)
}
const videoConfig = {
audio: false,
video: {
width: globalThis.screen.availWidth,
width: globalThis.screen.availWidth + extraHorizontalCaptureAreaInPixels,
height: globalThis.screen.availHeight,
frameRate: {
ideal: 60
}
},
transform: `translate(-${extraHorizontalCaptureAreaInPixels}px)`
}
}
const stream = await navigator.mediaDevices.getUserMedia(videoConfig)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { prepareRunChecker } from "../../../../lib/shared/util.js"
import { prepareRunChecker } from '../../../../lib/shared/util.js'
import { extraHorizontalCaptureAreaInPixels } from '../../../../lib/shared/camera.js'

const { shouldRun: scrollShouldRun } = prepareRunChecker({ timerDelay: 200 })
const { shouldRun: clickShouldRun } = prepareRunChecker({ timerDelay: 300 })
Expand Down Expand Up @@ -46,7 +47,7 @@ export default class HandGestureController {
for await (const { event, x, y } of this.#service.detectGestures(hands)) {
if (event === 'click') {
if (!clickShouldRun()) continue
this.#view.clickOnElement(x, y)
this.#view.clickOnElement(x - extraHorizontalCaptureAreaInPixels / 2, y)

continue
}
Expand Down
13 changes: 8 additions & 5 deletions classes/class03/pages/titles/src/views/handGestureView.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { extraHorizontalCaptureAreaInPixels } from '../../../../../lib/shared/camera.js'

export default class HandGestureView {
#handsCanvas = document.querySelector('#hands')
#canvasContext = this.#handsCanvas.getContext('2d')
#fingerLookupIndexes
constructor({ fingerLookupIndexes }) {
this.#handsCanvas.width = globalThis.screen.availWidth
this.#handsCanvas.width = globalThis.screen.availWidth + extraHorizontalCaptureAreaInPixels
this.#handsCanvas.height = globalThis.screen.availHeight
this.#fingerLookupIndexes = fingerLookupIndexes
this.#handsCanvas.style.transform = `translate(-${extraHorizontalCaptureAreaInPixels / 2}px)`
}

clearCanvas() {
Expand All @@ -27,11 +30,11 @@ export default class HandGestureView {
this.#drawFingersAndHoverElements(keypoints)
}
}

clickOnElement(x, y) {
const element = document.elementFromPoint(x, y)
if(!element) return;
if (!element) return;

const rect = element.getBoundingClientRect()
const event = new MouseEvent('click', {
view: window,
Expand Down Expand Up @@ -67,7 +70,7 @@ export default class HandGestureView {
// [0] é a palma da mao (wrist)
const [{ x, y }] = points
region.moveTo(x, y)
for(const point of points) {
for (const point of points) {
region.lineTo(point.x, point.y)
}
this.#canvasContext.stroke(region)
Expand Down