我是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函数?
我得到的只是未定义的值,我真的迷失了......
更新:
@Juan Mendes,
感谢您的帮助,但这似乎也没有用。
为了更好地理解“背面”代码,以下是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};
}
}
非常感谢任何帮助。提前致谢
答案 0 :(得分:1)
很难说出你在问什么,但希望这会有所帮助
我认为您需要的是以下内容
(function() {
// Save the startPoint so we can use it when the touch ends to draw the line
var startPoint;
canvas.onTouchStart = function(e) {
// set the shared variable
startPoint = {x: e.clientX, y: e.clientY};
}
canvas.onTouchEnd = function (e) {
drawLine(startPoint.x, startPoint.y, e.clientX, e.clientY);
}
})();
我认为活动的名称是ontouchstart
,而不是onTouchStart
以下是我认为您的代码应该是什么样的
function drawLine(sX, sY, eX, eY) {
// Bad use of ctx global, you should pass it into the function so you can support
// multiple contexts
ctx.beginPath()
ctx.moveTo(sX, sY);
ctx.lineTo(eX, eY);
ctx.stroke();
ctx.closePath();
// Kind of silly to return this object, the caller already passed it in
// Should be used only if you need some kind of chaining, but
// makes for a weird API
return { x: eX, y: eY };
}
// Don't name a function and a variable the same thing,
// you had canvas as a variable and as a function.
// They didn't cause a problem, but it's just not pretty to look at
function createCanvas() {
var canvas = new Canvas('canvas', 0, function () {
this.clear();
this.setAutoResize(true);
});
canvas.canvasElement.width = window.innerWidth;
canvas.canvasElement.height = window.innerHeight;
// Save the startPoint so we can use it when the touch ends to draw the line
var startPoint;
// Correct spelling for ontouchstart
canvas.ontouchstart = function(start) {
// set the shared variable that will be used when they finish touching
startPoint = {x: e.clientX, y: e.clientY};
}
// Draw the line
canvas.ontouchend = function (e) {
drawLine(startPoint.x, startPoint.y, e.clientX, e.clientY);
}
}
}
答案 1 :(得分:0)
我终于成功解决了这个问题。原来图书馆没有正确结束(还),我也在没有得到任何论据的情况下滥用这些功能。