autograph.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <view>
  3. <view class="dashbox">
  4. <view class="btnList">
  5. <view @click="clearCanvas">清空</view>
  6. </view>
  7. <view class="handCenter">
  8. <canvas class="handWriting" canvas-id="handWriting" :disable-scroll="true" @touchstart="scaleStart"
  9. @touchmove="scaleMove" @touchend="scaleEnd" type="2d">
  10. </canvas>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. canvasName: '#handWriting',
  20. ctx: '',
  21. canvasWidth: 0,
  22. canvasHeight: 0,
  23. startPoint: {
  24. x: 0,
  25. y: 0,
  26. },
  27. selectColor: 'black',
  28. lineColor: '#1A1A1A', // 颜色
  29. lineSize: 1.5, // 笔记倍数
  30. radius: 5, //画圆的半径
  31. }
  32. },
  33. onLoad() {
  34. },
  35. onShow() {
  36. },
  37. onReady() {
  38. var canvas = uni.createCanvasContext('#handWriting')
  39. console.log(canvas)
  40. this.ctx = canvas
  41. },
  42. methods: {
  43. scaleStart(event) {
  44. console.log(event,'kaishi')
  45. if (event.type != 'touchstart') return false;
  46. let currentPoint = {
  47. x: event.touches[0].x,
  48. y: event.touches[0].y
  49. }
  50. this.drawCircle(currentPoint);
  51. this.startPoint = currentPoint
  52. },
  53. scaleMove(e) {
  54. console.log(this.startPoint,e.touches[0],'开始画')
  55. this.drawLine({x:121,y:107},{x:208,y:127})
  56. },
  57. scaleEnd(e) {
  58. console.log(e,'结束画')
  59. },
  60. drawCircle(point) { //这里负责点
  61. let ctx = this.ctx;
  62. ctx.beginPath();
  63. ctx.setFillStyle(this.lineColor)
  64. //笔迹粗细由圆的大小决定
  65. ctx.arc(point.x, point.y, this.radius, 0, 2 * Math.PI);
  66. ctx.fill();
  67. ctx.closePath();
  68. },
  69. drawLine(sourcePoint, targetPoint) {
  70. let ctx = this.ctx;
  71. this.drawCircle(targetPoint);
  72. ctx.beginPath();
  73. //ctx.setLineWidth(this.radius * 2)//这里乘2是因为线条的粗细要和圆的直径相等
  74. ctx.moveTo(sourcePoint.x, sourcePoint.y);
  75. ctx.lineTo(targetPoint.x, targetPoint.y);
  76. ctx.stroke();
  77. ctx.closePath();
  78. },
  79. clearCanvas() { //清空画布
  80. let ctx = this.ctx;
  81. ctx.rect(0, 0, this.canvasWidth, this.canvasHeight);
  82. ctx.fillStyle = '#FFFFFF';
  83. ctx.fill();
  84. }
  85. }
  86. }
  87. </script>
  88. <style lang="scss" scoped>
  89. .btnList {
  90. width: 95%;
  91. margin: 0 auto;
  92. }
  93. .handWriting {
  94. background: #f5f5f5;
  95. width: 690rpx;
  96. height: 80vh;
  97. margin: 0 auto
  98. }
  99. </style>