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

vectortracer

v0.1.2

Published

Wasm bindings to visioncortex's vtracer library, a rust image vectorizer library.

Downloads

77

Readme

Build Release

Wasm bindings to Visioncortex's VTracer library.

Features

  • [x] Compatible with webworkers.
    • [ ] Webworker wrapper example.
  • [x] Optional debug mode (note this will be slower).
  • [x] Binary Image Converter
    • [x] Option to invert image data.
    • [x] Control over svg path and background color.
    • [x] Arbitrary attributes.
    • [x] Option to scale paths group.
  • [ ] Publish
  • [ ] Color Image Converter

Installation

npm install vectortracer

The package can be installed directly from the git repo like so:

npm install https://github.com/alanscodelog/vectortracer

Usage

This is a simple example showing how to create a wrapper function using a promise with setTimeout.

import { BinaryImageConverter, BinaryImageConverterParams, Options } from "vectortracer"

export function imageDataToSvg(
	imageData:ImageData,
	vtracerOptions:BinaryImageConverterParams,
	additionalOptions:Options
): string {
	const converter = new BinaryImageConverter(
		imageData,
		vtracerOptions,
		additionalOptions
	)

	return new Promise(resolve => {
		let done = false
		let progress = 0
		const tick = () => {
			done = converter.tick()
			progress = converter.progress()
			if (!done) {
				setTimeout(tick, 0)
			} else {
				const result = converter.getResult()
				converter.free()
				resolve(result)
			}
		}
		converter.init()
		setTimeout(tick, 0)
	}).catch(err => {
		...
	}) as Promise<string>
}

You can then use the function like so:

const canvas = document.getElementById("my-canvas")
const ctx = canvas.getContext("2d")
const imageData = ctx.getImageData(0, 0, size.width, size.height)

const result = await imageDataToSvg(imageData, vtracerOptions, additionalOptions)
// if you have access to the DOM you can parse it to an svg element like so

const parser = new DOMParser()
const svg = parser.parseFromString(res as string, "image/svg+xml").children[0] as SVGElement

Notes

The code is similar, but simpler than the one use in visioncortex's demo web app code. ImageData can be passed directly from js, instead of having to give it a canvas id and having that fragile connection. This means we can use the library from a webworker. What we do lose is the ability to get an SVG element back, again because we don't have access to the DOM in a webworker.

I have not benchmarked it properly yet, but in the app I wrote this for, the transformation seems to take on average a little bit more time (~20ms), but I get more varied times, sometimes faster, sometimes slower, and there are other things running in the background. Still, I think this is the simplest option* if we want to allow the UI to stay responsive. You can see in the demo app that with large images and expensive parameters the UI can freeze despite its use of setTimeout to try and mitigate this.

To try to avoid any slowdowns from crossing the js/wasm boundary, Tsify with serde-wasm-bindgen is used to provide faster serialization/deserialization (it converts js types directly to/from rust) compared to json serialization/deserialization, which is slower.

* The other possible option is having a wrapper function that takes care of creating a canvas, or using an offscreen canvas in the worker. I wanted to avoid this since for my use case I might not always have a canvas, but I do always have the ImageData.

Building

There is a root level package json that has the needed prepare script.

The pkg/package.json generated by wasm-pack is not used and is actually deleted upon building since it can cause issues because the js is esm, but the package.json is missing "type": "module" and the exports field.

It should theoretically be possible to also use the package from nodejs. It is at least properly importing for me without errors.