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

@jesperdj/pianokeys

v1.0.4

Published

Piano keyboard rendered as SVG.

Downloads

8

Readme

PianoKeys

Piano keyboard rendered as SVG. This is not a complete app; it is meant to be used as a component in webapps.

See the demo application.

Install with npm

You can install PianoKeys as an npm package in your project:

npm i @jesperdj/pianokeys

Using PianoKeys

Create a HTML div element in your HTML or using JavaScript that will hold the keyboard.

<div id="container"></div>

Then in your script, create an instance of PianoKeys.Keyboard, passing it the container element.

import PianoKeys from '@jesperdj/pianokeys';

const container = document.getElementById('container');
const keyboard = new PianoKeys.Keyboard(container);

This will render a keyboard corresponding to a regular 88-key piano, starting at the note A0 and ending at C8.

88-key keyboard

Rendering a partial keyboard

The constructor of PianoKeys.Keyboard takes an optional second parameter, which is an object with options. You can render a partial keyboard by setting the lowest and highest properties in the options object to set the lowest and highest note (key). These are specified as a note name with an octave number, for example 'A0', 'Bb4', 'D#6'.

Note: The keyboard always starts and ends with a white key. If you specify a note name that corresponds to a black key, the next lower or higher white key will be used.

Example:

const keyboard = new PianoKeys.Keyboard(container, {
    lowest: 'C2',
    highest: 'C5'
});

Keyboard staring at C2 and ending at C5

Using custom colors

You can set the following properties in the options object to specify custom colors:

  • keyStroke - stroke style for the outline of keys
  • whiteKeyFill - fill style for the white keys
  • blackKeyFill - fill style for the black keys

Example:

const keyboard = new PianoKeys.Keyboard(container, {
    lowest: 'C2',
    highest: 'C5',
    keyStroke: '#444',
    whiteKeyFill: 'black',
    blackKeyFill: 'white'
});

Keyboard with custom colors

Highlighting keys

To highlight keys, call fillKey() on the keyboard. Example:

keyboard.fillKey('C3');
keyboard.fillKey('Bb3');
keyboard.fillKey('Eb4');
keyboard.fillKey('G4');

Keyboard with highlighted keys

The fillKey() function optionally takes a second parameter to set the fill style to use for that key instead of the default highlight style.

keyboard.fillKey('C3', 'red');

Call clearKey() to unhighlight a key.

keyboard.clearKey('C3');

If you want to use a custom highlight fill style but you don't want to specify it in each call to fillKey(), then you can set the default highlight fill style by adding the following properties to the options that you pass to the constructor:

  • whiteKeyHighlightFill - default highlight fill style for white keys
  • blackKeyHighlightFill - default highlight fill style for black keys

Example:

const keyboard = new PianoKeys.Keyboard(container, {
    lowest: 'C2',
    highest: 'C5',
    keyStroke: '#444',
    whiteKeyFill: 'black',
    whiteKeyHighlightFill: 'yellow',
    blackKeyFill: 'white',
    blackKeyHighlightFill: 'orange'
});

Keyboard with custom default highlight colors