JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 42
  • Score
    100M100P100Q48474F
  • License MIT

A slider, except it's round

Package Exports

  • circle-slider

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (circle-slider) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

circle-slider

A slider, except it's round. Pretty good for selecting angles and stuff.

Demo here.

Install

$ npm i circle-slider

Usage

Basic usage:

HTML:

<div id="whatever"></div>

JS:

// Import the module at the top of your file
const CircleSlider = require("circle-slider");

// or ES6 style
import CircleSlider from "circle-slider";

// The id of your target element as the first arg
const cs = new CircleSlider("whatever");

All you have to do on the HTML side is to create a div with some id, which is the first parameter in the constructor of CircleSlider.

All you need is to create a div, the elements for the handle (and its container) will be created for you.

You can put anything you want inside the div, e.g. a div which displays the current angle (see the demo).

Remember to use Browserify/Webpack/whatever to handle the import.

Options

You can make the handle snap to multiples of some number, e.g. snap to every multiple of 45 by passing 45 as the optional second parameter.

new CircleSlider("id", 45);

You can also write the first parameter with a #:

new CircleSlider("#id");

Both work the same, so choose which one you prefer.

Methods

Here is the stuff you can do with CircleSlider:

// get the current angle value
cs.getAngle();

// set the angle manually
cs.setAngle(45);

// fires every time the slider is moved
// the emitted event will give you the angle
cs.on("sliderMove", (angle) => {
    someDiv.textContent = angle;
})

// fires every time the handle stops moving
// can be useful to avoid heavy operations every time the
// handle moves
cs.on("sliderUp", (angle) => {
    someDiv.textContent = angle;
})

Since CircleSlider extends eventemitter3, you can also use functions like once instead of on.

Further reading here (eventemitter3 uses the same API as node events).

Styling

I leave most styling up to you! However, there are some CSS rules that you must use in order for the handle two rotate properly and stuff. Here are some good defaults to get started with:

#slider {
  /*
    position: relative is needed for the handle to be
    positioned correctly, and border-radius: 100% just
    makes the div round.
  */
  position: relative;
  border-radius: 100%;

  /* Other than the above two, go wild! */
  height: 300px;
  width: 300px;
  background: linear-gradient(90deg, #FF9A9E, #FAD0C4);
}

/*
  Probably best to paste this exactly as is.
  These CSS rules make sure that the handle rotates
  properly, so don't change anything here.
*/
.cs-handle-container {
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 2px;
  margin-top: -1px;
}

/* Also paste as is */
.cs-handle {
  position: absolute;
  transform: translateY(-50%);
}

/* the appearance of the handle, feel free to change! */
#slider .cs-handle {
  height: 30px;
  width: 30px;
  /*
    Change 'right' to change the offset from the edge.
    E.g right: 0 puts the handle just next to the edge
    of #slider, on the inside
  */
  right: -15px;
  cursor: default;
  border-radius: 100%;
  background: linear-gradient(180deg, #FFFFFF, #efefef);
  box-shadow: rgba(0, 0, 0, 0.3) 0 1px 10px 0;
}

#slider .cs-handle:active {
  background: linear-gradient(180deg, #EBEBEB, #DFDFDF)
}