
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to take screenshot of a div with JavaScript
We are required to capture (convert into image) part(s) of our markup that lays out our website and save that captured image or do something with it. So, we are required to devise a way using which we can achieve this described behaviour.
As our problem includes capturing any markup element and not just canvas, it’s a bit complex and especially if we plan to do it from scratch. Therefore, for our ease we will use a third party library, htmltocanvas that does exactly what the name suggests, converting the desired markup to canvas, after which we can simply download the canvas element as an image.
Example
The code for doing so will be −
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <style> #capture{ height: 300px; width: 300px; display: flex; flex-direction: column; background-color: rgb(43, 216, 216); } span{ flex: 1; width: 100%; display: flex; text-align: center; justify-content: center; align-items: center; color: #ffffff; font-size: 20px; } #first{ background-color: rgb(15, 168, 15); } #second{ background-color: rgb(43, 93, 228); } #third{ background-color: rgb(194, 55, 13); } </style> <body> <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script> <script> const download = () => { html2canvas(document.querySelector('#capture')).then(canvas => { document.body.appendChild(canvas); }); } </script> <div id='capture'> <span id='first'>First Block</span> <span id='second'>Second Block</span> <span id='third'>Third Block</span> </div> <button onclick="download()"> Download </button> </body> </html>
Output
And the output of the following code will be −
AFTER CLICKING DOWNLOAD BUTTON
Note that the second image contains a canvas adjacent to the “Download” button, which we can simply right click and save to our local storage.
- Related Articles
- How to take screenshot with Selenium WebDriver?
- How to take partial screenshot (frame) with Selenium WebDriver?
- How to programmatically take a screenshot in android?
- How to programmatically take a screenshot on iOS?
- How to take partial screenshot with Selenium WebDriver in python?
- How to take screenshot in Android Programatically?
- How to take a screenshot programmatically in iPhone/iOS?
- How to take a screenshot of the window using Python?(Tkinter)
- Take screenshot of full page with Selenium Python with chromedriver.
- How to take a screenshot programmatically in Android using Kotlin?
- How to take a screenshot of my iOS application in the iOS simulator?
- Best way to take screenshot of a web page into Selenium?
- Can you take a screenshot of the page using HTML5 Canvas?
- Take screenshot of the options in dropdown in selenium c#.
- How to get the complete screenshot of a page in Selenium with python?
