简体   繁体   English

iPad HTML5画布不爽

[英]iPad HTML5 canvas not refreshing

Edit: Demo Code is found in the "parallelogram explorer" tab here: http://atmdev01.procloud.net/geometry_tools9/ 编辑:演示代码可在此处的“parallelogram explorer”选项卡中找到: 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. 所以我在文档加载时调用以下javascript函数来绘制平行四边形的边界,并且它正常工作。 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. 问题是当我从touchmove处理程序调用该函数以允许iPad用户调整平行四边形的大小时,画布不能正确地重绘形状。 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? 这可能是我清除画布的方式(“canvas.width = canvas.width + 0”方法)还是只是在iPad上刷新率的问题?

The interesting part is that it's working perfectly in a desktop browser using mousemove, but not on an iPad using touchmove. 有趣的是,它在使用mousemove的桌面浏览器中完美运行,但在使用touchmove的iPad上却没有。 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; canvas.width = canvas.width;无法正确清除画布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. 请记住,使用context.clearRect()清除画布的计算成本要低于重置画布大小。 This is really important for mobile devices and tablets. 这对移动设备和平板电脑非常重要。

Reference: http://www.html5rocks.com/en/tutorials/canvas/performance/#toc-clear-canvas 参考: 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. 原来问题是我没有在touchmove上正确更新我的cornerObjects的x和y坐标。 (the code excerpt above has no issue) (上面的代码摘录没有问题)

Also, for future reference, canvas.width = canvas.width + 0; 另外,为了将来参考,canvas.width = canvas.width + 0; works just fine on Safari and the iPad. 在Safari和iPad上运行得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM