前端知识框架 前端知识框架
首页
基础
框架
插件
Node
地图
更多
前端须知
  • 分类
  • 标签
  • 归档

BestIdea

首页
基础
框架
插件
Node
地图
更多
前端须知
  • 分类
  • 标签
  • 归档
  • 国产机环境浏览器兼容问题收集(持续收集)
  • canvas电子画板,适用于H5版本,组件产出为blob类型的文件流
  • Css3 filter(滤镜) 属性
  • css选择器
  • http缓存
  • less语法
  • New date()浏览器时间兼容性
  • rem自动适应性布局方案
  • 浏览器缓存机制
  • 屏幕适配
  • 前端基础方法汇总
  • 图片转码下载方式(解决浏览器下载图片时直接打开图片问题)
  • CSS命名规范
  • 大屏定位解决方案
  • TypeScript入门
  • 解决有些浏览器rem不以html的font-size为基准问题
  • 解决Vue项目页面缩放问题
  • H5扫一扫功能
  • crypto前端加密
  • 基础
Btzh
2022-03-26

canvas电子画板,适用于H5版本,组件产出为blob类型的文件流

# canvas电子画板,适用于H5版本,组件产出为blob类型的文件流
# 组件代码
<template>
  <div class="signatureBox" :style="{ height: 90 + '%'}">
    <div class="canvasBox" ref="canvasHW">
      <div class="closeBox">
        <span @click="surewrite">确认</span>
        <span @click="overwrite">重写</span>
        <span @click="close">关闭</span>
      </div>
      <canvas
        @touchstart="touchStart"
        @touchmove="touchMove"
        @touchend="touchEnd"
        ref="canvasF"
        @mousedown="mouseDown"
        @mousemove="mouseMove"
        @mouseup="mouseUp"
      ></canvas>
    </div>
  </div>
</template>

<script>
    import Vue from 'vue'
    import Oss from '../oss'
    export default {
      name: 'DrawBoard',
      data () {
        return {
          points: [],
          canvasTxt: null,
          startX: 0,
          startY: 0,
          moveY: 0,
          moveX: 0,
          endY: 0,
          endX: 0,
          w: null,
          h: null,
          isDown: false,
          color: '#000',
          linewidth: 3,
          isDraw: false // 签名标记
        }
      },
      mounted () {
        let canvas = this.$refs.canvasF
        canvas.height = this.$refs.canvasHW.offsetHeight - 20
        canvas.width = this.$refs.canvasHW.offsetWidth - 10
        this.canvasTxt = canvas.getContext('2d')
        this.canvasTxt.strokeStyle = this.color
        this.canvasTxt.lineWidth = this.linewidth
      },
      methods: {
        // 针对pc鼠标支持
        mouseDown (ev) {
          ev = ev || event
          ev.preventDefault()
          let obj = {
            x: ev.offsetX,
            y: ev.offsetY
          }
          this.startX = obj.x
          this.startY = obj.y
          this.canvasTxt.beginPath()
          this.points.push(obj)
          this.isDown = true
        },
        // 移动设备事件
        touchStart (ev) {
          ev = ev || event
          ev.preventDefault()
          if (ev.touches.length === 1) {
            this.isDraw = true // 签名标记
            let obj = {
              x: ev.targetTouches[0].clientX,
              y: ev.targetTouches[0].clientY - (document.body.offsetHeight * 0.1 + this.$refs.canvasHW.offsetHeight * 0.1)
            } // y的计算值中:document.body.offsetHeight*0.5代表的是除了整个画板signatureBox剩余的高,this.$refs.canvasHW.offsetHeight*0.1是画板中标题的高
            this.startX = obj.x
            this.startY = obj.y
            this.canvasTxt.beginPath()
            this.points.push(obj)
          }
        },
        // 电脑设备事件
        mouseMove (ev) {
          ev = ev || event
          ev.preventDefault()
          if (this.isDown) {
            let obj = {
              x: ev.offsetX,
              y: ev.offsetY
            }
            this.moveY = obj.y
            this.moveX = obj.x
            this.canvasTxt.moveTo(this.startX, this.startY)
            this.canvasTxt.lineTo(obj.x, obj.y)
            this.canvasTxt.stroke()
            this.startY = obj.y
            this.startX = obj.x
            this.points.push(obj)
          }
        },
        // 移动设备事件
        touchMove (ev) {
          ev = ev || event
          ev.preventDefault()
          if (ev.touches.length === 1) {
            let obj = {
              x: ev.targetTouches[0].clientX,
              y: ev.targetTouches[0].clientY - (document.body.offsetHeight * 0.1 + this.$refs.canvasHW.offsetHeight * 0.1)
            }
            this.moveY = obj.y
            this.moveX = obj.x
            this.canvasTxt.moveTo(this.startX, this.startY)
            this.canvasTxt.lineTo(obj.x, obj.y)
            this.canvasTxt.stroke()
            this.startY = obj.y
            this.startX = obj.x
            this.points.push(obj)
          }
        },
        // 电脑设备事件
        mouseUp (ev) {
          ev = ev || event
          ev.preventDefault()
          let obj = {
            x: ev.offsetX,
            y: ev.offsetY
          }
          this.canvasTxt.closePath()
          this.points.push(obj)
          this.points.push({ x: -1, y: -1 })
          this.isDown = false
        },
            // 移动设备事件
        touchEnd (ev) {
          ev = ev || event
          ev.preventDefault()
          if (ev.touches.length === 1) {
            let obj = {
              x: ev.targetTouches[0].clientX,
              y: ev.targetTouches[0].clientY - (document.body.offsetHeight * 0.5 + this.$refs.canvasHW.offsetHeight * 0.1)
            }
                    // this.canvasTxt.beginPath();
                    // this.canvasTxt.moveTo(this.startX, this.startY);
                    // this.canvasTxt.lineTo(obj.x, obj.y);
                    // this.canvasTxt.stroke();
            this.canvasTxt.closePath()
            this.points.push(obj)
            this.points.push({ x: -1, y: -1 })
          }
        },
       // 重写
        overwrite () {
          this.canvasTxt.clearRect(0, 0, this.$refs.canvasF.width, this.$refs.canvasF.height)
          this.points = []
          this.isDraw = false // 签名标记
        },
       // 关闭
        close () {
          this.canvasTxt.clearRect(0, 0, this.$refs.canvasF.width, this.$refs.canvasF.height)
          this.points = []
          this.$emit('update:close', false)
        },
        foo (blob) {
          // this.src = URL.createObjectURL(blob)
          console.log(blob)
          Vue.upload(null, {path: 'blobtest'}, (src, thumbnail) => {
            console.log(src, thumbnail)
            this.src = Oss.getOssFileUrl(src)
          }, null, blob)
        },
        // 确认签名
        surewrite () {
          this.$refs.canvasF.toBlob(this.foo, 'image/png', 0.5)
          if (this.isDraw) {
            alert('签名成功!')
            // this.$emit("surewrite",false);
          } else {
            alert('请签名后再确认!')
          }
        }
      }
    }
