开发者

iPad HTML5 canvas not refreshing

Edit: Demo Code is found in the "parallelogram explorer" tab here: http://atmdev01.procloud.net/geometry_tools9/

So I'm calling the following javascript function at document load to draw the perimeter of a parallelogram, and it is working just fine to do that. The issue is when I call the function from the touchmove handler to allow an iPad user to adjust the size of the parallelogram, the canvas is not properly redrawing the shape. In fact it is not responding at all. I've run some alerts to verify that this function is actually being run and it is.

Could it be an issue with the way I'm clearing the canvas (the "canvas.width = canvas.width + 0" method) or simply with refresh rates on the iPad?

The interesting part is that it's working perfectly in a desktop browser using mousemove, but not on an iPad using touchmove. Please advise...

(the "corners" in the code are the areas where the user can touch and drag to resize the parallelogram)

this.drawSides = function() {
    var order = [1, 2, 4, 3];
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    var firstCorner = this.getCornerObj(1);
    var secondCorner = this.getCornerObj(2);    
    var firstCornerOpp = this.getCornerObj(firstCorner.opp);
    var secondCornerOpp = this.getCornerObj(secondCorner.opp);      

    /* Clear the canvas and draw开发者_开发技巧 a parallelogram with the provided corners */
    canvas.width = canvas.width + 0; //clears the canvas faster than clearRect
    ctx.beginPath();
    for (i in order) {
        if (i < order.length) {
            var cornerObj = this.getCornerObj(order[i]);
            if (order[i] > 1) {
                var prevObj = this.getCornerObj(order[i-1]);
                ctx.moveTo(prevObj.x  + (prevObj.width)/2, prevObj.y + (prevObj.height)/2);
                ctx.lineTo(cornerObj.x + (cornerObj.width)/2, cornerObj.y + (cornerObj.height)/2);                  
            }
        }
    }

    ctx.lineTo(firstCorner.x + (firstCorner.width)/2, firstCorner.y + (firstCorner.height)/2);
    ctx.strokeStyle = "#300";
    ctx.stroke();
}


The canvas isn't cleared properly with canvas.width = canvas.width; in safari.


Keep in mind that it's less computationally expensive to clear the canvas using context.clearRect() than it is to reset the canvas size. This is really important for mobile devices and tablets.

Reference: http://www.html5rocks.com/en/tutorials/canvas/performance/#toc-clear-canvas


I've resolved this issue. Turns out the issue was that I wasn't properly updating my cornerObjects' x and y coordinates on touchmove. (the code excerpt above has no issue)

Also, for future reference, canvas.width = canvas.width + 0; works just fine on Safari and the iPad.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