gpt4 book ai didi

html - 使用浏览器缩放在 HTML5 Canvas 上绘制清晰的线条

转载 作者:行者123 更新时间:2023-11-28 00:07:49 25 4
gpt4 key购买 nike

将浏览器缩放设置为 150% 并在 Canvas 上绘制线条时,线条模糊。当我将 Canvas 的宽度和高度加倍并使用 css 将其缩小到原始宽度和高度时,线条清晰。

顶部三角形模糊,底部三角形清晰: http://jsbin.com/acimiq/1

是浏览器错误、操作系统错误还是我遗漏了什么?

相关

我已经在 Windows 7 上的 Chrome 27、FF 22 和 IE 10 上进行了测试。

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>canvas test</title>
<style id="jsbin-css">
#canvas1 {
image-rendering: optimizeSpeed; // Older versions of FF
image-rendering: -moz-crisp-edges; // FF 6.0+
image-rendering: -webkit-optimize-contrast; // Webkit
// (Safari now, Chrome soon)
image-rendering: -o-crisp-edges; // OS X & Windows Opera (12.02+)
image-rendering: optimize-contrast; // Possible future browsers.
-ms-interpolation-mode: nearest-neighbor; // IE
}

#canvas2 {
width: 300px;
height: 150px;
}
</style>
</head>
<body>
<canvas id="canvas1" width="300" height="150"></canvas>
<br/>
<canvas id="canvas2" width="600" height="300"></canvas>
<script>
var canvas1 = document.getElementById('canvas1');
var ctx1 = canvas1.getContext('2d');
ctx1.imageSmoothingEnabled = false;
ctx1.webkitImageSmoothingEnabled = false;
ctx1.mozImageSmoothingEnabled = false;
ctx1.beginPath();
ctx1.moveTo(125,125);
ctx1.lineTo(125,45);
ctx1.lineTo(45,125);
ctx1.closePath();
ctx1.stroke();

var canvas2 = document.getElementById('canvas2');
var ctx2 = canvas2.getContext('2d');
ctx2.scale(2, 2);
ctx2.beginPath();
ctx2.moveTo(125,125);
ctx2.lineTo(125,45);
ctx2.lineTo(45,125);
ctx2.closePath();
ctx2.stroke();
</script>
</body>
</html>

最佳答案

如果您想避免模糊的线条或浏览器缩放问题,请使用 SVG(可缩放矢量图形)。您现在可以在 html5 中编写 html 文件。

查看 wikipedia's entryw3school's entry在 SVG 上了解更多信息。

查看 this site何时使用 Canvas 与 SVG:

关于html - 使用浏览器缩放在 HTML5 Canvas 上绘制清晰的线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17553557/

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