Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active July 14, 2023 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/03437fcffa9579d539f9a3f48b9d0a16 to your computer and use it in GitHub Desktop.
Save code-boxx/03437fcffa9579d539f9a3f48b9d0a16 to your computer and use it in GitHub Desktop.
Javascript Take Screenshot

JAVASCRIPT TAKE SCREENSHOT

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Javascript Screenshot Demo</title>
<link rel="stylesheet" href="x-dummy.css">
<!-- (A) HTML2CANVAS LIBRARY -->
<!-- https://github.com/niklasvh/html2canvas -->
<!-- https://cdnjs.com/libraries/html2canvas -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
<script src="1b-html2canvas.js"></script>
</head>
<body>
<!-- (B) TEST -->
<h1>TEST</h1>
<textarea>Dummy Text - Change and see how it captures below!</textarea>
<input type="button" value="Capture" onclick="capture()">
</body>
</html>
function capture () {
html2canvas(document.body).then(canvas => {
let a = document.createElement("a");
a.download = "ss.png";
a.href = canvas.toDataURL("image/png");
a.click(); // MAY NOT ALWAYS WORK!
});
}
<!DOCTYPE html>
<html>
<head>
<title>Javascript Screenshot Demo</title>
<link rel="stylesheet" href="x-dummy.css">
<!-- (A) LOAD JS -->
<script src="2b-media.js"></script>
</head>
<body>
<!-- (B) TEST -->
<h1>TEST</h1>
<textarea>Dummy Text - Change and see how it captures below!</textarea>
<input type="button" value="Capture" onclick="capture()">
</body>
</html>
async function capture () {
// (A) GET MEDIA STREAM
const stream = await navigator.mediaDevices.getDisplayMedia({
preferCurrentTab: true
});
/*
const stream = await navigator.mediaDevices.getDisplayMedia({
video: { displaySurface: "window" } // window | browser | monitor
});*/
// (B) STREAM TO VIDEO
const vid = document.createElement("video");
// (C) VIDEO TO CANVAS
vid.addEventListener("loadedmetadata", function () {
// (C1) CAPTURE VIDEO FRAME ON CANVAS
const canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d");
ctx.canvas.width = vid.videoWidth;
ctx.canvas.height = vid.videoHeight;
ctx.drawImage(vid, 0, 0, vid.videoWidth, vid.videoHeight);
// (C2) STOP MEDIA STREAM
stream.getVideoTracks()[0].stop();
// (C3) "FORCE DOWNLOAD"
let a = document.createElement("a");
a.download = "ss.png";
a.href = canvas.toDataURL("image/png");
a.click(); // MAY NOT ALWAYS WORK!
});
// (D) GO!
vid.srcObject = stream;
vid.play();
}
// (A) APPEND SCREENSHOT TO DATA OBJECT
var data = new FormData();
data.append("screenshot", canvas.toDataURL("image/jpeg", 0.6));
// (B) UPLOAD SCREENSHOT TO SERVER
fetch("3b-upload.php", { method:"post", body:data })
.then(res => res.text())
.then(txt => alert(txt));
<?php
// https://netcell.netlify.com/blog/2016/04/image-base64.html
$file = fopen("screenshot.jpg", "w");
$data = explode(",", $_POST["screenshot"]);
$data = base64_decode($data[1]);
fwrite($file, $data);
fclose($file);
echo "OK";
/* (X) CSS COSMETICS - DOES NOT MATTER */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body { max-width: 300px; }
textarea, input { font-size: 1em; }
textarea {
display: block;
width: 100%; height: 200px;
}
input[type=button] {
color: #fff; background: #9d2631;
border: 0; padding: 10px; margin-top: 20px;
cursor: pointer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment