autograph-to-pic.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <view class="autograph-wrap">
  3. <!-- <view class="handRight">
  4. <view class="handTitle">手写板</view>
  5. </view>
  6. <view class="handBtn">
  7. <view class="slide-wrapper">
  8. <text>选择粗细</text>
  9. <slider @change="updateValue" value="50" show-value class="slider" step="25" />
  10. </view>
  11. <view class="color">
  12. <text>选择颜色</text>
  13. <image @click="selectColorEvent('black')" :src="selectColor === 'black' ? require('./img/color_black_selected.png') : require('./img/color_black.png')" :class="selectColor === 'black' ? 'color_select' : ''" class="black-select"></image>
  14. <image @click="selectColorEvent('red')" :src="selectColor === 'red' ? require('./img/color_red_selected.png') : require('./img/color_red.png') " :class="selectColor === 'red' ? 'color_select' : ''" class="red-select" ></image>
  15. </view>
  16. </view> -->
  17. <view class="handCenter">
  18. <canvas class="handWriting" disable-scroll="true" @touchstart="uploadScaleStart" @touchmove="uploadScaleMove" @touchend="uploadScaleEnd" :id="canvasId" :canvas-id="canvasId"></canvas>
  19. </view>
  20. <!-- <view class="showimg">
  21. <image v-if="showimg" :src="showimg" mode=""></image>
  22. <text v-else >图片展示</text>
  23. </view> -->
  24. <view class="buttons">
  25. <!-- <button @click="retDraw" class="delBtn">重写</button> -->
  26. <u-button size="mini" @click="retDraw" class="delBtn">重写</u-button>
  27. <!-- <button @click="subCanvas" class="subBtn">保存</button> -->
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import Handwriting from './js/signature.js'
  33. export default {
  34. props: {
  35. // 签字画布id
  36. canvasId: {
  37. type: String,
  38. default: ''
  39. },
  40. // 是否为空校验
  41. isEmpty: {
  42. type: Array,
  43. default: []
  44. }
  45. },
  46. data(){
  47. return{
  48. lineColor:'black',
  49. slideValue:50,
  50. handwriting:'',
  51. selectColor:'black',
  52. color:'',
  53. showimg:'',
  54. share_popup:false,
  55. }
  56. },
  57. watch: {
  58. isEmpty: {
  59. handler(value) {
  60. this.checkEmpty(value)
  61. },
  62. deep: true
  63. }
  64. },
  65. mounted() {
  66. this.handwriting = new Handwriting({
  67. lineColor: this.lineColor,
  68. slideValue: this.slideValue, // 0, 25, 50, 75, 100
  69. canvasName: this.canvasId,
  70. })
  71. console.log(this.handwriting)
  72. },
  73. methods:{
  74. // 为空校验
  75. checkEmpty(arr){
  76. console.log(arr)
  77. let isCheck = []
  78. arr.map(item => {
  79. if(item.state){
  80. isCheck.push(item.name)
  81. }
  82. })
  83. console.log(isCheck)
  84. isCheck.map(item => {
  85. const c = document.getElementById(item); // 获取html的canvas对象,我这个id="canvas"
  86. if(this.isCanvasBlank(c)){
  87. // alert("请绘制草图后再上传!")
  88. // return
  89. item.empty = true
  90. }else{
  91. item.empty = false
  92. }
  93. })
  94. console.log(isCheck,'===空空kong')
  95. },
  96. // 验证canvas画布是否为空函数
  97. isCanvasBlank(canvas) {
  98. console.log(canvas.width,canvas.height)
  99. // var blank = document.createElement('canvas');//系统获取一个空canvas对象
  100. // blank.width = canvas.width;
  101. // blank.height = canvas.height;
  102. // console.log(blank.toDataURL(),blank)
  103. // return canvas.toDataURL() == blank.toDataURL()//比较值相等则为空
  104. },
  105. // 选择画笔颜色
  106. selectColorEvent(event) {
  107. this.selectColor = event
  108. if(event=='black'){
  109. this.color= '#1A1A1A'
  110. }else if(event=='red'){
  111. this.color= '#ca262a'
  112. }
  113. this.handwriting.selectColorEvent(this.color)
  114. },
  115. // 重写
  116. retDraw() {
  117. this.handwriting.retDraw()
  118. },
  119. // 笔迹粗细滑块
  120. updateValue(e) {
  121. this.slideValue = e.detail.value
  122. this.handwriting.selectSlideValue(this.slideValue)
  123. },
  124. // 绑定到canvas的touchstart事件
  125. uploadScaleStart(event){
  126. this.handwriting.uploadScaleStart(event)
  127. },
  128. // 绑定到canvas的touchmove事件
  129. uploadScaleMove(event){
  130. this.handwriting.uploadScaleMove(event)
  131. },
  132. // 绑定到canvas的uploadScaleEnd事件
  133. uploadScaleEnd(event){
  134. this.handwriting.uploadScaleEnd(event)
  135. },
  136. // 保存 生成图片
  137. subCanvas(){
  138. this.handwriting.saveCanvas().then(res=>{
  139. this.showimg = res
  140. console.log(res)
  141. }).catch(err=>{
  142. console.log(err)
  143. })
  144. },
  145. }
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. .autograph-wrap {
  150. width: 100%;
  151. height: 100%;
  152. overflow: hidden;
  153. display: flex;
  154. align-content: center;
  155. flex-direction: column;
  156. justify-content: center;
  157. font-size: 28upx;
  158. .handRight {
  159. align-items: center;
  160. .handTitle {
  161. flex: 1;
  162. color: #666;
  163. justify-content: center;
  164. font-size: 30upx;
  165. }
  166. }
  167. .handBtn {
  168. flex-direction: column;
  169. padding: 40upx 20upx;
  170. .slide-wrapper {
  171. align-items: center;
  172. margin-bottom: 20upx;
  173. .slider{
  174. width: 400upx;
  175. padding-left: 20upx;
  176. }
  177. }
  178. .color{
  179. align-items: center;
  180. text{
  181. margin-right: 20upx;
  182. }
  183. .black-select, .red-select {
  184. width: 60upx;
  185. height: 60upx;
  186. vertical-align: super;
  187. }
  188. .black-select.color_select, .red-select.color_select {
  189. width: 90upx;
  190. height: 90upx;
  191. vertical-align: sub;
  192. }
  193. }
  194. }
  195. .handCenter {
  196. border: 4upx dashed #e9e9e9;
  197. flex: 5;
  198. overflow: hidden;
  199. box-sizing: border-box;
  200. width: 90%;
  201. margin: 0 auto;
  202. .handWriting {
  203. background: #fff;
  204. width: 100%;
  205. height: 300upx;
  206. }
  207. }
  208. .showimg{
  209. border: 4upx solid #e9e9e9;
  210. overflow: hidden;
  211. width: 90%;
  212. margin: 0 auto;
  213. background: #eee;
  214. height: 350upx;
  215. margin-top: 40upx;
  216. align-items: center;
  217. justify-content: center;
  218. image{
  219. width: 100%;
  220. height: 100%;
  221. }
  222. text{
  223. font-size: 40upx;
  224. color: #888;
  225. }
  226. }
  227. .buttons{
  228. margin: 20upx 5%;
  229. text-align: right;
  230. .delBtn {
  231. color: #666;
  232. }
  233. .subBtn {
  234. background: #008ef6;
  235. color: #fff;
  236. text-align: center;
  237. justify-content: center;
  238. }
  239. }
  240. }
  241. .drop {
  242. width: 50upx;
  243. height: 50upx;
  244. border-radius: 50%;
  245. background: #FFF;
  246. position: absolute;
  247. left: 0upx;
  248. top: -10upx;
  249. box-shadow: 0px 1px 5px #888888;
  250. }
  251. .slide {
  252. width: 250upx;
  253. height: 30upx;
  254. }
  255. </style>