肖强的博客 肖强的博客
Home
Guide
Web Document
  • Plugins
  • Case
  • Animation
Components
  • Categories
  • Tags
  • Archives
Website Tools

xiaoQiang

因为爱所以爱
Home
Guide
Web Document
  • Plugins
  • Case
  • Animation
Components
  • Categories
  • Tags
  • Archives
Website Tools
  • 案例
    • Gluttonous Snake
    • Verification Code
    • Star sky
    • gobang
    • drawing
    • china map
  • views
xiaoQiang
2021-10-03

drawing

drawing

画图板

线条宽度 颜色
<template>
  <div class="drawing__canvas">
    <canvas ref="drawingCanvas"></canvas>
    <div class="tools">
      <button type="button" class="btn" @click="empty">清空画板</button>
      <span class="ml-30">线条宽度</span>
      <select ref="sel">
        <option v-for="v in 10" :value="v" :key="v">{{ v }}</option>
      </select>
      <span class="ml-30">颜色</span>
      <input type="color" ref="col" />
    </div>
  </div>
</template>

<script>
function offset(ele) {
  let left = 0,
    top = 0
  function fn(e) {
    if (e) {
      if (e.offsetParent === document.body) {
        left += e.offsetLeft
        top += e.offsetTop
      } else {
        left += e.offsetLeft
        top += e.offsetTop
        fn(ele.offsetParent)
      }
    }
  }
  fn(ele)
  return {
    left,
    top,
  }
}

export default {
  name: 'drawing',

  data() {
    return {
      canvas: null,
      ctx: null,
    }
  },

  async mounted() {
    this.canvas = this.$refs.drawingCanvas
    this.ctx = this.canvas.getContext('2d')
    this.canvas.onmousedown = this.canvasStart
    this.canvas.touchstart = this.canvasStart
    this.canvas.onmouseup = this.canvasEnd
    this.canvas.touchend = this.canvasEnd
  },

  methods: {
    canvasStart(e) {
      let event = e || window.event
      let x = event.pageX - offset(this.canvas).left
      let y = event.pageY - offset(this.canvas).top
      this.ctx.beginPath()
      this.ctx.moveTo(x, y)
      this.ctx.lineWidth = this.$refs.sel.value
      this.canvas.onmousemove = this.canvasMove
      this.canvas.touchmove = this.canvasMove
    },
    canvasMove(e) {
      let event = e || window.event
      let mx = event.pageX - offset(this.canvas).left
      let my = event.pageY - offset(this.canvas).top
      this.ctx.lineTo(mx, my)
      this.ctx.strokeStyle = this.$refs.col.value
      this.ctx.stroke()
    },
    canvasEnd() {
      this.canvas.onmousemove = null
      this.canvas.touchmove = null
    },
    empty() {
      this.ctx.clearRect(0, 0, window.innerWidth, window.innerHeight)
    },
  },
}
</script>

<style lang="less" scoped>
.drawing__canvas {
  border: 1px solid black;
  box-shadow: 4px 4px 4px grey;
}
canvas {
  width: 100%;
  height: 100%;
  border-bottom: 2px solid red;
}
.tools {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px;
}
.btn {
  background: #2c68ff;
  outline: none;
  border: none;
  border-radius: 4px;
  color: white;
  padding: 4px 8px;
}
.ml-30 {
  margin-left: 30px;
}
</style>
#case
上次更新: 2021-10-10 23:28:19
gobang
china map

← gobang china map →

最近更新
01
drag
12-22
02
入场/出场05
10-16
03
入场/出场04
10-16
更多文章>
icon组件不能用
时间左侧 © 2021-2021 时间右侧
冀公网安备 13108202000744号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
欢迎来到我的家
Back