4

GitHub - barhatsor/joycon.js: Add controller functionality to your JavaScript ga...

 2 years ago
source link: https://github.com/barhatsor/joycon.js
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Joycon.js

Add controller functionality to your JavaScript game.

Get it

Add this line to the <body>:

<script src="https://cdn.cde.run/Joycon.min.js"></script>

Here's a demo.

Buttons

const controllers = Joycon.controllers;

controllers.on.press('start', (value) => {

  console.log('pressed start!', value); // value is 0 to 1

});

Button can be a, b, x, y, start, left-trigger, etc. (full list below)

Joystick

const controllers = Joycon.controllers;

controllers.on.move('left-joystick', (value) => {

  console.log('moved left joystick!', value.x, value.y); // value is -1 to 1 down/right

});

Also right-joystick.

Controller connected

const controllers = Joycon.controllers;

controllers.on.connect((controller) => {

  console.log('controller connected!', controller);

});

controllers.on.disconnect((controller) => {

  console.log('controller disconnected!', controller);

});

Vibrate

const controllers = Joycon.controllers;

// preset can be mild, medium or strong
controllers.vibrate({ preset: 'medium' }, 250); // -> time in ms

// or you can choose your own values
controllers.vibrate({
  mildMotorIntensity: 0.5,
  strongMotorIntensity: 0.5
}, 250);

Remove a listener

const controllers = Joycon.controllers;

controllers.removeListener('left-trigger');

Remove Joystick move events with left-joystick-move.
Remove controller connect events with controller-connect.

Button list

Buttons:
a left-shoulder select dpad-up home
b right-shoulder start dpad-down share
x left-trigger left-joystick dpad-left
y right-trigger right-joystick dpad-right

See above on how to use these.

License

MIT


Thanks for coming by! <3


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK