简体   繁体   English

"HTML5 画布禁用原始(曲线,...)上的抗锯齿"

[英]HTML5 canvas disable antialias on primitive (curves, ...)

How can I disable the antialias effect in the following code ?如何在以下代码中禁用抗锯齿效果? I'm using Chrome 29.我正在使用 Chrome 29。

JSFiddle here<\/a><\/strong> JSFiddle在这里<\/a><\/strong>

var canvas = document.getElementById( 'test' );
var context = canvas.getContext( '2d' );

// These lines don't change anything
context.mozImageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;

context.beginPath();
context.arc(100, 100, 100, 0, Math.PI * 2, true );
context.closePath();

context.fillStyle = 'red';
context.fill( );

The imageSmoothingEnabled only affects images drawn to canvas using drawImage() (hence the name image SmoothingEnabled). 所述imageSmoothingEnabled只影响使用吸引到画布图像 drawImage()因此命名图像 SmoothingEnabled)。 There is sadly no way to disable this for lines and shapes. 遗憾的是,没有办法禁用线条和形状。

As with 6502's answer and previous answers on SO you will need to implement "low-level" methods to achieve this. 与6502的答案以及之前的答案一样,您需要实现“低级”方法来实现这一目标。

Here are some links to some common algorithms for various primitives: 以下是一些针对各种基元的常用算法的链接:

The way you need to implement this is to get the x and y coordinates from these formulas. 您需要实现此方法的方法是从这些公式中获取x和y坐标。 Then you need to either to use rect(x, y, 1, 1) or set a pixel directly by altering the image data buffer directly. 然后你需要使用rect(x, y, 1, 1)或直接通过改变图像数据缓冲区来设置像素。 The latter tend to be faster for these sort of things. 对于这类事情,后者往往更快。

In both cases you will have a relatively huge performance reduction as you need to iterate using JavaScript versus browser's internal compiled iteration. 在这两种情况下,由于需要使用JavaScript与浏览器的内部编译迭代进行迭代,因此性能会相对较高。

Unfortunately there is no portable way to disable antialiasing when drawing on a canvas. 不幸的是,在画布上绘制时没有可移植的方法来禁用抗锯齿。

The only solution I found is reimplementing graphic primitives writing directly on the image data. 我找到的唯一解决方案是重新实现直接在图像数据上书写的图形基元。

Is this what you're looking for?这是你要找的吗? <canvas style="image-rendering: pixelated"><\/code>

"

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

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