使用(?)语言将元素的内容绘制到画布元素/捕获网站作为图像

发布于 2024-11-08 07:26:55 字数 290 浏览 0 评论 0原文

我问了一个关于从 HTML 编译图像文件的问题。 Michaël Witrant 回复并告诉我有关 canvas 元素和 html5 的信息。

我在网上查看过,但是我没有找到任何有关将杂项元素的内容绘制到画布上的内容。这可能吗?

例如,假设我有一个带有背景图像的 div。有没有办法让这个元素及其背景图像“到”画布上?我问这个问题是因为我找到了一个脚本,允许将画布元素保存为 PNG,但我真正想做的是将 DOM 元素集合保存为图像。

编辑

无论什么语言,只要它能工作,我都愿意尝试。

I asked a question on SO about compiling an image file from HTML. Michaël Witrant responded and told me about the canvas element and html5.

I'm looked on the net and SO, but i haven't found anything regarding drawing a misc element's contents onto a canvas. Is this possible?

For example, say i have a div with a background image. Is there a way to get this element and it's background image 'onto' the canvas? I ask because i found a script that allows one to save the canvas element as a PNG, but what i really want to do is save a collection of DOM elements as an image.

EDIT

It doesn't matter what language, if it could work, i'm willing to attempt it.

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

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

发布评论

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

评论(3

猫弦 2024-11-15 07:26:55

郑重声明,drawWindow 仅适用于 Firefox。

该代码只能在本地运行,不能在互联网上运行,使用带有外部元素的drawWindow会产生安全异常。

在我们回答其他问题之前,您必须向我们提供更多背景信息。

For the record, drawWindow only works in Firefox.

This code will only work locally and not on the internet, using drawWindow with an external element creates a security exception.

You'll have to provide us with a lot more context before we can answer anything else.

我最亲爱的 2024-11-15 07:26:55

http://cutycapt.sourceforge.net/

CutyCapt 是一个命令行实用程序,使用 Webkit 将 HTML 渲染为 PNG 、PDF、SVG 等。您需要以某种方式与它交互(例如 PHP 中的 shell_exec),但它非常强大。站点的呈现方式与 Webkit 浏览器中的呈现方式完全相同。

我没有专门使用过 CutyCapt,但我强烈推荐它。我也使用过一款名为 WkHtmlToPdf 的类似产品,就我个人的体验而言,该产品非常棒。

http://cutycapt.sourceforge.net/

CutyCapt is a command line utility that uses Webkit to render HTML into PNG, PDF, SVG, etc. You would need to interface with it somehow (such as a shell_exec in PHP), but it is pretty robust. Sites render exactly as they do in Webkit browsers.

I've not used CutyCapt specifically, but it came to me highly recommended. And I have used a similar product called WkHtmlToPdf, which has been awesome in my personal experience.

幻梦 2024-11-15 07:26:55

经过多次尝试使用 drawWindow 参数绘制错误的部分或元素后,我设法通过两个步骤处理来完成此操作:首先在画布中捕获整个页面,然后在另一个画布中绘制该画布的一部分。

这是在 XUL 扩展中完成的。由于安全原因,drawWindow 无法在其他浏览器中工作,并且可能无法在非特权上下文中工作。

function nodeScreenshot(aSaveLocation, aFileName, aDocument, aCSSSelector) {
  var doc = aDocument;
  var win = doc.defaultView;
  var body = doc.body;
  var html = doc.documentElement;

  var selection = aCSSSelector
    ? Array.prototype.slice.call(doc.querySelectorAll(aCSSSelector))
    : [];

  var coords = {
    top: 0,
    left: 0,
    width: Math.max(body.scrollWidth, body.offsetWidth, 
                      html.clientWidth, html.scrollWidth, html.offsetWidth),
    height: Math.max(body.scrollHeight, body.offsetHeight, 
                       html.clientHeight, html.scrollHeight, html.offsetHeight)

  var canvas = document.createElement("canvas");

  canvas.width = coords.width;
  canvas.height = coords.height;

  var context = canvas.getContext("2d");
  // Draw the whole page
  // coords.top and left are 0 here, I tried to pass the result of
  // getBoundingClientRect() here but drawWindow was drawing another part,
  // maybe because of a margin/padding/position ? Didn't solve it.
  context.drawWindow(win, coords.top, coords.left,
                     coords.width, coords.height, 'rgb(255,255,255)');

  if (selection.length) {
    var nodeCoords = selection[0].getBoundingClientRect();
    var tempCanvas = document.createElement("canvas");
    var tempContext = tempCanvas.getContext("2d");

    tempCanvas.width = nodeCoords.width;
    tempCanvas.height = nodeCoords.height;
    // Draw the node part from the whole page canvas into another canvas
    // void ctx.drawImage(image, sx, sy, sLargeur, sHauteur,
                                 dx, dy, dLargeur, dHauteur)
    tempContext.drawImage(canvas,
      nodeCoords.left, nodeCoords.top, nodeCoords.width, nodeCoords.height,
      0, 0, nodeCoords.width, nodeCoords.height);

    canvas = tempCanvas;
    context = tempContext;
  }

  var dataURL = canvas.toDataURL('image/jpeg', 0.95);
  return dataURL;
}

After many attempts using drawWindow parameters, that were drawing wrong parts or the element, I managed to do it with a two steps processing : first capture the whole page in a canvas, then draw a part of this canvas in another one.

This was done in a XUL extension. drawWindow will not work in other browsers, and may not work in a non-privileged context due to security reasons.

function nodeScreenshot(aSaveLocation, aFileName, aDocument, aCSSSelector) {
  var doc = aDocument;
  var win = doc.defaultView;
  var body = doc.body;
  var html = doc.documentElement;

  var selection = aCSSSelector
    ? Array.prototype.slice.call(doc.querySelectorAll(aCSSSelector))
    : [];

  var coords = {
    top: 0,
    left: 0,
    width: Math.max(body.scrollWidth, body.offsetWidth, 
                      html.clientWidth, html.scrollWidth, html.offsetWidth),
    height: Math.max(body.scrollHeight, body.offsetHeight, 
                       html.clientHeight, html.scrollHeight, html.offsetHeight)

  var canvas = document.createElement("canvas");

  canvas.width = coords.width;
  canvas.height = coords.height;

  var context = canvas.getContext("2d");
  // Draw the whole page
  // coords.top and left are 0 here, I tried to pass the result of
  // getBoundingClientRect() here but drawWindow was drawing another part,
  // maybe because of a margin/padding/position ? Didn't solve it.
  context.drawWindow(win, coords.top, coords.left,
                     coords.width, coords.height, 'rgb(255,255,255)');

  if (selection.length) {
    var nodeCoords = selection[0].getBoundingClientRect();
    var tempCanvas = document.createElement("canvas");
    var tempContext = tempCanvas.getContext("2d");

    tempCanvas.width = nodeCoords.width;
    tempCanvas.height = nodeCoords.height;
    // Draw the node part from the whole page canvas into another canvas
    // void ctx.drawImage(image, sx, sy, sLargeur, sHauteur,
                                 dx, dy, dLargeur, dHauteur)
    tempContext.drawImage(canvas,
      nodeCoords.left, nodeCoords.top, nodeCoords.width, nodeCoords.height,
      0, 0, nodeCoords.width, nodeCoords.height);

    canvas = tempCanvas;
    context = tempContext;
  }

  var dataURL = canvas.toDataURL('image/jpeg', 0.95);
  return dataURL;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文