It's now 2018, and we finally have cheap ways to do something around it...
Indeed, since the 2d context API now has a filter
property, and that this filter property can accept SVGFilters, we can build an SVGFilter that will keep only fully opaque pixels from our drawings, and thus eliminate the default anti-aliasing.
So it won't deactivate antialiasing per se, but provides a cheap way both in term of implementation and of performances to remove all semi-transparent pixels while drawing.
I am not really a specialist of SVGFilters, so there might be a better way of doing it, but for the example, I'll use a <feComponentTransfer>
node to grab only fully opaque pixels.
var ctx = canvas.getContext('2d');_x000D_
ctx.fillStyle = '#ABEDBE';_x000D_
ctx.fillRect(0,0,canvas.width,canvas.height);_x000D_
ctx.fillStyle = 'black';_x000D_
ctx.font = '14px sans-serif';_x000D_
ctx.textAlign = 'center';_x000D_
_x000D_
// first without filter_x000D_
ctx.fillText('no filter', 60, 20);_x000D_
drawArc();_x000D_
drawTriangle();_x000D_
// then with filter_x000D_
ctx.setTransform(1, 0, 0, 1, 120, 0);_x000D_
ctx.filter = 'url(#remove-alpha)';_x000D_
// and do the same ops_x000D_
ctx.fillText('no alpha', 60, 20);_x000D_
drawArc();_x000D_
drawTriangle();_x000D_
_x000D_
// to remove the filter_x000D_
ctx.filter = 'none';_x000D_
_x000D_
_x000D_
function drawArc() {_x000D_
ctx.beginPath();_x000D_
ctx.arc(60, 80, 50, 0, Math.PI * 2);_x000D_
ctx.stroke();_x000D_
}_x000D_
_x000D_
function drawTriangle() {_x000D_
ctx.beginPath();_x000D_
ctx.moveTo(60, 150);_x000D_
ctx.lineTo(110, 230);_x000D_
ctx.lineTo(10, 230);_x000D_
ctx.closePath();_x000D_
ctx.stroke();_x000D_
}_x000D_
// unrelated_x000D_
// simply to show a zoomed-in version_x000D_
var zCtx = zoomed.getContext('2d');_x000D_
zCtx.imageSmoothingEnabled = false;_x000D_
canvas.onmousemove = function drawToZoommed(e) {_x000D_
var x = e.pageX - this.offsetLeft,_x000D_
y = e.pageY - this.offsetTop,_x000D_
w = this.width,_x000D_
h = this.height;_x000D_
_x000D_
zCtx.clearRect(0,0,w,h);_x000D_
zCtx.drawImage(this, x-w/6,y-h/6,w, h, 0,0,w*3, h*3);_x000D_
}
_x000D_
<svg width="0" height="0" style="position:absolute;z-index:-1;">_x000D_
<defs>_x000D_
<filter id="remove-alpha" x="0" y="0" width="100%" height="100%">_x000D_
<feComponentTransfer>_x000D_
<feFuncA type="discrete" tableValues="0 1"></feFuncA>_x000D_
</feComponentTransfer>_x000D_
</filter>_x000D_
</defs>_x000D_
</svg>_x000D_
_x000D_
<canvas id="canvas" width="250" height="250" ></canvas>_x000D_
<canvas id="zoomed" width="250" height="250" ></canvas>
_x000D_
And for the ones that don't like to append an <svg>
element in their DOM, you can also save it as an external svg file and set the filter
property to path/to/svg_file.svg#remove-alpha
.