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-2-canvas

v0.0.8

Published

html renderer for canvas

Downloads

232

Readme

html-2-canvas

This a pure javascript html renderer for canvas, using the libraries parse5, css-parse and css-select.

It is intended for use with both node with (node-canvas), and the latest browsers.

Demo

Caveats

  • simple flow engine, no div floating, all elements are either a new line or a line element.
  • Treats all elements as div's except for tags the following tags
    • span, strong, em, code, samp, kbd, var, b
    • This can be expanded on via options.lineTags: [String]
  • font*, text*, color are the only style tags that get applied to children elements.
  • fetch is required for downloading images. a polyfill (whatwg-fetch) for unsupported browsers and for node support it needs to be passed as an option.fetch: fetch.
  • font-size and font-family must be defined on any parent element for text to render.
  • for webpack you need to add the following to config for it to compile
{
  "node": {
    "fs": "empty"
  }
}

Supported Html Tags

  • div (treats all unknown elements as divs);
  • img
  • br
  • span, strong, em, code, samp, kbd, var, b
    • it will render these tags as a line item, though you will need to provide a style for them

Supported CSS Tags

  • margin (shorthand and expanded) px only
  • padding (shorthand and expanded);
  • font (shorthand and expanded) 'shorthand' thanks to canvas font support
  • color
  • background-color
  • width
  • em and % text sizing.
body {
  font-family: Arial;
  font-size: 14px;
  padding: 5px;
  background-color: rgba(0,0,0);
  color: #fff;
}
p {
  padding-top: 0px;
  padding-left: 5px;
  padding-bottom: 5px;
}
strong, b { 
  font-weight: bold;
}
em, i, var { 
  font-style: italic;
}
code, samp, kbd {
  font-family: monospace;
}
center {
  text-align: center;
}

Planned Support

  • text-decoration
  • background (shorthand and expanded)
  • border (shorthand and expanded)
  • transform
  • box-shadow
  • position
  • margin: auto
  • float: left, right, clear: both

TODO

  • reduce render time - optimise the multiple loops into a single process loop?

Example - Node >= 7.10

import fetch from "node-fetch";

import sourceMapSupport from "source-map-support";
sourceMapSupport.install();
import path from "path";
import fs from "fs";

import Html2Canvas from "html-2-canvas";
import Canvas from "canvas";

const stylesheet = `
body {
  font-family: Arial;
  font-size: 14px;
  padding: 5px;
  background-color: rgba(0,0,0);
  color: #fff;
}
p {
  padding-top: 0px;
  padding-left: 5px;
  padding-bottom: 5px;
}
`;
(async() => {
  try {
    const html = `<html>
  <head>
    <style>
      strong, b { 
        font-weight: bold;
      }
      em, i, var { 
        font-style: italic;
      }
      code, samp, kbd {
        font-family: monospace;
      }
      center {
        text-align: center;
      }
    </style>
  </head>
  <body>
    <div class="row">
      <p><span>asdd<span style="font-size:36px; font-style: italic;">HELLO</span></span></p>
      <p>Howdy&#x0022;&nbsp;&#x0022;<br /><strong>Stranger</strong><br/><br/><br/>lets play?</p>
      <p>2nd</p>
      <p style="text-align: right;">
        <span style="font-size: 12px;">3rd</span>4th
      </p>
      <p style="width: 50px;">
        <span style="background-color: rgba(255,255,255,0.3);">1111111111<em><strong>111111</strong></em>11111111111</span>
      </p>
      <p style="background-color: rgba(255,255,255,0.5)">
        <span style="font-size: 12px">3rd</span>4th<br/>
        5th<span>lol</span>
        <span>asdd<span style="font-size:36px">HELLO</span></span>
      </p>
      <p style="background-color: rgba(255,255,255,0.1); text-align: center;">
        <img src="https://lh3.googleusercontent.com/6bYd0ESbr0b8MLVMv_CAT74WpyHOQSuE6NzDwey4Cw8DHkChPyZi263mIfJdJtwjSBs=w170" style="width: 100px; height: 100px;" />
      </p>
      <p>
        <center>Centered By Tag</center>
      </p>
      <div style="float: right; width: 100px; height: 100px;">

      </div>
    </div>
  </body>
  </html>`;
    const canvas = new Canvas(500, 500);
    const renderer = new Html2Canvas({
      stylesheet,
      createCanvas({height, width}) {
        return new Canvas(height, width);
      },
      createImage() {
        return new Canvas.Image();
      },
      fetch: fetch,
    });
    await renderer.render(html, canvas);
    canvas.createJPEGStream().pipe(fs.createWriteStream(path.join(process.cwd(), "image.jpg")));
  } catch (err) {
    console.log("err", err);
  }
})();

Change Log

0.0.2

  • added support for inline styles
  • line elements can now use background color
  • added lineTags as an options to extend the classification of line typed element
  • fixed css inheritancy issue between the parent element and the line elements
  • fixed render issue for non renderable elements, added option.doNotRender: [String]
  • fixed render issue with children line elements chaining styles from parent line elements

0.0.3

  • added support for all css sizing units for all elements
  • separated out process and render from core render func
  • added test cases for new size unit class

0.0.4

  • patch for recursive em font sizing

0.0.5

  • removed inheritancy of number with unit size class as it breaks object compare.
  • stopped em calculation from multipling on it self.

0.0.6

  • uglifyjs has a problem with Size class name or is it the babel compile?

0.0.7

  • yes it does.

0.0.8

  • Added trim to preprocess each line of the html file instead of trimming the elements
  • another em inheritancy fix
  • cleaned up files to be a little bit more coherent
  • Adjusted line renderer to allow for setting line-height