</script>

<style type="less" scoped>
  .signatureBox {
    position: fixed;
    bottom: 0px;
    width: 100%;
    box-sizing: border-box;
    overflow: hidden;
    z-index: 100;
    background: #fff;
    border-top-left-radius: 15px;
    border-top-right-radius: 15px;
    box-shadow: 0 0 2px 1px #b5b3b3;
    /*display: flex;*/
    /*flex-direction: column;*/
  }
  .canvasBox {
    padding: 0px 5px;
    height: 92%;
  }
  canvas {
    background: #f2f5f9;
    border-radius: 10px;
    box-shadow: 0px 0 4px 2px #d4cfcf inset;
  }
  .btnBox {
    height: 30px;
    padding: 5px;
    text-align: center;
    line-height: 30px;
  }
  .btnBox button {
    border: 1px solid dodgerblue;
    background: dodgerblue;
    color: #fff;
    border-radius: 4px;
    padding: 2px 30px;
    margin: 0 15px;
    font-size: 14px;
  }
  .closeBox {
    text-align: center;
    height: 10%;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
  .closeBox span {
    font-size: 15px;
    float: left;
  }
  .closeBox p {
    font-size: 22px;
    width: 30px;
    height: 30px;
    line-height: 30px;
    border-radius: 30px;
    border: none;
    background: gray;
    color: white;
    float: right;
  }
</style>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# 使用方式
<template>
		<div>
        <draw-board v-if="drawing" :close.sync="drawing"></draw-board>
    </div>
</template>
<script>
import DrawBoard from '@/components/DrawBoard'

export default {
  components: { DrawBoard },
  data () {
    return {
      drawing: false
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
上次更新: 2022/05/05, 17:47:41
国产机环境浏览器兼容问题收集(持续收集)
Css3 filter(滤镜) 属性

← 国产机环境浏览器兼容问题收集(持续收集) Css3 filter(滤镜) 属性→

最近更新
01
webpack打包替换类名命名空间
05-01
02
Vite常用配置
02-26
03
crypto前端加密
01-18
更多文章>
Theme by Vdoing | Copyright © 2022-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式