简体   繁体   English

如何从画布中删除对象?

[英]How to remove an object from the canvas?

I am making this script that will rotate a needle on a tachometer using canvas.我正在制作这个脚本,它将使用画布在转速计上旋转指针。 I am a newbie to this canvas.我是这个画布的新手。 This is my code:这是我的代码:

function startup() {
  var canvas = document.getElementById('canvas');
  var context = canvas.getContext('2d');
  var meter = new Image();
  meter.src = 'background.png';
  var pin = new Image();
  pin.src = 'needle.png';
  context.drawImage(meter,0,0);
  context.translate(275,297);
  for (var frm = 0; frm < 6000; frm++){
    var r=frm/1000;               //handle here                                
    var i=r*36-27;  //angle of rotation from value of r and span
    var angleInRadians = 3.14159265 * i/180;  //converting degree to radian                
    context.rotate(angleInRadians); //rotating by angle
    context.drawImage(pin,-250,-3);  //adjusting pin center at meter center
  }
}

Here is the script in action:这是正在运行的脚本:

http://www.kingoslo.com/instruments/ http://www.kingoslo.com/instruments/

The problem is, as you can see, that the red needle is not removed beetween each for-loop.问题是,正如您所看到的,每个 for 循环之间的红针没有被移除。

What I need to do is to clear the canvas for the pin object between each cycle of the loop.我需要做的是在循环的每个循环之间清除 pin 对象的画布。 How do I do this?我该怎么做?

Thanks.谢谢。

Kind regards,亲切的问候,
Marius马吕斯

Use clearRect to clear the canvas (either parts of it or the whole thing).使用clearRect清除画布(部分或全部)。 An HTML canvas object is just a flat bitmap of pixels, so there is no notion of "objects" on the canvas. HTML canvas对象只是像素的平面位图,因此画布上没有“对象”的概念。

Also keep in mind that JavaScript is single threaded, so your for loop will not animate the needle, it will just sit there and draw all the updates, after it's done the browser will actually refresh the visible canvas with its latest state.还要记住,JavaScript 是单线程的,所以你的 for 循环不会为针设置动画,它只会坐在那里并绘制所有更新,完成后浏览器实际上会以其最新状态刷新可见画布。

If you want to animate it you will have to create a rendering loop.如果要为其设置动画,则必须创建一个渲染循环。 Dev.Opera has an article about framerate control , that should get you started, then you just need to animate your needle on each frame. Dev.Opera 有一篇关于帧率控制的文章,它应该让你开始,然后你只需要在每一帧上为你的针设置动画。

Simple answer: Redraw the canvas with right angle.简单的答案:以直角重新绘制画布。

Use context.clearRect() or set the canvas width to the same value (changing it to whatever value will clear the canvas);使用 context.clearRect() 或将画布宽度设置为相同的值(将其更改为将清除画布的任何值);

It's fast and there is no way to move just the needle.它很快,而且没有办法只移动针。 All canvas stuff are build that way.所有画布的东西都是这样构建的。

Draw.画。 Change?改变? Redraw.重绘。

As you are using a static background image and only the needle is dynamic you could simply use multiple canvases.当您使用静态背景图像并且只有针是动态的时,您可以简单地使用多个画布。 Position them on top of each other using css.使用 css 将它们放在彼此的顶部。 Add the tachometer image to the canvas in the back.将转速计图像添加到后面的画布上。 Apply the needle script to the top canvas.将针脚脚本应用到顶部画布。 This way you don't have to redraw the image everytime you update the needle.这样您就不必每次更新针头时都重新绘制图像。 And as the other answers explain redraw means clear context and draw needle again.正如其他答案所解释的那样,重绘意味着清除上下文并再次绘制针。

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

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