Take a screenshot of a DOM container and use it as a background image

I am currently developing a drag and drop interface for a course builder. Courses are split into lessons and lessons into slides. While switching between slides, I wanted to have a small thumbnail representing the current slide content.

One option would have been to use something like PhantomJS / CasperJS.  A headless browser , to take a screenshot of the slide. I have done that in the past and it works perfectly.

This time, I decided to go with another solution, that allows me to convert html to a canvas element.

The project is called html2canvas (GitHub).

“This script allows you to take screenshots of webpages or parts of it, directly on the users browser. 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.”

EXAMPLE

  1. html2canvas(document.body, {
  2.   onrendered: function(canvas) {
  3.     document.body.appendChild(canvas);
  4.   }
  5. });

CANVAS TO BACKGROUND IMAGE

I have a slide container (#slideContainer), that holds the active slide for editing and a thumb navigation to switch between slides (.slide.thumb).

  1. var current = $(".slide.thumb.active");
  2.  
  3. html2canvas($("#slideContainer").get(0), {
  4.   onrendered: function(canvas) {
  5.          
  6.    var dataURL = canvas.toDataURL();
  7.    current.css({"background" :"url("+dataURL+")",
  8.                 "background-size":"contain",
  9.                 "background-position": "center",
  10.                 "background-repeat":"no-repeat"});
  11.   }
  12. });

When switching between slides, I am doing a snapshot of the current slideContainer and add it as a new background image to the thumb navigation item for that slide. This is done with canvas.toDataURL() and by embedding the resulting image  using a data: url.

Works perfectly. html2canvas has no full css support, so the output will not always match 100%, but its close enough to get a glimpse of what is stored per slide ;)

html2canvas
PhantomJS
CasperJS

Enjoy coding …

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

4 weeks ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago

Archaeology Travel Booth – Travel Innovation Summit 2023

Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More

6 months ago