是否有技术原因导致画布的drawImage()不进行子像素渲染/抗锯齿?

发布于 2024-11-15 08:06:58 字数 485 浏览 5 评论 0原文

Ttl;博士: 我需要在canvas中非常缓慢地移动图像,而没有明显的逐像素移动。我需要某种抗锯齿功能。


最近,我的任务是在网页中水平制作一些“云”绘图的动画。

我很轻松地将图像放入 DOM 中,并使用 CSS3 转换,并为尚不支持 CSS 转换的浏览器回退到 jQuery 动画。

一切看起来都不错。我有一些云在页面上平稳地移动。

然后我不断收到设计师的要求,要求减慢运动速度……减慢速度。

由于浏览器不会对 DOM 对象进行子像素渲染,因此动画似乎以 6 FPS 运行。

因此,我将图像放入画布中进行一些快速测试,发现它也不会自动进行子像素渲染。

我的快速画布动画演示 (它不能准确地计算运动时间,请处理它。:- p)

Ttl;dr:
I need to move an image VERY slowly in canvas without obvious pixel by pixel movement. I need some sort of anti-aliasing.


Recently I was tasked with animating some "cloud" drawings horizontally in a webpage.

Easily enough I just threw the image into the DOM and used CSS3 transforms with a fallback to jQuery animation for browsers that don't support CSS transforms yet.

Everything looked pretty good. I had some clouds moving smoothly moving across the page.

Then I kept getting requests from the designer to slow the movement down...way down.

Because browsers don't do sub-pixel rendering for DOM objects the animation appears to run at 6 FPS.

So, I slap the image into canvas for some quick tests and find out that it doesn't do sub-pixel rendering automatically, either.

My quick canvas animation demo (it doesn't accurately time the movements, deal with it. :-p )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

陌路黄昏 2024-11-22 08:06:58

对于较旧的浏览器,您可以为精灵设置动画。创建图像的 4 个版本,每个版本比前一个版本向左移动 0.25 像素。将它们粘贴到精灵中,然后为背景位置设置动画。

function moveClouds(n)
{
    var v = (n % 4) * 417;
    var h = Math.ceil(n / 4);
    clouds.style.backgroundPosition = h + " " + v;
}

For older browsers, you could animate a sprite. Create maybe 4 versions of your image, each shifted 0.25px to the left from the previous one. Paste those together in a sprite and then animate the background-position.

function moveClouds(n)
{
    var v = (n % 4) * 417;
    var h = Math.ceil(n / 4);
    clouds.style.backgroundPosition = h + " " + v;
}
冷月断魂刀 2024-11-22 08:06:58

这是一个已知的 Chrome 问题,如 http://code.google 中所述。 com/p/chromium/issues/detail?id=7508

仍然没有解决方案或解决方法。

This is a known Chrome issue as documented at http://code.google.com/p/chromium/issues/detail?id=7508

still no resolution or workaround for it..

骄傲 2024-11-22 08:06:58

你可以在这里做一个 hack,但它非常丑陋。

您将画布元素的分辨率设置为高于实际显示尺寸,然后浏览器将消除您的翻译锯齿。 Tada,chrome 中的亚像素动画!

There is a hack you can do here, but it's pretty ugly.

You set the resolution of the canvas element to be higher than the actual display size, and then your translations are antialiased by the browser. Tada, sub-pixel animation in chrome!

旧话新听 2024-11-22 08:06:58

我已经尝试了一些你的代码示例,在我看来,较慢的滚动看起来非常流畅:

ctx.drawImage(img, left -= 0.0025, 0, 633, 417);    

我使用的浏览器是 mac 上的 chrome。

I've tried around a bit with your code example and it seems to me that slower scrolling appears very smooth:

ctx.drawImage(img, left -= 0.0025, 0, 633, 417);    

The browser I use is chrome on a mac.

樱花落人离去 2024-11-22 08:06:58

据我了解...没有进行太多的抗锯齿浏览器测试。当 drawImage(img1, x,y,width,height) x 或 y 不是整数时,它就会启动。当画布标签 width="600" height="600" 大于 style="width:600; height:600" 时启动,

将样式设置为画布大小的 1/2 可以强制消除锯齿。上次我研究过这个话题。我不认为浏览器会 100% 执行该标准。

注意视频的绿色屏蔽抗锯齿对于消除伪影非常重要。

更新:在你的例子中,我没有使用 chrome 来获得抗锯齿效果,而我使用的是 firefox。
适用于 Win XP 的两种浏览器的最新版本。在您的示例中,Firefox fps 非常慢。

另一个更新
将画布样式大小设置为画布大小的 1/3 [3 个画布像素对应 1 个屏幕像素] 似乎确实可以使子像素渲染正常工作......但以这种方式完成它对性能的影响可能会很大。

Sub-pixel canvas test.<br/>
<canvas id="canvas" width="317" height="351" 
style="border:solid 1px  #000; width:100px; height:100px"></canvas>

抱歉,这不是您真正想要的答案。

As I understand it ... not done much browser testing of anti-aliasing. It kicks in when drawImage(img1, x,y,width,height) x or y are not integers. And kicks in when the canvas tags width="600" height="600" are larger than the style="width:600; height:600"

Setting the style to 1/2 the canvas size is suppose to force anti-aliasing. Last time I researched the subject. I would not assume browsers will implement the standard to 100%.

Note for green screening of videos anti-aliasing is important to remove artifacts.

Update: On your example I am not getting the anti-aliasing using chrome I am with firefox.
latest releases of both browsers for Win XP. Firefox fps is painfully slow on your example.

Another Update
Setting canvas style size to 1/3 of the canvas size [3 canvas pixels for 1 screen pixel] does appear to get the sub pixel rendering to work ... but the performance hit may be to big to accomplish it this way.

Sub-pixel canvas test.<br/>
<canvas id="canvas" width="317" height="351" 
style="border:solid 1px  #000; width:100px; height:100px"></canvas>

Sorry not the answer you actually wanted.

若言繁花未落 2024-11-22 08:06:58

我测试的所有浏览器都支持画布中的抗锯齿功能,只需将图像放在非整数坐标上即可看到它的发生。但没有子像素渲染(澄清一下,即使用 LCD 的红、绿、蓝子像素排序来增强水平分辨率,这对文本来说非常有用。)

根据 html5 预标准,由浏览器决定是否要这样做是否抗锯齿。

All browsers I test support Anti-Aliasing in the canvas, just put the image on an non integer coords to see it happening. But none subpixel rendering (for clarification, that is using LCDs ordering of red, green, blue subpixels to enhance horizontal resolution, great for text.)

According to the html5 pre-standard, its up to the browser to decide if it wants to do anti-aliasing or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文