【问题标题】:html canvas clearRect() doesnt workhtml画布clearRect()不起作用
【发布时间】:2015-06-02 13:58:17
【问题描述】:

我正在研究 html canvas 并尝试制作简单的动画。 我想让矩形向右移动。 你可以在这里查看我的代码。 http://codepen.io/inkluter/pen/GgeQqj

var x = 0, y = 0, w = 200, h = 100;

    function draw() {
       c.clearRect(0, 0, c.width, c.height);

       x++;
       c.beginPath();
       c.strokeRect(x, y, w, h);
       c.closePath();

       requestAnimationFrame(draw); 
    };

问题是 clearRect() 函数似乎不起作用。旧矩形仍在画布上显示。

我做错了什么?

【问题讨论】:

    标签: javascript animation canvas


    【解决方案1】:

    'width' 和 'height 属性是在 canvas 元素中定义的,而不是在上下文中。改写 c.clearRect(0, 0, canvas.width, canvas.height)。

    【讨论】:

    • 哦,多么愚蠢的错误。谢谢!我为此花了 2 个小时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 2015-02-01
    • 2013-07-31
    相关资源
    最近更新 更多