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

@phbalance/d3-piano

v0.2.0

Published

Configurable SVG piano drawn using D3.

Downloads

9

Readme

A D3 library for drawing a piano

Configurable SVG piano drawn with generated events for key press and release using D3. Implemented using TypeScript.

Installation

npm install --save @phbalance/d3-piano
npm install --save d3-selection # peer dependency
npm install --save d3-scale # peer dependency

Use

The drawing of the piano into an SVG element is achieved by calling drawPiano. Provide the svg element, a callback to be invoked when an action is performed on a key (i.e. event generated for "key" pressed or depressed), and configuration details. The configuration includes:

export enum PianoKeyLabel {
	NONE = 0,
	SIMPLE = 1,
	FULL = 2,
}

export interface IPianoDrawConfig {
	steps: string[];
	octaves: number[];
	invalidNotes: string[];

	stopKeyPressWithExit: boolean;
    
	keyLabel: PianoKeyLabel;

	width: number;
}

Configuration Parameters

invalidNotes is a list of notes to exclude from the provided set of steps and octaves.

keyLabel indicates what kind of label the key should have: none, a simple note name, or a full name of octave plus the simple note name.

width is the width of the piano drawn in the svg in pixels.

octaves is an array of which octaves to draw.

steps is an array of the names of the keys starting from left most (lowest frequency) key.

stopKeyPressWithExit indicates if mouseout or pointerout events should be bound to the keys. PointerEvents are a consolidation of mouse and touch events. If true, if the mouse/pointer cursor leaves the bounds of the piano key it will be the same as if the key were no longer pressed even if the mouse is still held down. If false there is no mouseout or pointerout event bound. Thus by pushing the mouse down, moving the cursor out of that key, and then releasing the mouse you will not generate a key released message. For touch devices you probably just need to drag your finger a bit. You can use this approach to crudely generate chords.

Example

An example of use:

import { drawPiano, IKey, PianoKeyAction, PianoKeyLabel } from "@phbalance/d3-piano";

...

const svgToDrawInto = do something to create the SVG you want the piano to be drawn into

function keyAction(action: PianoKeyAction, keyInfo: IKey): void {
    if(action === PianoKeyAction.PRESS_START) {
        // Do stuff. Perhaps generate a tone?
    } else {
        // Do stuff. Perhaps turn off a tone?
    }
}

const config: IKey = {
    steps: ["C", "D", "E", "F", "G", "A", "B"],
    octaves: [3, 4, 5, 6, 7, 8], // Computer speakers probably can't reproduce octaves 0 & 1 & most of 2 so ignore them
    invalidNotes: [ // For an 88 key keyboard
        "0C", "0D", "0E", "0F", "0G",
        "8C", "8D", "8E", "8F", "8G", "8A", "8B",
    ],
    stopKeyPressWithExit: true,
    keyLabel: PianoKeyLabel.NONE,
    width: 800
};

// Note, if you're using a class method as the passed in callback you may need to do something like this.keyAction.bind(this)
drawPiano(svgToDrawInto, keyAction, config);

Touch Support

Touch support via PointerEvents was added in version 0.1.1.

Being Notified of a Key Press

As mentioned above, the 2nd parameter of drawPiano is a function that is called as a result of keys being pressed by a user.

Programmatically Generating a Key Press

This was added in 0.1.1.

Normally, a user will use an input device to manipulate the keyboard elements. However, it is possible to programmatically generate key presses in a way similar to a player piano moves keys without the user. For this use the press and release keys of the object returned by drawPiano.

Both the press and release methods have the same signature:

type CreatePianoKeyActionFunction = (containingSvgEle: SVGSVGElement, note: string, octave?: number) => void;

Thus, provide the svg element that you passed into drawPiano as the containingSvgEle and then the note and, optionally, octave. If you do not provide a value for octave then any octave will be used and multiple key presses will be genereated at the "same time" (technically sequential mouse events).

Reporting Issues

You can report bugs here. Feel free to make suggestions as well.