PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

如何在javascript中绘制方块

藏色散人
藏色散人 原创
2021-04-08 14:57:33 1985浏览

在javascript中绘制方块的方法:首先创建一个html示例文件;然后在body中添加canvas标签;接着在head中添加script标签,用于输入javascript代码;最后通过draw方法绘制方块即可。

本文操作环境:windows7系统、javascript1.8.5&&html5版、Dell G3电脑。

html中插入javascript画矩形

<!DOCTYPE <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>canvas元素示例</title>
<script>
function draw(id)
{
var canvas = document.getElementById(id);
if(canvas == null)
return false;
var context = canvas.getContext(&#39;2d&#39;);
context.fillStyle = "#eeefff";
context.fillRect(0,0,400,300);
context.fillStyle = "green";
context.strokeStyle = "red";
context.lineWidth = 1;
context.fillRect(50,50,100,100);
context.strokeRect(50,50,100,100);
}
</script>
</head>
<body οnlοad="draw(&#39;canvas&#39;);">
<h1>canvas中画矩形</h1>
<canvas id="canvas" width="400" height="300"/>
</body>
</html>

212b49891904563556222d95b591135.png

【推荐学习:javascript高级教程

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。