Foros del Web » Programando para Internet » Javascript »

Evento touchstart en ocasiones no se lanza [HTML + JavaScript]

Estas en el tema de Evento touchstart en ocasiones no se lanza [HTML + JavaScript] en el foro de Javascript en Foros del Web. Tengo problemas con eventos touch de javascript en navegadores móviles. Normalmente funciona como es de esperar, pero hay ocasiones en las se accede a dicha ...
  #1 (permalink)  
Antiguo 27/11/2017, 02:31
 
Fecha de Ingreso: octubre-2006
Mensajes: 8
Antigüedad: 17 años, 6 meses
Puntos: 0
Evento touchstart en ocasiones no se lanza [HTML + JavaScript]

Tengo problemas con eventos touch de javascript en navegadores móviles.
Normalmente funciona como es de esperar, pero hay ocasiones en las se accede a dicha página realizando desplazamientos y taps sobre la pantalla, mientras se carga "la única manera, con la que siempre logro reproducirlo". Esto hace que no se lancen ninguno de los eventos touch y sea imposible realizar los trazos en el canvas.

He simplificado bastante el código, para evitar interferencias con el resto de código de la página.

***index.php***

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<button onclick="location.href='/firmar.php'"><h1>ejemplo firma1</h1></button>
</body>
</html>


***firmar.php*** solicita una firma que se capta mediante un canvas.


<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script type="text/javascript">
var ongoingTouches = [];

function handleStart(evt) {
evt.preventDefault();
var el = document.getElementsByTagName("canvas")[0];
var ctx = el.getContext("2d");
var touches = evt.changedTouches;

for (var i = 0; i < touches.length; i++) {
ongoingTouches.push(copyTouch(touches[i]));
var color = colorForTouch(touches[i]);
ctx.beginPath();
ctx.arc(touches[i].pageX, touches[i].pageY, 4, 0, 2 * Math.PI, false); // a circle at the start
ctx.fillStyle = color;
ctx.fill();
}
}

function handleMove(evt) {
evt.preventDefault();
var el = document.getElementsByTagName("canvas")[0];
var ctx = el.getContext("2d");
var touches = evt.changedTouches;

for (var i = 0; i < touches.length; i++) {
var color = colorForTouch(touches[i]);
var idx = ongoingTouchIndexById(touches[i].identifier);

if (idx >= 0) {
ctx.beginPath();
ctx.moveTo(ongoingTouches[idx].pageX, ongoingTouches[idx].pageY);
ctx.lineTo(touches[i].pageX, touches[i].pageY);
ctx.lineWidth = 4;
ctx.strokeStyle = color;
ctx.stroke();

ongoingTouches.splice(idx, 1, copyTouch(touches[i])); // swap in the new touch record
}
}
}

function handleEnd(evt) {
evt.preventDefault();
var el = document.getElementsByTagName("canvas")[0];
var ctx = el.getContext("2d");
var touches = evt.changedTouches;

for (var i = 0; i < touches.length; i++) {
var color = colorForTouch(touches[i]);
var idx = ongoingTouchIndexById(touches[i].identifier);

if (idx >= 0) {
ctx.lineWidth = 4;
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(ongoingTouches[idx].pageX, ongoingTouches[idx].pageY);
ctx.lineTo(touches[i].pageX, touches[i].pageY);
ctx.fillRect(touches[i].pageX - 4, touches[i].pageY - 4, 8, 8); // and a square at the end
ongoingTouches.splice(idx, 1); // remove it; we're done
}
}
}

function handleCancel(evt) {
evt.preventDefault();
var touches = evt.changedTouches;

for (var i = 0; i < touches.length; i++) {
var idx = ongoingTouchIndexById(touches[i].identifier);
ongoingTouches.splice(idx, 1); // remove it; we're done
}
}

function colorForTouch(touch) {
var r = touch.identifier % 16;
var g = Math.floor(touch.identifier / 3) % 16;
var b = Math.floor(touch.identifier / 7) % 16;
r = r.toString(16); // make it a hex digit
g = g.toString(16); // make it a hex digit
b = b.toString(16); // make it a hex digit
var color = "#" + r + g + b;
return color;
}

function copyTouch(touch) {
return { identifier: touch.identifier, pageX: touch.pageX, pageY: touch.pageY };
}

function ongoingTouchIndexById(idToFind) {
for (var i = 0; i < ongoingTouches.length; i++) {
var id = ongoingTouches[i].identifier;

if (id == idToFind) {
return i;
}
}
return -1; // not found
}

function startup() {
var el = document.getElementsByTagName("canvas")[0];
el.addEventListener("touchstart", handleStart, false);
el.addEventListener("touchend", handleEnd, false);
el.addEventListener("touchcancel", handleCancel, false);
el.addEventListener("touchmove", handleMove, false);

}
</script>

<style>
body{
padding:0;
margin:0
}

svg:not(:root){
display:block
}
.playable-code{
background-color:#f4f7f8;
border:none;
border-left:6px solid #558abb;
border-width:medium medium medium 6px;
color:#4d4e53;
height:100px;
width:90%;
padding:10px 10px 0
}
.playable-canvas{
border:1px solid #4d4e53;
border-radius:2px
}
.playable-buttons{
text-align:right;
/*width:90%;*/
padding:5px 10px 5px 26px
}
</style>
</head>
<body onload="startup()">

<canvas id="canvas" width="300px" height="200px" style="border:solid black 1px;">
Your browser does not support canvas element.
</canvas>

<button onclick="location.href='../index.php'">ir a inicio</button>

</body>
</html>

Gracias

Etiquetas: evento, html
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:47.