let canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext('2d');
let gpTexture = new cc.RenderTexture(); //绘制
//@ts-ignore
gpTexture.initWithSize(width, height, cc.game._renderContext.STENCIL_INDEX8);
this.gpCamera.targetTexture = gpTexture;
this.gpCamera.render(this.node);//相机绘制,将屏幕上的内容更新到renderTexture中
let data = gpTexture.readPixels();//读取renderTexture中的数据
// data = this.changeAlpha(data);
let rowBytes = width * 4;
for (let row = 0; row < height; row++) {
let srow = height - 1 - row;
let imageData = ctx.createImageData(width, 1);
let start = srow * width * 4;
for (let i = 0; i < rowBytes; i++) {
imageData.data[i] = data[start + i];
}
ctx.putImageData(imageData, 0, row);
}
let _tempFilePath = await canvas.toTempFilePathSync(
{
x: 0,
y: 0,
width: width,
height: height,
destWidth: 380,
destHeight: 380,
fileType: 'png',
quality: 1
});
wx.previewImage({
urls: [_tempFilePath]
})
您好,请问您的预期是什么,能否提供个可运行的代码片段,这边复现一下看看(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
const {ccclass, property} = cc._decorator;
@ccclass
export default class TD extends cc.Component {
@property(cc.Camera)
spC: cc.Camera = null as any;
// onLoad () {}
async jietu () {
let width = this.node.width;
let height = this.node.height;
//截图的本质是创建一个canvas,然后通过canvas生成图片材质
this.spC.node.x = 0;
this.spC.node.y = 0;
let canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext('2d');
let gpTexture = new cc.RenderTexture(); //绘制
//@ts-ignore
gpTexture.initWithSize(width, height, cc.game._renderContext.STENCIL_INDEX8);
this.spC.targetTexture = gpTexture;
this.spC.render(this.node);//相机绘制,将屏幕上的内容更新到renderTexture中
let data = gpTexture.readPixels();//读取renderTexture中的数据
let rowBytes = width * 4;
for (let row = 0; row < height; row++) {
let srow = height - 1 - row;
let imageData = ctx.createImageData(width, 1);
let start = srow * width * 4;
for (let i = 0; i < rowBytes; i++) {
imageData.data[i] = data[start + i];
}
ctx.putImageData(imageData, 0, row);
}
if (CC_WECHATGAME) {
//@ts-ignore
let _tempFilePath = await canvas.toTempFilePathSync(
{
x: 0,
y: 0,
width: width,
height: height,
destWidth: 380,
destHeight: 380,
fileType: 'png',
quality: 1
});
//显示图片
wx.previewImage({
urls: [_tempFilePath]
})
}
}
// update (dt) {}
}
我是要对某个节点截图 但是要截图的部分 透明区域应该是透明的才对 但是现在显示的图片 透明区域是黑色的