drawCanvas.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. class DrawCanvas {
  2. constructor() {
  3. this._ctx = null
  4. this._canvas = null
  5. this._executionArr = []
  6. this._pathArr = []
  7. this._style = null
  8. this.baseMapUrl = null
  9. this.baseWidth = null
  10. this.baseHeight = null
  11. }
  12. _init({ width, height, canvas, baseMapUrl = '' }) {
  13. if (!width || !height || !canvas) return
  14. const ctx = canvas.getContext('2d')
  15. const dpr = wx.getSystemInfoSync().pixelRatio
  16. canvas.width = width * dpr
  17. canvas.height = height * dpr
  18. ctx.scale(dpr, dpr)
  19. this._canvas = canvas
  20. this._ctx = ctx
  21. this._executionArr = [] //每移动一笔的数据
  22. this._pathArr = [] //抬起落下为一笔
  23. this.baseHeight = height
  24. this.baseWidth = width
  25. this.baseMapUrl = baseMapUrl
  26. if (baseMapUrl) this.addBaseMap()
  27. }
  28. mouseStart() {
  29. this._executionArr = []
  30. }
  31. mouseMove({ x = 0, y = 0 }) {
  32. if (!this._style) return
  33. this._executionArr.push([x, y])
  34. this._ctx.fillStyle = this._style
  35. this._ctx.fillRect(x - 5, y - 5, 10, 10)
  36. console.log('画图并存储');
  37. }
  38. mouseEnd() {
  39. this._pathArr.push({
  40. fillStyle: this._style,
  41. path: this._executionArr
  42. })
  43. }
  44. mouseBack() {
  45. // 删除每个移动数据
  46. if (this._pathArr.length) {
  47. this._ctx.globalCompositeOperation = "source-over" //覆盖
  48. // 清空画布
  49. this._ctx.clearRect(0, 0, this._ctx.canvas.width, this._ctx.canvas.height)
  50. this._pathArr.pop()
  51. // 画上底图
  52. this.addBaseMap(() => {
  53. // 画上路径
  54. for (let k = 0; k < this._pathArr.length; k++) {
  55. const el = this._pathArr[k];
  56. for (let j = 0; j < el.path.length; j++) {
  57. const es = el.path[j];
  58. this._ctx.fillStyle = el.fillStyle
  59. this._ctx.fillRect(es[0] - 5, es[1] - 5, 10, 10)
  60. }
  61. }
  62. })
  63. }
  64. }
  65. // 添加底图
  66. addBaseMap(callback) {
  67. const img = this._canvas.createImage()
  68. img.onload = () => {
  69. this._ctx.drawImage(img, 0, 0, this.baseWidth, this.baseHeight)
  70. this._ctx.globalCompositeOperation = "source-atop" //画笔重叠部分覆盖
  71. if (callback) callback()
  72. }
  73. img.src = this.baseMapUrl
  74. }
  75. // 切换画笔
  76. changeBrush(url = '') {
  77. if (!url) return
  78. const img = this._canvas.createImage()
  79. img.onload = () => {
  80. this._style = this._ctx.createPattern(img, 'repeat')
  81. }
  82. img.src = url
  83. }
  84. // 清除画布
  85. destory() {
  86. // 清空画布
  87. this._ctx.clearRect(0, 0, this._ctx.canvas.width, this._ctx.canvas.height)
  88. this._ctx.globalCompositeOperation = "source-over" //覆盖
  89. // 画上底图
  90. this.addBaseMap()
  91. // 清除路径数据
  92. this._pathArr = []
  93. this._executionArr = []
  94. }
  95. // 保存
  96. save() {
  97. wx.canvasToTempFilePath({
  98. canvas: this._canvas,
  99. success(res) {
  100. console.log('保存', res.tempFilePath);
  101. }
  102. })
  103. }
  104. }
  105. export { DrawCanvas }