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

@arayutw/matrix-pointer

v0.0.1

Published

This is a library that assists in focusing on any position of a matrix and moving relative or absolute to that position.

Downloads

17

Readme

MatrixPointer

This is a library that assists in focusing on any position of a matrix and moving relative or absolute to that position.

Features

  • Written in Typescript
  • Has no dependencies (e.g. jquery)
  • Lightweight, 1KB
  • Supports ES6

Install

git

git clone https://github.com/arayutw/matrix-pointer.git
cd matrix-pointer
npm install
npm run build

npm

npm install @arayutw/matrix-pointer

CDN

Please find various build files (esm, cjs, umd).

Package

If you plan to use it as a package, it is recommended to use @arayutw/matrix-pointer-import. It is a minimal project with only TypeScript files that has the same contents as this repository.

npm install @arayutw/matrix-pointer-import
import MatrixPointer from "@arayutw/matrix-pointer-import";

Demo

Check out the demo to see the possibilities.
https://arayutw.github.io/matrix-pointer/demo/

Usage

load

ESM

import MatrixPointer from "<pathto>/matrix-pointer.esm"

UMD

<script src="https://unpkg.com/@arayutw/matrix-pointer@latest/dist/scripts/matrix-pointer.js"></script>

initialize

The matrix parameter is required.

const mp = new MatrixPointer({
  jump?: Jump
  loop?: Loop
  loose?: Loose
  matrix: [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ]
});

Matrix

Please represent the matrix as a collection of arrays of rows.

[
  [1, 2, 3], // row
  [4, 5, 6], // row
]

You do not need to align the number of elements in each row.

[
  [1, 2, 3],
  [4],
  [5, 6]
]

Please specify a unique ID for each element in the row. The type of the ID is arbitrary, but it needs to be comparable with the === operator.

[
  [1, 2, 3],
  ["A", "B", "C"],
  [HTMLElement, Node, Symbol("a")],
]

Duplicate values will cause calculation errors, so please be careful.

[
  [1, 2, 3],
  [1, 2, 3],
]

null is special and treated as a blank. Anything other than null (such as undefined) is not treated as a blank.

[
  [1, 2, 3],
  [4, null, null],
]

A matrix can be specified using a function when it is dynamic. The function is called at an appropriate timing.

matrix: () => {
  return [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ];
}

Loop, Jump, Loose

Options can be defined as boolean values or x and y objects representing horizontal and vertical directions respectively.

loop: true
loop: false
loop: {
  x: true,
  y: false,
}

You can also make changes later.

mp.loop = true

| name | default | description | | --- | --- | --- | | loop | true | Wrap-around movement is allowed. | | jump | true | When moving beyond the edge, move to the next/prev row or column. | | loose | false | When the destination is blank, shift the row or column to move. Depending on the matrix structure, unreachable positions may exist, so please set accordingly. |

moveTo()

You can move by specifying the x and y array indices or the ID directly. If the movement fails, null will be returned, otherwise a Position object representing the new position information will be returned.

moveTo(x: number, y: number) => Position | null
moveTo(id: unknown) => Position | null
/*
[
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]
*/

mp.moveTo(1, 2);  // id=8(x=1,y=2)
const newPosition = mp.moveTo(5); // id=5(x=1,y=1)
/*
{
  id: unknown
  x: number
  y: number
}
*/

moveBy()

Move relative to the current position. See moveTo() for return value description.

moveBy(direction: "x"|"y", next: boolean) => Position | null
mp.moveBy("x", false);  // left
mp.moveBy("x", true); // right
mp.moveBy("y", false);  // top
const newPosition = mp.moveBy("y", true); // bottom

on()

const handler = (event) => {
  //{
  //  target: MatrixPointer
  //  type: "blur" | "focus"
  //  id: unknown
  //  x: number
  //  y: number
  //}
  console.log(event);
}

a.on("blur", handler, {
  once: false,
});

a.on("focus", handler, {
  once: false,
});

off()

mp.off("focus", handler);

emit()

mp.emit("click", {
  x: 12,
  y: 34,
});

destroy

mp.destroy();