在 canvas 中支持触摸界面
Supporting Touch Interface in a canvas
我用的是canvas,在Javascript中设置支持鼠标拖动:
canvas.onmousedown
canvas.onmouseup
canvas.onmousemove
这有效..我可以支持鼠标拖动操作。
在iOS safari 浏览器中,用手指拖动不会触发鼠标功能。
相反,整个网页只是向上或向下滚动。
起初我想添加 ontouchmove
和其他人,会解决这个问题。但事实并非如此。
移动设备上的浏览器如何判断触摸何时针对 canvas,以及何时针对浏览器本身?
canvas.ontouchmove = function(ev) {
var x = ev.touches[0].clientX;
var y = ev.touches[0].clientY;
if ( dragging) {
drag(canvas, x, y);
}
}
有touchstart、touchmove、touchend。如果您希望浏览器不响应触摸事件,那么您需要告诉它不响应它们。您可以使用 addEventListener
而不是 ontouchstart
并将 {passive: false}
作为最后一个参数传递。否则浏览器在响应触摸事件之前不会等待 JavaScript。然后,您在传递给处理程序的事件对象上调用 preventDefault
,告诉浏览器不要执行正常操作(滚动 window)
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
canvas.addEventListener('touchstart', handleTouchStart, {passive: false});
canvas.addEventListener('touchmove', handleTouchMove);
function handleTouchStart(e) {
e.preventDefault();
}
function handleTouchMove(e) {
const rect = canvas.getBoundingClientRect();
const cssX = e.touches[0].clientX - rect.left;
const cssY = e.touches[0].clientY - rect.top;
const pixelX = cssX * canvas.width / rect.width;
const pixelY = cssY * canvas.height / rect.height;
ctx.fillStyle = `hsl(${performance.now() % 360 | 0},100%,50%)`;
ctx.fillRect(pixelX - 15, pixelY - 15, 30, 30);
}
canvas {
border: 1px solid black;
width: 300px;
height: 150px;
}
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<h1>spacing</h1>
<canvas width="600" height="300"></canvas>
<h1>spacing1</h1>
<h1>spacing2</h1>
<h1>spacing3</h1>
<h1>spacing4</h1>
<h1>spacing5</h1>
<h1>spacing6</h1>
<h1>spacing7</h1>
<h1>spacing8</h1>
<h1>spacing9</h1>
<h1>spacing10</h1>
<h1>spacing11</h1>
<h1>spacing12</h1>
<h1>spacing13</h1>
<h1>spacing14</h1>
注意这里的间距是为了确保有足够的 space 如果您拖动手指 window 会滚动显示当您拖动 canvas 时它不会滚动.元标记在那里,因此浏览器在移动设备上显示更适合移动设备的比例。
我用的是canvas,在Javascript中设置支持鼠标拖动:
canvas.onmousedown
canvas.onmouseup
canvas.onmousemove
这有效..我可以支持鼠标拖动操作。
在iOS safari 浏览器中,用手指拖动不会触发鼠标功能。
相反,整个网页只是向上或向下滚动。
起初我想添加 ontouchmove
和其他人,会解决这个问题。但事实并非如此。
移动设备上的浏览器如何判断触摸何时针对 canvas,以及何时针对浏览器本身?
canvas.ontouchmove = function(ev) {
var x = ev.touches[0].clientX;
var y = ev.touches[0].clientY;
if ( dragging) {
drag(canvas, x, y);
}
}
有touchstart、touchmove、touchend。如果您希望浏览器不响应触摸事件,那么您需要告诉它不响应它们。您可以使用 addEventListener
而不是 ontouchstart
并将 {passive: false}
作为最后一个参数传递。否则浏览器在响应触摸事件之前不会等待 JavaScript。然后,您在传递给处理程序的事件对象上调用 preventDefault
,告诉浏览器不要执行正常操作(滚动 window)
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
canvas.addEventListener('touchstart', handleTouchStart, {passive: false});
canvas.addEventListener('touchmove', handleTouchMove);
function handleTouchStart(e) {
e.preventDefault();
}
function handleTouchMove(e) {
const rect = canvas.getBoundingClientRect();
const cssX = e.touches[0].clientX - rect.left;
const cssY = e.touches[0].clientY - rect.top;
const pixelX = cssX * canvas.width / rect.width;
const pixelY = cssY * canvas.height / rect.height;
ctx.fillStyle = `hsl(${performance.now() % 360 | 0},100%,50%)`;
ctx.fillRect(pixelX - 15, pixelY - 15, 30, 30);
}
canvas {
border: 1px solid black;
width: 300px;
height: 150px;
}
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<h1>spacing</h1>
<canvas width="600" height="300"></canvas>
<h1>spacing1</h1>
<h1>spacing2</h1>
<h1>spacing3</h1>
<h1>spacing4</h1>
<h1>spacing5</h1>
<h1>spacing6</h1>
<h1>spacing7</h1>
<h1>spacing8</h1>
<h1>spacing9</h1>
<h1>spacing10</h1>
<h1>spacing11</h1>
<h1>spacing12</h1>
<h1>spacing13</h1>
<h1>spacing14</h1>
注意这里的间距是为了确保有足够的 space 如果您拖动手指 window 会滚动显示当您拖动 canvas 时它不会滚动.元标记在那里,因此浏览器在移动设备上显示更适合移动设备的比例。