简体   繁体   English

替代HTML5画布清除方法

[英]Alternative HTML5 Canvas Clear Method

The answer to this stackoverflow question effectively says to retrace the steps taken to draw a rectangle: 这个stackoverflow问题的答案有效地说明了追溯绘制矩形的步骤:

fillRect

by doing: 通过做:

clearRect

First of all when I read this answer, I thought to just draw a white box the entire width and height of the canvas. 首先,当我阅读此答案时,我想只是在画布的整个宽度和高度上绘制一个白框。

After thinking a bit more about how the Canvas element can save, and restore and the fact it implements a clearRect , does this mean that simply drawing another large rectangle could eat resources, if doing something like redrawing an entire bar graph every 100ms. clearRect考虑了Canvas元素如何保存和恢复以及实现了clearRect的事实clearRect ,这是否意味着如果进行每100毫秒重绘整个条形图之类的操作,仅绘制另一个大矩形就可能会clearRect资源。

Originally drawing a bar graph every 100ms drew bars on top of each other, thus not being able to see the new bars because they're being layered. 最初每100毫秒绘制一个条形图,使条形图彼此重叠,因此无法看到新条形图,因为它们是分层的。

But now, drawing a white rectangle as well, means that many rectangles are getting drawn, layered and it seems that the Canvas element tracks these? 但是现在,还要绘制一个白色矩形,就意味着要绘制,分层许多矩形,并且Canvas元素似乎可以跟踪这些矩形吗?

Does this mean that it is possible to effectively overload the element, or overload the browser? 这是否意味着可以有效地重载元素,或重载浏览器?

the canvas does not track the drawing operations, but it tracks states. 画布不跟踪绘制操作,但跟踪状态。 And the color values of individual pixels. 以及各个像素的颜色值。 If you draw a red rectangle you actually set the state to "draw red" and then set a rectangular shaped area of pixels to the currently drawn color (there are also different other drawing operations than just draw red on-top but I don't have experience with the so I can't tell you much other than that they exist) 如果您绘制红色矩形,则实际上将状态设置为“绘制红色”,然后将像素的矩形区域设置为当前绘制的颜色(除了在顶部绘制红色以外,还有其他绘制操作,但我没有有经验,所以除了他们存在之外,我无法告诉您其他任何信息)

For performance reasons you want to (among many other things) minimize 出于性能方面的考虑,您希望(除其他事项外)最小化

  • the amount of pixels you change 您更改的像素数量
  • the amount of states you change 您更改状态的数量

the difference between clearRect and width = width is that clearRect clears out the pixel data in the given area, while width = width clears out all pixel data and all states, like transformations, and styles. clearRectwidth = width之间的区别在于, clearRect清除给定区域中的像素数据,而width = width清除所有像素数据和所有状态,例如变换和样式。 I think you already see the difference, there are a lot more things to consider (like Garbage Collection blocking your drawloop being one) but that would get a bit offtopic. 我认为您已经看到了区别,还有很多需要考虑的事情(例如Garbage Collection阻止您的drawloop是其中之一),但这会有些偏离主题。

I'm not sure what you mean with overloading the browser. 我不确定浏览器过载的含义。 If you mean blocking and making the UI unresponsive then yes it's a thing that can and will happen since javascript is single-threaded and there are many ways you can accomplish it, but most likely not with such a reasonable operation :) 如果您的意思是阻止并使UI无响应,那么是的,因为JavaScript是单线程的,并且可以通过多种方法来实现它,所以这将是可能会发生的事情,但是很可能没有这样合理的操作:)

The worst thing you can do with drawing is make it super choppy on slow CPUs. 绘制最糟糕的事情是在慢速的CPU上使它变得断断续续。 Using requestAnimationFrame() instead of setTimeout() (which I assume you currently use because you mentioned 100ms) for your drawloop(s) is almost always a good and safe way to make sure your drawing will not block the UI. 使用requestAnimationFrame()而不是setTimeout() (我假设您当前使用它是因为提到了100ms)作为绘制循环,这几乎始终是确保绘图不会阻塞UI的好方法。

the Canvas element tracks these [rectangles] “画布”元素会跟踪这些[矩形]

It doesn't track painting operations, as far as I know. 据我所知,它不跟踪绘画操作。 (It's up to the implementation, but I don't know of any implementation that does.) You might be thinking of how it can save and restore things like transforms and paint colors. (这取决于实现,但是我不知道有任何实现。)您可能正在考虑它如何保存和恢复诸如变换和绘制颜色之类的东西。

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

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