使用 jQuery 选择画布内的图像

发布于 2024-12-26 04:25:38 字数 363 浏览 1 评论 0原文

我有以下 JavaScript 代码:

var canvas = document.getElementsByTagName('canvas')[0];
var ctx = canvas.getContext('2d');
var img = new Image;
var x;
img.onload = function(){ ctx.drawImage(img,0,0); };
img.src='img.svg';

执行上述代码后,我想使用 jQuery 选择 SVG 图像。为此,我编写了以下代码 $('svg'),但这不起作用。

有人可以帮助我选择图像吗?

非常感谢您的帮助!

I have following JavaScript code:

var canvas = document.getElementsByTagName('canvas')[0];
var ctx = canvas.getContext('2d');
var img = new Image;
var x;
img.onload = function(){ ctx.drawImage(img,0,0); };
img.src='img.svg';

After execution of the above code I would like to select the SVG-image with jQuery. To do this I wrote following code $('svg'), but this doesn't work.

Can somebody help me so that I can select the image?

Thank you very much for your help!

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

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

发布评论

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

评论(2

<逆流佳人身旁 2025-01-02 04:25:38

图像本身在 DOM 中不可用,您只是临时创建它以将其绘制到画布中。所以画布保存了图像的内容,图像本身并不在 DOM 中。尽管如此,您可以使用 $(img) 来获取图像,但对此元素的任何操作都不会显示在任何地方。

The image itself is not available in the DOM, you just created it temporarily to draw it into the canvas. So the canvas holds the content of the image, the image itself is not in the DOM. Nevertheless you can get a hold on the image using $(img), but any manipulation of this element will not show anywhere.

关于从前 2025-01-02 04:25:38

画布只是一个包含图像数据的位图,没有内置的诸如对象之类的抽象概念。您可以做的就是在画布上写入和读取。也就是说,只要没有不安全的(即来自其他域的)写入画布即可。不幸的是,当在画布上写入/绘制 SVG 时,它会被错误地视为不安全内容,并且您将无法再读取位图。

The canvas is just a bitmap containing image data, there are no built-in abstract concepts such as objects. What you can do is write to and read from the canvas. That is, as long as no unsecure (i.e. from other domains) are written to the canvas. Unfortunately when writing/drawing SVG to the canvas it's faulty considered to be unsecure content and you will no longer be able to read the bitmap.

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