autograph-to-pic.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. import { saveImgToAliOss } from '@/libs/tools.js'
  34. export default {
  35. props: {
  36. // 签字画布id
  37. canvasId: {
  38. type: String,
  39. default: ''
  40. },
  41. },
  42. data(){
  43. return{
  44. lineColor:'black',
  45. slideValue:50,
  46. handwriting:'',
  47. selectColor:'black',
  48. color:'',
  49. showimg:'',
  50. share_popup:false,
  51. isEmpty: true
  52. }
  53. },
  54. mounted() {
  55. this.handwriting = new Handwriting({
  56. lineColor: this.lineColor,
  57. slideValue: this.slideValue, // 0, 25, 50, 75, 100
  58. canvasName: this.canvasId,
  59. })
  60. },
  61. methods:{
  62. // 选择画笔颜色
  63. selectColorEvent(event) {
  64. this.selectColor = event
  65. if(event=='black'){
  66. this.color= '#1A1A1A'
  67. }else if(event=='red'){
  68. this.color= '#ca262a'
  69. }
  70. this.handwriting.selectColorEvent(this.color)
  71. },
  72. // 重写
  73. retDraw() {
  74. this.handwriting.retDraw()
  75. this.isEmpty = true
  76. },
  77. // 笔迹粗细滑块
  78. updateValue(e) {
  79. this.slideValue = e.detail.value
  80. this.handwriting.selectSlideValue(this.slideValue)
  81. },
  82. // 绑定到canvas的touchstart事件
  83. uploadScaleStart(event){
  84. this.handwriting.uploadScaleStart(event)
  85. },
  86. // 绑定到canvas的touchmove事件
  87. uploadScaleMove(event){
  88. this.handwriting.uploadScaleMove(event)
  89. },
  90. // 绑定到canvas的uploadScaleEnd事件
  91. uploadScaleEnd(event){
  92. this.handwriting.uploadScaleEnd(event)
  93. this.isEmpty = false
  94. },
  95. // 保存 生成图片
  96. subCanvas(){
  97. const _this = this
  98. this.handwriting.saveCanvas().then(res=>{
  99. this.showimg = res
  100. console.log(res)
  101. saveImgToAliOss(res, function(ret) {
  102. console.log(ret,'-----阿里云')
  103. _this.$emit('generatePictures', ret)
  104. })
  105. }).catch(err=>{
  106. console.log(err)
  107. })
  108. },
  109. }
  110. }
  111. </script>
  112. <style lang="scss" scoped>
  113. .autograph-wrap {
  114. width: 100%;
  115. height: 100%;
  116. overflow: hidden;
  117. display: flex;
  118. align-content: center;
  119. flex-direction: column;
  120. justify-content: center;
  121. font-size: 28upx;
  122. .handRight {
  123. align-items: center;
  124. .handTitle {
  125. flex: 1;
  126. color: #666;
  127. justify-content: center;
  128. font-size: 30upx;
  129. }
  130. }
  131. .handBtn {
  132. flex-direction: column;
  133. padding: 40upx 20upx;
  134. .slide-wrapper {
  135. align-items: center;
  136. margin-bottom: 20upx;
  137. .slider{
  138. width: 400upx;
  139. padding-left: 20upx;
  140. }
  141. }
  142. .color{
  143. align-items: center;
  144. text{
  145. margin-right: 20upx;
  146. }
  147. .black-select, .red-select {
  148. width: 60upx;
  149. height: 60upx;
  150. vertical-align: super;
  151. }
  152. .black-select.color_select, .red-select.color_select {
  153. width: 90upx;
  154. height: 90upx;
  155. vertical-align: sub;
  156. }
  157. }
  158. }
  159. .handCenter {
  160. border: 4upx dashed #e9e9e9;
  161. flex: 5;
  162. overflow: hidden;
  163. box-sizing: border-box;
  164. width: 90%;
  165. margin: 0 auto;
  166. .handWriting {
  167. background: #fff;
  168. width: 100%;
  169. height: 300upx;
  170. }
  171. }
  172. .showimg{
  173. border: 4upx solid #e9e9e9;
  174. overflow: hidden;
  175. width: 90%;
  176. margin: 0 auto;
  177. background: #eee;
  178. height: 350upx;
  179. margin-top: 40upx;
  180. align-items: center;
  181. justify-content: center;
  182. image{
  183. width: 100%;
  184. height: 100%;
  185. }
  186. text{
  187. font-size: 40upx;
  188. color: #888;
  189. }
  190. }
  191. .buttons{
  192. margin: 20upx 5%;
  193. text-align: right;
  194. .delBtn {
  195. color: #666;
  196. }
  197. .subBtn {
  198. background: #008ef6;
  199. color: #fff;
  200. text-align: center;
  201. justify-content: center;
  202. }
  203. }
  204. }
  205. .drop {
  206. width: 50upx;
  207. height: 50upx;
  208. border-radius: 50%;
  209. background: #FFF;
  210. position: absolute;
  211. left: 0upx;
  212. top: -10upx;
  213. box-shadow: 0px 1px 5px #888888;
  214. }
  215. .slide {
  216. width: 250upx;
  217. height: 30upx;
  218. }
  219. </style>