gpt4 book ai didi

javascript - Canvas 周围不需要的边框

转载 作者:行者123 更新时间:2023-11-30 11:39:27 24 4
gpt4 key购买 nike

我在 Canvas 上写文字并画一条线。不知何故,我的 Canvas 周围出现了不需要的边框:

enter image description here

首先我在右上角写下文字并调用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();
}

fiddle 是here .这是怎么回事?

最佳答案

边框来自context.rect(0, 0, canvas.width, canvas.height)。如果您在 paintName(context, canvas) 之前添加另一个 context.beginPath(),那么边框就会消失。

关于javascript - Canvas 周围不需要的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43256270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com