如何在 Javascript 中从屏幕的某个区域创建图像?

发布于 2024-11-27 16:30:08 字数 152 浏览 0 评论 0原文

我有一个网络应用程序,用户可以从一组用户界面组件中组装出一个用户界面(例如另一个网页),并且我想提供创建用户所创建内容的图像快照的选项。通过这种方式,我希望能够使用 JS(例如,使用 jQuery)以编程方式选择浏览器可显示区域的一个区域,并从该区域创建一个位图。

谢谢你!

I have a web app where a user can assemble a user interface (such as another webpage) out of some set of user interface components and I want to provide the option to create an image snapshot of what the user has created. In this way I want to be able to programmatically select, using JS (for example, using jQuery) a region of the browser's displayable area and create a bitmap from this region.

Thank you!

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

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

发布评论

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

评论(4

刘备忘录 2024-12-04 16:30:08

也许这可以帮助您:http://html2canvas.hertzen.com/

Maybe this can help you: http://html2canvas.hertzen.com/

瀞厅☆埖开 2024-12-04 16:30:08

直接的 JavaScript,不太可能......但你没有提到你的整体“设置”,所以很难确定。执行您所描述内容的各个方面的工具。

PhantomJS - 带有 JavaScript API 的无头 WebKit。对 DOM 处理、CSS 选择器、JSON、Canvas 和 SVG 的本机支持。用于测试基于 Web 的应用程序、网站抓取、页面捕获、SVG 渲染器、PDF 转换器等。

Zombie.js - 使用 Node.js 进行“极其快速”的无头全栈测试。 Zombie.js 是一个轻量级框架,用于在模拟环境中测试客户端 JavaScript 代码。无需浏览器。

webkit2png - 是一个命令行工具,用于创建网页的 png 屏幕截图。

Straight JavaScript, unlikely.. But you don't mention your overall "setup", so it's hard to know for sure. Tools that perform aspects of what you are describing..

PhantomJS - headless WebKit with JavaScript API. Native support for DOM handling, CSS selector, JSON, Canvas, and SVG. For testing of web-based applications, site scraping, pages capture, SVG renderer, PDF converter, etc.

Zombie.js - "Insanely fast", headless full-stack testing using Node.js. Zombie.js is a lightweight framework for testing client-side JavaScript code in a simulated environment. No browser required.

webkit2png - is a command line tool that creates png screenshots of webpages.

拥醉 2024-12-04 16:30:08

我设法通过使用 HTML2Canvas 拍摄整个页面的快照并将画布裁剪为正确的大小来实现这一点。

const x = 100;
const y = 200;
const width = 200;
const height = 200;

// get the whole HTML into a canvas
html2canvas(document.querySelector('html')).then(canvas => {
    const context = canvas.getContext('2d');

    // crop the image
    context.getImageData(x, y, width, height);

    // add the copied 
    const tempCanvas = document.createElement('canvas');
    const tempContext = tempCanvas.getContext('2d');

    tempCanvas.width = width;
    tempCanvas.height = height;

    tempContext.drawImage(canvas, 0, 0);

    // load the new canvas 
    const img = tempCanvas.toDataURL('image/png');

    /*
     * ...
     * you can use the img
     * ...
     * ...
     */
});

I managed to make this happen with taking a snapshot of the whole page with HTML2Canvas and cropping canvas to the right size.

const x = 100;
const y = 200;
const width = 200;
const height = 200;

// get the whole HTML into a canvas
html2canvas(document.querySelector('html')).then(canvas => {
    const context = canvas.getContext('2d');

    // crop the image
    context.getImageData(x, y, width, height);

    // add the copied 
    const tempCanvas = document.createElement('canvas');
    const tempContext = tempCanvas.getContext('2d');

    tempCanvas.width = width;
    tempCanvas.height = height;

    tempContext.drawImage(canvas, 0, 0);

    // load the new canvas 
    const img = tempCanvas.toDataURL('image/png');

    /*
     * ...
     * you can use the img
     * ...
     * ...
     */
});
—━☆沉默づ 2024-12-04 16:30:08

也许wkhtmltopdf会起作用,他们有一个似乎处于测试阶段的“to image”。然而,这需要的不仅仅是 JavaScript 才能完成,其他方法也是如此。

Maybe wkhtmltopdf would work, they have a 'to image' that appears to be in beta. This however requires more than just javascript to accomplish, as do other methods.

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