MIT License

项目介绍

vaneenige/unswitch 是一个托管在 GitHub 上的开源项目,主要使用 JavaScript 编写,采用 MIT License 许可证,目前获得 571 个 Star、10 次 Fork。

仓库全名
vaneenige/unswitch
开源许可证
MIT License
Star 数
571
Fork 数
10

README 摘要

Unswitch Unswitch is a tiny event handler for Switch controllers on the web based on the Gamepad API. Attach callbacks to button presses (up & down) and the joystick! Install Use script tags or modules? Check out the version on unpkg! Setup Connecting a Switch controller is easy: pair it with bluetooth and you're ready to go! This library doesn't listen to connected or disconnected events but in some cases it might…

完整内容见 GitHub 原始 README 或本页 。

常见问题

unswitch 用什么语言开发?

主要使用 JavaScript 编写,采用 MIT License 许可证,目前有 571 个 Star。

unswitch 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 unswitch 的源码?

可以直接克隆仓库:git clone https://github.com/vaneenige/unswitch.git,默认分支为 master;安装与运行步骤见仓库 README。

unswitch 最近还在维护吗?

仓库最近一次更新为 2018年8月10日,当前没有未关闭的 Issue;Star 数为 571,可作为社区活跃度的参考。

关于

🕹 A tiny event handler for Switch controllers!

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
596
价值分
346
活跃状态
活跃

语言占比

JavaScript

Release

README

Unswitch

npm version gzip size license dependencies

Unswitch is a tiny event handler for Switch controllers on the web based on the Gamepad API. Attach callbacks to button presses (up & down) and the joystick!

Install

$ npm install --save unswitch

Use script tags or modules? Check out the version on unpkg!

Setup

Connecting a Switch controller is easy: pair it with bluetooth and you’re ready to go! This library doesn’t listen to connected or disconnected events but in some cases it might be useful. Here’s how you can listen to these events:

window.addEventListener("gamepadconnected", ({ gamepad }) => {});
window.addEventListener("gamepaddisconnected", ({ gamepad }) => {});

Usage

// Import the library
import Unswitch from 'unswitch';

// Observe a controller
const unswitch = new Unswitch({
  side: 'L', // or R
  buttons: (button, pressed, side) => console.log(`Catch-all - button: ${button} was ${pressed ? 'pressed' : 'released'} on the ${side} side`),
  b: p => {},
  a: p => {},
  y: p => {},
  x: p => {},
  sl: p => {},
  sr: p => {},
  minus: p => {},
  plus: p => {},
  lstick: p => {},
  rstick: p => {},
  home: p => {},
  screenshot: p => {},
  bumper: p => {}, // L or R
  trigger: p => {}, // ZL or ZR
  axes: p => {},
});

function render() {
  // Call the update function manually
  unswitch.update();
  requestAnimationFrame(render);
}

render();

Please note that it’s not required to pass all button-functions to Unswitch and will only be executed when you provide them. You are able to use the buttons function to catch all button presses and implement your own logic using the provided data. The buttons function will always be executed when provided, even when the button is also passed as property.

It’s possible to connect up to two controllers at the same time. To make this work side is to be passed with either L (left) or R (right) for the controllers respectively. Calling unswitch.update() will check every button for a change in state. If a callback is provided the new state is passed along. The axis works in the same way, but instead of a boolean it will return a number from 0 to 8. Number 0 to 7 are for the joystick positions going clockwise, number 8 is used as default (center).

Have a look at the demo on Codepen!

Contribute

Are you excited about this library and have interesting ideas on how to improve it? Please tell me or contribute directly! 🙌

npm install > npm run demo > http://localhost:8080

License

MIT © Colin van Eenige

评论

暂无评论