shopro-canvas.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <view class="invite-poster-content">
  3. <!-- TODO: 小程序端使用type='2d'会有报错 -->
  4. <canvas class="hideCanvas" canvas-id="self_canvas" :style="{ width: (poster.width || 1) + 'px', height: (poster.height || 1) + 'px' }"></canvas>
  5. </view>
  6. </template>
  7. <script>
  8. /**
  9. * shopro-canvas - canvas
  10. * @property {Object} canvasParams - 自定义参数
  11. * @property {Boolean} isAutoInit = [true] - 是否自动渲染canvas
  12. */
  13. import _app from '@/shopro/poster/QS-SharePoster/app.js';
  14. import { getSharePoster } from '@/shopro/poster/QS-SharePoster/QS-SharePoster.js';
  15. import tools from '@/shopro/poster/tools.js';
  16. import { mapMutations, mapActions, mapState, mapGetters } from 'vuex';
  17. let ctx = null;
  18. export default {
  19. components: {},
  20. data() {
  21. return {
  22. poster: {},
  23. canvasId: 'self_canvas'
  24. };
  25. },
  26. props: {
  27. canvasParams: {
  28. type: Object,
  29. default: () => {}
  30. },
  31. isAutoInit: {
  32. type: Boolean,
  33. default: true
  34. }
  35. },
  36. computed: {
  37. ...mapGetters(['initShare', 'userInfo'])
  38. },
  39. async mounted() {
  40. ctx = uni.createCanvasContext(this.canvasId, this);
  41. ctx && this.isAutoInit && this.shareFc();
  42. },
  43. methods: {
  44. async shareFc() {
  45. let that = this;
  46. _app.showLoading('绘制中...');
  47. let config = {};
  48. if (that.canvasParams.backgroundImage) {
  49. config = {
  50. backgroundImage: tools.checkImgHttp(that.canvasParams.backgroundImage, 'bgImage')
  51. };
  52. }
  53. if (that.canvasParams.background) {
  54. config = {
  55. background: {
  56. width: that.canvasParams.background?.width || 100,
  57. height: that.canvasParams.background?.height || 100,
  58. backgroundColor: that.canvasParams.background?.color || '#000'
  59. }
  60. };
  61. }
  62. try {
  63. _app.log('准备生成:' + new Date());
  64. const d = await getSharePoster({
  65. _this: that, //若在组件中使用 必传
  66. ...config,
  67. posterCanvasId: that.canvasId, //canvasId
  68. Context: ctx,
  69. delayTimeScale: 10, //延时系数
  70. draw: false, //是否执行ctx.draw方法, 推荐false,自己去draw
  71. drawArray: ({ bgObj, type, bgScale }) => {
  72. let arr = tools.initDrawArray(bgObj, that.canvasParams.drawArray);
  73. return new Promise((rs, rj) => {
  74. rs(arr);
  75. });
  76. },
  77. setCanvasWH: ({ bgObj, type, bgScale }) => {
  78. // 为动态设置画布宽高的方法,
  79. this.poster = bgObj;
  80. }
  81. });
  82. await that.drawPoster();
  83. } catch (e) {
  84. _app.hideLoading();
  85. _app.showToast(JSON.stringify(e));
  86. console.log(JSON.stringify(e));
  87. }
  88. },
  89. async drawPoster() {
  90. let that = this;
  91. ctx.draw(false, () => {
  92. uni.canvasToTempFilePath(
  93. {
  94. canvasId: that.canvasId,
  95. success: res => {
  96. _app.hideLoading();
  97. that.$emit('success', res.tempFilePath);
  98. _app.log('海报生成成功, 时间:' + new Date() + ', 临时路径: ' + res.tempFilePath);
  99. },
  100. fail: err => {
  101. _app.hideLoading();
  102. console.log('生成异常', err);
  103. }
  104. },
  105. that
  106. );
  107. });
  108. }
  109. }
  110. };
  111. </script>
  112. <style lang="scss">
  113. .hideCanvas {
  114. position: fixed;
  115. top: -99999upx;
  116. left: -99999upx;
  117. z-index: -99999;
  118. }
  119. </style>