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

phaser-shape-recognition

v1.2.0

Published

Phaser 3 class for recognition of shapes.

Downloads

9

Readme

Phaser shape recognition

This class provides some utilities for shape and stroke recognition using Phaser 3.
Can be used to process image data and feed neural networks.
Try demo here: https://jjcapellan.github.io/phaser-shape-recognition/

Table of contents

Installation

Browser

There are two alternatives:

  • Point a script tag to the CDN link:
<script src="https://cdn.jsdelivr.net/gh/jjcapellan/phaser-shape-recognition/dist/phaser-shape-recognition.umd.js">
<script src="localPath/phaser-shape-recognition.umd.js">

Then you can access the class by the global ShapeRec:

// In your create function ...
const shapeRec = new ShapeRec(this); 

From NPM

npm install phaser-shape-recognition

Then you can acces the class as:

  • CommonJS module
const ShapeRec = require('phaser-shape-recognition');

// In your create function ...
const shapeRec = new ShapeRec(this); 
  • ES6 module
import ShapeRec from 'phaser-shape-recognition';

// In your create function ...
const shapeRec = new ShapeRec(this); 

Usage

Example (Image vs Points comparison):

// In create function ...
const shapeRec = new ShapeRec(this);

// Makes an array of 10x10 of booleans from the image.
const imageMatrix = shapeRec.makeMatrix('textureKey', 'textureFrame', 10);

// Hardcoded array of points
const points =[{x: 2, y: 4}, {x: 4, y: 14}, { ... } ...];

// Makes an array of 10x10 of booleans from the points array
const pointsMatrix = shapeRec.makeMatrix(points, null, 10)

// Test the similitude between the image and the shape represented in the points array
// shapeRec.test() returns a number between 0 and 1. 1 means the two shapes are identical and 0 the opposite.
let similitude = shapeRec.test(imageMatrix, pointsMatrix);

Important:

  • About images: makeMatrix() only use the alpha values, so the image should have transparent background.
  • test() only can compare matrix of the same size. Ex: you can't compare 10x10 vs 12x12.

Methods

makeMatrix(source, frame, resolution)

Transforms an array of raw points or an image into a "normalized" array of booleans (matrix)
Params:

  • source { string | Point[]} Can be the string key of a texture or an array of points (Ex: [{x: 2, y:2}, {x:3, y:5}, ...])
  • frame { (string | number) } String or index of the texture frame. Not used if source is an array.
  • [resolution = 10] { number } Size of the matrix. High values reduce false positives and increase false negatives in shape recognition. With low values the opposite occurs.

Returns:

  • { boolean[][] } Matrix of booleans. Each cell of the matrix represents one sector of the image. If in that sector exists some positive alpha then its value will be "true".

test(matrix1, matrix2, checkNeighbors)

Compares two matrix of same size:

  • matrix1[i][j] == matrix2[i][j] ---> HIT
  • matrix1[i][j] != matrix2[i][j] ---> FAIL
    Params:
  • matrix1 { boolean[][] } Matrix of first shape
  • matrix2 { boolean[][] } Matrix of second shape
  • checkNeighbors { boolean } Fail is not added if there are some neighbor cell with "true"

Returns:

  • { number } Number between 0 and 1: hits / (hits + fails)

License

This code is released under MIT LICENSE.