使用 Jquery 将 HTML 编译/保存/导出为 PNG 图像

发布于 2024-11-06 17:59:05 字数 279 浏览 1 评论 0原文

我有一个包含多个变量的设置,用户可以更改该变量来影响元素的视觉表示。所有这些都是由 jquery 脚本控制的。如果有一种方法可以根据浏览器呈现的内容保存结果图像,那就太酷了。从用户角度来看,它与屏幕截图没有什么不同,尽管它只会捕获相关区域。

我有一个名为 Page Saver 的 FF 插件,它的功能几乎就是我想要的,但如果可能的话,可以使用 jquery 或常规 javascript。

我更多的是寻求提示,以及你们建议我去追求这样的功能的总体方向。我不想学习另一种语言来做到这一点,但如果我必须......

I have a set-up with multiple variables that users can alter that effect a visual representation of an element. All of this is controlled by jquery scripts. It would be cool if there was a way to save the resultant image as per what the browser renders. It'd be no different than a screencapture from a user perspective, though it would only capture the relevant area.

I have a plugin for FF called Page Saver, and it's functionality is pretty much what i am looking for, but with jquery or regular javascript if possible.

I'm more asking for tips, and a general direction that you guys would advise me to go in in order to pursue such functionality. I'd prefer not to learn another language to do this, but if i must...

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

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

发布评论

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

评论(2

所有深爱都是秘密 2024-11-13 17:59:06

编辑:此方法仅适用于 Firefox 扩展。

您可以使用 HTML5 canvas、Firefox 的 drawWindowtoDataURL 方法。例如:

var capture = function() {
  var root = document.documentElement;
  var canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'html:canvas');
  var context = canvas.getContext('2d');
  var selection = {
    top: 0,
    left: 0,
    width: root.scrollWidth,
    height: root.scrollHeight,
  };

  canvas.height = selection.height;
  canvas.width = selection.width;

  context.drawWindow(
    window,
    selection.left,
    selection.top,
    selection.width,
    selection.height,
    'rgb(255, 255, 255)'
  );

  return canvas.toDataURL('image/png', '');
};

您可以调整 topleftwidthheight 以仅捕获网页的一部分。

结果是一个 数据 URI 字符串。您可以将其发送到您的服务器或将其绘制在另一个画布上:

  var canvas = document.getElementById('captured');
  var ctx = canvas.getContext('2d');
  var image = new Image();
  image.src = capture();

  // the image is not immediately usable
  $(image).load(function() { // jquery way
    canvas.width = image.width;
    canvas.height = image.height;
    ctx.drawImage(image, 0, 0);
  });

您的插件可能使用此方法。您还可以检查其源代码。

编辑:要使用 JQuery 将其发送到服务器,您可以执行以下操作:

$("#send-capture-button").click(function() {
  $.post("/url-to-send-image-to", {image_data: capture()})
});

在服务器端,您必须对数据 URL 进行解码。

Edit : This method only works in Firefox extensions.

You can use HTML5 canvas, Firefox' drawWindow and the toDataURL method. For example:

var capture = function() {
  var root = document.documentElement;
  var canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'html:canvas');
  var context = canvas.getContext('2d');
  var selection = {
    top: 0,
    left: 0,
    width: root.scrollWidth,
    height: root.scrollHeight,
  };

  canvas.height = selection.height;
  canvas.width = selection.width;

  context.drawWindow(
    window,
    selection.left,
    selection.top,
    selection.width,
    selection.height,
    'rgb(255, 255, 255)'
  );

  return canvas.toDataURL('image/png', '');
};

You can adjust top, left, width and height to capture only a part of the web page.

The result is a data URI string. You can send it to your server or draw it on another canvas:

  var canvas = document.getElementById('captured');
  var ctx = canvas.getContext('2d');
  var image = new Image();
  image.src = capture();

  // the image is not immediately usable
  $(image).load(function() { // jquery way
    canvas.width = image.width;
    canvas.height = image.height;
    ctx.drawImage(image, 0, 0);
  });

Your plugin probably uses this method. You can also check its source code.

Edit: To send it to your server with JQuery you can do something like that:

$("#send-capture-button").click(function() {
  $.post("/url-to-send-image-to", {image_data: capture()})
});

On the server side you'll have to decode the data URL.

我不是你的备胎 2024-11-13 17:59:06

遗憾的是,浏览器中没有内置 JS 功能,可以让您将图像转换为 HTML。因此,您需要安装一个浏览器插件或扩展程序,其中包含可以调用的 JS API。但即便如此,显然也需要在该人的浏览器上安装插件。

There is no JS functionality built into browsers that would allow you turn an image into HTML sadly. So you would need something like a browser plugin or extension installed that has a JS API that you can call into. But even then, it would obviously require that plugin be installed on that persons browser.

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