Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Canvas and Anti-aliasing [duplicate]

How to turn on the anti-aliasing on an canvas.

The following code doesn't draw a smooth line:

var context = mainCanv.getContext("2d"); if (context) {    context.moveTo(0,0);    context.lineTo(100,75);     context.strokeStyle = "#df4b26";    context.lineWidth = 3;    context.stroke(); } 
like image 554
KRouane Avatar asked Nov 23 '10 21:11

KRouane


People also ask

Why is my Canvas blurry?

However, the Canvas still looks pixelated. This is because the Canvas is rendering to a bitmap of one size then scaling the bitmap to fit the CSS dimensions. To fix this, we modify the Canvas's bitmap dimensions to match the CSS dimensions using JavaScript.

How fast is HTML5 Canvas?

The Canvas tab loaded in one second and takes up 30MB. It also takes up 13% of CPU time all of the time, regardless of whether or not one is looking at it. Video on the HTML page, while I am not moving objects, is actually perfectly smooth.

Can you have multiple canvases HTML?

A webpage may contain multiple canvas elements. Each canvas may have an id using which you may target a specific canvas through JavaScript. Each canvas element has a 2D Context. This again has objects, properties, and methods.

Is Canvas a new tag in HTML5?

The <canvas> tag in HTML is used to draw graphics on a web page using JavaScript. It can be used to draw paths, boxes, texts, gradients, and adding images. By default, it does not contain borders and text. Note: The <canvas> tag is new in HTML5.


2 Answers

You may translate canvas by half-pixel distance.

ctx.translate(0.5, 0.5); 

Initially the canvas positioning point between the physical pixels.

like image 121
Bacher Avatar answered Sep 30 '22 15:09

Bacher


Anti-aliasing cannot be turned on or off, and is controlled by the browser.

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

like image 42
Gaurav Avatar answered Sep 30 '22 17:09

Gaurav