vue中使用canvas

1、页面代码

<canvas id="canvas" width="800" height="600" ref="canvas"></canvas>

 2、初始化绘制信息

img: new Image(), // 背景图片缓存
context: {}, // canvas对象
/**
* describe 初始化绘制信息
* @author JKD
* @data 2019/4/20 16:56
*/
initDraw () {
  // 初始化画布
  this.img.src = this.imageUrl
  const canvas = this.$refs.canvas
  this.context = canvas.getContext('2d')
  this.context.drawImage(this.img, 0, 0, 800, 600)
  this.context.strokeStyle = '#FF0000'
}

 3、在画布上画图

/**
* describe 在画布上画图
* @author JKD
* @data 2019/4/20 16:57
*/
showMaterialMark (start_x, start_y, end_x, end_y, mark_type) {
  if (mark_type === 'line') {
    this.context.beginPath()
    this.context.moveTo(start_x, start_y)
    this.context.lineTo(end_x, end_y)
    this.context.stroke()
    this.context.closePath()
  }
  if (mark_type === 'circular') {
    this.paramEllipse(this.context, (start_x + end_x) / 2, (start_y + end_y) / 2,
      (end_x - start_x) / 2, (end_y - start_y) / 2)
  }
  if (mark_type === 'rectangle') {
    this.context.beginPath()
    this.context.strokeRect(start_x, start_y, (end_x - start_x), (end_y - start_y))
    this.context.stroke()
    this.context.closePath()
  }
}

 

4、画圆的方法

/**
* describe 画圆方法
* @author JKD
* @data 2019/4/20 16:57
*/
paramEllipse (context, x, y, a, b) {
  var step = (a > b) ? 1 / a : 1 / b
  context.beginPath()
  context.moveTo(x + a, y)
  for (var i = 0; i < 2 * Math.PI; i += step) {
    context.lineTo(x + a * Math.cos(i), y + b * Math.sin(i))
  }
  context.closePath()
  context.stroke()
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值