autograph-to-pic.vue 4.7 KB

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