html
wx.createSelectorQuery()
.select("#myCanvas") // 在 WXML 中填入的 id
.fields({ node: true, size: true })
.exec((res) => {
const canvas = res[0].node;
const renderWidth = res[0].width;
const renderHeight = res[0].height;
const ctx = canvas.getContext("2d");
const dpr = wx.getWindowInfo().pixelRatio;
canvas.width = 187.5 * dpr;
canvas.height = 196 * dpr;
ctx.scale(dpr, dpr);
//第一个矩形,宽高是正常的
ctx.fillStyle = "#ff3435";
ctx.fillRect(0, 0, 187.5 * dpr, 196 * dpr);
//第二个矩形,预期是w335,h332,实际输出后的效果见附图
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(10, 15, 167.5 * dpr, 166 * dpr);
你把第二个矩形的宽高从小到大试一下就知道你的问题在哪里了
himage.src =
"https://sit-ie-uocs-core-shenzhen-xili1-oss.sit.sf-express.com/v1.2/AUTH_sit-IE-UOCS-CORE/sfosspublic001/applet/banner/1695028323388-DEEQAVToralPH1MW6KM%2BMhTE%2FuV7k%3D.png";
himage.onload = (res) => {
ctx.drawImage(himage, 10, 20, 30 * dpr, 5 * dpr);
};
wx.canvasToTempFilePath({
canvas: canvas,
success(res) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
console.log("ok");
},
});
},
});
大佬,就是我再画布上画了一张图,就是在画布上能显示,但是保存到本地的时候就没有了,也没有报错,不知道错在哪里
const imageCode = canvas.createImage();//加的
imageCode.src = this.data.imgPathSave[0];//加的
const himage = canvas.createImage();
himage.src =
"https://sit-ie-uocs-core-shenzhen-xili1-oss.sit.sf-express.com/v1.2/AUTH_sit-IE-UOCS-CORE/sfosspublic001/applet/banner/1695028323388-DEEQAVToralPH1MW6KM%2BMhTE%2FuV7k%3D.png";
himage.onload = (res) => {
ctx.drawImage(himage, 60.55, 28.9, 30 * dpr, 5 * dpr);
ctx.drawImage(imageCode, 60.55, 100, 30 * dpr, 5 * dpr);//加的
wx.canvasToTempFilePath({
canvas: canvas,
success(res) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
console.log("ok");
},
});
},
});
};