# layered-sine

> Fills a canvas with sine wave(s), and generates animation.

Latest version **1.1.1** (published 2023-07-26) · GPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install layered-sine
pnpm add layered-sine
yarn add layered-sine
bun add layered-sine
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2023-07-26 |
| First published | 2023-02-22 |
| Weekly downloads | 0 |
| License | GPL-3.0-or-later |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 44.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | NorthWestWind |
| Maintainers | northwestwind |
| Keywords | canvas, sine, fill |

## Links

- npm: https://www.npmjs.com/package/layered-sine
- Repository: https://github.com/North-West-Wind/layered-sine
- Homepage: https://github.com/North-West-Wind/layered-sine#readme
- Issues: https://github.com/North-West-Wind/layered-sine/issues
- npm.io page: https://npm.io/package/layered-sine

## Dependencies (2)

- [canvas](https://npm.io/package/canvas.md) ^2.11.2
- [upng-js](https://npm.io/package/upng-js.md) ^2.1.0

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2023-07-26
- 1.1.0 — 2023-07-26
- 1.0.1 — 2023-02-22
- 1.0.0 — 2023-02-22

## README

# Layered Sine
Fills a canvas with sine wave(s), and generates animation, using the [`canvas`](https://github.com/Automattic/node-canvas) package.

## Usage
Install this package by:
```bash
npm i layered-sine
```
Import the package in your script by:
```ts
import { SineWave, FillMode, animateSines, animateSinesFrames, drawSine } from "layered-sine"
```
The functions and classes are defined as follows:
```ts
// Generates an animation of sine wave(s) moving. Returns the buffer of an APNG file. Save it as PNG my friend.
function animateSines(waves: SineWave[], canvas: Canvas, fps: number): ArrayBuffer
// Generates frames for the animation and saves them in <path> as PNGs. Returns the number of PNGs generated.
function animateSinesFrames(waves: SineWave[], canvas: Canvas, fps: number, path: string): number
// Draws a sine wave onto the canvas. Returns the drawn canvas.
function drawSine(wave: SineWave, canvas: Canvas, t = 0): Canvas
```
- `wave`/`waves`: Sine wave(s) to draw.
- `canvas`: Background canvas.
- `fps`: Framerate of animation.
- `path`: Path to store the frames.
- `t`: Time. Also determines the phase of wave.

```ts
class SineWave {
	amplitude: number;
	wavelength: number;
	waveNum: number;
	period: number;
	nFreq: number;
	phase: number;
	y: number;
	fill: FillMode;
	color: number;

	constructor(amplitude: number, wavelength: number, period: number, phase: number, y: number) {
		this.amplitude = amplitude;
		this.wavelength = wavelength;
		this.waveNum = 2 * Math.PI / wavelength;
		this.period = period;
		this.nFreq = 2 * Math.PI / period;
		this.phase = phase;
		this.y = y;
	}
}
```
This is in the form of `A sin( kx - wt + p ) + y`, where:
- `A` (Amplitude) = `amplitude`
- `k` (Wave Number) = 2π / `wavelength`
- `w` (Natural Frequency) = 2π / `period`
- `p` (Phase) = `phase` (in radian)
- `y` (Y Offset) = `y` (Y-axis is flipped in rendering)

Other properties of `SineWave` class:
- `fill`: `FillMode` of the wave, can be `LINE` (0), `UP` (1) or `DOWN` (2).
- `color`: Color of the wave. Must be between `0x000000` and `0xFFFFFF`.

Here's the `FillMode` enum:
```ts
enum FillMode {
	LINE = 0,
	UP = 1,
	DOWN = 2
}
```

## Example/Testing
Check `test/test.ts` for example.
1. Clone this repo.
2. Run `npm i`.
3. Run `npm run build`.
4. Run `npm test`. This will execute `test/test.ts`.

## License
GNU GPL v3

---
_Source: https://npm.io/package/layered-sine · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
