x-plot

a web component that plots zoomable waveforms

Usage no npm install needed!

<script type="module">
  import xPlot from 'https://cdn.skypack.dev/x-plot';
</script>

README


<x-plot>

a web component that plots zoomable waveforms

🔧 Install · 🧩 Example · 📜 API docs · 🔥 Releases · 💪🏼 Contribute · 🖐️ Help


Install

$ npm i x-plot

Or directly from jsDelivr:

import { PlotElement } from 'https://cdn.jsdelivr.net/gh/stagas/x-plot/x-plot.min.js'

customElements.define('x-plot', PlotElement)

Example

<x-plot width="200" height="60"></x-plot>
<x-plot width="200" height="60" background="#f08" color="#ff0" zoom="4" line-width="3"></x-plot>
<x-plot width="200" height="60" background="#066" color="yellow" zoom="20" line-width="6"></x-plot>
<x-plot width="200" height="60" background="#000"></x-plot>
import { PlotElement } from 'x-plot'

// any name works for the custom element, here: <x-plot>
customElements.define('x-plot', PlotElement)

// then simply put array-like .data on the elements
const plots = container.querySelectorAll('x-plot')

const rate = 44100
const sine = (i, hz) => Math.sin(hz * (i * (1 / rate)) * Math.PI * 2)

plots[0].data = Array(rate)
  .fill(0)
  .map((_, i) => sine(i, 1))
plots[1].data = Array(rate)
  .fill(0)
  .map((_, i) => sine(i, 10))
plots[2].data = Array(rate)
  .fill(0)
  .map((_, i) => sine(i, 100))
plots[3].data = [1, 0, -1, 0, 1, 0, -1]

API

Table of Contents

HTMLPlotElement

src/index.ts:16-35

Plot settings.

All values have to be set in the html attribute (or using setAttribute).

Camel cased keys can be accessed with their kebab-case name, i.e lineWidth => line-width

The data can only be set directly at the element instance for example acquiring it through a querySelector().

width

src/index.ts:18-18

Width of plot

Type: number

height

src/index.ts:20-20

Height of plot

Type: number

pixelRatio

src/index.ts:22-22

The pixel ratio. Defaults to window.devicePixelRatio

Type: number

data

src/index.ts:24-24

Array-like number data to plot

Type: Array<number>

zoom

src/index.ts:26-26

Zoom amount

Type: number

autoresize

src/index.ts:28-28

Autoresize

Type: boolean

lineWidth

src/index.ts:30-30

Line width

Type: number

background

src/index.ts:32-32

Background color

Type: string

color

src/index.ts:34-34

Stroke color

Type: string

Contribute

Fork or edit and submit a PR.

All contributions are welcome!

License

MIT © 2021 stagas