我正在使用 jQuery Mobile UI 框架制作一个 PhoneGap 应用程序。我需要一个用户可以在屏幕上绘制内容的页面。我将此用作参考,它在 Ripple Emulator 中效果很好。但是,在我的实际设备 Nexus 4 上,每次 touchmove 不是一行,而是两行。我在做什么有问题吗?
编辑:我在 github 中发现了一个类似的问题。这似乎是Android浏览器的问题。两条线是由于重叠的画布元素。唯一的解决方案是画布尺寸小于 256 像素。这是链接: https ://github.com/jquery/jquery-mobile/issues/5107
这是我的代码
// start canvas code
var canvas = null; //canvas object
var context = null; //canvas's context object
var clearBtn = null; //clear button object
var buttonDown = false;
function captureDraw(){
canvas = document.getElementById('canvas');
clearBtn = document.getElementById('clearBtn');
setCanvasDimension();
initializeEvents();
context = canvas.getContext('2d');
}
function setCanvasDimension() {
//canvas.width = 300;
// canvas.width = window.innerWidth;
// canvas.height = window.innerHeight; //setting the height of the canvas
}
function initializeEvents() {
canvas.addEventListener('touchstart', startPaint, false);
canvas.addEventListener('touchmove', continuePaint, false);
canvas.addEventListener('touchend', stopPaint, false);
clearBtn.addEventListener('touchend', clearCanvas,false);
}
function clearCanvas() {
context.clearRect(0,0,canvas.width,canvas.height);
}
function startPaint(evt) {
if(!buttonDown)
{
context.beginPath();
context.moveTo(evt.touches[0].pageX, evt.touches[0].pageY);
buttonDown = true;
}
evt.preventDefault();
}
function continuePaint(evt) {
if(buttonDown)
{
context.lineTo(evt.touches[0].pageX,evt.touches[0].pageY);
context.stroke();
}
}
function stopPaint() {
buttonDown = false;
}
// end canvas code
谢谢!