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

@breakfast-dlc/visualize

v0.2.4

Published

Visualize is a library that uses the Web Audio API to create live audio visualizations in the browser.

Downloads

15

Readme

Visualize

Visualize is a library that uses the Web Audio API to create audio visualizations in the browser.

Installation

You can add Visualize as a package using npm

npm install @breakfast-dlc/visualize

or you can add it to a page using a script tag.

<script src="https://cdn.jsdelivr.net/npm/@breakfast-dlc/[email protected]/dist/index.js"></script>

Usage

To import the library

import * as Visualize from "@breakfast-dlc/visualize";

or you can import specific visualizer classes.

import { Oscillope } from "@breakfast-dlc/visualize";

Getting Started

Creating a Visualizer

Visualizers require an AnalyserNode and a canvas element. Visualize will automatically create these for you.

//Create visualizer
let visual = new Visualize.FrequencyGraph();

//Add canvas to document
let container = document.getElementById("container");
container.append(visual.canvas);

//Connect audio source to visualizer
let audio = document.getElementById("audio");
let mediaSource = visual.audioContext.createMediaElementSource(audio);
mediaSource.connect(visual.analyser);

You can also assign your own AnalyserNode and canvas element

//Create AnalyserNode
let audioContext = new window.AudioContext();
let analyser = audioContext.createAnalyser();

//Connect audio to analyser
let audio = document.getElementById("audio");
let mediaSource = audioContext.createMediaElementSource(audio);
mediaSource.connect(analyser);

//Get canvas element
let canvas = document.getElementById("canvas");

//Create visualizer
let visual = new Visualize.FrequencyGraphBlocks({ analyser, canvas });

Visualizer Properties

All visualizers have the following properties:

  • analyser {AnalyserNode}: The AnalyserNode the visualizer will use to get data about the audio.
  • canvas {HTMLCanvasElement}: The canvas element the visual will use to render the visual.
  • fps {number}: The frames per second that the visualition should run at. Defaults to the highest possible fps in the browser, which is typically around 60 fps.
  • aspectRatio { {height: number , width: number } }: The aspect ratio to use when sizing the canvas. Defaults to 16:9.
  • backgroundColor {string | string[]}: The background color to use for the visualization.
  • color {string | string[]}: The foreground color to use for the visualization.

All properties can be set directly

let visual = Visualize.Oscillope();
visual.analyser = customAnalyser;
visual.canvas = customCanvas;
visual.lineWidth = 5;
visual.backgroundColor = ["#333333", "#DDDDDD"];
visual.color = "aqua";

as well as set when the object is created.

let visual = Visualize.Oscillope({
    analyser: customAnalyser,
    canvas: customCanvas,
    lineWidth: 5,
    backgroundColor: ["#333333", "#DDDDDD"],
    color: "aqua",
});

Available Visualizers

Here is a list of all available visualizers as well as any additional properties they may have:

Oscillope

  • lineWidth {number}: The width of the Oscillope's line in pixels.

FrequencyCurve

  • lineWidth {number}: The width of the Frequency Curve's line in pixels.
  • fillColor {string | string[]}: The color / array of color to fill the space under the curve.

FrequencyGraph

  • columnCount {number}: The number of columns or bars to use in the visualization.
  • gap {number}: The amount of space (in pixels) between each column.

FrequencyGraphBlocks

  • columnCount {number}: The number of columns or bars to use in the visualization.
  • rowCount {number}: The number of blocks each column will be split into.
  • gap {number}: The amount of space (in pixels) between each column and each row.

Visualizer Callbacks

You can add a callback to a visualizer that will run at a specific point each time a frame of the visualization is rendered.

let visual = new Visualize.FrequencyCurve({ analyser, canvas });
visual.on("setUpForeground", (canvasContext) => {
    canvasContext.shadowColor = "#CCCCCC";
    canvasContext.shadowBlur = 15;
});

Available Callback Events

  • setUpForeground: Runs right before the foreground element is rendered.

Examples

Live Examples

View a live example here.

Code Snippets

Connect a visualizer to an HTML audio element:

//Get audio element
let audio = document.getElementById("audio");

//Create Visualizer
let visual = new Visualize.FrequencyGraphBlocks.fromMediaElement(audio);
visual.backgroundColor = ["#333333", "#CCCCCC"];
visual.color = ["orange", "gold", "yellow"];

//Add canvas to document
document.getElementById("container").append(visual.canvas);

//Start Audio
audio.play();

Make a visualizer fill an html element:

let container = document.getElementById("container");
let visual = new Visualize.Oscillope();
container.append(visual.canvas);
visual.aspectRatio = {
    width: container.clientWidth,
    height: container.clientHeight,
};

Resize a visualizer when the window resizes:

let visual = new Visualize.FrequencyGraph({ analyser, canvas });

window.addEventListener("resize", () => {
    visual.resize();
});