html5 canvas fillRect坐标和大小的问题解决方法_HTML5教程-查字典教程网
html5 canvas fillRect坐标和大小的问题解决方法
html5 canvas fillRect坐标和大小的问题解决方法
发布时间:2016-12-27 来源:查字典编辑
摘要:fillRect(100,100,100,100)前2个100是指坐标,后2个100是指宽和高。今天学习html5的canvas,发现fil...

fillRect(100,100,100,100) 前2个100是指坐标,后2个100是指宽和高。

今天学习html5 的canvas,发现fillRect的坐标和大小一直不对,研究了半天,发现canvas的宽度和高度必须内联在canvas标签中才对。郁闷了半天。

错误的方式1:

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Document</title>

<style>

#mycanvas{

width: 200px;

height: 200px;

background: yellow;

}

</style>

</head>

<body>

<canvas id='mycanvas' ></canvas>

<script>

var c = document.getElementById('mycanvas');

var ctx = c.getContext("2d");

ctx.fill;

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'></canvas>

<script>

var c = document.getElementById('mycanvas');

var ctx = c.getContext("2d");

ctx.fill;

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'></canvas>

<script>

var c = document.getElementById('mycanvas');

var ctx = c.getContext("2d");

ctx.fill;

ctx.fillRect(100, 100, 100, 100);

</script>

</body>

</html>

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新HTML5教程学习
    热门HTML5教程学习
    网页设计子分类