|
| 1 | +<template> |
| 2 | + <div class="container"> |
| 3 | + <div id="canvasBox" :style="getHorizontalStyle" v-show="!showBox"> |
| 4 | + <div class="greet"> |
| 5 | + <span>{{msg}}</span> |
| 6 | + <input type="button" value="清屏" @touchstart="clear" @mousedown="clear"/> |
| 7 | + <input type="button" value="生成png图片" @touchstart="savePNG" @mousedown="savePNG"/> |
| 8 | + </div> |
| 9 | + <canvas></canvas> |
| 10 | + </div> |
| 11 | + <div class="image-box" v-show="showBox"> |
| 12 | + <header> |
| 13 | + 请长按图片并保存至本地后发送好友 |
| 14 | + <input type="button" value="返回" @click="showBox = false"/> |
| 15 | + </header> |
| 16 | + <img :src="signImage"> |
| 17 | + </div> |
| 18 | + </div> |
| 19 | +</template> |
| 20 | + |
| 21 | +<script> |
| 22 | +import Draw from '../utils/draw'; |
| 23 | +
|
| 24 | +export default { |
| 25 | + name: 'canvas', |
| 26 | + data() { |
| 27 | + return { |
| 28 | + msg: '请在下方空白处签名', |
| 29 | + degree: 90, |
| 30 | + signImage: null, |
| 31 | + showBox: false, |
| 32 | + }; |
| 33 | + }, |
| 34 | + components: { |
| 35 | + Draw, |
| 36 | + }, |
| 37 | + beforeCreate() { |
| 38 | + document.title = '手写签名'; |
| 39 | + }, |
| 40 | + mounted() { |
| 41 | + this.canvasBox = document.getElementById('canvasBox'); |
| 42 | + this.initCanvas(); |
| 43 | + }, |
| 44 | + computed: { |
| 45 | + getHorizontalStyle() { |
| 46 | + const d = document; |
| 47 | + const w = window.innerWidth || d.documentElement.clientWidth || d.body.clientWidth; |
| 48 | + const h = window.innerHeight || d.documentElement.clientHeight || d.body.clientHeight; |
| 49 | + let length = (h - w) / 2; |
| 50 | + let width = w; |
| 51 | + let height = h; |
| 52 | +
|
| 53 | + switch (this.degree) { |
| 54 | + case -90: |
| 55 | + length = -length; |
| 56 | + case 90: |
| 57 | + width = h; |
| 58 | + height = w; |
| 59 | + break; |
| 60 | + default: |
| 61 | + length = 0; |
| 62 | + } |
| 63 | + if (this.canvasBox) { |
| 64 | + this.canvasBox.removeChild(document.querySelector('canvas')); |
| 65 | + this.canvasBox.appendChild(document.createElement('canvas')); |
| 66 | + setTimeout(() => { |
| 67 | + this.initCanvas(); |
| 68 | + }, 200); |
| 69 | + } |
| 70 | + return { |
| 71 | + transform: `rotate(${this.degree}deg) translate(${length}px,${length}px)`, |
| 72 | + width: `${width}px`, |
| 73 | + height: `${height}px`, |
| 74 | + transformOrigin: 'center center', |
| 75 | + }; |
| 76 | + }, |
| 77 | + }, |
| 78 | + methods: { |
| 79 | + initCanvas() { |
| 80 | + const canvas = document.querySelector('canvas'); |
| 81 | + this.draw = new Draw(canvas, -this.degree); |
| 82 | + }, |
| 83 | + clear() { |
| 84 | + this.draw.clear(); |
| 85 | + }, |
| 86 | + download() { |
| 87 | + this.draw.downloadPNGImage(this.draw.getPNGImage()); |
| 88 | + }, |
| 89 | + savePNG() { |
| 90 | + this.signImage = this.draw.getPNGImage(); |
| 91 | + this.showBox = true; |
| 92 | + }, |
| 93 | + upload() { |
| 94 | + const image = this.draw.getPNGImage(); |
| 95 | + const blob = this.draw.dataURLtoBlob(image); |
| 96 | +
|
| 97 | + const url = ''; |
| 98 | + const successCallback = (response) => { |
| 99 | + console.log(response); |
| 100 | + }; |
| 101 | + const failureCallback = (error) => { |
| 102 | + console.log(error); |
| 103 | + }; |
| 104 | + this.draw.upload(blob, url, successCallback, failureCallback); |
| 105 | + }, |
| 106 | + }, |
| 107 | +}; |
| 108 | +
|
| 109 | +
|
| 110 | +</script> |
| 111 | + |
| 112 | +<style> |
| 113 | +.container { |
| 114 | + width: 100%; |
| 115 | + height: 100%; |
| 116 | +} |
| 117 | +#canvasBox { |
| 118 | + display: flex; |
| 119 | + flex-direction: column; |
| 120 | + height: 100%; |
| 121 | +} |
| 122 | +.greet { |
| 123 | + padding: 20px; |
| 124 | + font-size: 20px; |
| 125 | + user-select: none; |
| 126 | +} |
| 127 | +input { |
| 128 | + font-size: 20px; |
| 129 | +} |
| 130 | +.greet select { |
| 131 | + font-size: 18px; |
| 132 | +} |
| 133 | +canvas { |
| 134 | + flex: 1; |
| 135 | + cursor: crosshair; |
| 136 | + border:2px dashed lightgray; |
| 137 | +} |
| 138 | +.image-box { |
| 139 | + width: 100%; |
| 140 | + height: 100%; |
| 141 | +} |
| 142 | +.image-box header{ |
| 143 | + font-size: 18px; |
| 144 | +} |
| 145 | +.image-box img { |
| 146 | + max-width: 80%; |
| 147 | + max-height: 80%; |
| 148 | + margin-top: 50px; |
| 149 | + border: 1px solid gray; |
| 150 | +} |
| 151 | +</style> |
0 commit comments