Using HTML5/Canvas/JavaScript to take in-browser screenshots

Using HTML5/Canvas/JavaScript to take in-browser screenshots

Google’s “Report a Bug” or “Feedback Tool” lets you select an area of your browser window to create a screenshot that is submitted with your feedback about a bug.

Screenshot by Jason Small, posted in a duplicate question.
How are they doing this? Google’s JavaScript feedback API is loaded from here and their overview of the feedback module will demonstrate the screenshot capability.


Solution 1:

JavaScript can read the DOM and render a fairly accurate representation of that using canvas. I have been working on a script which converts HTML into a canvas image. Decided today to make an implementation of it into sending feedbacks like you described.

The script allows you to create feedback forms which include a screenshot, created on the client’s browser, along with the form. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.

It does not require any rendering from the server, as the whole image is created on the client’s browser. The HTML2Canvas script itself is still in a very experimental state, as it does not parse nearly as much of the CSS3 attributes I would want it to, nor does it have any support to load CORS images even if a proxy was available.

Still quite limited browser compatibility (not because more couldn’t be supported, just haven’t had time to make it more cross browser supported).

For more information, have a look at the examples here:

The html2canvas script is now available separately here and some examples here.

edit 2
Another confirmation that Google uses a very similar method (in fact, based on the documentation, the only major difference is their async method of traversing/drawing) can be found in this presentation by Elliott Sprehn from the Google Feedback team:

Solution 2:

Your web app can now take a ‘native’ screenshot of the client’s entire desktop using getUserMedia():

Have a look at this example:

The client will have to be using chrome (for now) and will need to enable screen capture support under chrome://flags.

Solution 3:

As Niklas mentioned you can use the html2canvas library to take a screenshot using JS in the browser. I will extend his answer in this point by providing an example of taking a screenshot using this library:

function report() {
  let region = document.querySelector("body"); // whole screen
  html2canvas(region, {
    onrendered: function(canvas) {
      let pngUrl = canvas.toDataURL(); // png in dataURL format
      let img = document.querySelector(".screen");
      img.src = pngUrl; 

      // here you can allow user to set bug-region
      // and send it with 'pngUrl' to server
.container {
  margin-top: 10px;
  border: solid 1px black;
<script src=""></script>
<div>Screenshot tester</div>
<button onclick="report()">Take screenshot</button>

<div class="container">
  <img width="75%" class="screen">

In report() function in onrendered after getting image as data URI you can show it to the user and allow him to draw “bug region” by mouse and then send a screenshot and region coordinates to the server.

In this example async/await version was made: with nice makeScreenshot() function.


Simple example which allows you to take screenshot, select region, describe bug and send POST request (here jsfiddle) (the main function is report()).

async function report() {
    let screenshot = await makeScreenshot(); // png dataUrl
    let img = q(".screen");
    img.src = screenshot; 
    let c = q(".bug-container");
    let box = await getBox();    

    send(screenshot,box); // sed post request  with bug image, region and description
    alert('To see POST requset with image go to: chrome console > network tab');

// ----- Helper functions

let q = s => document.querySelector(s); // query selector helper = report; // bind report be visible in fiddle html

async function  makeScreenshot(selector="body") 
  return new Promise((resolve, reject) => {  
    let node = document.querySelector(selector);
    html2canvas(node, { onrendered: (canvas) => {
        let pngUrl = canvas.toDataURL();      

async function getBox(box) {
  return new Promise((resolve, reject) => {
     let b = q(".bug");
     let r = q(".region");
     let scr = q(".screen");
     let send = q(".send");
     let start=0;
     let sx,sy,ex,ey=-1;;;
     let drawBox= () => {   = (ex > 0 ? sx : sx+ex ) +'px';     = (ey > 0 ? sy : sy+ey) +'px';  = Math.abs(ex) +'px'; = Math.abs(ey) +'px'; 
     //console.log({b,r, scr});
     b.addEventListener("click", e=>{
       if(start==0) {
     b.addEventListener("mousemove", e=>{
       if(start==1) {
     send.addEventListener("click", e=>{
       let a=100/75 //zoom out img 75%       
          x:Math.floor(((ex > 0 ? sx : sx+ex )-scr.offsetLeft)*a),
          y:Math.floor(((ey > 0 ? sy : sy+ey )-b.offsetTop)*a),
          desc: q('.bug-desc').value

function send(image,box) {

    let formData = new FormData();
    let req = new XMLHttpRequest();
    formData.append("box", JSON.stringify(box)); 
    formData.append("screenshot", image);     "POST", '/upload/screenshot');
.bug-container { background: rgb(255,0,0,0.1); margin-top:20px; text-align: center; }
.send { border-radius:5px; padding:10px; background: green; cursor: pointer; }
.region { position: absolute; background: rgba(255,0,0,0.4); }
.example { height: 100px; background: yellow; }
.bug { margin-top: 10px; cursor: crosshair; }
.hide { display: none; }
<script src=""></script>
<div>Screenshot tester</div>
<button onclick="report()">Report bug</button>

<div class="example">Lorem ipsum</div>

<div class="bug-container hide">
  <div>Select bug region</div>
  <div class="bug">    
    <img width="75%" class="screen" >
    <div class="region"></div> 
    <textarea class="bug-desc">Describe bug here...</textarea>
  <div class="send">SEND BUG</div>