Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

canvas arc too pixelated

I try to draw an circles in canvas, I ever do it before but i found it very pixelated this time

var game;

function game (){

    this.canvas = document.getElementById('canvas');

    this.ctx = this.canvas.getContext('2d');



    this.initCanvas = function(){

    }

    this.draw1 = function(){
        this.ctx.beginPath();
        this.ctx.arc(50, 75, 10, 0, Math.PI*2, true); 
        this.ctx.closePath();
        this.ctx.fill();
    }

    this.draw2 = function(){
        this.ctx.beginPath();
        this.ctx.arc(100,75,10,0,Math.PI*2,true);
        this.ctx.closePath();
        this.ctx.stroke();
    }

    this.run = function(){
        this.initCanvas();
        this.draw1();
        this.draw2();
    }

    window.onresize = function() {
        this.canvas.width = window.innerWidth;
        this.canvas.height = window.innerHeight;
    };
}

game = new game();

http://jsfiddle.net/RG6dn/6/

I don't know if this is due to the browser ( I have the same thing on chrome and firefox ) or my computer

Thanks

like image 415
Ajouve Avatar asked Dec 14 '12 09:12

Ajouve


People also ask

How do I fill an arc in canvas?

How it works: First, select the canvas by using the querySelector() method. Next, get the 2D drawing context if the canvas API is supported. Then, set the stroke, fill, and line width by using the strokeStyle , fillStyle , and lineWidth property of the 2D drawing context.

How do I clear my canvas for redrawing?

To clear the Canvas, you can use the clearRect() method. This method performs pretty well than others for clearing the canvas (such as resetting the width/height, destroying the canvas element and then recreating it, etc..) const context = canvas. getContext('2d'); context.

How do I delete an arc in canvas?

You can use the clearRect() method to erase a portion of the canvas (including your arc), but when you're using clearRect() with arcs or anything else that you used beginPath() and closePath() for while drawing, you'll need to handle the paths while erasing, too.


1 Answers

You probably were setting the width of your canvas using CSS. Altering the width of a canvas element in CSS stretches the pixels within the canvas

Eg.

<canvas style='width:400px;height:400px'></canvas>

Instead, you need to use the width and height properties of the canvas element itself to define how many pixels the canvas contains.

Correct way:

<canvas width='400px' height='400px'><canvas>

like image 185
Adam Botley Avatar answered Oct 07 '22 11:10

Adam Botley