[0]Laya2.x3D截图并保存到相册功能

 saveFunc(picUrl_) {
    Config.preserveDrawingBuffer = true;
    var picUrl = picUrl_; 
    // 动态计算截图尺寸和位置
    var screenshotWidth = Laya.Browser.clientWidth * 0.7; // 70%的屏幕宽度
    var screenshotHeight = Laya.Browser.clientHeight * 0.6; // 60%的屏幕高度
    var screenshotLeft = (Laya.Browser.clientWidth - screenshotWidth) / 2; // 居中
    var screenshotTop = (Laya.Browser.clientHeight - screenshotHeight) / 2; // 居中 
    // 创建图片元素并设置样式
    var imgOutput = Laya.Browser.document.createElement("img");
    imgOutput.style.position = "absolute";
    imgOutput.style.zIndex = Laya.Render.canvas.style.zIndex + 1;
    imgOutput.style.left = screenshotLeft + "px";
    imgOutput.style.top = screenshotTop + "px";
    imgOutput.style.width = screenshotWidth + "px";
    imgOutput.style.height = screenshotHeight + "px";
    imgOutput.src = picUrl; 
    // 动态计算提示位置
    var tipLeft = screenshotLeft + screenshotWidth * 0.2 + 20; // 水平居中
    var tipTop = screenshotTop + screenshotHeight - 38; // 距离截图底部 20 像素 
    // 创建提示元素并设置样式
    var tipElement = Laya.Browser.document.createElement("div");
    tipElement.style.position = "absolute";
    tipElement.style.zIndex = Laya.Render.canvas.style.zIndex + 2;
    tipElement.style.left = tipLeft + "px";
    tipElement.style.top = tipTop + "px";
    tipElement.style.color = "white";
    tipElement.style.fontFamily = "Arial";
    tipElement.style.fontSize = "20px"; // 文字大小为20
    tipElement.style.textAlign = "center"; // 居中对齐
    tipElement.innerText = "长按截图保存"; 
    // 创建关闭按钮并设置样式
    var closeButton = Laya.Browser.document.createElement("button");
    closeButton.style.position = "absolute";
    closeButton.style.zIndex = Laya.Render.canvas.style.zIndex + 2;
    closeButton.style.top = screenshotTop + 5 + "px";
    closeButton.style.right = Laya.Browser.clientWidth - screenshotLeft - screenshotWidth + 5 + "px";
    closeButton.style.background = "url(res/bubbleUI/x.png)";
    closeButton.style.backgroundSize = "contain";
    closeButton.style.width = "35px";
    closeButton.style.height = "35px";
    closeButton.style.border = "none";
    closeButton.onclick = function () {
      // 移除图片元素和按钮
      Laya.Browser.document.body.removeChild(imgOutput);
      Laya.Browser.document.body.removeChild(closeButton);
      Laya.Browser.document.body.removeChild(saveButton);
      Laya.Browser.document.body.removeChild(tipElement);
    }; 
    // 创建保存按钮并设置样式
    var saveButton = Laya.Browser.document.createElement("button");
    saveButton.innerText = "保存图片";
    saveButton.style.position = "absolute";
    saveButton.style.zIndex = Laya.Render.canvas.style.zIndex + 2;
    saveButton.style.left = "50%";
    saveButton.style.bottom = "40px";
    saveButton.style.transform = "translateX(-50%)";
    saveButton.onclick = function () {
      // 创建一个链接并设置下载属性
      var downloadLink = Laya.Browser.document.createElement("a");
      downloadLink.href = picUrl;
      downloadLink.download = "img.png";
      downloadLink.click();
    }; 
    // 将图片元素、提示和按钮添加到页面
    Laya.Browser.document.body.appendChild(imgOutput);
    Laya.Browser.document.body.appendChild(tipElement);
    Laya.Browser.document.body.appendChild(saveButton);
    Laya.Browser.document.body.appendChild(closeButton);
  }
3D截图并保存到相册.png
已邀请:

phhh

赞同来自:

感谢分享

135*****336

赞同来自:

感谢分享~

要回复问题请先

商务合作
商务合作