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

canvas-to-dom

v1.0.2

Published

Generate dom tree from HTML5 canvas frame

Downloads

3

Readme

canvas-to-dom

canvas-to-dom is an experimental project, which generates DOM from HTML5 canvas through visual inference. It serves as a demonstration of how computer vision techniques can be applied to automate web animation testing process.

Library extracts canvas state information from arbitrary frame. It is intended to be used as a testing framework and visualization tool for HTML5 canvas.

Project Status

This project is currently in development. Users can generate DOM from frames which contain simple geometric shapes.

Installation

If you use npm,

$ npm install canvas-to-dom --save-dev 

Otherwise, download minifed version from github repository.

canvas-to-dom is written using ES2015 modules. Create custom bundle with webpack. You can use configuration given in examples.

Library is available both for browser and node.

Quick start

Main library functions:

loadCanvasToDom();
canvasToDOM(canvasEl, options);
canvasDOMCompare(canvasEl1, canvasEl2, options);

browser

<canvas id="app"></canvas>
<script>
    loadCanvasToDom().then(() => {
        const dom = canvasToDom("app");
    });
</script>

Output format type can be specified with options. Supported output types: JSON, string, document (Document object model). Last one is the default.

Generating information as JSON:

<canvas id="app"></canvas>
<script>
    loadCanvasToDom().then(() => {
        const dom = canvasToDom("app", {
            "type": "json"
        });
    });
</script>

To extract information from part of canvas, use options:

<canvas id="app"></canvas>
<script>
    loadCanvasToDom().then(() => {
        const dom = canvasToDom("app", {
            "type": "document",
            "fragment": {
                "x": 100,
                "y": 100,
                "width": 200, 
                "height": 200,
            }
        });
    });
</script>

For comparing two different canvas data:

<canvas id="app"></canvas>
<script>
    loadCanvasToDom().then(() => {
        const dom = canvasToDom("app");
        const expected = "<canvas><line center=\"(149.5,149.5)\" point-0=\"(128,129)\" point-1=\"(128,129)\" \
                            color=\"#fefefe\" z-order=\"1\"orientation=\"43.23\"></line></canvas>";
        canvasDOMCompare(dom, expected);
    });
</script>

canvasDOMCompare function compares all supported types. Given argument can be either canvas element, canvas id or any type of result generated from canvasToDom function. Using comparator options:

<canvas id="app"></canvas>
<script>
    loadCanvasToDom().then(() => {
        const dom = canvasToDom("app");
        const expected = "<canvas><line center=\"(149.5,149.5)\" point-0=\"(128,129)\" point-1=\"(128,129)\" \
                            color=\"#fefefe\" z-order=\"1\"orientation=\"43.23\"></line></canvas>";
        canvasDOMCompare(dom, expected, {
            "center": {
                "delta": 2 
            },
            "width": {
                "delta": -1
            },
            "height": {
                "delta": 0
            }, 
            "diameter": {
                "delta": 4
            },
            "points": {
                "delta": -3
            },
            "orientation": {
                "delta": 2,
            },
            "color": {
                "precision": "identical",
                "method": "CIE2000"
            },
            "zOrder": {
                "delta": -1
            }
        });
    });
</script>

Every property in options can be specified separately. Default values for property delta-s are zeros. For color property comparison precision option has five values: identical, close, similar, distinct and opposite. Supported comparator methods are: CIE2000, CIE94, CIE76. CIE2000 is the default. Default precision option - identical.

node

All the above methods are available in node.

import { createCanvasFromImage, installDomParser, loadOpenCV, canvasToDOM } from "../../index.js";

(async () => {
    await loadOpenCV();

    const canvasEl = createCanvasFromImage("./screenshot.png");
    const dom = canvasToDOM(canvasEl, {
        "type": "json"
    });
})();

For generating Document object model from canvasToDom use installDomParser function.


(async () => {
    await loadOpenCV();

    const canvasEl = createCanvasFromImage("./screenshot.png");
    installDomParser();
    const dom = canvasToDOM(canvasEl, {
        "type": "document"
    });
})();

It's possible to load dom and opencv.js separately using functions:

loadDOM();
loadOpenCV();

For more information, see examples

Licence

This project is licensed under the MIT License.