简体   繁体   English

Javascript-从画布元素下的图像获取像素数据?

[英]Javascript - get pixel data from image under canvas element?

You can sample pixel color data on a canvas image with getImageData(). 您可以使用getImageData()在画布图像上采样像素颜色数据。 If I layer a transparent canvas element over a background image or flash movie, can I sample a color from that via the canvas tag? 如果我在背景图像或Flash电影上叠加了透明的画布元素,是否可以通过canvas标签从其中采样颜色?

Phil's right, you can't do it. 菲尔是对的,你做不到。 Canvas, by design, only returns its own intrinsic pixel data, not any on-screen rendered content originating from other elements. 根据设计,画布仅返回其自身的固有像素数据,而不返回任何其他元素在屏幕上呈现的内容。

You could make a canvas element that covers the entire page and captures an image of the rendered screen. 您可以制作一个canvas元素来覆盖整个页面并捕获渲染屏幕的图像。

If possible, that would be a cross-site-scripting security hole. 如果可能的话,那将是一个跨站点脚本的安全漏洞。 You could iframe a target site (leveraging the users cookies/authentication/intranet to access it), then suck out the content from it pixel by pixel. 您可以对目标站点进行构架(利用用户的cookie /身份验证/内联网访问它),然后逐个像素地从目标站点中吸取内容。

I think not. 我觉得不是。 You would get the colour of the canvas at that point. 届时您将获得画布的颜色。 I did find the method document.defaultview.getComputedStyle(element,pseudoElt) , but on a 1 pixel div with no background-color, it returned 'transparent'. 我确实找到了方法document.defaultview.getComputedStyle(element,pseudoElt) ,但是在没有背景色的1像素div上,它返回了“透明”。

So, unfortunately, this may be impossible at present. 因此,不幸的是,目前这可能是不可能的。 What is your higher-level aim? 您的更高目标是什么? We may be able to come up with an alternative route. 我们也许可以提出一条替代路线。

"I had an idea earlier to copy image data into a javascript object. You could make a canvas element that covers the entire page and captures an image of the rendered screen. Once you have that data, you could do real-world cross browser testing, perhaps." “我之前有一个将图像数据复制到javascript对象中的想法。您可以制作一个canvas元素来覆盖整个页面并捕获呈现的屏幕图像。一旦有了这些数据,就可以进行真实世界的跨浏览器测试, 也许。”

If you don't mind that it's a static image, there is a great website that does this for you: Browser Shots . 如果您不介意这是静态图片,那么可以使用一个很棒的网站: Browser Shots It generates screenshots of your website running in a huge range of different browsers. 它生成在各种不同浏览器中运行的网站的屏幕截图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM