gpt4 book ai didi

javascript - Canvas 外的线条

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:47 25 4
gpt4 key购买 nike

我将 Canvas 定义为 <canvas id="maincanvas" class="canvas"></canvas>与风格

.canvas {
width: 1000px;
height: 750px;
border: 1px dotted;
}

然后我尝试从左上角到右下角画一条线:

function GenerateSym() {
var c = document.getElementById("maincanvas");
var ctx = c.getContext("2d");
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(5, 5);
ctx.lineTo(995, 745);
ctx.stroke();

不幸的是,线条似乎开始时太低了几像素。虽然它将 Canvas 完全留在底部中心,而不是在右下角之前结束。此外,这条线似乎至少有 3 像素宽,抗锯齿效果很差。

我在 Firefox 58 上运行 Mint 18。谢谢!

最佳答案

您不能使用 CSS 样式来调整 Canvas 元素的大小,否则您将遇到此问题。 Canvas DOM 元素具有属性 width 和 height ,它们等同于属性 "width=.."和 "height=.."。正如这里所说:Canvas width and height in HTML5 .所以正确的做法是:

.canvas{ style: 1px dotted;}

<script>
var c = document.getElementById("maincanvas");
var ctx = c.getContext("2d");
c.width= 1000;
c.height= 750;
ctx.beginPath();
ctx.lineWidth = 1;
ctx.moveTo(5, 5);
ctx.lineTo(995, 745);
ctx.stroke();</script>

关于javascript - Canvas 外的线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49338907/

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