drawCanvas.js 3.1 KB

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