Aquí hay una solución alternativa que requiere dibujar líneas píxel por píxel, pero evitará el anti aliasing.
// some helper functions
// finds the distance between points
function DBP(x1,y1,x2,y2) {
return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
// finds the angle of (x,y) on a plane from the origin
function getAngle(x,y) { return Math.atan(y/(x==0?0.01:x))+(x<0?Math.PI:0); }
// the function
function drawLineNoAliasing(ctx, sx, sy, tx, ty) {
var dist = DBP(sx,sy,tx,ty); // length of line
var ang = getAngle(tx-sx,ty-sy); // angle of line
for(var i=0;i<dist;i++) {
// for each point along the line
ctx.fillRect(Math.round(sx + Math.cos(ang)*i), // round for perfect pixels
Math.round(sy + Math.sin(ang)*i), // thus no aliasing
1,1); // fill in one pixel, 1x1
}
}
Básicamente, se encuentra la longitud de la línea, y paso a paso transversal esa línea, el redondeo de cada posición, y rellenando un píxel.
de llamadas con
var context = cv.getContext("2d");
drawLineNoAliasing(context, 20,30,20,50); // line from (20,30) to (20,50)
De acuerdo con [esta pregunta StackOverflow] (http://stackoverflow.com/questions/195262/can -i-turn-off-antialiasing-on-an-html-canvas-element) parecería que el lienzo * es * anti-aliased por defecto. ¿Qué OS/navegador/versión estás usando? – Phrogz
Cuando dices que no es fácil, ¿qué quieres decir? ¿Estás viendo bordes/píxeles irregulares o solo está borroso? –
Ocurre en Firefox Mobile en Android, hay un borde gris feo alrededor de la línea roja. –