如题 是否有demo?
img标签和node的尺寸是否有一个正确比例?
已解决…
大佬
能发个demo嘛
大佬 发个demo学习学习
后边会发的
/**
* web识别/保存图片
*/
static webNodeToImg(nodeCapture: cc.Node, parent: cc.Node, imgH: string = "") {
let nodeCamera = new cc.Node();
nodeCamera.parent = parent;
let camera = nodeCamera.addComponent(cc.Camera);
let position = nodeCapture.getPosition();
let width = nodeCapture.width;
let height = nodeCapture.height;
camera.alignWithScreen = false;
camera.ortho = true;
camera.orthoSize = height / 2;
let texture = new cc.RenderTexture();
texture.initWithSize(width, height);
camera.targetTexture = texture;
// 创建画布
let canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext('2d');
nodeCapture.setPosition(cc.Vec2.ZERO);
camera.render(nodeCapture);
nodeCapture.setPosition(position);
let data = texture.readPixels();
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/png");
let img = document.createElement("img");
img.src = dataURL; //截图数据
img.id = "capture";
img.alt = "capture";
img.style.position = "absolute";
img.style.opacity = "0"
img.style.left = img.style.top = img.style.right = img.style.bottom = "0";
img.style.width = "100%"
img.style.height = imgH + "%"
if (imgH == "100") {
img.style.margin = "auto";
}
let divGame = document.getElementById("Cocos2dGameContainer");
divGame.appendChild(img);
nodeCamera.destroy();
}
3赞
mark!
不需要得时候记得删掉
该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。