我對JavaScript相當陌生,並且一直在使用HTML5元素。JavaScript對象/屬性
問題是我現在正在使用一個庫來幫助簡化在多種設備(包括移動設備)上使用畫布,但是現在我有一個問題,因爲它看起來很簡單,米無法找到解決方案。我相信這可能是一個新手的錯誤,但這裏的交易:
我有這2個功能:
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函數?
所有我得到的是沒有定義的值,我真的在這裏失去了..
UPDATE:
@Juan門德斯,
感謝您的幫助,但沒看起來也不行。
爲了更好地理解的「背面」的代碼,這裏的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 PARAM這樣的:
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};
}
}
任何幫助將是非常讚賞。在此先感謝
那麼,我會試試你的建議。順便說一句,onTouchStart拼寫的方式,因爲它是一個庫方法。我正在使用HTML Canvas Lib:http://html-canvas-lib.sourceforge.net/ –
Hi @Juan。謝謝你的幫助。我修改了一些代碼,但仍然無法工作。我已經在下一個「答案」中發佈了新代碼,那麼請你參加一下嗎?謝謝 –
@Zed_Blade錯誤信息? –