HTML5绘制几何图形

CanvasRenderingContext2D只提供了2个方法来绘制几何图形:
1.填充矩形区域:

fillRect(double x, double y, double width, double height) 
  1. 填充矩形边框
strokeRect(double x, double y, double width, double height)

其他几何形状(如圆、三角形等)将使用路径的方式绘制。
如示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<canvas id="mc" width="200" height="100" style="border: 1px solid black"></canvas>
<script type="text/javascript">
    let canvas = document.getElementById("mc")
    let ctx = canvas.getContext("2d")
    ctx.fillStyle = "#f00"
    ctx.fillRect(10, 20, 70, 60)

    ctx.strokeStyle = "#00f"
    ctx.lineWidth = 10
    ctx.strokeRect(90,20,70,60)

</script>
</body>
</html>
Demo.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。