我正在使用 Cordova + Onsen Ui 2 + javascript 开发一个应用程序,但我在从 javascript 移动事件中获取坐标 X 和 Y 时遇到问题。我尝试了 mousemove(它没有触发)和拖动(但是当我尝试从事件对象获取 pageX 或 clientX 时我得到了未定义)。我还没有找到任何关于用画布绘图的例子。提前谢谢大家!
Javascript:
var canvasListener = function(){
canvas = document.getElementById("canvas");
canvas.addEventListener('mousedown', function(event){
var coordinates = painting(event);
});
canvas.addEventListener('drag', function(event){
var coordinates = painting(event);
});
canvas.addEventListener('mouseup', function(event){
var coordinates = painting(event);
});
}
function painting(event){
var x = event.clientX;
var y = event.clientY;
var touchX = x - signatureCanvas.offsetLeft;
var touchY = y - signatureCanvas.offsetTop;
var localCoordinates;
if(event.type == 'mouseup'){
localCoordinates = {
x: 0,
y: 0
};
}else{
localCoordinates = {
x: touchX,
y: touchY
};
}
return localCoordinates;
}
html:
<canvas id="canvas"></canvas>