Hier ist ein Workaround, bei dem Sie Linien Pixel für Pixel zeichnen müssen, aber Anti-Aliasing verhindern.
// 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
}
}
Grundsätzlich man die Länge der Zeile zu finden, und Schritt für Schritt Verfahrweg dieser Linie, um jede Position Abrunden, und in einem Pixel zu füllen.
Nennen Sie es mit
var context = cv.getContext("2d");
drawLineNoAliasing(context, 20,30,20,50); // line from (20,30) to (20,50)
Nach [diese Frage Stackoverflow] (http://stackoverflow.com/questions/195262/can -i-turn-off-antialiasing-on-einem-html-canvas-element) es scheint, dass die Leinwand * standardmäßig * anti-aliased ist. Welche OS/Browser/Version verwenden Sie? – Phrogz
Wenn Sie sagen, es ist nicht glatt, was meinst du? Sehen Sie gezackte Ränder/Pixel oder ist es nur verschwommen? –
Es passiert bei Firefox Mobile auf Android, es gibt einen hässlichen grauen Rand um die rote Linie. –