小程序
小游戏
企业微信
微信支付
扫描小程序码分享
在2.9.4微信基础库 不行 在2.6.0基础库的时候又可以?
4 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
你好,请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
请问现在解决了吗
const { ccclass, property } = cc._decorator;
@ccclass
export default class Test extends cc.Component {
@property(cc.Camera)
camera: cc.Camera = null; //相机
@property(cc.Node)
sprite: cc.Node = null; //需要去截的图
drawsprite: cc.Node = null; //截图后放入该节点
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start() {
let gpTexture = new cc.RenderTexture(); //绘制
//@ts-ignore
gpTexture.initWithSize(cc.winSize.width, cc.winSize.height, cc.game._renderContext.STENCIL_INDEX8);
// gpTexture.initWithSize(this.sprite.width, this.sprite.height);
this.camera.targetTexture = gpTexture;//相机绘制,将屏幕上的内容更新到renderTexture中
this.camera.render(this.sprite);
let v: cc.Vec2 = this.node.convertToWorldSpaceAR(this.sprite.getPosition())
let x = v.x - this.sprite.width * this.sprite.anchorX;
let y = v.y - this.sprite.height * this.sprite.anchorY;
let data: Uint8Array;
// console.log("drawData",x,y,node.width,node.height);
data = gpTexture.readPixels(data, x, y, this.sprite.width, this.sprite.height);//读取renderTexture中的数据
// data = gpTexture.readPixels();//读取renderTexture中的数据
let canvas = this.createCanvasByDate(data, this.sprite.width, this.sprite.height);
let texture = new cc.Texture2D();
texture.initWithElement(canvas);
let spriteFrame = new cc.SpriteFrame();
spriteFrame.setTexture(texture);
this.drawsprite.getComponent(cc.Sprite).spriteFrame = spriteFrame;
}
//根据纹理data返回canvas
createCanvasByDate(data: Uint8Array, width: number, height: number) {
let canvas;
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
canvas = wx.createCanvas();
} else {
canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext('2d');
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);
return canvas;
// update (dt) {}
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
你好,请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
这种截图方法 得到的截图 Uint8Array 全是 0 就是空白图
请问现在解决了吗
const { ccclass, property } = cc._decorator;
@ccclass
export default class Test extends cc.Component {
@property(cc.Camera)
camera: cc.Camera = null; //相机
@property(cc.Node)
sprite: cc.Node = null; //需要去截的图
@property(cc.Node)
drawsprite: cc.Node = null; //截图后放入该节点
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start() {
let gpTexture = new cc.RenderTexture(); //绘制
//@ts-ignore
gpTexture.initWithSize(cc.winSize.width, cc.winSize.height, cc.game._renderContext.STENCIL_INDEX8);
// gpTexture.initWithSize(this.sprite.width, this.sprite.height);
this.camera.targetTexture = gpTexture;//相机绘制,将屏幕上的内容更新到renderTexture中
this.camera.render(this.sprite);
let v: cc.Vec2 = this.node.convertToWorldSpaceAR(this.sprite.getPosition())
let x = v.x - this.sprite.width * this.sprite.anchorX;
let y = v.y - this.sprite.height * this.sprite.anchorY;
let data: Uint8Array;
// console.log("drawData",x,y,node.width,node.height);
data = gpTexture.readPixels(data, x, y, this.sprite.width, this.sprite.height);//读取renderTexture中的数据
// data = gpTexture.readPixels();//读取renderTexture中的数据
let canvas = this.createCanvasByDate(data, this.sprite.width, this.sprite.height);
let texture = new cc.Texture2D();
texture.initWithElement(canvas);
let spriteFrame = new cc.SpriteFrame();
spriteFrame.setTexture(texture);
this.drawsprite.getComponent(cc.Sprite).spriteFrame = spriteFrame;
}
//根据纹理data返回canvas
createCanvasByDate(data: Uint8Array, width: number, height: number) {
let canvas;
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
canvas = wx.createCanvas();
} else {
canvas = document.createElement('canvas');
}
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext('2d');
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);
}
return canvas;
}
// update (dt) {}
}
你好,请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
这种截图方法 得到的截图 Uint8Array 全是 0 就是空白图