render.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // #ifdef APP-VUE
  2. // import { Signature } from '@signature'
  3. import {
  4. Signature
  5. } from './signature.js'
  6. import {
  7. isTransparent
  8. } from './utils'
  9. export default {
  10. data() {
  11. return {
  12. canvasid: null,
  13. signature: null,
  14. observer: null,
  15. options: {},
  16. saveCount: 0,
  17. }
  18. },
  19. mounted() {
  20. this.$nextTick(this.init)
  21. },
  22. methods: {
  23. init() {
  24. const el = this.$refs.limeSignature || this.$ownerInstance.$el;
  25. this.canvas = document.createElement('canvas')
  26. this.canvas.style = 'width: 100%; height: 100%;'
  27. el.appendChild(this.canvas)
  28. this.signature = new Signature({
  29. el: this.canvas
  30. })
  31. this.signature.pen.setOption(this.options)
  32. const width = this.signature.canvas.get('width')
  33. const height = this.signature.canvas.get('height')
  34. this.emit({
  35. changeSize: {
  36. width,
  37. height
  38. }
  39. })
  40. },
  41. redo(v) {
  42. if (v && this.signature) {
  43. this.signature.redo()
  44. }
  45. },
  46. undo(v) {
  47. if (v && this.signature) {
  48. this.signature.undo()
  49. }
  50. },
  51. clear(v) {
  52. if (v && this.signature) {
  53. this.signature.clear()
  54. }
  55. },
  56. destroy() {
  57. if (this.canvas) {
  58. this.canvas.remove()
  59. }
  60. },
  61. mask(param={}) {
  62. if (this.signature) {
  63. let {destWidth=0, destHeight=0} = JSON.parse(param)
  64. let canvas = document.createElement('canvas')
  65. const ctx = canvas.getContext('2d');
  66. const pixelRatio = this.signature.canvas.get('pixelRatio')
  67. let width = this.signature.canvas.get('width')
  68. let height = this.signature.canvas.get('height')
  69. let context = this.signature.canvas.get('context')
  70. canvas.width = width * pixelRatio
  71. canvas.height = height * pixelRatio
  72. const imageData = context.getImageData(0, 0, width * pixelRatio, height * pixelRatio);
  73. for (let i = 0; i < imageData.data.length; i += 4) {
  74. // 判断当前像素是否透明
  75. const isTransparent = imageData.data[i + 3] === 0;
  76. if (isTransparent) {
  77. // 将透明像素设置为黑色背景
  78. imageData.data[i] = 0;
  79. imageData.data[i + 1] = 0;
  80. imageData.data[i + 2] = 0;
  81. } else {
  82. // 将非透明像素设置为白色内容
  83. imageData.data[i] = 255;
  84. imageData.data[i + 1] = 255;
  85. imageData.data[i + 2] = 255;
  86. }
  87. }
  88. ctx.putImageData(imageData, 0, 0);
  89. if(destWidth&&destHeight){
  90. const _canvas = document.createElement('canvas')
  91. _canvas.width = destWidth
  92. _canvas.height = destHeight
  93. const _context = _canvas.getContext('2d')
  94. _context.drawImage(canvas, 0, 0, destWidth, destHeight)
  95. canvas.remove()
  96. canvas = _canvas
  97. }
  98. this.emit({
  99. save: canvas.toDataURL()
  100. })
  101. canvas.remove()
  102. }
  103. },
  104. save(param) {
  105. let {
  106. fileType = 'png',
  107. quality = 1,
  108. n,
  109. destWidth = 0,
  110. destHeight = 0,
  111. } = JSON.parse(param)
  112. const type = `image/${fileType}`.replace(/jpg/, 'jpeg');
  113. if (n !== this.saveCount) {
  114. this.saveCount = n;
  115. const {
  116. backgroundColor,
  117. backgroundImage,
  118. landscape,
  119. boundingBox
  120. } = this.options
  121. const flag = landscape || backgroundColor || boundingBox||destWidth&&destHeight
  122. const image = this.signature.canvas.get('el').toDataURL(!flag && type, !flag && quality)
  123. if (flag) {
  124. let canvas = document.createElement('canvas')
  125. const pixelRatio = this.signature.canvas.get('pixelRatio')
  126. let width = this.signature.canvas.get('width')
  127. let height = this.signature.canvas.get('height')
  128. let x = 0
  129. let y = 0
  130. const next = () => {
  131. const size = [width, height]
  132. if (landscape) {
  133. size.reverse()
  134. }
  135. canvas.width = size[0] * pixelRatio
  136. canvas.height = size[1] * pixelRatio
  137. const param = [x, y, width, height, 0, 0, width, height].map(item => item * pixelRatio)
  138. const context = canvas.getContext('2d')
  139. if (landscape) {
  140. context.translate(0, width * pixelRatio)
  141. context.rotate(-Math.PI / 2)
  142. }
  143. if (backgroundColor && !isTransparent(backgroundColor)) {
  144. context.fillStyle = backgroundColor
  145. context.fillRect(0, 0, width * pixelRatio, height * pixelRatio)
  146. }
  147. const drawImage = () => {
  148. // param
  149. context.drawImage(this.signature.canvas.get('el'), ...param)
  150. if(destWidth&&destHeight){
  151. const _canvas = document.createElement('canvas')
  152. _canvas.width = destWidth
  153. _canvas.height = destHeight
  154. const _context = _canvas.getContext('2d')
  155. _context.drawImage(canvas, 0, 0, destWidth, destHeight)
  156. canvas.remove()
  157. canvas = _canvas
  158. }
  159. this.emit({
  160. save: canvas.toDataURL(type, quality)
  161. })
  162. canvas.remove()
  163. }
  164. if (backgroundImage) {
  165. const img = new Image();
  166. img.onload = () => {
  167. context.drawImage(img, ...param)
  168. drawImage()
  169. }
  170. img.src = backgroundImage
  171. }
  172. if (!backgroundImage) {
  173. drawImage()
  174. }
  175. }
  176. if (boundingBox) {
  177. const res = this.signature.getContentBoundingBox()
  178. width = res.width
  179. height = res.height
  180. x = res.startX
  181. y = res.startY
  182. next()
  183. } else {
  184. next()
  185. }
  186. } else {
  187. this.emit({
  188. save: image
  189. })
  190. }
  191. }
  192. },
  193. isEmpty(v) {
  194. if (v && this.signature) {
  195. const isEmpty = this.signature.isEmpty()
  196. this.emit({
  197. isEmpty
  198. })
  199. }
  200. },
  201. emit(event) {
  202. this.$ownerInstance.callMethod('onMessage', {
  203. detail: {
  204. data: [{
  205. event
  206. }]
  207. }
  208. })
  209. },
  210. update(v) {
  211. if (v) {
  212. if (this.signature) {
  213. this.options = v
  214. this.signature.pen.setOption(v)
  215. } else {
  216. this.options = v
  217. }
  218. }
  219. }
  220. }
  221. }
  222. // #endif