Using HTML5/Canvas/JavaScript to take in-browser screenshots
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:
http://hertzen.com/experiments/jsfeedback/
edit
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:
http://www.elliottsprehn.com/preso/fluentconf/
How to take a screenshot of a webpage using html5, without rendering it on a canvas via JS
This is simpler than it seemed. The missing part, saving a still shot of the video to png, can be achieved with code from this answer. The complete code would be as follows:
const v = document.getElementById("v");const b = document.getElementById("b");const i = document.getElementById("i");
navigator.mediaDevices.getDisplayMedia({ audio: false}).then((r) => { console.log(r); v.srcObject = r;}).catch((e) => { console.log("this must be run in a secure context. Stack snippets (rightfully) refuse to run this.");});
b.onclick = () => { // take screenshot // from https://stackoverflow.com/a/44325898/15472 let scale = 1;
const canvas = document.createElement("canvas"); canvas.width = v.clientWidth * scale; canvas.height = v.clientHeight * scale; canvas.getContext('2d').drawImage(v, 0, 0, canvas.width, canvas.height);
i.src = canvas.toDataURL(); // you can send i.src here to a server
// stop video let tracks = v.srcObject.getTracks(); tracks.forEach(track => track.stop()); v.srcObject = null;}
#v,#i { width: 400; height: 300;}
#v { border: 1px solid blue;}
#i { border: 1px solid green;}
<div> <video id="v" autoplay></video> <button id="b">take screenshot</button> <img id="i" src="//:0" /></div>
How to take screenshot of canvas?
It depends on your framework but basically you can use canvas.toDataURL()
Here is a complete example
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="578" height="200"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
// draw cloud
context.beginPath();
context.moveTo(170, 80);
context.bezierCurveTo(130, 100, 130, 150, 230, 150);
context.bezierCurveTo(250, 180, 320, 180, 340, 150);
context.bezierCurveTo(420, 150, 420, 120, 390, 100);
context.bezierCurveTo(430, 40, 370, 30, 340, 50);
context.bezierCurveTo(320, 5, 250, 20, 250, 50);
context.bezierCurveTo(200, 5, 150, 20, 170, 80);
context.closePath();
context.lineWidth = 5;
context.fillStyle = '#8ED6FF';
context.fill();
context.strokeStyle = '#0000ff';
context.stroke();
// save canvas image as data url (png format by default)
var dataURL = canvas.toDataURL();
</script>
</body>
</html>
dataUrl will contains the image and you can save it wherever you want.
Take Screenshot of html element for mobile view using JavaScript
There might be better ways to solve it but this is the one I used.
First remove Html table responsive class
document.getElementById('table-you-want-convert').classList.remove("table-responsive");
Change html2canvas function to
html2canvas(document.getElementById("table-you-want-convert"),{
windowHeight: window.outerHeight + window.innerHeight,
windowWidth: window.outerWidth + window.innerWidth,
allowTaint: true,
useCORS: true,
})
.then(function(canvas) {
//save or download image
//add responsive class again
document.getElementById('table-you-want-convert').classList.add("table-responsive");
}
Related Topics
Html-Encoding Lost When Attribute Read from Input Field
How to Know Which Radio Button Is Selected Via Jquery
How to Move an Element into Another Element
How to Scroll an HTML Page to a Given Anchor
How to Turn Off Antialiasing on an HTML ≪Canvas≫ Element
Case Insensitive Xpath Contains() Possible
Change :Hover CSS Properties With JavaScript
How to Make HTML Element Resizable Using Pure JavaScript
Disable Interpolation When Scaling a ≪Canvas≫
Using Html5/Canvas/JavaScript to Take In-Browser Screenshots
Using Jquery to Center a Div on the Screen
How to Programmatically Set the Value of a Select Box Element Using JavaScript
Changing Source on Html5 Video Tag
Html Script Tag: Type or Language (Or Omit Both)