【发布时间】:2018-08-12 11:42:27
【问题描述】:
我刚开始使用 HTML 中的 <canvas>,我遇到了一些问题。
- 我正在尝试使用 CSS 设置画布的宽度和高度,但没有获得实际值,即即使在 CSS 中更改高度后,它仍将高度设为 150,宽度始终设为 300。
- fillRect() 给了我模糊的图像,但我想要清晰的图像。为此,我查找了其他资源,其中说 fillRect() 和 strokeRect() 一半在 x、y 坐标内部和一半在外部。这是link。但我无法理解这个人究竟在做什么来解决这个问题。
这是我的代码
<!DOCTYPE html>
<html>
<head>
<title>Canvas Drawing</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
html, body {
margin: 0;
padding: 0;
}
h1 {
background-color: #2DE0E6;
color: black;
text-align: center;
font-style: oblique;
width: 100%;
height: 100px;
line-height: 100px;
letter-spacing: 2px;
text-transform: uppercase;
font-family: 'Courier New';
font-size: 40px;
}
#canvas-container {
width: 100%;
text-align: center;
margin: auto;
float: right;
}
#myCanvas {
border: 1px solid #c3c3c3;
width: 65%;
height: 450px;
display: inline;
}
</style>
</head>
<body>
<h1>Canvas Drawing</h1>
<div id="canvas-container">
<canvas id="myCanvas"></canvas>
<br /><br />
Draw a rectangle
</div>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.fillStyle = "#FF0000";
context.fillRect(5.5, 10.5, 90, 80);
</script>
</body>
</html>
此外,我们非常感谢您提供一些深入了解 <canvas> 的资源。
【问题讨论】:
标签: html css html5-canvas