今天学习ht ML 5 的canvas,发现fillRect的坐标和大小一直不对,研究了 半 天,发现canvas的宽度和高度必须内联在canvas标签中才对。郁闷了半天。
错误的方式1:
复制代码
代码如下:
<!docty PE html>
<html lang="en">
<head>
< ;m eta charset="UTF-8">
<t IT le>Document</title>
<style>
# mycanvas{
width: 200px;
h ei ght: 200px;
background: yellow;
}
</style>
</head>
<body>
<canvas id='mycanvas' ></canvas>
<script>
VAR c = document.getElementById('mycanvas');
var ctx = c.getContext("2d");
ctx.fillStyle='#f36';
ctx.fillRect(100, 100, 100, 100);
</script>
</body>
</html>
错误的方式2:
复制代码
代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<canvas id='mycanvas' style='width:200px;height:200px;background:yellow'></canvas>
<script>
var c = document.getElementById('mycanvas');
var ctx = c.getContext("2d");
ctx.fillStyle='#f36';
ctx.fillRect(100, 100, 100, 100);
</script>
</body>
</html>
显示结果:
正确的方式:
复制代码
代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<canvas id='mycanvas' width='200px' height='200px' style='background:yellow'></canvas>
<script>
var c = document.getElementById('mycanvas');
var ctx = c.getContext("2d");
ctx.fillStyle='#f36';
ctx.fillRect(100, 100, 100, 100);
</script>
</body>
</html>
总结
以上是 为你收集整理的 html5 canvas fillRect坐标和大小的问题解决方法 全部内容,希望文章能够帮你解决 html5 canvas fillRect坐标和大小的问题解决方法 所遇到的问题。
如果觉得 网站内容还不错, 推荐好友。
查看更多关于html5 canvas fillRect坐标和大小的问题解决方法的详细内容...