| 123456789101112131415161718192021222324252627282930313233343536373839 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- #canvas{
- background-color: black;
- }
- </style>
- </head>
- <body>
- <!-- canvas 画布标签 -->
- <canvas id="canvas" width="200" height="200"></canvas>
- <script>
- // 获取元素
- var canvas = document.querySelector("#canvas");
- // 获取上下文
- var ctx = canvas.getContext("2d");
- // 绘制一条线
- // 设置线的颜色
- ctx.strokeStyle = "white";
- // 设置线的宽度
- ctx.lineWidth = 5;
- // 开始绘制 beginPath
- ctx.beginPath();
- // moveTo 移动到指定位置(落笔点)
- ctx.moveTo(10, 10);
- // lineTo 绘制一条线到指定位置
- ctx.lineTo(100, 100);
- ctx.lineTo(80, 150);
- ctx.lineTo(100, 180);
- // stroke 绘制线
- ctx.stroke();
- </script>
- </body>
- </html>
|