4_canvas.html 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. #canvas{
  9. background-color: black;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <!-- canvas 画布标签 -->
  15. <canvas id="canvas" width="200" height="200"></canvas>
  16. <script>
  17. // 获取元素
  18. var canvas = document.querySelector("#canvas");
  19. // 获取上下文
  20. var ctx = canvas.getContext("2d");
  21. // 绘制一条线
  22. // 设置线的颜色
  23. ctx.strokeStyle = "white";
  24. // 设置线的宽度
  25. ctx.lineWidth = 5;
  26. // 开始绘制 beginPath
  27. ctx.beginPath();
  28. // moveTo 移动到指定位置(落笔点)
  29. ctx.moveTo(10, 10);
  30. // lineTo 绘制一条线到指定位置
  31. ctx.lineTo(100, 100);
  32. ctx.lineTo(80, 150);
  33. ctx.lineTo(100, 180);
  34. // stroke 绘制线
  35. ctx.stroke();
  36. </script>
  37. </body>
  38. </html>