// this.capture()
// this.captureAndShow()
// this.saveFile('ttt')
capture () {
let camera = this.camera = cc.find('Canvas/Camera').getComponent(cc.Camera);
// 设置你想要的截图内容的 cullingMask
camera.cullingMask = 0xffffffff;
// 新建一个 RenderTexture,并且设置 camera 的 targetTexture 为新建的 RenderTexture,这样 camera 的内容将会渲染到新建的 RenderTexture 中。
let texture = new cc.RenderTexture();
this.texture = texture ;
let gl = cc.game._renderContext;
// 如果截图内容中不包含 Mask 组件,可以不用传递第三个参数
texture.initWithSize(cc.visibleRect.width, cc.visibleRect.height, gl.STENCIL_INDEX8);
camera.targetTexture = texture;
let width = this.texture.width;
let height = this.texture.height;
if (!this.canvas) {
this.canvas = document.createElement('canvas');
this.canvas.width = width;
this.canvas.height = height;
}
let ctx = this.canvas.getContext('2d');
this.camera.render();
let data = this.texture.readPixels();
// write the render 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);
}
var dataURL = this.canvas.toDataURL("image/jpeg");
var img = document.createElement("img");
img.src = dataURL;
return img;
},
//需要显示时 调用此方法
captureAndShow () {
var img = this.capture();
// You can save the image or show it.
// img.style.position = 'absolute';
// img.style.display = 'block';
// img.style.left = '0px'
// img.style.top = '0px';
// img.zIndex = 100;
// img.style.transform = cc.game.container.style.transform;
// img.style['transform-origin'] = cc.game.container.style['transform-origin'];
// img.style.margin = cc.game.container.style.margin;
// img.style.padding = cc.game.container.style.padding;
// img.onclick = function (event) {
// event.stopPropagation();
// img.remove();
// }
// document.body.appendChild(img);
let texture = new cc.Texture2D();
texture.initWithElement(img);
let spriteFrame = new cc.SpriteFrame();
spriteFrame.setTexture(texture);
let node = new cc.Node();
node.parent = cc.director.getScene();
node.zIndex = cc.macro.MAX_ZINDEX;
let sprite = node.addComponent(cc.Sprite);
sprite.spriteFrame = spriteFrame;
node.x = cc.winSize.width/2;
node.y = cc.winSize.height/2;
node.scaleX = 1/2
node.scaleY = 1/2
//测试用 点击销毁图片
node.on(cc.Node.EventType.TOUCH_START, () => {
node.destroy();
});
},
saveFile ( fileName){
//保存的方法前端有很多,这里暂时用这个
let data =this.canvas.toDataURL("image/png");
let save_link = document.createElement('a');
save_link.href = data;
save_link.download = fileName;
let event = document.createEvent('MouseEvents');
event.initEvent("click", true, false);
save_link.dispatchEvent(event);
},
原文 https://blog.csdn.net/qq_20173207/article/details/85522653
https://blog.csdn.net/yzx5452830/article/details/83446310
https://github.com/Leo501/CocosCreatorTutorial/blob/master/Screenshot/assets/Script/Screenshot.js
如果截图后背景是黑色的可以参考下面把camera设置background为(0,0,0,0)
https://www.jianshu.com/p/e5a8ffb0fd77
去掉背景,截取某一个node的test
captureToNode () {
var node = this.node
var my = node.getChildByName('my')
var bg = my.getChildByName("bg")
var mmm = my.getChildByName("mmm")
var size = cc.winSize
cc.log(size)
var mmmSize = cc.size(mmm.width*mmm.scaleX, mmm.height*mmm.scaleY)
mmmSize.x = mmm.x - mmm.width*mmm.anchorX*mmm.scaleX
mmmSize.y = mmm.y - mmm.height*mmm.anchorY*mmm.scaleY
cc.log(mmmSize)
bg.active = false
var img = util.capture()
bg.active = true
var texture = new cc.Texture2D();
texture.initWithElement(img);
var spriteFrame = new cc.SpriteFrame();
spriteFrame.setTexture(texture);
spriteFrame.setRect(cc.rect(mmmSize.x,size.height - mmmSize.y - mmmSize.height,mmmSize.width,mmmSize.height));
var node = new cc.Node();
var sprite = node.addComponent(cc.Sprite);
sprite.spriteFrame = spriteFrame;
return node
},
var node = this.captureToNode()
node.parent = cc.director.getScene();
node.zIndex = cc.macro.MAX_ZINDEX;
node.setAnchorPoint(0, 0);
node.setScale(2, 2);