div to image

by Chance Nursey-Bush

HTML

<script src="https://github.com/niklasvh/html2canvas/releases/download/0.4.1/html2canvas.js"></script>
<script src="http://www.nihilogic.dk/labs/canvas2image/base64.js"></script>
<script src="http://www.nihilogic.dk/labs/canvas2image/canvas2image.js"></script>
<script src="https://rawgit.com/nurseymybush/90c4e045998e6effb82a38ee65b271d2/raw/eb539fd02ab3376e31b1c0b769ee0253709d9c3c/screenshot.js"></script>

<span id="widget" class="widget" field="AGE" roundby="20" description="Patient age, in years">
    <div class="header">
      <h3>
        Testing header
      </h3>
    </div>
    <img src="http://lorempixel.com/output/technics-q-c-400-200-2.jpg" id="myimage">
    <div class="element ng-scope">
      <div class="content">
        <table>
          <colgroup>
            <col/>
            <col width="60x"/>
            <col width="100px"/>
          </colgroup>
          <thead>
            <tr>
              <th class="cell value">Value</th>
              <th class="cell freq">Freq</th>
              <th class="cell value"></th>
            </tr>
          </thead>
          <tbody>
            <tr class="selectable">
              <td class="cell value"><span tooltip="0 to 19">0 to 19</span></td>
              <td class="cell freq">17.2%</td>
              <td class="cell glyph">
                <span class="bar bar-both" style="width: 17.234468937875754%;"></span>
                <span class="bar bar-fg" style="width: 0%;"></span>
                <span class="bar bar-bg" style="width: 0%;"></span>
              </td>
          </tr>
          <tr class="selectable">
            <td class="cell value"><span tooltip="20 to 39">20 to 39</span></td>
            <td class="cell freq">18.0%</td>
            <td class="cell glyph">
              <span class="bar bar-both" style="width: 18.03607214428858%;"></span>
              <span class="bar bar-fg" style="width: 0%;"></span>
              <span class="bar bar-bg" style="width: 0%;"></span>
            </td>
  ...

CSS

body {
    font-family: "Lucida Grande", "Lucida Sans", Arial, sans-serif;
    font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;

}

.dataset {
    float: left;
    vertical-align: top;


}

.widget{
    display: inline-block;
    background-color: white;
    font-size: 14px !important;
    line-height: 20px !important;

    margin: 5px;
    vertical-align: top;
    color: #333;

    border-radius: 5px;
    margin: 10px;
    padding-bottom: 20px;
    border: 1px solid lightgray;


    border-radius:5px;
    -webkit-border-radius: 5px;


    display: inline-block;
    page-break-after: always;

}

.widget .header p {
   padding: 10px;
   border-bottom: 1px solid lightgrey;
   max-width: 360px;
}

.widget .header .title {
    font-weight: bold;
    vertical-align: middle;
    min-height: 36px;
    padding: 5px 10px 5px 10px;

}

.widget .header:hover {
    background-color: #f4f4f4;
}

.widget .header  .title.selected {
    border-color: cornflowerblue;
    background-color: #EEF;

}

.widget .content {
    padding: 5px;
    overflow-y: auto;
    max-height: 400px;
}

.autolayout {
    display: inline-block;
}

.element {
    width: 360px;
}

.compact .content {
    display: table;
    width: 100%;
}

.compact .row {
    display: table-row;
    width: 100%;
}

.compact .cell {
    display: table-cell;
}

.compact .row.selected {
    background-color: #eee;
}



.toolbar {
    display: block;
    vertical-align: top;
    margin: 10px;
}

.toolbar .basis {
    min-width: 100px;
}

.btn {
    /*min-width: 60px;*/
}


.cell.value {
    overflow: hidden;
    text-wrap: none;
    white-space: nowrap;
    text-overflow: ellipsis;
    text-align: right;
    padding-right: 10px;
}

.cell.freq {
    width: 60px;
}

.cell.glyph {
    vertical-align: middle;
    width: 100px;
}


.element {

}
.element table {
    table-layout: fixed;
    width: 100%;
}

.element td {
    padding: 0px;

}

.element...

JavaScript

$(function() { 
		$("#btnSave").click(function() { 
        html2canvas($("#widget"), {
            onrendered: function(canvas) {
                var context=canvas.getContext("2d"); // returns the 2d context object
                var img=new Image() //creates a variable for a new image
                var offsets = document.getElementById('myimage').getBoundingClientRect();
                var imgTop = offsets.top;
								var imgLeft = offsets.left;
                
                //Find actual position with parent cotainer
                var offsetsContainer = document.getElementById('widget').getBoundingClientRect();
								imageLeft = parseInt(imgLeft) - parseInt(offsetsContainer.left);
                imageTop = parseInt(imgTop) - parseInt(offsetsContainer.top);
                
                img.src= document.getElementById('myimage').src; // specifies the location of the image
               
                img.onload = function() {
                    context.drawImage(img, imageLeft, imageTop);// draws the image at the specified x and y location
                }
                // Convert and download as image 
                theCanvas = canvas;
                
                canvas.toBlob(function(blob) {
          				saveAs(blob, "highchart.png");
        				});
            }
        });
    });
});