In the "wider" line, you refer to the smoothing results that are automatically executed by the browser.
Smoothing is used to display a visually less jagged line.
Shrinking from pixel by pixel, there is currently no way to turn off anti-aliasing drawn by the browser.
You can use the Bresenham line algorithm to draw your line by specifying individual pixels. Of course, setting individual pixels leads to less performance.
Here's a sample code and demo: http://jsfiddle.net/m1erickson/3j7hpng0/

<!doctype html> <html> <head> <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> <style> body{ background-color: ivory; } canvas{border:1px solid red;} </style> <script> $(function(){ var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); var imgData=ctx.getImageData(0,0,canvas.width,canvas.height); var data=imgData.data; bline(50,50,250,250); ctx.putImageData(imgData,0,0); function setPixel(x,y){ var n=(y*canvas.width+x)*4; data[n]=255; data[n+1]=0; data[n+2]=0; data[n+3]=255; } </script> </head> <body> <canvas id="canvas" width=300 height=300></canvas> </body> </html>
source share