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
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With