-1

我对 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};
}
}

任何帮助将不胜感激。提前致谢

4

2 回答 2

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);
    }    
}

}

于 2012-09-14T18:51:05.040 回答
0

我终于设法解决了这个问题。原来图书馆还没有正确结束(还),我也在没有得到任何参数的情况下滥用这些函数。

于 2012-09-22T01:22:29.970 回答