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

joystick-controller

v1.1.1

Published

A JavaScript virtual joystick controller for both desktop and mobile devices.

Downloads

5,868

Readme

Joystick Controller

npm GitHub License buyMeACoffee

joystick-controller

A fully customizable JavaScript virtual joystick controller for both desktop and mobile devices supporting multi instances. Live Demo

Installation

npm install joystick-controller

Quick Start

import JoystickController from "joystick-controller";

const joystick = new JoystickController({}, (data) => console.log(data));

The import example here is module style import. JoystickController also supports global and commonJs import style.

Options

You can pass a set of options as the first argument to further customize your joystick controller

| Name | Type | Default | Description | | --------------------- | -------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | | maxRange | number | 100 | Maximum range of the joystick dot (number of pixels) | | level | number | 10 | Number of level of the joystick (eg 10 would return integers between -10 and 10) | | radius | number | 50 | Radius of the joystick container (number of pixels) | | joystickRadius | 30 | number | Radius of the joystick inner dot (number of pixels) | | opacity | number | 0.8 | Opacity of the joystick | | containerClass | string | '' | Class for the joystick container for adding additional styles (outer container) | | controllerClass | string | '' | Class for the joystick controller for adding additional styles (inner container) | | joystickClass | string | '' | Class for the joystick dot for adding additional styles | | leftToRight | boolean | true | Left to right adjustment (x position from left)(ignored if dynamicPosition=true) | | bottomToUp | boolean | true | Bottom to up adjustment (y position from bottom)(ignored if dynamicPosition=true) | | x | string | '50%' | x position of the joystick controller on screen (equal to left/right of css)(ignored if dynamicPosition=true) | | y | string | '50%' | y position of the joystick controller on screen (equal to bottom/top of css)(ignored if dynamicPosition=true) | | distortion | boolean | false | if true, the joystick will be distorted when the dot is moved to the edge of the joystick | | dynamicPosition | boolean | false | Shows the joystick when the user touch/click on the screen at the position where it was clicked/touched | | dynamicPositionTarget | HTMLElement | null | If dynamicPosition true, uses this target to set the event listener on (if not provided use document) | | mouseClickButton | string|number | "ALL" (-1) | click button to show the joystick (if not provided, shows on all clicks)(Values: ALL, LEFT, MIDDLE, RIGHT, or button number (-1 to 4. -1 for all)) | | hideContextMenu | boolean | false | if true, hides the context menu on right click (Recommended to be used dynamicPosition and dynamicPositionTarget) |

Callback Arguments

Joystick would trigger the callback on each move event. The following arguments are passed to the callback. | Name | Type | Description | | ------------- | ------------- | ------------- | | x | number | x position of the joystick relative to the center of it | y | number | y position of the joystick relative to the center of it | leveledX | number | x position scaled and rounded to be a step between -level to level (level comes from options) | leveledY | number | y position scaled and rounded to be a step between -level to level (level comes from options) | angle | number | angle of the line between center of the joystick and position of the dot in radians | distance | number | distance of the dot from the center joystick

Customized Example

All the options are optional, but a customized instance would look like this:

// Static Example
const staticJoystick = new JoystickController(
  {
    maxRange: 70,
    level: 10,
    radius: 50,
    joystickRadius: 30,
    opacity: 0.5,
    leftToRight: false,
    bottomToUp: true,
    containerClass: "joystick-container",
    controllerClass: "joystick-controller",
    joystickClass: "joystick",
    distortion: true,
    x: "25%",
    y: "25%",
    mouseClickButton: "ALL",
    hideContextMenu: false,
  },
  ({ x, y, leveledX, leveledY, distance, angle }) =>
    console.log(x, y, leveledX, leveledY, distance, angle)
);
// Dynamic Position Example
const dynamicJoystick = new JoystickController(
  {
    maxRange: 70,
    level: 10,
    radius: 50,
    joystickRadius: 30,
    opacity: 0.5,
    containerClass: "joystick-container",
    controllerClass: "joystick-controller",
    joystickClass: "joystick",
    distortion: true,
    dynamicPosition: true,
    dynamicPositionTarget: document.getElementById("root"),
    mouseClickButton: "ALL",
    hideContextMenu: true,
  },
  ({ x, y, leveledX, leveledY, distance, angle }) =>
    console.log(x, y, leveledX, leveledY, distance, angle)
);

Clean Up

Run destroy function to clear all the event listeners and remove the joystick from the document

joystick.destroy();

Copyright(c) Cyrus Mobini 2023

License MIT