Compile/Save/Export HTML as a PNG Image using Jquery

23,382

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.

Share:
23,382
C_K
Author by

C_K

Updated on October 20, 2020

Comments

  • C_K
    C_K over 3 years

    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...

  • C_K
    C_K almost 13 years
    Wow, I'll have o play around with this and see if I cant get it to do what I need. Thanks for the very indepth answer! This uses HTML5, so only newer browsers will be able to employ such functionality?
  • Michaël Witrant
    Michaël Witrant almost 13 years
    You can check caniuse.com/#search=canvas but I don't know if the "canvas basic support" includes this method.
  • C_K
    C_K almost 13 years
    It took me a while before i was ready to implement this script. I'm not sure how to implement it though, how do i trigger the image creation, and where would the resultant image show up?
  • C_K
    C_K almost 13 years
    could you clarify what html structure i'd need for this?
  • Michaël Witrant
    Michaël Witrant almost 13 years
    You don't need any HTML structure (except if you want to use the sample that draws the image on the document, then you'd need a <canvas id="captured"/> somewhere on the page). I added a sample to send the image to your server.
  • Admin
    Admin over 11 years
    this does not work: Uncaught TypeError: Object #<CanvasRenderingContext2D> has no method 'drawWindow' look:jsbin.com/oribiy/1/edit
  • Michaël Witrant
    Michaël Witrant over 11 years
    You're right. It only works in Firefox extensions. I edited the answer.
  • Moe Sweet
    Moe Sweet almost 11 years
    does it have to be canvas or does it also work for any HTML element?