Hi everyone,
I’m having a problem with the Safari browser on my iPhone. The problem is that the objects are not clickable, but the click event works perfectly in desktop browsers and Chrome on iPhone.
for more information: Panning with orbitcontrols on the iPhone works. Only when I want to click on an object on the scene, the click event doesn’t work.
Here are some of my codes:
Has anyone else been in this situation?
var mouse = new THREE.Vector2(),
INTERSECTED;
var intersected;
function onDocumentTouchStart(event) {
if (event.touches.length == 1) {
mouse.x = (event.touches[0].pageX / window.innerWidth) * 2 - 1;
mouse.y = -(event.touches[0].pageY / window.innerHeight) * 2 + 1;
}
}
function onDocumentTouchMove(event) {
if (event.touches.length == 1) {
event.preventDefault();
mouse.x = (event.touches[0].pageX / window.innerWidth) * 2 - 1;
mouse.y = -(event.touches[0].pageY / window.innerHeight) * 2 + 1;
}
}
function onDocumentTouchEnd(event) {
if (event.touches.length == 1) {
mouse.x = (event.touches[0].pageX / window.innerWidth) * 2 - 1;
mouse.y = -(event.touches[0].pageY / window.innerHeight) * 2 + 1;
}
}
window.addEventListener("touchstart", onDocumentTouchStart, false);
window.addEventListener("touchmove", onDocumentTouchMove, false);
window.addEventListener("touchend", onDocumentTouchEnd, false);
let isMobile = false;
if (
navigator.userAgent.match(/Android/i) ||
navigator.userAgent.match(/webOS/i) ||
navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/iPod/i) ||
navigator.userAgent.match(/BlackBerry/i) ||
navigator.userAgent.match(/Windows Phone/i)
) {
isMobile = true;
}
window.addEventListener("click", (event) => {
event.preventDefault();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects([...sceneMeshes, ...cubes]);
if (intersects.length > 0 && currentIntersect) {
for (let i = 0; i < intersects.length; i++) {
cameraTurnAndLook(currentIntersect);
currentIntersect = intersects[0];
}
}
});
function onMouseMove(event) {
event.preventDefault();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects([...sceneMeshes, ...cubes]);
if (intersects.length) {
if (!currentIntersect) {
$("html,body").css("cursor", "crosshair");
}
currentIntersect = intersects[0].object;
} else {
if (currentIntersect) {
$("html,body").css("cursor", "context-menu");
}
currentIntersect = null;
}
}
window.addEventListener("mousemove", onMouseMove, false);
if (!isMobile) {
window.addEventListener("touchstart", (event) => {
if (event.touches.length == 1) {
mouse.x = (event.touches[0].pageX / window.innerWidth) * 2 - 1;
mouse.y = -(event.touches[0].pageY / window.innerHeight) * 2 + 1;
}
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects([...sceneMeshes, ...cubes]);
if (intersects.length > 0 && currentIntersect) {
for (let i = 0; i < intersects.length; i++) {
camera.getWorldPosition(camerapos);
cameraTurnAndLook(currentIntersect);
currentIntersect = intersects[0];
}
}
});
}