多个叠加,右键保存只保存一个

发布于 2025-01-09 08:07:55 字数 945 浏览 0 评论 0原文

新人,正在尝试。我试图通过堆叠随机选择的图层在 html/javascript 中创建单个图像(请参见下面的代码)。但是,当我右键单击图像时,它仅保存顶层,而不保存所有图层。我该如何解决这个问题(如果不使用任何库,则可以使用按钮 - 屏幕截图也一样)?

非常感谢!

船长

//html
<div><div id="layers"></div>

<canvas id="canvas1"></canvas>

// script

//create and attach layer1 to body;
            var img = document.createElement('img');
            img.alt = "Layer1";
            img.src = layer1_array[getRandomInt(0,3)];
            img.setAttribute("id","myImg");
            img.style.decoding = "async";
            document.getElementById("layers").appendChild(img);


//create and attach layer1 to body;
            var img = document.createElement('img');
            img.alt = "Layer2";
            img.src = layer2_array[getRandomInt(0,3)];
            img.setAttribute("id","myImg");
            img.style.decoding = "async";
            document.getElementById("layers").appendChild(img);

Newbie here, experimenting. I'm trying to create in html/javascript a single image by stacking randomly selected layers (see code below). However, when I right-click the image it only saves the top layer, not all layers. How do I fix this (a button would be possible if it does not use any libraries -same goes for screenshots)?

Many thanks in advance!

Skipper

//html
<div><div id="layers"></div>

<canvas id="canvas1"></canvas>

// script

//create and attach layer1 to body;
            var img = document.createElement('img');
            img.alt = "Layer1";
            img.src = layer1_array[getRandomInt(0,3)];
            img.setAttribute("id","myImg");
            img.style.decoding = "async";
            document.getElementById("layers").appendChild(img);


//create and attach layer1 to body;
            var img = document.createElement('img');
            img.alt = "Layer2";
            img.src = layer2_array[getRandomInt(0,3)];
            img.setAttribute("id","myImg");
            img.style.decoding = "async";
            document.getElementById("layers").appendChild(img);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我也只是我 2025-01-16 08:07:55
<script>
    const canvas = document.querySelector('canvas');
    const ctx = canvas.getContext("2d");

    const arrayImage = ["./img/img1.png", "./img/img2.png", "./img/img3.png"];

    const innerImage = (index) => {
        const image = new Image(canvas.width, canvas.height);
        image.addEventListener("load", () => {
            ctx.drawImage(image, 0, 0);
        });
        image.src = arrayImage[index];
    };
    innerImage(0);
    innerImage(2);
    innerImage(1);


</script>
<script>
    const canvas = document.querySelector('canvas');
    const ctx = canvas.getContext("2d");

    const arrayImage = ["./img/img1.png", "./img/img2.png", "./img/img3.png"];

    const innerImage = (index) => {
        const image = new Image(canvas.width, canvas.height);
        image.addEventListener("load", () => {
            ctx.drawImage(image, 0, 0);
        });
        image.src = arrayImage[index];
    };
    innerImage(0);
    innerImage(2);
    innerImage(1);


</script>
岁月如刀 2025-01-16 08:07:55
(async () => {
    innerImage(0);
    innerImage(1);
    innerImage(2);
})()
(async () => {
    innerImage(0);
    innerImage(1);
    innerImage(2);
})()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文