squigonometry
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Squigonometry

Approximations of trigonometric functions in $p$-norms for JavaScript; supports $p = 1, 2, 3$. Helpful for drawing squircles.

Usage

npm install squigonometry

then

import { sinp, cosp } from "squigonometry";

const canvas = document.getElementById("canvas");

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

let degrees = 0;

function render() {
  const ctx = canvas.getContext("2d");
  const width = window.innerWidth;
  const height = window.innerHeight;
  const minorAxis = Math.min(width, height);

  const cosp1Value = cosp(1, (degrees / 180) * Math.PI);
  const sinp1Value = sinp(1, (degrees / 180) * Math.PI);
  const x1 = cosp1Value * (minorAxis / 4) + width / 2;
  // Multiply by -1 to flip the y-axis (<canvas> coordinates are measured top-down)
  const y1 = -sinp1Value * (minorAxis / 4) + height / 2;

  ctx.fillStyle = `hsl(${degrees}, 100%, 50%)`;
  ctx.fillRect(x1, y1, 2, 2);

  const cosp2Value = cosp(2, (degrees / 180) * Math.PI);
  const sinp2Value = sinp(2, (degrees / 180) * Math.PI);
  const x2 = cosp2Value * (minorAxis / 4) + width / 2;
  const y2 = -sinp2Value * (minorAxis / 4) + height / 2;

  ctx.fillStyle = `hsl(${degrees}, 100%, 50%)`;
  ctx.fillRect(x2, y2, 2, 2);

  const cosp3Value = cosp(3, (degrees / 180) * Math.PI);
  const sinp3Value = sinp(3, (degrees / 180) * Math.PI);
  const x3 = cosp3Value * (minorAxis / 4) + width / 2;
  const y3 = -sinp3Value * (minorAxis / 4) + height / 2;

  ctx.fillStyle = `hsl(${degrees}, 100%, 50%)`;
  ctx.fillRect(x3, y3, 2, 2);

  degrees += 1;

  requestAnimationFrame(render);
}

requestAnimationFrame(render);

Roadmap

Contributing

  • npm build to package for NPM
  • npm test to run tests
  • npm run demo to run the demo webpage (runs at http://localhost:8080)

Readme

Keywords

none

Package Sidebar

Install

npm i squigonometry

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

6.08 kB

Total Files

6

Last publish

Collaborators

  • nathanhleung