收藏
回答

Wxml2Canvas有的手机把图片下载到手机后发现背景色全是黑色,但是文字都能显示,咋回事?有点急

import Wxml2Canvas from 'wxml2canvas';

export default {

methods: {

drawMyCanvas() {

uni.showLoading({

title: '下载中...'

});

const that = this;

const query = uni.createSelectorQuery().in(this);

query

.select('#my-canvas')

.fields(

{

// 选择需要生成canvas的范围

size: true,

scrollOffset: true

},

(data) => {

let width = data.width;

let height = data.height;

that.width = width;

that.height = height;

setTimeout(() => {

that.startDraw();

}, 1500);

}

)

.exec();

},

startDraw() {

console.log(2);

let that = this;

// 创建wxml2canvas对象

let drawMyImage = new Wxml2Canvas(

{

element: 'myCanvas', // canvas的id,

obj: that, // 传入当前组件的this

width: that.width * 2,

height: that.height * 2,

progress(percent) {

// 进度

// console.log(percent);

},

finish(url) {

console.log(url);

// 生成的图片

uni.hideLoading();

setTimeout(() => {

that.savePoster(url);

}, 500);

},

error(res) {

// 失败原因

console.log(res);

uni.hideLoading();

}

},

this

);

let data = {

// 获取wxml数据

list: [

{

type: 'wxml',

class: '.my_canvas .my_draw_canvas', // my_canvas要绘制的wxml元素根类名, my_draw_canvas单个元素的类名(所有要绘制的单个元素都要添加该类名)

limit: '.my_canvas', // 要绘制的wxml元素根类名

x: 0,

y: 0

}

]

};

// 绘制canvas

drawMyImage.draw(data, this);

},

savePoster(imgUrl) {

console.log(3, imgUrl);

const that = this;

uni.saveImageToPhotosAlbum({

filePath: imgUrl,

success: function () {

uni.showToast({

title: '保存成功',

icon: 'none',

duration: 1500

});

},

fail(err) {

if (

err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' ||

err.errMsg === 'saveImageToPhotosAlbum:fail auth deny' ||

err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response'

) {

uni.showModal({

title: '提示',

content: '需要您授权保存相册',

showCancel: false,

success: (modalSuccess) => {

uni.openSetting({

success(settingdata) {

if (settingdata.authSetting['scope.writePhotosAlbum']) {

uni.saveImageToPhotosAlbum({

filePath: imgUrl,

success: function () {

uni.showToast({

title: '保存成功',

icon: 'success',

duration: 2000

});

}

});

} else {

uni.showToast({

title: '授权失败,请稍后重新获取',

icon: 'none',

duration: 1500

});

}

}

});

}

});

}

}

});

}

}

};

回答关注问题邀请回答
收藏

2 个回答

  • Demons
    Demons
    11-12

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    11-12
    有用
    回复
  • ㅤ
    11-11

    每个要绘制的子元素都需要加my_draw_canvas类,文字部分给标签加上data-type="text" data-text="测试文本",图片加data-type="image" data-url="https://测试链接",图片的data-url需要外网能访问到。

    11-11
    有用
    回复 3
    • Brother
      Brother
      11-11
      我的标签上都有这些,刚才自研解决了,只不过效果不是很理想,谢谢
      11-11
      回复
    • Brother
      Brother
      11-11
      目前是没法高度根据内容自适应,就很难受
      11-11
      回复
    • Brother
      Brother
      发表于移动端
      11-11
      上面问题都解决了,但是左右边框不显示了,而且样式明显错乱
      11-11
      回复
登录 后发表内容