test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // pages/mine/mine.js
  2. var content = null;
  3. var touchs = [];
  4. var canvasw = 0;
  5. var canvash = 0;
  6. //获取系统信息
  7. wx.getSystemInfo({
  8. success: function (res) {
  9. canvasw = res.windowWidth;
  10. canvash = canvasw * 9 / 16;
  11. },
  12. }),
  13. Page({
  14. /**
  15. * 页面的初始数据
  16. */
  17. data: {
  18. signImage: '',
  19. },
  20. // 画布的触摸移动开始手势响应
  21. start: function (event) {
  22. // console.log("触摸开始" + event.changedTouches[0].x)
  23. // console.log("触摸开始" + event.changedTouches[0].y)
  24. //获取触摸开始的 x,y
  25. let point = { x: event.changedTouches[0].x, y: event.changedTouches[0].y }
  26. touchs.push(point)
  27. },
  28. // 画布的触摸移动手势响应
  29. move: function (e) {
  30. let point = { x: e.touches[0].x, y: e.touches[0].y }
  31. touchs.push(point)
  32. if (touchs.length >= 2) {
  33. this.draw(touchs)
  34. }
  35. },
  36. // 画布的触摸移动结束手势响应
  37. end: function (e) {
  38. console.log("触摸结束" + e)
  39. //清空轨迹数组
  40. for (let i = 0; i < touchs.length; i++) {
  41. touchs.pop()
  42. }
  43. },
  44. // 画布的触摸取消响应
  45. cancel: function (e) {
  46. console.log("触摸取消" + e)
  47. },
  48. // 画布的长按手势响应
  49. tap: function (e) {
  50. console.log("长按手势" + e)
  51. },
  52. error: function (e) {
  53. console.log("画布触摸错误" + e)
  54. },
  55. /**
  56. * 生命周期函数--监听页面加载
  57. */
  58. onLoad: function (options) {
  59. //获得Canvas的上下文
  60. content = wx.createCanvasContext('firstCanvas')
  61. //设置线的颜色
  62. content.setStrokeStyle("#00ff00")
  63. //设置线的宽度
  64. content.setLineWidth(5)
  65. //设置线两端端点样式更加圆润
  66. content.setLineCap('round')
  67. //设置两条线连接处更加圆润
  68. content.setLineJoin('round')
  69. },
  70. /**
  71. * 生命周期函数--监听页面初次渲染完成
  72. */
  73. onReady: function () {
  74. },
  75. //绘制
  76. draw: function (touchs) {
  77. let point1 = touchs[0]
  78. let point2 = touchs[1]
  79. touchs.shift()
  80. content.moveTo(point1.x, point1.y)
  81. content.lineTo(point2.x, point2.y)
  82. content.stroke()
  83. content.draw(true)
  84. },
  85. //清除操作
  86. clearClick: function () {
  87. //清除画布
  88. content.clearRect(0, 0, canvasw, canvash)
  89. content.draw(true)
  90. },
  91. //保存图片
  92. saveClick: function () {
  93. var that = this
  94. wx.canvasToTempFilePath({
  95. canvasId: 'firstCanvas',
  96. success: function (res) {
  97. //打印图片路径
  98. console.log(res.tempFilePath)
  99. //设置保存的图片
  100. that.setData({
  101. signImage: res.tempFilePath
  102. })
  103. }
  104. })
  105. }
  106. })