Skip to content

Navigation Menu

Sign in
Sign up

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

TouchPad

A tiny, dependency-free JavaScript library for adding on-screen touch joysticks ("touchpads") to mobile web games and apps. Each touchpad renders a pot (the outer base) with a draggable stick, and reports position, angle, and power back to your code through callbacks.

  • No dependencies. ~200 lines of plain JS.
  • UMD wrapper — works with an AMD loader (define) or as a global window.TouchPad.
  • Reports x/y offset, angle (radians or degrees), and power (0–1) on every touch event.
  • Multiple pads on one screen (e.g. move + aim); retrieve them all with TouchPad.getPads().
  • Optional built-in "simple" theme, or style it yourself via CSS classes.

Install

Drop the file into your project and include it:

<script src="TouchPad.js"></script>

Or load it as an AMD module named TouchPad.

Usage

var pad = TouchPad({
 parent: document.querySelector('#controls'), // defaults to <body>
 pot: { radius: 75, theme: 'simple' }, // outer base
 stick: { radius: 40, theme: 'simple' }, // draggable knob
 cb: {
 start: function (s) { /* touch down */ },
 move: function (s) { /* dragging */ },
 end: function (s) { /* released */ },
 },
});

Each callback receives a state object:

Field Meaning
x, y Stick offset from center, in pixels
power Distance from center as a ratio, 01
rads Stick angle in radians
degs Stick angle in degrees

On release the stick recenters automatically and end fires with a zeroed state.

Styling

The pot and stick get the CSS classes tc_pot and tc_stick. Either use the bundled theme: 'simple' preset or leave the theme off and style those classes yourself.

API

Call Description
TouchPad(opts) Create and mount a touchpad, returns the instance
instance.show() / instance.hide() Toggle visibility
instance.centerStick() Recenter the stick programmatically
instance.getStickAngle('rads'|'degs') Current angle
instance.getPower() Current power, 01
TouchPad.getPads() Array of all created touchpads

License

MIT — see LICENSE.

About

A simple class for adding mobile touch controls to a project

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /