canvas绘制圆形简明教程

画直线图

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>canvas demo</title>
</head>
<body>
  <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  </canvas>
  <script>
    window.onload = function() {
      var canvas = document.querySelector('canvas')

      canvas.height = 768
      canvas.width = 1024

      var context = canvas.getContext('2d')
      if (!context) {
        alert('当前浏览器不支持canvas,请更换浏览器后再试')
      }

      // 每次绘制都要用beginPath和closePath分开
      context.beginPath()
      // 把画笔落在(100,100)处
      context.moveTo(100, 100)
      context.lineTo(600,600)
      context.lineTo(700,600)
      context.lineTo(700,300)
      context.lineTo(0,600)
      context.closePath()
      context.lineWidth = 5
      context.strokeStyle = 'green'
      context.fillStyle = 'lightgreen'
      // 填充颜色
      context.fill()
      // 绘制边框
      context.stroke()
      // 每次绘制都要用beginPath和closePath分开
      context.beginPath()
      context.moveTo(650,650)
      context.lineTo(650,750)
      context.lineTo(750,750)
      context.lineTo(750,650)
      context.closePath()
      context.strokeStyle = 'lightblue'
      context.stroke()
    }
  </script>
</body>
</html>

效果:
在这里插入图片描述
比较重要的API:

// 初始化
// 获取当前canvas
var canvas = document.querySelector('canvas')
// 设置canvas大小
canvas.height = 768
canvas.width = 1024
// 获取context作为画布,这里绘制2d图形
var context = canvas.getContext('2d')
// 每次绘制都要用beginPath和closePath分开
context.beginPath()
// 把画笔落在(100,100)处
context.moveTo(100, 100)
// 画到下一个点的位置
context.lineTo(600,600)
// 结束绘画路径
context.closePath()
// 填充颜色
context.fill()
// 绘制边框
context.stroke()

画曲线图

在这里插入图片描述
在这里插入图片描述

    window.onload = function () {
      var canvas = document.querySelector('#canvas')
      canvas.height = 768
      canvas.width = 1024
      var context = canvas.getContext('2d')
      if(!context) {
        alert('当前浏览器不支持canvas')
      } else {
        context.lineWidth = 5
        context.strokeStyle = 'lightblue'
        context.arc(300,300,100,0,1.5*Math.PI, true)
        context.stroke()
      }
    }

效果:
在这里插入图片描述
context.arc最后一个参数是true,表示逆时针,默认是false,表示顺时针:

context.arc(300,300,100,0,1.5*Math.PI, false)

在这里插入图片描述

注意,context.closePath()会自动把图形连接成一个封闭图形:
    window.onload = function () {
      var canvas = document.querySelector('#canvas')
      canvas.height = 768
      canvas.width = 1024
      var context = canvas.getContext('2d')
      if(!context) {
        alert('当前浏览器不支持canvas')
      } else {
        context.lineWidth = 5
        context.strokeStyle = 'lightblue'
        context.beginPath()
        context.arc(300,300,100,0,1.5*Math.PI, false)
        context.closePath()
        context.stroke()
      }
    }

在这里插入图片描述
再看一个例子:

window.onload = function () {
      var canvas = document.querySelector('#canvas')
      canvas.height = 768
      canvas.width = 1024
      var context = canvas.getContext('2d')
      if (!context) {
        alert('当前浏览器不支持canvas')
      } else {
        context.lineWidth = 5
        context.strokeStyle = 'lightblue'

        for (var i = 0; i < 10; i++) {
          context.beginPath()
          context.arc(50 + i * 100, 200, 40, 0, 2 * Math.PI * (i + 1) / 10)
          context.stroke()
        }

		context.fillStyle = 'pink'
        for (var i = 0; i < 10; i++) {
          context.beginPath()
          context.arc(50 + i * 100, 400, 40, 0, 2 * Math.PI * (i + 1) / 10)
          context.stroke()
          context.fill()
        }
      }
    }

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值