jp-merge.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <view class="share">
  3. <canvas
  4. canvas-id="shareCanvas"
  5. class="canvas"
  6. bindlongpress="saveImg"
  7. catchtouchmove="true"
  8. style="position:fixed;left:500%"
  9. :style="{height: canvasHeight+'px',width:canvasWidth+'px'}"
  10. >
  11. </canvas>
  12. </view>
  13. </template>
  14. <!-- 有项目需要开发的请联系 扣 - 371524845 -->
  15. <script>
  16. export default {
  17. props: {
  18. canvasHeight: {
  19. type: Number,
  20. default: 400,
  21. },
  22. canvasWidth: {
  23. type: Number,
  24. default: 400,
  25. },
  26. width: {
  27. type: Number,
  28. default: 80,
  29. },
  30. height: {
  31. type: Number,
  32. default: 50,
  33. },
  34. left: {
  35. type: Number,
  36. default: 300,
  37. },
  38. top: {
  39. type: Number,
  40. default: 320,
  41. },
  42. bgImage: {
  43. type: String,
  44. default: '',
  45. },
  46. },
  47. data(){
  48. return {
  49. ctx:null
  50. }
  51. },
  52. created() {
  53. //初始化画布
  54. this.ctx = wx.createCanvasContext('shareCanvas',this)
  55. },
  56. methods:{
  57. //获取图片的基本信息,即将网络图片转成本地图片,
  58. getImageInfo(src) {
  59. return new Promise((resolve, reject) => {
  60. wx.getImageInfo({
  61. src,
  62. success: (res) => resolve(res),
  63. fail: (res) => reject(res)
  64. })
  65. });
  66. },
  67. exportPost(image2){
  68. let that = this
  69. return new Promise(function (resolve, reject) {
  70. let image = that.bgImage
  71. //获取系统的基本信息,为后期的画布和底图适配宽高
  72. uni.getSystemInfo({
  73. success: function (res) {
  74. Promise.all([that.getImageInfo(image),that.getImageInfo(image2)]).then(res=>{
  75. //获取底图和二维码图片的基本信息,通常前端导出的二维码是base64格式的,所以要转成图片格式的才可以获取图片的基本信息
  76. that.ctx.drawImage(res[0].path,0 , 0,that.canvasWidth,that.canvasHeight);
  77. that.ctx.drawImage(res[1].path,that.left,that.top,that.width, that.height);
  78. that.ctx.draw(false,function(){
  79. wx.canvasToTempFilePath({
  80. x: 0,
  81. y: 0,
  82. width:that.canvasWidth,
  83. height:that.canvasHeight,
  84. destWidth:that.canvasWidth*2,//这里乘以2是为了保证合成图片的清晰度
  85. destHeight:that.canvasHeight*2,
  86. canvasId: 'shareCanvas',
  87. fileType:'jpg',//设置导出图片的后缀名
  88. success: function (res) {
  89. resolve(res.tempFilePath)
  90. },
  91. fail: function (res) {
  92. reject(res)
  93. },
  94. })
  95. });
  96. })
  97. }
  98. })
  99. })
  100. },
  101. },
  102. }
  103. </script>