render.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // #ifdef APP-VUE
  2. // import { Signature } from '@signature'
  3. import { Signature } from './signature'
  4. export default {
  5. data() {
  6. return {
  7. canvasid: null,
  8. signature: null,
  9. observer: null,
  10. options: {},
  11. saveCount: 0,
  12. }
  13. },
  14. mounted() {
  15. this.$nextTick(this.init)
  16. },
  17. methods: {
  18. init() {
  19. const el = this.$refs.limeSignature;
  20. const canvas = document.createElement('canvas')
  21. canvas.style = 'width:100%; height: 100%;'
  22. el.appendChild(canvas)
  23. this.signature = new Signature({
  24. el: canvas
  25. })
  26. this.signature.pen.setOption(this.options)
  27. const width = this.signature.canvas.get('width')
  28. const height = this.signature.canvas.get('height')
  29. this.emit({
  30. changeSize: {
  31. width,
  32. height
  33. }
  34. })
  35. },
  36. undo(v) {
  37. if (v && this.signature) {
  38. this.signature.undo()
  39. }
  40. },
  41. clear(v) {
  42. if (v && this.signature) {
  43. this.signature.clear()
  44. }
  45. },
  46. save(v) {
  47. if (v !== this.saveCount) {
  48. this.saveCount = v;
  49. const image = this.signature.canvas.get('el').toDataURL()
  50. const {backgroundColor,landscape} = this.options
  51. if (landscape || backgroundColor) {
  52. const canvas = document.createElement('canvas')
  53. const width = this.signature.canvas.get('width')
  54. const height = this.signature.canvas.get('height')
  55. const pixelRatio = this.signature.canvas.get('pixelRatio')
  56. const size = [width, height]
  57. if(landscape) {size.reverse()}
  58. canvas.width = size[0] * pixelRatio
  59. canvas.height = size[1] * pixelRatio
  60. const context = canvas.getContext('2d')
  61. context.scale(pixelRatio, pixelRatio)
  62. if (landscape) {
  63. context.translate(0, width)
  64. context.rotate(-Math.PI / 2)
  65. }
  66. if (backgroundColor) {
  67. context.fillStyle = backgroundColor
  68. context.fillRect(0, 0, width, height)
  69. }
  70. context.drawImage(this.signature.canvas.get('el'), 0, 0, width, height)
  71. this.emit({
  72. save: canvas.toDataURL()
  73. })
  74. canvas.remove()
  75. } else {
  76. this.emit({
  77. save: image
  78. })
  79. }
  80. }
  81. },
  82. isEmpty(v) {
  83. if (v && this.signature) {
  84. const isEmpty = this.signature.isEmpty()
  85. this.emit({
  86. isEmpty
  87. })
  88. }
  89. },
  90. emit(event) {
  91. this.$ownerInstance.callMethod('onMessage', {
  92. detail: {
  93. data: [{
  94. event
  95. }]
  96. }
  97. })
  98. },
  99. update(v) {
  100. if (v) {
  101. if (this.signature) {
  102. this.options = v
  103. this.signature.pen.setOption(v)
  104. } else {
  105. this.options = v
  106. }
  107. }
  108. }
  109. }
  110. }
  111. // #endif