Skip to content
Merged
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
46 changes: 25 additions & 21 deletions packages/modelviewer.dev/examples/stagingandcameras/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ <h4>Event listeners can cooperate with camera-controls</h4>
Math.abs(pointer.clientY - startY) > tapDistance)
return;
const rect = modelViewer.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const x = pointer.clientX - rect.left;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lack of type-checking bit us again. Surprisingly, the browser injects event as a variable in the event handler scope.
Try:
window.addEventListener('click', () => console.log(event))

It was working with mouse events because unlike TouchEvent, MouseEvent has properties clientX and clientY.

const y = pointer.clientY - rect.top;
const hit = modelViewer.positionAndNormalFromPoint(x, y);
modelViewer.cameraTarget =
hit == null ? 'auto auto auto' : hit.position.toString();
Expand All @@ -269,15 +269,15 @@ <h4>Event listeners can cooperate with camera-controls</h4>
}, true);

modelViewer.addEventListener('touchstart', (event) => {
startX = event.touches[0].clientX;
startY = event.touches[0].clientY;
panning = event.touches.length === 2;
const {targetTouches, touches} = event;
startX = targetTouches[0].clientX;
startY = targetTouches[0].clientY;
panning = targetTouches.length === 2 && targetTouches.length === touches.length;
if (!panning)
return;

const {touches} = event;
lastX = 0.5 * (touches[0].clientX + touches[1].clientX);
lastY = 0.5 * (touches[0].clientY + touches[1].clientY);
lastX = 0.5 * (targetTouches[0].clientX + targetTouches[1].clientX);
lastY = 0.5 * (targetTouches[0].clientY + targetTouches[1].clientY);
startPan();
}, true);

Expand All @@ -290,22 +290,26 @@ <h4>Event listeners can cooperate with camera-controls</h4>
}, true);

modelViewer.addEventListener('touchmove', (event) => {
if (!panning || event.touches.length !== 2)
if (!panning || event.targetTouches.length !== 2)
return;

const {touches} = event;
const thisX = 0.5 * (touches[0].clientX + touches[1].clientX);
const thisY = 0.5 * (touches[0].clientY + touches[1].clientY);
const {targetTouches} = event;
const thisX = 0.5 * (targetTouches[0].clientX + targetTouches[1].clientX);
const thisY = 0.5 * (targetTouches[0].clientY + targetTouches[1].clientY);
movePan(thisX, thisY);
}, true);

self.addEventListener('mouseup', (event) => {
recenter(event);
}, true);

self.addEventListener('touchend', (event) => {
if (event.touches.length === 0) {
modelViewer.addEventListener('touchend', (event) => {
if (event.targetTouches.length === 0) {
recenter(event.changedTouches[0]);

if (event.cancelable) {
event.preventDefault();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prevents emulation of mouseup event which was invoking recenter a second time.

}
}
}, true);
})();
Expand Down Expand Up @@ -369,12 +373,12 @@ <h2 class="demo-title">Add other interactions like rotating the skybox</h2>
}, true);

modelViewer.addEventListener('touchstart', (event) => {
panning = event.touches.length === 2;
const {targetTouches, touches} = event;
panning = targetTouches.length === 2 && targetTouches.length === touches.length;
if (!panning)
return;

const {touches} = event;
lastX = 0.5 * (touches[0].clientX + touches[1].clientX);
lastX = 0.5 * (targetTouches[0].clientX + targetTouches[1].clientX);
startPan();
}, true);

Expand All @@ -387,19 +391,19 @@ <h2 class="demo-title">Add other interactions like rotating the skybox</h2>
}, true);

modelViewer.addEventListener('touchmove', (event) => {
if (!panning || event.touches.length !== 2)
if (!panning || event.targetTouches.length !== 2)
return;

const {touches} = event;
const thisX = 0.5 * (touches[0].clientX + touches[1].clientX);
const {targetTouches} = event;
const thisX = 0.5 * (targetTouches[0].clientX + targetTouches[1].clientX);
updatePan(thisX);
}, true);

self.addEventListener('mouseup', (event) => {
panning = false;
}, true);

self.addEventListener('touchend', (event) => {
modelViewer.addEventListener('touchend', (event) => {
panning = false;
}, true);
</script>
Expand Down