react-boop
Version:
A lightweight, responsive virtual joystick for React.
91 lines (81 loc) • 4.42 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.reactBoop = {}, global.React));
})(this, (function (exports, React) { 'use strict';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
function VirtualJoystick(_a) {
var onChange = _a.onChange, _b = _a.style, style = _b === void 0 ? {} : _b, _c = _a.size, size = _c === void 0 ? 96 : _c;
var joystickRef = React.useRef(null);
var _d = React.useState(false), dragging = _d[0], setDragging = _d[1];
var _e = React.useState(null), origin = _e[0], setOrigin = _e[1];
// useEffect(() => {
// console.log("VirtualJoystick component mounted!");
// }, []);
var handleTouchStart = function (e) {
var touch = e.touches[0];
setOrigin({ x: touch.clientX, y: touch.clientY });
setDragging(true);
};
var handleTouchMove = function (e) {
if (!dragging || !origin)
return;
var touch = e.touches[0];
var deltaX = touch.clientX - origin.x;
var deltaY = touch.clientY - origin.y;
var maxDistance = size / 2;
var dx = deltaX, dy = deltaY;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > maxDistance) {
var angle = Math.atan2(dy, dx);
dx = Math.cos(angle) * maxDistance;
dy = Math.sin(angle) * maxDistance;
}
onChange({ x: dx / maxDistance, y: -dy / maxDistance });
};
var handleTouchEnd = function () {
setDragging(false);
setOrigin(null);
onChange({ x: 0, y: 0 });
};
return (React.createElement("div", { ref: joystickRef, role: "button", style: __assign({ width: size || 96, height: size || 96, backgroundColor: "#D1D5DB", borderRadius: "50%", position: "absolute", top: "50%", left: "50%", transform: "translate(-50%, -50%)", opacity: 0.5, touchAction: "none" }, style), onTouchStart: handleTouchStart, onTouchMove: handleTouchMove, onTouchEnd: handleTouchEnd },
React.createElement("div", { style: {
width: (size || 96) / 2.5, // Safe fallback
height: (size || 96) / 2.5,
backgroundColor: "#374151",
borderRadius: "50%",
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
} })));
}
exports.VirtualJoystick = VirtualJoystick;
}));
//# sourceMappingURL=index.umd.js.map