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

@rnacanvas/points.oopified

v1.9.0

Published

Work with 2D points in an object-oriented way

Downloads

239

Readme

Installation

With npm:

npm install @rnacanvas/points.oopified

Usage

All exports of this package can be accessed as named imports.

// an example import
import { Point } from '@rnacanvas/points.oopified';

Point

The Point class represents a 2D point.

var p = new Point(28.1, -55.4);

p.x; // 28.1
p.y; // -55.4

// can be modified
p.x = 101.2;
p.y = 38.1;

// is iterable
[...p]; // [101.2, 38.1]

static matching()

Creates and returns a new Point instance matching a point-like object (e.g., an object with x and y properties).

var p = Point.matching({ x: 901.7, y: -11 });

p.x; // 901.7
p.y; // -11

displacementTo()

Returns a new Vector instance that is the vector going from the point to the specified point.

See @rnacanvas/vectors.oopified package for documentation on the Vector class.

var p = new Point(0, 0);
var d = p.displacementTo({ x: 1, y: Math.sqrt(3) });

d.x; // 1
d.y; // Math.sqrt(3)

d.magnitude; // 2
d.direction; // Math.PI / 3

displacementFrom()

Returns a new Vector instance that is the vector going from the specified point to the point that this method was called on.

See @rnacanvas/vectors.oopified package for documentation on the Vector class.

var p = new Point(0, 0);
var d = p.displacementFrom({ x: 1, y: Math.sqrt(3) });

d.x; // -1
d.y; // -Math.sqrt(3)

d.magnitude; // 2
d.direction; // -2 * Math.PI / 3

distanceTo()

Returns the distance from the point to the specified point.

var p = new Point(10, 10);

p.distanceTo({ x: 13, y: 14 }); // 5

distanceFrom()

An alias for the distanceTo() method.

var p = new Point(80, 70);

p.distanceFrom({ x: 75, y: 82 }); // 13

directionTo()

Returns the angle (in radians) that is the direction from the point to the specified point in the standard Cartesian coordinate system.

var p = new Point(0, 0);

p.directionTo({ x: 1, y: 0 }); // 0
p.directionTo({ x: 0, y: 1 }); // Math.PI / 2
p.directionTo({ x: -1, y: -1 }); // -3 * Math.PI / 4

directionFrom()

Returns the angle (in radians) that is the direction from the specified point to the point that this was method was called on in the standard Cartesian coordinate system.

var p = new Point(0, 0);

p.directionFrom({ x: 1, y: 0 }); // Math.PI
p.directionFrom({ x: 0, y: 1 }); // -Math.PI / 2
p.directionFrom({ x: -1, y: -1 }); // Math.PI / 4

RelativePoint

The RelativePoint class represents a point that is expressed relative to a reference point.

var refP; // reference point
var relP = new RelativePoint(refP);

refP.x = 57;
refP.y = -81;

// moves with the reference point
relP.x; // 57
relP.y; // -81

relP.x += 12;
relP.y -= 15;

refP.x = 102;
refP.y = 3;

relP.x; // 102 + 12
relP.y; // 3 - 15

The reference point must fulfill the TrackablePoint interface below.

interface TrackablePoint {
  readonly x: number;
  readonly y: number;

  // the listener is to be called whenever the point moves
  // (i.e., whenever its X or Y coordinates change)
  addEventListener(name: 'move', listener: () => void): void;
}

Listening for move events

Listeners can be attached that will be called whenever a relative point moves (i.e., whenever the reference point moves or its displacement from the reference point changes).

var refP; // reference point
var relP = new RelativePoint(refP);

var listener = () => {};
relP.addEventListener('move', listener);

refP.x += 1000;
listener; // called once

relP.x -= 12;
listener; // called a second time

relP.removeEventListener('move', listener);

relP.x += 20;
listener; // not called a third time