How to fill the area, why is it a rectangle? Shouldn't the coordinate grid be in pixels?

https://jsfiddle.net/fLn0nt4f/

<canvas id="canvas"></canvas> <style type="text/css"> #canvas { height: 500px; width: 500px; } </style> <script type='text/javascript'> var w = 50; var h = 50; var pointR = 10; var x = (w / 2); var y = (h / 2); var c = document.getElementById('canvas'); var ctx = c.getContext('2d'); // Create gradient var grd = ctx.createRadialGradient(x, y, 9, x, y, 10); grd.addColorStop(0, '#fff'); grd.addColorStop(1, '#000'); // Fill with gradient ctx.fillStyle = grd; ctx.fillRect(0, 0, w, h); </script> 

    1 answer 1

    because the canvas dimensions should be specified in the canvas tag

     var w = 50; var h = 50; var pointR = 10; var x = (w / 2); var y = (h / 2); var c = document.getElementById('canvas'); var ctx = c.getContext('2d'); // Create gradient var grd = ctx.createRadialGradient(x, y, 9, x, y, 10); grd.addColorStop(0, '#fff'); grd.addColorStop(1, '#000'); // Fill with gradient ctx.fillStyle = grd; ctx.fillRect(0, 0, w, h); 
     #canvas { height: 500px; width: 500px; } 
     <canvas width="500" height="500" id="canvas"></canvas> 

    • And if I want to fit it to the width of the page? - Fangog
    • @Fangog customize, what's the problem then? - ampawd