흰색 채워진 캔버스에 검은 선을 그리려합니다. 몇 가지 이유 때문에 그 라인의 내부 픽셀 (lineWidth = 3)만이 실제로 검은 색입니다. 선의 가장자리로 가면 갈수록 회색 계단의 계단이 보입니다. lineWith = 1로 그릴 경우 픽셀은 실제로 검정색이 아닙니다.HTML 5 캔버스 : 선 가장자리에서 색 그라디언트를 제거하십시오.
<html>
<head>
<script type="text/javascript">
function page_loaded()
{
var cv = document.getElementById("cv");
var ctx = cv.getContext("2d");
ctx.fillStyle = "#ffffff";
ctx.strokeStyle = "#000000";
ctx.lineWidth = 3;
ctx.lineCap = "round";
ctx.lineJoin = "bevel";
ctx.fillRect(0, 0, cv.width, cv.height);
ctx.moveTo(5, 5);
ctx.lineTo(10, 10);
ctx.stroke();
var imgdata = ctx.getImageData(0, 0, cv.width, cv.height);
for (var y = 0; y < cv.height; y ++)
{
for (var x = 0; x < cv.width; x ++)
{
var idx = 4 * (y * cv.width + x);
var c = imgdata.data[idx]<<16 | imgdata.data[idx+1]<<8 | imgdata.data[idx+2];
if (c != 0xffffff)
{
document.getElementById("debug").innerHTML += x+"x, "+y+"y: "+c.toString(16)+"<br/>";
}
}
}
}
</script>
</head>
<body onload="page_loaded()">
<p>
<canvas id="cv" width="15" height="15"></canvas>
</p>
<p>
Found colors:
<div id="debug"></div>
</body>
</html>
이되지 않는 이유는 라인이 검게 어떻게 내가이 문제를 해결할 수 있습니다 : http://jsfiddle.net/xr5az/
그냥 완성도를 위해, 여기에 다시 코드는 다음과 같습니다
나는 무슨의 작은 데모를 썼다 ?고맙습니다.
그래서이 코드는 행의 anti-alisaing과 관련이 있다고 주장 할 수 있습니다. 'lineWidth'를 단지 1로 변경하면 발견 된 유일한 색은 검은 색이므로 도움이 될 수도 있습니다 : http://stackoverflow.com/questions/195262/can-i-turn-off-antialiasing-on-an-html -canvas-element –
@AhmedNuaman - 오히려 오래된 문서입니다. 최근 답변을 찾았습니다. http://stackoverflow.com/questions/4261090/html5-canvas-and-anti-aliasing. JS 디버거의 "Watches"목록 (getContext ('2d')에 의해 얻음)에서 캔버스의 컨텍스트를 보면, imageSmoothingEnabled 플래그와 webkitImageSmoothingEnabled 플래그를 볼 수 있습니다. 이전에 거짓을 설정하면 후자에도 적용됩니다. 그러나 어느 것도 공급 된 바이올린에 영향을 미치지 않습니다. – enhzflep
@AhmedNuaman : lineWith를 1로 설정하면 4x, 4y와 같은 색상을 얻습니다. fbfbfb; 5x, 4y : d0d0d0; 4x, 5y : c4c4c4; 5x, 5y : 151515; 6x, 5y : a7a7a7 예를 들면 Firefox에서. – Robert