Cocos Creator 截屏方法
function screenshot() {
let node = cc.director.getScene().getChildByName("Canvas").getChildByName("screenshot");
let camera = node?.getComponent(cc.Camera);
if(!node){
node = new cc.Node("screenshot");
node.parent = cc.director.getScene().getChildByName("Canvas");
camera = node.addComponent(cc.Camera);
}
camera.cullingMask = 0xffffffff;
let texture = new cc.RenderTexture();
texture.initWithSize(cc.visibleRect.width, cc.visibleRect.height);
camera.targetTexture = texture;
camera.render();
let data = texture.readPixels();
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
canvas.width = texture.width;
canvas.height = texture.height;
let width = texture.width;
let height = texture.height;
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 dataURL = canvas.toDataURL("image/jpeg");
let img = document.createElement("img");
img.src = dataURL;
return img;
}
浏览器控制台打印图片方法
function setConsoleImage(url) {
var img = new Image();
img.style.display = "none";
document.body.appendChild(img);
img.src = url;
img.onload = function () {
var css = [
"padding: " + (img.height / 2 - 8) + "px " + img.width / 2 + "px;",
"line-height: " + img.height + "px;",
"background-image: url(" + url + ");",
];
console.log("%c ", css.join(""));
};
}
|