Can I turn off antialiasing on an HTML <canvas> element?

100,314

Solution 1

For images there's now context.imageSmoothingEnabled= false.

However, there's nothing that explicitly controls line drawing. You may need to draw your own lines (the hard way) using getImageData and putImageData.

Solution 2

Draw your 1-pixel lines on coordinates like ctx.lineTo(10.5, 10.5). Drawing a one-pixel line over the point (10, 10) means, that this 1 pixel at that position reaches from 9.5 to 10.5 which results in two lines that get drawn on the canvas.

A nice trick to not always need to add the 0.5 to the actual coordinate you want to draw over if you've got a lot of one-pixel lines, is to ctx.translate(0.5, 0.5) your whole canvas at the beginning.

Solution 3

It can be done in Mozilla Firefox. Add this to your code:

contextXYZ.mozImageSmoothingEnabled = false;

In Opera it's currently a feature request, but hopefully it will be added soon.

Solution 4

It must antialias vector graphics

Antialiasing is required for correct plotting of vector graphics that involves non-integer coordinates (0.4, 0.4), which all but very few clients do.

When given non-integer coordinates, the canvas has two options:

  • Antialias - paint the pixels around the coordinate based on how far the integer coordinate is from non-integer one (ie, the rounding error).
  • Round - apply some rounding function to the non-integer coordinate (so 1.4 will become 1, for example).

The later strategy will work for static graphics, although for small graphics (a circle with radius of 2) curves will show clear steps rather than a smooth curve.

The real problem is when the graphics is translated (moved) - the jumps between one pixel and another (1.6 => 2, 1.4 => 1), mean that the origin of the shape may jump with relation to the parent container (constantly shifting 1 pixel up/down and left/right).

Some tips

Tip #1: You can soften (or harden) antialiasing by scaling the canvas (say by x) then apply the reciprocal scale (1/x) to the geometries yourself (not using the canvas).

Compare (no scaling):

A few rectangles

with (canvas scale: 0.75; manual scale: 1.33):

Same rectangles with softer edges

and (canvas scale: 1.33; manual scale: 0.75):

Same rectangles with darker edges

Tip #2: If a jaggy look is really what you're after, try to draw each shape a few times (without erasing). With each draw, the antialiasing pixels get darker.

Compare. After drawing once:

A few paths

After drawing thrice:

Same paths but darker and no visible antialiasing.

Solution 5

I would draw everything using a custom line algorithm such as Bresenham's line algorithm. Check out this javascript implementation: http://members.chello.at/easyfilter/canvas.html

I think this will definitely solve your problems.

Share:
100,314
Blorgbeard
Author by

Blorgbeard

Updated on July 08, 2022

Comments

  • Blorgbeard
    Blorgbeard almost 2 years

    I'm playing around with the <canvas> element, drawing lines and such.

    I've noticed that my diagonal lines are antialiased. I'd prefer the jaggy look for what I'm doing - is there any way of turning this feature off?