npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-element-to-image

v2.1.1

Published

Capture a image from any given HTML element, fast.

Downloads

24

Readme

html-element-to-image

📷 Capture an image of any given HTML element.

Install

// npm
npm install html-element-to-image

// yarn
yarn add html-element-to-image

📖 Example

Codepen: https://codepen.io/Hammster/pen/ZEYvxLa

ESM

import nodeToDataURL from 'html-element-to-image'

const visualSource = document.getElementById('source')
const imageTarget = document.getElementById('target')
const excluded = document.querySelectorAll('.unwanted-element')

nodeToDataURL({
  targetNode: visualSource,
  excludedNodes: excluded,
  customStyle: '.highlighted-element { background: red; }'
})
.then((url) => {
  imageTarget.setAttribute('src', url);
})

UMD

Exposes the function nodeToDataURL to the window/global

🔬 Differences to Other Libraries

Compared to html-to-image, dom-to-image and dom-to-image-more this library uses a different approach, which leads to a big performance increase. Instead of making a deep copy of the targeted element with applied styles via el.getComputedStyle(). This library uses the available DOM for serialization. Before and after the serialization, obfuscated classes get added to hide the parts we don't want to be displayed.

Another difference is that this library has no issue at all with SVG Sprite Sheets and ShadowDOM since we use the given DOM already.

🛡️ Limitation

  • If the DOM node you want to render includes a <canvas> element with something drawn on it, it should be handled fine, unless the canvas is tainted - in this case rendering will rather not succeed.
  • Rendering will failed on huge DOM due to the dataURI limit variations in browser implementations.
  • Transforms may be affected. Elements that depend on transform-style: preserve-3d; do not work in a foreignObject, everything get flattened. And this is sadly a requirement for creating a image buffer, and also effects html-to-image, dom-to-image and dom-to-image-more
    • this can be avoided partially by adding a fallback z-index

💔 Know Issues

  • The canvas is not yet DPI aware
  • styles from html and body can sometimes be overwritten by the Specificity