该主题可能不足以解决我的问题,对此感到抱歉,因为我对这些条款不太胜任。问题是我有一个画布,当我点击鼠标时可以在上面画东西。但是当我尝试用触摸(即iphone和其他触摸操作设备)来做它时,它没有检测到运动,所以什么都不能画画布..我需要什么来检测触摸动作?任何想法都会很棒..
这是我的 js 小提琴链接http://jsfiddle.net/regeme/xNSVm/
这里也是绘图的脚本
painting = false;
var WIDTH = 300;
var HEIGHT =300;
function clear() {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.fillStyle = "white";
}
function generate(){
var newCanvas = document.createElement('canvas');
newCanvas.width = WIDTH;
newCanvas.height = HEIGHT;
document.getElementById('container').appendChild(newCanvas);
ctx = newCanvas.getContext('2d');
ctx.fillStyle = "black";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.fillStyle = "white";
newCanvas.onmousedown = function(e) {
painting = true;
ctx = newCanvas.getContext('2d');
lastX = e.pageX - this.offsetLeft;
lastY = e.pageY - this.offsetTop;
};
newCanvas.onmouseup = function(e) {
painting = false;
};
newCanvas.onmousemove = function(e) {
if (painting) {
mouseX = e.pageX - this.offsetLeft;
mouseY = e.pageY - this.offsetTop;
// find all points between
var x1 = mouseX,
x2 = lastX,
y1 = mouseY,
y2 = lastY;
var steep = (Math.abs(y2 - y1) > Math.abs(x2 - x1));
if (steep){
var x = x1;
x1 = y1;
y1 = x;
var y = y2;
y2 = x2;
x2 = y;
}
if (x1 > x2) {
var x = x1;
x1 = x2;
x2 = x;
var y = y1;
y1 = y2;
y2 = y;
}
var dx = x2 - x1,
dy = Math.abs(y2 - y1),
error = 0,
de = dy / dx,
yStep = -1,
y = y1;
if (y1 < y2) {
yStep = 1;
}
for (var x = x1; x < x2; x++) {
if (steep) {
ctx.fillRect(y, x, 1, 1);
} else {
ctx.fillRect(x, y, 1, 1);
}
error += de;
if (error >= 0.5) {
y += yStep;
error -= 1.0;
}
}
lastX = mouseX;
lastY = mouseY;
}
};
}
document.getElementById('button').onclick = clear;
document.getElementById('generate').onclick = generate;
document.onmouseup = function(e) {
painting = false;
};
generate();