javascript - iPad HTML5 canvas not refreshing - Stack Overflow

Edit: Demo Code is found in the "parallelogram explorer" tab here: So I'm calling the f

Edit: Demo Code is found in the "parallelogram explorer" tab here: /

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

Edit: Demo Code is found in the "parallelogram explorer" tab here: http://atmdev01.procloud/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();
}
Share Improve this question edited Feb 24, 2011 at 19:49 mr_z_ro asked Feb 24, 2011 at 10:56 mr_z_romr_z_ro 7738 silver badges16 bronze badges 3
  • Providing a portion of your code is nice. Much better, though, would be to host a reproducible pared-down test case online showing the interaction of all your pieces. – Phrogz Commented Feb 24, 2011 at 16:05
  • @Phrogs Here is a demo: atmdev01.procloud/geometry_tools9 – mr_z_ro Commented Feb 24, 2011 at 19:48
  • Thanks; when I get home to my iPad I'll take a look. (If you had spelled my name correctly, I would have seen this last night. ;) – Phrogz Commented Feb 25, 2011 at 16:50
Add a ment  | 

3 Answers 3

Reset to default 4

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

Keep in mind that it's less putationally 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./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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742388353a4434518.html

相关推荐

  • javascript - iPad HTML5 canvas not refreshing - Stack Overflow

    Edit: Demo Code is found in the "parallelogram explorer" tab here: So I'm calling the f

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信