我在画布上写文字并画一条线。不知怎的,我结束了在我的画布不必要的边界:画布周围不需要的边框
首先,我写在右上角的文本,并呼吁context.save()
,然后我划清界线,并呼吁context.stroke()
。
代码:
$(document).ready(function() {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
context.beginPath();
context.rect(0, 0, canvas.width, canvas.height);
context.fillStyle = 'black';
context.fill();
paintName(context, canvas);
drawLine(context);
});
function paintName(context, canvas) {
context.textAlign = "left";
context.font = "16pt Arial";
context.fillStyle = 'red';
context.fillText('G5', context.canvas.width - 35, 18);
context.strokeStyle = 'red';
context.save();
}
function drawLine(context){
var gatingCoords = [[30, 120], [50, 300]];
var nextX, nextY, pointX, pointy;
context.lineWidth = 4;
for (var i = 0; i < gatingCoords.length; i++) {
pointX = gatingCoords[i][0];
pointY = gatingCoords[i][1];
if (i === gatingCoords.length - 1) {
nextX = gatingCoords[0][0];
nextY = gatingCoords[0][1];
} else {
nextX = gatingCoords[i + 1][0];
nextXY = gatingCoords[i + 1][1];
}
context.moveTo(pointX, pointY);
context.lineTo(nextX, nextY);
}
context.stroke();
}
和小提琴是here。这是怎么发生的?
可能的[删除HTML画布边框]的副本(http://stackoverflow.com/questions/14042938/remove-html-canvas-border) –
您需要重置默认浏览器样式,因为浏览器提供了一些默认的边距和填充。 [查看这个dup问题的详细信息](http://stackoverflow.com/a/14042964/3931488)。 –
但边界是在图像上....右击并下载,你会看到它的PNG – Mark