Canvas Haven的创建图像将附加到HTML容器

发布于 2025-01-26 00:23:32 字数 695 浏览 3 评论 0 原文

我有画布。现在,我想制作画布的图像。我已经开发了这个功能。我有一个与正确的SRC-Attribute的图像。但是,如果我想将此图像元素附加到html-container上,则它不起作用。什么都没有发生。

我不知道为什么。 HTML容量可用,图像正确。

这是一些代码:

创建

function createPicture(canvas, container){
    
    let dataURL = canvas.toDataURL({
        width: canvas.width,
        height: canvas.height,
        left: 0,
        top: 0,
        format: 'jpeg',
    });
    
    let image = new Image();
    image.setAttribute("src", dataURL);

    container.append(image);
}

呼叫此功能图像的图片的函数:

createPicture(canvas, $("#test"));

在控制台中

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD…">

I have a canvas. Now i want to make an image of the canvas. This function i already developed. I've got an image with the right src-attribute. But if i want to append this image-element to the html-container, it doesn't works. Nothing happens.

I don't know why. The html-container is available, the image is correct.

Here is some code:

Function to create the picture

function createPicture(canvas, container){
    
    let dataURL = canvas.toDataURL({
        width: canvas.width,
        height: canvas.height,
        left: 0,
        top: 0,
        format: 'jpeg',
    });
    
    let image = new Image();
    image.setAttribute("src", dataURL);

    container.append(image);
}

Calling this function

createPicture(canvas, $("#test"));

Image in the console:

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD…">

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

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

发布评论

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

评论(1

陌路终见情 2025-02-02 00:23:32

您的代码对我来说很好,请参阅下面的示例

function createPicture(canvas, container){
    let dataURL = canvas.toDataURL({
        width: canvas.width,
        height: canvas.height,
        left: 0, top: 0,
        format: 'jpeg',
    });
    
    let image = new Image();
    image.setAttribute("src", dataURL);
    container.append(image);
}

let co = document.getElementById("container")
let ca = document.getElementById("canvas")
var ctx = ca.getContext("2d");
ctx.rect(10, 10, 80, 60);
ctx.arc(50, 50, 30, 0, 4);
ctx.stroke();

createPicture(ca, co)
canvas { 
  border: 3px solid blue;
}
div { 
  border-color: red;
  border-style: solid;
  width: 100px;
  height: 100px;
}
<canvas id="canvas" width=100 height=100></canvas>
<div id="container"></div>

我唯一能想到的是,您将createPicture称为期望在那里的createpicture后在画布上做某事,如果您仍然遇到问题,我建议您创建一个代码代码来重现问题,并在此处将其发布在此处


检查您的开发控制台,确保您看到图像元素:

如果您看不到它,那么问题必须是容器

Your code works fine for me, see my sample below

function createPicture(canvas, container){
    let dataURL = canvas.toDataURL({
        width: canvas.width,
        height: canvas.height,
        left: 0, top: 0,
        format: 'jpeg',
    });
    
    let image = new Image();
    image.setAttribute("src", dataURL);
    container.append(image);
}

let co = document.getElementById("container")
let ca = document.getElementById("canvas")
var ctx = ca.getContext("2d");
ctx.rect(10, 10, 80, 60);
ctx.arc(50, 50, 30, 0, 4);
ctx.stroke();

createPicture(ca, co)
canvas { 
  border: 3px solid blue;
}
div { 
  border-color: red;
  border-style: solid;
  width: 100px;
  height: 100px;
}
<canvas id="canvas" width=100 height=100></canvas>
<div id="container"></div>

Only thing I could think of is you are doing something on your canvas after you call the createPicture an expecting it to be there, if you still run into problems I would recommend you to create a code snippet reproducing your problem and post it here


Check on your dev console, make sure you see the image element:

If you don't see it then the problem must be the container

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文