我对 JavaScript 相当陌生,并且一直在摆弄 HTML5 元素。
问题是我现在正在使用一个库来帮助简化画布在包括移动设备在内的多种设备上的使用,但我现在有一个问题困扰着我,因为它看起来很简单但我无法找到一个办法。我确实相信这可能是一个新手错误,但这是交易:
我有这两个功能:
function drawLine(sX, sY, eX, eY) {
ctx.beginPath()
ctx.moveTo(sX, sY);
ctx.lineTo(eX, eY);
ctx.stroke();
ctx.closePath();
return { x: eX, y: eY };
}
function canvas() {
var canvas = new Canvas ('canvas', 0, function () {
this.clear();
this.setAutoResize(true);
});
canvas.canvasElement.width = window.innerWidth;
canvas.canvasElement.height = window.innerHeight;
canvas.onTouchStart = function(start) {
var sX; var sY;
return {sX: start.clientX, sY: start.clientY};
}
canvas.onTouchEnd = function (end) {
var eX; var eY;
return {eX: end.clientX, eY: end.clientY};
}
// canvas.onTouchMove = drawLine(sX, sY, eX, eY);
}
没有详细介绍,我将如何使用 onTouchStart() 和 onTouchEnd() 返回的值将 x,y 位置传递给 drawLine 函数?
我得到的只是没有定义的值,我真的迷失在这里..
更新:
@胡安门德斯,
感谢您的帮助,但这似乎也不起作用。
为了更好地理解“后台”代码,这里是一个 touchstart 的例子:
this.canvasElement.addEventListener('touchstart', function(event) {
var touchCount = event.changedTouches.length;
var touches = [];
var touch = null;
for (var i = 0; i < touchCount; i++) {
touch = event.changedTouches[i];
var touchInfo = {
pageX : touch.pageX,
pageY : touch.pageY,
clientX : touch.clientX,
clientY : touch.clientY,
screenX : touch.screenX,
screenY : touch.screenY,
target : touch.target,
identifier : touch.identifier
};
self.onTouchStart(touchInfo, i, touchCount, event);
touches.push(touchInfo);
self.previousTouchInfo[touch.identifier] = touchInfo;
}
if (touchCount == 1) {
touch = event.changedTouches[0];
var x = touch.clientX;
var y = touch.clientY;
if (self.touchEmulatesMouse) {
self.mouse.x = x;
self.mouse.y = y;
self.mouse.left = true;
if (self.layerParent != null) {
self.layerParent.onMouseDown(x, y, 0);
}
self.onMouseDown(x, y, 0);
}
} else {
self.onMultiTouchStart(touches, event);
}
}, false);
/* A bit more down the library code */
/**
* Called at the start of every touch start (including when multiple touches occured)
*
* The info object contains the folloring info:
* - pageX - X coordinate relative to the full page (includes scrolling)
* - pageY - Y coordinate relative to the full page (includes scrolling)
* - clientX - X coordinate of touch relative to the viewport (excludes scroll offset)
* - clientY - Y coordinate of touch relative to the viewport (excludes scroll offset)
* - screenX - X coordinate relative to the screen
* - screenY - Y coordinate relative to the screen
*
* @param {object} info Touch info
* @param {integer} index Touch index
* @param {integer} count The total number of active touches
* @param {object} event The actual touch event
*/
onTouchStart: function(info, index, count, event) {},
而且我还更改了代码以反映您的帮助,进行了一些更改,并尝试使用 touchInfo 参数,如下所示:
function createCanvas() {
var canvas = new Canvas ('canvas', 0, function () {
this.clear();
this.setAutoResize(true);
});
var cWidth = canvas.canvasElement.width = window.innerWidth;
var cHeight = canvas.canvasElement.height = window.innerHeight;
var startPoint = null;
function fill() {
canvas.fillStyle = "black";
canvas.fillRect(0, 0, cWidth, cHeight);
canvas.beginPath();
}
// Draw a line on the canvas from (s)tart to (e)nd
function drawLine(sX, sY, eX, eY) {
canvas.beginPath()
canvas.moveTo(sX, sY);
canvas.lineTo(eX, eY);
canvas.stroke();
canvas.closePath();
//return { x: eX, y: eY };
}
fill();
canvas.onTouchStart = function(start) {
startPoint = {sX: this.clientX, sY: this.clientY};
}
canvas.onTouchEnd = function (end) {
drawLine(startPoint.sX, startPoint.sY, this.clientX, this.clientY);
// return {eX: end.clientX, eY: end.clientY};
}
}
任何帮助将不胜感激。提前致谢