wirte.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. var b5_width = 176; // B5 纸的码点宽度
  2. var b5_height = 250; // B5纸的码点高度
  3. var x_codepoint_size = 1.524; // 横坐标码点的大小
  4. var y_codepoint_size = 1.524; // 纵坐标码点的大小
  5. ax=b5_width/x_codepoint_size
  6. ay=b5_height/y_codepoint_size
  7. class myCanvas {
  8. constructor(canvasId, canvasWidth, canvasHeight, imgUrls) {
  9. this.canvas = null
  10. this.canvasNode = null
  11. this.ctx = null
  12. this.allImage = {}
  13. this.force = 0
  14. this.canvasWidth = canvasWidth
  15. this.canvasHeight = canvasHeight
  16. this.mapData = new Map();
  17. this.x_coordinate = null
  18. this.y_coordinate = null
  19. this.color = '#000'
  20. this.isDown = false
  21. this.lastPage = null
  22. this.initCanvas(canvasId, canvasWidth, canvasHeight)
  23. this.isReplay = false
  24. this.p_index = 0
  25. this.scale = 1
  26. this.offsetX = 0
  27. this.offsetY = 0
  28. this.lineWidth = 2
  29. this.penWidth = 1.0
  30. this.currentX = 0
  31. this.currentY = 0
  32. this.currentMidX = 0
  33. this.currentMidY = 0
  34. this.preMidX = 0
  35. this.preMidY = 0
  36. this.preX = 0
  37. this.preY = 0
  38. this.lastPointX = 0
  39. this.lastPointY = 0
  40. this.imgUrls = imgUrls
  41. }
  42. // 初始化canvas
  43. async initCanvas(canvasId, canvasWidth, canvasHeight) {
  44. this.canvasNode = await new Promise((resolve, reject) => {
  45. const query = wx.createSelectorQuery()
  46. query.select(canvasId).fields({
  47. node: true,
  48. size: true
  49. }).exec(res => {
  50. resolve(res[0])
  51. })
  52. })
  53. this.canvas = this.canvasNode.node
  54. this.ctx = this.canvas.getContext('2d')
  55. // 以下为canvas的缩放比例 默认为2倍 如有其它需求 请自行更改
  56. this.canvas.width = canvasWidth * 2
  57. this.canvas.height = canvasHeight * 2
  58. }
  59. // 绘制图片
  60. async drawImage(imgUrl, x1, y1, x2, y2) {
  61. console.log(this)
  62. if (!this.allImage[imgUrl]) {
  63. this.allImage[imgUrl] = await new Promise( (resolve) =>{
  64. console.log(this.canvas)
  65. const img = this.canvas.createImage()
  66. img.src = imgUrl
  67. img.onload = () => {
  68. resolve(img)
  69. }
  70. })
  71. }
  72. this.ctx.drawImage(this.allImage[imgUrl], x1, y1, x2, y2)
  73. }
  74. // 四舍五入算法
  75. roundNum(number, fractionDigits) {
  76. return Math.round(number * Math.pow(10, fractionDigits)) / Math.pow(10, fractionDigits);
  77. }
  78. // 数据处理
  79. getDrawData(data) {
  80. if (data.dotType === 'PEN_DOWN') {
  81. this.addPageId(data.pageID, data)
  82. }
  83. this.force = data.force
  84. let xPoint = this.roundNum((((data.x + (data.fx / 100)) * this.cvsWidth) / ax), 13)
  85. let yPoint = this.roundNum((((data.y + (data.fy / 100)) * this.cvsHeight) / ay), 13);
  86. this.x_coordinate = data.x + (data.fx / 100)
  87. this.y_coordinate = data.y + (data.fx / 100)
  88. this.mapData.get(data.pageID).push({
  89. bookId: data.bookID,
  90. pageID: data.pageID,
  91. timeLog: data.timeLong,
  92. xPoint,
  93. yPoint,
  94. dotType: data.dotType,
  95. penWidth:data.force,
  96. color: this.color
  97. })
  98. this.penType(data.dotType, xPoint, yPoint, color)
  99. }
  100. addPageId(myPageId) {
  101. if (myPageId === this.lastPage) {
  102. !this.mapData.get(myPageId) && this.mapData.set(myPageId, [])
  103. return
  104. }
  105. this.changePage(myPageId);
  106. !this.mapData.has(myPageId) && this.mapData.set(myPageId, [])
  107. this.mapData.get(myPageId).forEach((item) => {
  108. penWidth = this.setPenFront(item.force)
  109. this.penType(item.dotType, item.xPoint, item.yPoint, item.color)
  110. })
  111. }
  112. setPenFront() {
  113. }
  114. // 点分类型
  115. penType(penType, x, y, color) {
  116. penType === "PEN_DOWN" ? this.penDown(x, y, color) : penType === "PEN_MOVE" ? this.penmove(x, y, color) : this.penup(x, y, color)
  117. }
  118. // down点
  119. penDown(x, y, color) {
  120. if (this.isDown) {
  121. this.drawPen(scale, this.offsetX, this.offsetY, this.lineWidth, this.preX, this.preY, 0)
  122. this.isDown = false
  123. }
  124. this.p_index = 0
  125. this.color = color
  126. this.drawPen(scale, this.offsetX, this.offsetY, this.lineWidth, x, y, thos.force);
  127. this.isDown = true
  128. this.lastPointX = 0
  129. this.lastPointY = 0
  130. }
  131. // move点
  132. penMove(x, y, color) {
  133. if (!this.isDown) {
  134. this.p_index = 0
  135. this.isDown = true
  136. this.drawPen(this.scale, this.offsetX, this.offsetY, this.lineWidth, x, y, this.force)
  137. return
  138. }
  139. this.p_index += 1
  140. this.drawPen(this.scale, this.offsetX, this.offsetY, this.lineWidth, x, y, 0)
  141. }
  142. // up点
  143. penUp(x, y, color) {
  144. this.isDown = false
  145. this.drawPen(this.scale, this.offsetX, this.offsetY, this.lineWidth, x, y, this.force)
  146. }
  147. // 写字方法
  148. drawPen(scale, offsetX, offsetY, lineWidth, x_o, y_o, force) {
  149. if (this.p_index === 0) {
  150. this.currentMidX = this.preX = this.preMidX = this.currentX = x_o * scale + offsetX + 0.1
  151. this.currentMidY = this.preY = this.preMidY = this.currentY = y_o * scale + offsetY
  152. return
  153. }
  154. if (this.p_index > 1) {
  155. this.currentX = x_o * scale + offsetX + 0.1
  156. this.currentY = y_o * scale + offsetY
  157. this.currentMidX = (this.currentX + this.preX) / 2.0
  158. this.currentMidY = (this.currentY + this.preY) / 2.0
  159. this.ctx.beginPath();
  160. // this.ctx.setLineCap('round');
  161. // this.ctx.setLineJoin('miter');
  162. this.ctx.lineCap="round"
  163. this.ctx.lilneJoin="miter"
  164. this.ctx.moveTo(this.preMidX, this.preMidY);
  165. this.ctx.lineWidth = 2
  166. this.ctx.quadraticCurveTo(this.preX , this.preY , this.currentMidX , this.currentMidY);
  167. this.ctx.setStrokeStyle("#000");
  168. this.ctx.fill();
  169. this.ctx.stroke();
  170. this.ctx.closePath();
  171. // this.ctx.draw(true);
  172. if (force === 0) {
  173. this.ctx.moveTo(this.currentMidX , this.currentMidY )
  174. this.ctx.lineWidth = this.penWidth
  175. this.ctx.lineTo(this.currentX , this.currentY )
  176. }
  177. this.preMidX = this.currentMidX
  178. this.premidY = this.currentMidY
  179. this.preX = this.currentX
  180. this.preY = this.currentY
  181. }
  182. }
  183. // 切页
  184. changePage(index) {
  185. if (index === this.lastPage && !this.isReplay) {
  186. return
  187. }
  188. this.lastPage = index
  189. this.drawImage(this.imgUrls[(index & 1 ? 1 : 0)], 0, 0, this.canvasWidth * 2, this.canvasHeight * 2)
  190. }
  191. }
  192. export {
  193. myCanvas
  194. }