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

geometrizejs

v0.0.19

Published

Geometrize JavaScript API. Generated from geometrize-haxe library directly. Zero dependencies. No implementation, just typings. For node.js and browsers.

Downloads

50

Readme

geometrizejs

See the playground to understand what's this is all about.

Contents

Summary

Usage

Note that this library is just types for official geometrize-haxe so it doesn't support any image read/write and the API could seems low level.

Checkout geometrizejs-extra which provides an easy to use API and supports several image formats both in node and browser.

npm install --save geometrizejs

This example uses jimp to load images which supports formats both in node.js and browsers. Nevertheless any library can be used, such as pngjs.

Examples

Render SVG

import Jimp from 'jimp'
import { Bitmap, ImageRunner, ShapeTypes, SvgExporter } from 'geometrizejs'

(async () => {
  // load png/jpeg/gif,bmp/tiff image from url, file path or Buffer using jimp:
  const image = await Jimp.read('test/assets/logo.png')
  const bitmap = Bitmap.createFromByteArray(image.bitmap.width, 
    image.bitmap.height, image.bitmap.data)
  const runner = new ImageRunner(bitmap)
  const options = {
    shapeTypes: [ShapeTypes.CIRCLE, ShapeTypes.TRIANGLE],
    candidateShapesPerStep: 50,
    shapeMutationsPerStep: 100,
    alpha: 128
  }
  const iterations = 500
  const svgData = []
  for (let i = 0; i < iterations; i++) {
    svgData.push(SvgExporter.exportShapes(runner.step(options)))
  }
  const svg = SvgExporter.getSvgPrelude() + 
    SvgExporter.getSvgNodeOpen(bitmap.width, bitmap.height) + 
    svgData.join('\n') + 
    SvgExporter.getSvgNodeClose()

  // in node.js:
  writeFileSync('output.svg', svg)

  // in the browser:
  document.getElementById('svg-container').innerHTML = svg
})()

Render bitmap

import Jimp from 'jimp'
import { Bitmap, ImageRunner, ShapeTypes } from 'geometrizejs'

(async () => {
  // load png/jpeg/gif,bmp/tiff image from url, file path or Buffer using jimp:
   const image = await Jimp.read('test/assets/logo.png')
  const bitmap = Bitmap.createFromByteArray(image.bitmap.width, 
    image.bitmap.height, image.bitmap.data)
  const runner = new ImageRunner(bitmap)
  const options = {
    shapeTypes: [ShapeTypes.TRIANGLE],
    candidateShapesPerStep: 50,
    shapeMutationsPerStep: 100,
    alpha: 128
  }
  const iterations = 2000
  for (let i = 0; i < iterations; i++) {
    const r = runner.step(options)
  }
  const bytes = runner.getImageData().getBytes().b
  const out = new Jimp({ 
    width: image.bitmap.width, 
    height: image.bitmap.height,
    data: bytes
  })
  // in node.js we could write it to file
  await out.writeAync('tmp/bitmap/logo.png')

  // in the browser we could write it to a <img> element as data url
  document.getElementById('target-image').src = await out.getBase64Async()
})()

Render JSON

Getting the shapes as JSON can be used to render them using other technology or processing / analyse them:

import Jimp from 'jimp'
import { Bitmap, ImageRunner, ShapeTypes } from 'geometrizejs'

(async () => {
  const image = await Jimp.read('test/assets/logo.png')
  const bitmap = Bitmap.createFromByteArray(image.bitmap.width, 
    image.bitmap.height, image.bitmap.data)
  const runner = new ImageRunner(bitmap)
  const options = {
    shapeTypes: [ShapeTypes.CIRCLE],
    candidateShapesPerStep: 50,
    shapeMutationsPerStep: 100,
    alpha: 128
  }
  const iterations = 200
  const shapes = []
  for (let i = 0;i < iterations;i++) {
    shapes.push(...JSON.parse(ShapeJsonExporter.exportShapes(runner.step(options))))
  }
  // shapes is an array of objects serializable with JSON.stringify() 
})()

API docs

TODO

Build geometrize.js

git clone --recurse-submodules https://github.com/cancerberoSgx/geometrizejs.git
cd geometrizejs
sh generate-geometrize-js.sh

That should re-generate geometrizejs/src/geometrize.js.

ImageRunnerOptions

  • shapeTypes: Array<ShapeTypes>: The types of shapes to use when generating the image.
  • alpha: number: The opacity of the shapes (0-255).
  • candidateShapesPerStep: number: The number of candidate shapes to try per model step.
  • shapeMutationsPerStep: number: The number of times to mutate each candidate shape.

TODO / Roadmap

  • [ ] research wasm build using haxe
  • [ ] browser tests
  • [x] support bitmap-regions branch
  • [x] sourcemaps https://haxe.org/manual/debugging-source-map.html
  • [x] bitmap output tests
  • [x] JSON output tests

Extras / ideas

The following are features not supported by haxe implementation. If implemented it will be in a separate project so this project keeps being zero-implementation:

  • [ ] performance tests. use different options and input image sizes and generate timings and output image diffs (as numbers - for example using imagemagick) - so we can better understand how options/image size/output quality relationships are. See https://github.com/Tw1ddle/geometrize-haxe-web/issues/3#issuecomment-504424092
  • performance on haxe lib:
    • explicit option to not copy buffers
    • https://stackoverflow.com/a/16679447/1179379 - seems transforming u8int to u32int array is faster to extract colors ? maybe even in haxe
    • use Uint8ClampedArray ?
  • [x] Be able to change options while the iteration is still running
  • [x] be able to change iterations value ( while is running)
  • [ ] Be able to pause an iteration and serialize its state (options and model, current iteration, iterations value) so later, in another process, we can load it and resume it (even increase iterations value)
  • [x] implement quadratic blezier curves based on https://github.com/Tw1ddle/geometrize-lib/blob/be14f7bf0d183faa03127c6500c6194877b3ee3d/geometrize/geometrize/rasterizer/rasterizer.cpp#L308 ?