pixijs-input-devices 0.1.3 â 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -571
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +159 -117
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# đšī¸ pixijs-input-devices  
|
|
1
|
+
# đšī¸ pixijs-input-devices  [](https://www.npmjs.com/package/pixijs-input-devices) [](https://bundlephobia.com/package/pixijs-input-devices) [](https://www.npmjs.com/package/pixijs-input-devices) [](https://github.com/reececomo/pixijs-input-devices/actions/workflows/tests.yml) [](https://github.com/reececomo/pixijs-input-devices/blob/main/LICENSE)
|
|
2
2
|
|
|
3
3
|
WIP
|
|
4
4
|
|
|
5
5
|
- Adds support for â¨ī¸ **Keyboard**, đŽ **Gamepads**, and other human-interface devices
|
|
6
|
-
- A simple `Navigation` API which hooks devices into existing pointer events
|
|
7
|
-
- A powerful event-based API for event-driven
|
|
6
|
+
- A simple `Navigation` API which hooks devices into existing pointer/mouse events
|
|
7
|
+
- A powerful event-based API for event-driven interactions
|
|
8
8
|
- and of course, a high-performance API for real-time applications
|
|
9
9
|
|
|
10
10
|
<hr/>
|
|
@@ -15,33 +15,19 @@ WIP
|
|
|
15
15
|
npm i pixijs-input-devices
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
#### Setup
|
|
19
19
|
|
|
20
20
|
```ts
|
|
21
|
-
|
|
22
|
-
Ticker.shared.add(() =>
|
|
23
|
-
InputDevice.shared.update()
|
|
24
|
-
);
|
|
21
|
+
import { InputDevice, Navigation } from "pixijs-input-devices"
|
|
25
22
|
|
|
26
|
-
//
|
|
27
|
-
registerPixiJSInputDeviceMixin( Container )
|
|
23
|
+
// register mixin
|
|
24
|
+
registerPixiJSInputDeviceMixin( Container )
|
|
28
25
|
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### Event API
|
|
26
|
+
// add update loop
|
|
27
|
+
Ticker.shared.add( () => InputDevice.update() )
|
|
34
28
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
InputDevice.gamepads[0].on( Button.A, () =>
|
|
38
|
-
alert("I've been pressed!")
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
// or the common labels
|
|
42
|
-
InputDevice.gamepads[0].on( "Back", () =>
|
|
43
|
-
alert("Let's get out of here.")
|
|
44
|
-
);
|
|
29
|
+
// enable nav
|
|
30
|
+
Navigation.stage = app.stage
|
|
45
31
|
```
|
|
46
32
|
|
|
47
33
|
### Realtime API
|
|
@@ -49,594 +35,184 @@ InputDevice.gamepads[0].on( "Back", () =>
|
|
|
49
35
|
Simple example:
|
|
50
36
|
|
|
51
37
|
```ts
|
|
52
|
-
let jump = false
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
for ( const device of InputDevice.shared.devices )
|
|
57
|
-
{
|
|
58
|
-
if ( device.type === "keyboard" )
|
|
59
|
-
{
|
|
60
|
-
if ( device.key.ArrowUp ) jump = true
|
|
61
|
-
if ( device.key.ArrowDown ) hide = true
|
|
62
|
-
if ( device.key.ArrowLeft ) moveX = -1.0
|
|
63
|
-
if ( device.key.ArrowRight ) moveX = 1.0
|
|
64
|
-
}
|
|
38
|
+
let jump = false,
|
|
39
|
+
hide = false,
|
|
40
|
+
moveX = 0.0;
|
|
65
41
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
42
|
+
// keyboard:
|
|
43
|
+
const kb = InputDevice.keyboard;
|
|
44
|
+
if ( kb.key.ArrowUp ) jump = true
|
|
45
|
+
if ( kb.key.ArrowDown ) hide = true
|
|
46
|
+
if ( kb.key.ArrowLeft ) moveX = -1.0
|
|
47
|
+
else if ( kb.key.ArrowRight ) moveX = 1.0
|
|
71
48
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
#### Assigning devices
|
|
78
|
-
|
|
79
|
-
```ts
|
|
80
|
-
InputDevice.shared.on("deviceconnected", ({ device }) => {
|
|
81
|
-
device.meta.localPlayerId = 123;
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
for ( const device of InputDevice.shared.devices )
|
|
49
|
+
// gamepads:
|
|
50
|
+
for ( const gamepad of InputDevice.gamepads )
|
|
85
51
|
{
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
52
|
+
if ( gamepad.button.A ) jump = true
|
|
53
|
+
if ( gamepad.rightTrigger > 0.25 ) hide = true
|
|
54
|
+
if ( gamepad.leftJoystick.x !== 0.0 ) moveX = gamepad.leftJoystick.x
|
|
90
55
|
}
|
|
91
56
|
```
|
|
92
57
|
|
|
93
|
-
|
|
94
|
-
> You may also use `device.button.RightTrigger` to detect triggers as a button press.
|
|
95
|
-
|
|
96
|
-
> [!NOTE]
|
|
97
|
-
> You can use `device.pressingAll([...])` and `device.pressingAny([...])` to check groups of keys/buttons.
|
|
98
|
-
|
|
99
|
-
> [!NOTE]
|
|
100
|
-
> You can use `device.assignee` to store any device assignment-related data (such as a user ID).
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
### Event-driven API
|
|
58
|
+
### Event API
|
|
104
59
|
|
|
105
60
|
```ts
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
InputDevice.gamepads.bind({
|
|
111
|
-
Back: () => {
|
|
112
|
-
|
|
113
|
-
}
|
|
61
|
+
// listen to device events
|
|
62
|
+
InputDevice.on( "deviceconnected", ({ device }) => {
|
|
63
|
+
console.debug( "A new " + device.type + " device connected!" ) // "keyboard" | "gamepad" | "custom"
|
|
114
64
|
})
|
|
115
65
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
keydown: () => {},
|
|
120
|
-
keyup: () => {},
|
|
121
|
-
}
|
|
66
|
+
// keyboard events
|
|
67
|
+
InputDevice.keyboard.on( "layoutdetected", ({ layout }) => {
|
|
68
|
+
console.debug( "layout detected as " + layout ); // "JCUKEN" | "AZERTY" | "QWERTZ" | "QWERTY"
|
|
122
69
|
})
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
By default, any element with "mousedown" or "pointerdown" will
|
|
126
|
-
|
|
127
|
-
Container events | description
|
|
128
|
-
------------------|--------------------------------------------------------
|
|
129
|
-
`focus` | Target became focused. args: direction: { x, y }
|
|
130
|
-
`blur` | Target lost focus. args: direction: { x, y }
|
|
131
|
-
`focusHinted` | Target may be about to become focused. args: direction: { x, y }
|
|
132
|
-
`blurHinted` | Target may be about to lose focus. args: direction: { x, y }
|
|
133
|
-
|
|
134
|
-
Container properties | description
|
|
135
|
-
---------------------|-----------------------------------------------------
|
|
136
|
-
`focusable` | boolean?, default: `undefined`
|
|
137
|
-
`isFocusGroup` | boolean?, default: `false`
|
|
138
|
-
`focusPriority` | number?, default: `0`
|
|
139
|
-
`focusGroup` | PIXI.Container?, default: `app.stage ?? undefined`
|
|
140
|
-
`focusTransform` | `{ x, y }`, default: `{ x: 5, y: 5 }`
|
|
141
|
-
`isFocusable` | get () => boolean, returns true if "focusable" = true, or InputDevice.defaultOptions.autoFocusable and has "mousedown", "pointerdown"
|
|
142
|
-
|
|
143
|
-
Device gesture | Keyboard (primary) | Keyboard (alt) | Gamepad
|
|
144
|
-
-----------------|--------------------|-----------------|------------------
|
|
145
|
-
`"moveFocusLeft"` | Left Arrow Key | A Key | Left Stick Left
|
|
146
|
-
`"moveFocusRight"` | Right Arrow Key | D Key | Left Stick Right
|
|
147
|
-
`"moveFocusUp"` | Up Arrow Key | W Key | Left Stick Up
|
|
148
|
-
`"moveFocusDown"` | Down Arrow Key | S Key | Left Stick Down
|
|
149
|
-
`"focusover"` | Return Key | Space | A Button
|
|
150
|
-
`"moveUpOneLevel"` | Escape Key | Backspace | B Button
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
### Global defaults:
|
|
154
|
-
|
|
155
|
-
```ts
|
|
156
|
-
InputDevice.defaultOptions = {
|
|
157
|
-
/** Globally enable/disable the navigation API. (i.e. if the browser loses focus). */
|
|
158
|
-
focusEnabled: true,
|
|
159
|
-
/**
|
|
160
|
-
* (default: true) When enabled, any element with "mousedown" or "pointerdown"
|
|
161
|
-
* is considered focusable. The "focusableTransform" will also apply.
|
|
162
|
-
*/
|
|
163
|
-
autoFocusable: true,
|
|
164
|
-
focusableTransform: { x: 5, y: 5 },
|
|
165
|
-
keyboard: {
|
|
166
|
-
enabled: true,
|
|
167
|
-
layouts: [ "qwerty", "azerty", "jcuken", "qwertz" ],
|
|
168
|
-
autoLanguageLayout: true, // fr*/nl-BE* -> azerty, uk*/ru* -> jcuken
|
|
169
|
-
internationalization: {
|
|
170
|
-
/** when true, labels are mapped to their layout */
|
|
171
|
-
mapLabels: true,
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
gamepad: {
|
|
175
|
-
enabled: true,
|
|
176
|
-
layouts: [ "standard", "nintendo" ],
|
|
177
|
-
firstIntentCooldownMs: 400,
|
|
178
|
-
intentCooldownMs: 100,
|
|
179
|
-
},
|
|
180
|
-
}
|
|
181
|
-
```
|
|
70
|
+
InputDevice.keyboard.on( "Escape", () => showMenu() )
|
|
182
71
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
72
|
+
// gamepad events
|
|
73
|
+
InputDevice.gamepads[0].on( Button.Back, () => showMenu() )
|
|
74
|
+
InputDevice.gamepads[0].on( "Back", () => showMenu() )
|
|
187
75
|
|
|
188
76
|
```
|
|
189
77
|
|
|
78
|
+
#### InputAction Events
|
|
190
79
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
⥠Powerful, high-performance animations for PixiJS
|
|
80
|
+
| Event | Description | Payload |
|
|
81
|
+
|---|---|---|
|
|
82
|
+
| `"deviceconnected"` | `{device}` | A device has become available. |
|
|
83
|
+
| `"devicedisconnected"` | `{device}` | A device has been removed. |
|
|
197
84
|
|
|
198
|
-
|
|
199
|
-
| ------ | ------ |
|
|
200
|
-
| đŽ Simple, declarative API | đŦ Based on [Cocos2d](https://docs.cocos2d-x.org/cocos2d-x/v3/en/actions/getting_started.html)/[SKActions](https://developer.apple.com/documentation/spritekit/getting_started_with_actions) |
|
|
201
|
-
| đ 35+ [built-in actions](#action-initializers), 30+ [timing modes](#timing-modes) | đ Reuseable, chainable & reversible |
|
|
202
|
-
| đ No dependencies & tree-shakeable | â Full speed/pausing control |
|
|
203
|
-
| đ¤ `~4.3kb` minzipped | ⨠Supports PixiJS 8+, 7+, 6.3+ |
|
|
85
|
+
#### Keyboard Events
|
|
204
86
|
|
|
87
|
+
| Event | Description | Payload |
|
|
88
|
+
|---|---|---|
|
|
89
|
+
| `"layoutdetected"` | `{layout,layoutSource,device}` | The keyboard layout (`"QWERTY"`, `"QWERTZ"`, `"AZERTY"`, or `"JCUKEN"`) has been detected, either from the native API or from keypresses. |
|
|
90
|
+
| **Key presses:** | | |
|
|
91
|
+
| `"KeyA"` \| `"KeyB"` \| ... 103 more ... | `{event,keyCode,keyLabel,device}` | `"KeyA"` was pressed. |
|
|
205
92
|
|
|
206
|
-
|
|
93
|
+
#### Gamepad Events
|
|
207
94
|
|
|
208
|
-
|
|
95
|
+
| Event | Description | Payload |
|
|
96
|
+
|---|---|---|
|
|
97
|
+
| **Button presses:** | | |
|
|
98
|
+
| `"A"` \| `"B"` \| `"X"` \| ... 13 more ... | `{button,buttonCode,device}` | Button `"A"` was pressed. Equivalent to `0`. |
|
|
99
|
+
| ... | ... | ... |
|
|
100
|
+
| **Button presses (no label):** | | |
|
|
101
|
+
| `0` \| `1` \| `2` \| ... 13 more ... | `{button,buttonCode,device}` | Button `0` was pressed. Equivalent to `"A"`. |
|
|
102
|
+
| ... | ... | ... |
|
|
209
103
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
Action.fadeOut(0.2).easeIn(),
|
|
215
|
-
Action.removeFromParent(),
|
|
216
|
-
Action.run(() => console.info('⨠done!'))
|
|
217
|
-
]);
|
|
218
|
-
|
|
219
|
-
// Run an action
|
|
220
|
-
mySprite.run(spinAndRemove);
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
## Getting Started with PixiJS Actions
|
|
224
|
-
|
|
225
|
-
*Everything you need to quickly build beautiful animations.*
|
|
226
|
-
|
|
227
|
-
**PixiJS Actions** is based off the idiomatic and expressive [**SKActions API**](https://developer.apple.com/documentation/spritekit/getting_started_with_actions), extending `Container` to add first-class support for running and managing actions.
|
|
228
|
-
|
|
229
|
-
The core concepts are:
|
|
104
|
+
> [!TIP]
|
|
105
|
+
> **Multiplayer:** For multiple players, consider assigning devices
|
|
106
|
+
> using `device.meta` (e.g. `device.meta.player = 1`) and use
|
|
107
|
+
> `InputDevice.devices` to iterate through devices instead.
|
|
230
108
|
|
|
231
|
-
|
|
232
|
-
2. **Actions:** _Stateless, reusable recipes_ (e.g. animations, triggers, and more)
|
|
233
|
-
3. **TimingMode & speed:** _Controls for the speed & smoothness of actions and animations_
|
|
109
|
+
##### Gamepad Layouts
|
|
234
110
|
|
|
235
111
|
> [!NOTE]
|
|
236
|
-
>
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
**
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
>
|
|
267
|
-
>
|
|
112
|
+
> **Gamepad Labels:** The gamepad buttons are aliased with generic standard controller buttons in a Logitech/Xbox/Steam controller layout.
|
|
113
|
+
|
|
114
|
+
| Button | ButtonCode | Name | Generic | Nintendo<br/>(*physical) | Playstation | Xbox |
|
|
115
|
+
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
|
|
116
|
+
| `0` | `"A"` | **A** | A | B | Cross | A |
|
|
117
|
+
| `1` | `"B"` | **B** | B | A | Circle | B |
|
|
118
|
+
| `2` | `"X"` | **X** | X | Y | Square | X |
|
|
119
|
+
| `3` | `"Y"` | **Y** | Y | X | Triangle | Y |
|
|
120
|
+
| `4` | `"LeftShoulder"` | **Left Shoulder** | LeftShoulder | L | L1 | LB |
|
|
121
|
+
| `5` | `"RightShoulder"` | **Right Shoulder** | RightShoulder | R | R1 | RB |
|
|
122
|
+
| `6` | `"LeftTrigger"` | **Left Trigger** | LeftTrigger | L2 | ZL | LT |
|
|
123
|
+
| `7` | `"RightTrigger"` | **Right Trigger** | RightTrigger | R2 | ZR | RT |
|
|
124
|
+
| `8` | `"Back"` | **Back** | Back | Minus | Options | Back |
|
|
125
|
+
| `9` | `"Start"` | **Start** | Start | Plus | Select | Start |
|
|
126
|
+
| `10` | `"LeftStick"` | **Left Stick** | LeftStick | L3 | LeftStick | LSB |
|
|
127
|
+
| `11` | `"RightStick"` | **Right Stick** | RightStick | R3 | RightStick | RSB |
|
|
128
|
+
| `12` | `"DPadUp"` | **D-Pad Up** | DPadUp | DPadUp | DPadUp | DPadUp |
|
|
129
|
+
| `13` | `"DPadDown"` | **D-Pad Down** | DPadDown | DPadDown | DPadDown | DPadDown |
|
|
130
|
+
| `14` | `"DPadLeft"` | **D-Pad Left** | DPadLeft | DPadLeft | DPadLeft | DPadLeft |
|
|
131
|
+
| `15` | `"DPadRight"` | **D-Pad Right** | DPadRight | DPadRight | DPadRight | DPadRight |
|
|
132
|
+
|
|
133
|
+
> [!CAUTION]
|
|
134
|
+
> ***Nintendo:** Both the labels and physical positions of the A,B,X,Y buttons are different
|
|
135
|
+
> on Nintendo controllers.
|
|
136
|
+
>
|
|
137
|
+
> Set `GamepadDevice.defaultOptions.remapNintendoMode` to apply the remapping as required.
|
|
138
|
+
>
|
|
139
|
+
> - `"physical"` _(default)_ &ndash A,B,X,Y refer the physical layout of a standard controller (Left=X,Top=Y,Bottom=A,Right=B).
|
|
140
|
+
> - `"accurate"` &ndash A,B,X,Y refer to the exact Nintendo labels (Left=Y,Top=X,Bottom=B,Right=A).
|
|
141
|
+
> - `"none"` &ndash A,B,X,Y refer to the button indices 0,1,2,3 (Left=Y,Top=B,Bottom=X,Right=A).
|
|
142
|
+
>
|
|
143
|
+
> ```
|
|
144
|
+
> standard nintendo nintendo nintendo
|
|
145
|
+
> layout "physical" "accurate" "none"
|
|
146
|
+
> reference (default)
|
|
268
147
|
>
|
|
269
|
-
>
|
|
270
|
-
>
|
|
271
|
-
>
|
|
272
|
-
>
|
|
148
|
+
> Y Y X B
|
|
149
|
+
> X B X B Y A Y A
|
|
150
|
+
> A A B X
|
|
151
|
+
>
|
|
152
|
+
> 3 3 2 1
|
|
153
|
+
> 2 1 2 1 3 0 3 0
|
|
154
|
+
> 0 0 1 2
|
|
273
155
|
> ```
|
|
274
156
|
|
|
275
|
-
|
|
276
|
-
> _If not using a PixiJS ticker, then just put `Action.tick(elapsedMs)` in the appropriate equivalent place (i.e. your `requestAnimationFrame()` render loop)._
|
|
277
|
-
|
|
278
|
-
**3.** Done!
|
|
279
|
-
|
|
280
|
-
⨠You are now ready to run your first action!
|
|
281
|
-
|
|
282
|
-
## Running Actions
|
|
283
|
-
|
|
284
|
-
*Running actions in your canvas.*
|
|
285
|
-
|
|
286
|
-
```ts
|
|
287
|
-
// Hide me instantly!
|
|
288
|
-
mySprite.run(Action.hide(), () => {
|
|
289
|
-
console.log('where did I go?');
|
|
290
|
-
});
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
Nodes are extended with a few new methods and properties to make it easy to interact with actions.
|
|
294
|
-
|
|
295
|
-
| Property | Description |
|
|
296
|
-
| :----- | :------ |
|
|
297
|
-
| `speed` | A speed modifier applied to all actions executed by the node and its descendants. Defaults to `1.0`. |
|
|
298
|
-
| `isPaused` | A boolean value that determines whether actions on the node and its descendants are processed. Defaults to `false`. |
|
|
299
|
-
|
|
300
|
-
| Method | Description |
|
|
301
|
-
| :----- | :------ |
|
|
302
|
-
| `run(action)` | Run an action. |
|
|
303
|
-
| `run(action, completion)` | Run an action with a completion handler. |
|
|
304
|
-
| `runWithKey(action, withKey)` | Run an action, and store it so it can be retrieved later. |
|
|
305
|
-
| `runAsPromise(action): Promise<void>` | Run an action as a promise. |
|
|
306
|
-
| `action(forKey): Action \| undefined` | Return an action associated with a specified key. |
|
|
307
|
-
| `hasActions(): boolean` | Return a boolean indicating whether the node is executing actions. |
|
|
308
|
-
| `removeAllActions(): void` | End and removes all actions from the node. |
|
|
309
|
-
| `removeAction(forKey): void` | Remove an action associated with a specified key. |
|
|
310
|
-
|
|
311
|
-
### Running Identifiable Actions
|
|
312
|
-
|
|
313
|
-
```ts
|
|
314
|
-
// Repeat an action forever!
|
|
315
|
-
const spin = Action.repeatForever(Action.rotateBy(5, 1.0));
|
|
316
|
-
mySprite.runWithKey(spin, 'spinForever');
|
|
317
|
-
|
|
318
|
-
// Or remove it later.
|
|
319
|
-
mySprite.removeAction('spinForever');
|
|
320
|
-
```
|
|
321
|
-
|
|
322
|
-
### Pausing All Actions
|
|
323
|
-
|
|
324
|
-
```ts
|
|
325
|
-
mySprite.isPaused = true;
|
|
326
|
-
// All actions will stop running.
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
### Manipulating Action Speed
|
|
330
|
-
|
|
331
|
-
Speed can be manipulated on both actions and nodes.
|
|
332
|
-
|
|
333
|
-
```ts
|
|
334
|
-
const moveAction = Action.moveByX(10, 4.0);
|
|
335
|
-
moveAction.speed = 2.0;
|
|
336
|
-
// moveAction will now take only 2 seconds instead of 4.
|
|
337
|
-
|
|
338
|
-
const repeatAction = Action.repeat(moveAction, 5);
|
|
339
|
-
repeatAction.speed = 2.0;
|
|
340
|
-
// Each moveAction will only take 1 second, for a total of 5 seconds.
|
|
341
|
-
|
|
342
|
-
mySprite.run(moveAction);
|
|
343
|
-
mySprite.speed = 2.0;
|
|
344
|
-
// mySprite is running at 2x speed!
|
|
345
|
-
// The entire action should now take ~2.5 seconds.
|
|
346
|
-
|
|
347
|
-
mySprite.parent!.speed = 1 / 4;
|
|
348
|
-
// Now we've slowed down mySprite's parent.
|
|
349
|
-
// The entire action will now take ~10 seconds.
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
> [!NOTE]
|
|
353
|
-
> Changes to nodes' `speed` will take effect immediately, however changes to an `Action` initializer's `speed` or `timingMode` will not affect any actions that have already begun running.
|
|
354
|
-
|
|
355
|
-
## Action Initializers
|
|
356
|
-
|
|
357
|
-
*Combine these initializers to create expressive animations and behaviors.*
|
|
358
|
-
|
|
359
|
-
Most actions implement specific predefined animations that are ready to use. If your animation needs fall outside of the suite provided here, then you should implement a custom action (see [Creating Custom Actions](#creating-custom-actions)).
|
|
360
|
-
|
|
361
|
-
| Action | Description | Reversible? |
|
|
362
|
-
| :----- | :---------- | :---------- |
|
|
363
|
-
|***Chaining Actions***|||
|
|
364
|
-
| `Action.group(actions)` | Run multiple actions in parallel. | Yes |
|
|
365
|
-
| `Action.sequence(actions)` | Run multiple actions sequentially. | Yes |
|
|
366
|
-
| `Action.repeat(action, count)` | Repeat an action a specified number of times. | Yes |
|
|
367
|
-
| `Action.repeatForever(action)` | Repeat an action indefinitely. | Yes |
|
|
368
|
-
|***Animating a Node's Position in a Linear Path***|||
|
|
369
|
-
| `Action.moveBy(vector, duration)` | Move a node by a relative vector `{ x, y }` (e.g. `PIXI.Point`). | Yes |
|
|
370
|
-
| `Action.moveBy(dx, dy, duration)` | Move a node by relative values. | Yes |
|
|
371
|
-
| `Action.moveByX(dx, duration)` | Move a node horizontally by a relative value. | Yes |
|
|
372
|
-
| `Action.moveByY(dy, duration)` | Move a node vertically by a relative value. | Yes |
|
|
373
|
-
| `Action.moveTo(position, duration)` | Move a node to a specified position `{ x, y }` (e.g. `PIXI.Point`, `PIXI.Container`). | _*No_ |
|
|
374
|
-
| `Action.moveTo(x, y, duration)` | Move a node to a specified position. | _*No_ |
|
|
375
|
-
| `Action.moveToX(x, duration)` | Move a node to a specified horizontal position. | _*No_ |
|
|
376
|
-
| `Action.moveToY(y, duration)` | Move a node to a specified vertical position. | _*No_ |
|
|
377
|
-
|***Animating a Node's Position Along a Custom Path***|||
|
|
378
|
-
| `Action.follow(path, duration)` | Move a node along a path, optionally orienting the node to the path. | Yes | Yes |
|
|
379
|
-
| `Action.followAtSpeed(path, speed)` | Move a node along a path at a specified speed, optionally orienting the node to the path. | Yes |
|
|
380
|
-
|***Animating the Rotation of a Node***|||
|
|
381
|
-
| `Action.rotateBy(delta, duration)` | Rotate a node by a relative value (in radians). | Yes |
|
|
382
|
-
| `Action.rotateByDegrees(delta, duration)` | Rotate a node by a relative value (in degrees). | Yes |
|
|
383
|
-
| `Action.rotateTo(radians, duration)` | Rotate a node to a specified value (in radians). | _*No_ |
|
|
384
|
-
| `Action.rotateToDegrees(degrees, duration)` | Rotate a node to a specified value (in degrees). | _*No_ |
|
|
385
|
-
|***Animating the Scaling of a Node***|||
|
|
386
|
-
| `Action.scaleBy(vector, duration)` | Scale a node by a relative vector `{ x, y }` (e.g. `PIXI.Point`). | Yes |
|
|
387
|
-
| `Action.scaleBy(scale, duration)` | Scale a node by a relative value. | Yes |
|
|
388
|
-
| `Action.scaleBy(dx, dy, duration)` | Scale a node in each axis by relative values. | Yes |
|
|
389
|
-
| `Action.scaleByX(dx, duration)` | Scale a node horizontally by a relative value. | Yes |
|
|
390
|
-
| `Action.scaleByY(dy, duration)` | Scale a node vertically by a relative value. | Yes |
|
|
391
|
-
| `Action.scaleTo(size, duration)` | Scale a node to achieve a specified size `{ width, height }` (e.g. `PIXI.ISize`, `PIXI.Container`). | _*No_ |
|
|
392
|
-
| `Action.scaleTo(scale, duration)` | Scale a node to a specified value. | _*No_ |
|
|
393
|
-
| `Action.scaleTo(x, y, duration)` | Scale a node in each axis to specified values. | _*No_ |
|
|
394
|
-
| `Action.scaleToX(x, duration)` | Scale a node horizontally to a specified value. | _*No_ |
|
|
395
|
-
| `Action.scaleToY(y, duration)` | Scale a node vertically to a specified value. | _*No_ |
|
|
396
|
-
|***Animating the Transparency of a Node***|||
|
|
397
|
-
| `Action.fadeIn(duration)` | Fade the alpha to `1.0`. | Yes |
|
|
398
|
-
| `Action.fadeOut(duration)` | Fade the alpha to `0.0`. | Yes |
|
|
399
|
-
| `Action.fadeAlphaBy(delta, duration)` | Fade the alpha by a relative value. | Yes |
|
|
400
|
-
| `Action.fadeAlphaTo(alpha, duration)` | Fade the alpha to a specified value. | _*No_ |
|
|
401
|
-
|***Controlling a Node's Visibility***|||
|
|
402
|
-
| `Action.unhide()` | Set a node's `visible` property to `true`. | Yes |
|
|
403
|
-
| `Action.hide()` | Set a node's `visible` property to `false`. | Yes |
|
|
404
|
-
|***Removing a Node from the Canvas***|||
|
|
405
|
-
| `Action.removeFromParent()` | Remove a node from its parent. | _â Identical_ |
|
|
406
|
-
|***Running Actions on Children***|||
|
|
407
|
-
| `Action.runOnChild(nameOrLabel, action)` | Add an action to a child node. | Yes |
|
|
408
|
-
|***Delaying Actions***|||
|
|
409
|
-
| `Action.waitForDuration(duration)` | Idle for a specified period of time. | _â Identical_ |
|
|
410
|
-
| `Action.waitForDurationWithRange(duration, range)` | Idle for a randomized period of time. | _â Identical_ |
|
|
411
|
-
|***Triggers and Custom Actions***|||
|
|
412
|
-
| `Action.run(callback)` | Execute a block (i.e. trigger another action). | _â Identical_ |
|
|
413
|
-
| `Action.customAction(duration, stepHandler)` | Execute a custom stepping function over the action duration. | _â Identical_ |
|
|
414
|
-
|***Manipulating the Action Speed of a Node***|||
|
|
415
|
-
| `Action.speedBy(delta, duration)` | Change how fast a node executes its actions by a relative value. | Yes |
|
|
416
|
-
| `Action.speedTo(speed, duration)` | Set how fast a node executes actions to a specified value. | _*No_ |
|
|
417
|
-
|
|
418
|
-
> [!IMPORTANT]
|
|
419
|
-
> #### Reversing Actions
|
|
420
|
-
> Every action initializer has a `.reversed()` method which will return a new action. Some actions are **not reversible**, and these cases are noted in the table above:
|
|
421
|
-
> - _**â Identical**_ — The reversed action is identical to the original action.
|
|
422
|
-
> - _**\*No**_ — The reversed action will idle for the equivalent duration.
|
|
423
|
-
|
|
424
|
-
### Action Chaining
|
|
425
|
-
|
|
426
|
-
Many actions can be joined together using `Action.sequence()`, `Action.group()`, `Action.repeat()` and `Action.repeatForever()` to quickly create complex animations:
|
|
427
|
-
|
|
428
|
-
```ts
|
|
429
|
-
import { Action } from 'pixijs-input-devices';
|
|
430
|
-
|
|
431
|
-
// Expand and contract smoothly over 2 seconds
|
|
432
|
-
const pulsate = Action.sequence([
|
|
433
|
-
Action.scaleTo(1.5, 1.0).easeOut(),
|
|
434
|
-
Action.scaleTo(1, 1.0).easeIn()
|
|
435
|
-
]);
|
|
436
|
-
|
|
437
|
-
// Follow a complex path (e.g. a bezier curve)
|
|
438
|
-
const path = [
|
|
439
|
-
{ x: 0, y: 0 },
|
|
440
|
-
{ x: 100, y: 0 },
|
|
441
|
-
{ x: 100, y: 100 },
|
|
442
|
-
{ x: 200, y: 200 }
|
|
443
|
-
];
|
|
444
|
-
const followPath = Action.follow(path, 5.0);
|
|
445
|
-
|
|
446
|
-
// Create a 10 second loop that goes back and forth
|
|
447
|
-
const moveBackAndForthWhilePulsating = Action.group([
|
|
448
|
-
Action.repeat(pulsate, 5),
|
|
449
|
-
Action.sequence([followPath, followPath.reversed()]),
|
|
450
|
-
]);
|
|
451
|
-
|
|
452
|
-
// ⨠Animate continuously
|
|
453
|
-
mySprite.run(Action.repeatForever(moveBackAndForthWhilePulsating));
|
|
454
|
-
```
|
|
455
|
-
|
|
456
|
-
## Timing Modes
|
|
457
|
-
|
|
458
|
-
Every action has a `timingMode` which controls the timing curve of its execution.
|
|
459
|
-
|
|
460
|
-
The default timingMode for all actions is `TimingMode.linear`, which causes an animation to occur evenly over its duration.
|
|
461
|
-
|
|
462
|
-
You can customize the speed curve of actions in many ways:
|
|
463
|
-
|
|
464
|
-
```ts
|
|
465
|
-
// Default easings:
|
|
466
|
-
Action.fadeIn(0.3).easeIn();
|
|
467
|
-
Action.fadeIn(0.3).easeOut();
|
|
468
|
-
Action.fadeIn(0.3).easeInOut();
|
|
469
|
-
|
|
470
|
-
// Set a specific TimingMode:
|
|
471
|
-
Action.fadeIn(0.3).setTimingMode(TimingMode.easeInOutCubic);
|
|
472
|
-
|
|
473
|
-
// Set a custom timing function:
|
|
474
|
-
Action.fadeIn(0.3).setTimingMode(x => x * x);
|
|
475
|
-
```
|
|
476
|
-
|
|
477
|
-
> [!IMPORTANT]
|
|
478
|
-
> **Timing Mutators:** The `.easeIn()`, `.easeOut()`, `.easeInOut()`, `setTimingMode(âĻ)`, `setSpeed(âĻ)` methods mutate the underlying action.
|
|
479
|
-
|
|
480
|
-
### Built-in TimingMode Options
|
|
481
|
-
|
|
482
|
-
See the following table for default `TimingMode` options.
|
|
483
|
-
|
|
484
|
-
| Pattern | Ease In, Ease Out | Ease In | Ease Out | Description |
|
|
485
|
-
| --------------- | ----- | -- | --- | ----------- |
|
|
486
|
-
| **Linear** | `linear` | - | - | Constant motion with no acceleration or deceleration. |
|
|
487
|
-
| **Sine** | `easeInOutSine` | `easeInSine` | `easeOutSine` | Gentle start and end, with accelerated motion in the middle. |
|
|
488
|
-
| **Circular** | `easeInOutCirc` | `easeInCirc` | `easeOutCirc` | Smooth start and end, faster acceleration in the middle, circular motion. |
|
|
489
|
-
| **Cubic** | `easeInOutCubic` | `easeInCubic` | `easeOutCubic` | Gradual acceleration and deceleration, smooth motion throughout. |
|
|
490
|
-
| **Quadratic** | `easeInOutQuad` | `easeInQuad` | `easeOutQuad` | Smooth acceleration and deceleration, starts and ends slowly, faster in the middle. |
|
|
491
|
-
| **Quartic** | `easeInOutQuart` | `easeInQuart` | `easeOutQuart` | Slower start and end, increased acceleration in the middle. |
|
|
492
|
-
| **Quintic** | `easeInOutQuint` | `easeInQuint` | `easeOutQuint` | Very gradual start and end, smoother acceleration in the middle. |
|
|
493
|
-
| **Exponential** | `easeInOutExpo` | `easeInExpo` | `easeOutExpo` | Very slow start, exponential acceleration, slow end. |
|
|
494
|
-
| **Back** | `easeInOutBack` | `easeInBack` | `easeOutBack` | Starts slowly, overshoots slightly, settles into final position. |
|
|
495
|
-
| **Bounce** | `easeInOutBounce` | `easeInBounce` | `easeOutBounce` | Bouncy effect at the start or end, with multiple rebounds. |
|
|
496
|
-
| **Elastic** | `easeInOutElastic` | `easeInElastic` | `easeOutElastic` | Stretchy motion with overshoot and multiple oscillations. |
|
|
497
|
-
|
|
498
|
-
### Default Timing Modes
|
|
499
|
-
|
|
500
|
-
The `.easeIn()`, `.easeOut()`, `.easeInOut()`, and `.linear()` mutator methods on `Action` instances will set the timing mode of that action to the global default timing mode for that curve type.
|
|
501
|
-
|
|
502
|
-
| TimingMode mutator | Global setting | Default value |
|
|
503
|
-
| :--- | :--- | :--- |
|
|
504
|
-
| `action.easeIn()` | `Action.DefaultTimingModeEaseIn` | `TimingMode.easeInSine` |
|
|
505
|
-
| `action.easeOut()` | `Action.DefaultTimingModeEaseOut` | `TimingMode.easeOutSine` |
|
|
506
|
-
| `action.easeInOut()` | `Action.DefaultTimingModeEaseInOut` | `TimingMode.easeInOutSine` |
|
|
507
|
-
| `action.linear()` | _(n/a)_ | `TimingMode.linear` |
|
|
508
|
-
|
|
509
|
-
Global default timing modes can be set like so:
|
|
157
|
+
#### Assigning devices
|
|
510
158
|
|
|
511
159
|
```ts
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
// apply
|
|
516
|
-
myNode.run(myAction.easeIn());
|
|
160
|
+
InputDevice.on("deviceconnected", ({ device }) => {
|
|
161
|
+
device.meta.localPlayerId = 123
|
|
162
|
+
})
|
|
517
163
|
|
|
518
|
-
|
|
519
|
-
|
|
164
|
+
for ( const device of InputDevice.devices )
|
|
165
|
+
{
|
|
166
|
+
if ( device.meta.localPlayerId === 123 )
|
|
167
|
+
{
|
|
168
|
+
// do something
|
|
169
|
+
}
|
|
170
|
+
}
|
|
520
171
|
```
|
|
521
172
|
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
Beyond combining chaining actions like `sequence()`, `group()`, `repeat()` and `repeatForever()`, you can provide code that implements your own action.
|
|
173
|
+
#### Custom devices
|
|
525
174
|
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
Actions are stateless and reusable, so you can create complex animations once, and then run them on many nodes.
|
|
175
|
+
You can add custom devices to the device manager:
|
|
529
176
|
|
|
530
177
|
```ts
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
]),
|
|
539
|
-
Action.sequence([
|
|
540
|
-
Action.rotateByDegrees(-2, 0.33).easeOut(),
|
|
541
|
-
Action.rotateByDegrees(4, 0.34).easeInOut(),
|
|
542
|
-
Action.rotateByDegrees(-2, 0.33).easeIn(),
|
|
543
|
-
]),
|
|
544
|
-
])
|
|
545
|
-
);
|
|
546
|
-
|
|
547
|
-
// Run it over here
|
|
548
|
-
someSprite.run(rockBackAndForth);
|
|
549
|
-
|
|
550
|
-
// Run it somewhere else
|
|
551
|
-
someOtherContainer.run(rockBackAndForth);
|
|
552
|
-
```
|
|
553
|
-
|
|
554
|
-
You can combine these with dynamic actions for more variety:
|
|
178
|
+
// 1. subclass CustomDevice
|
|
179
|
+
class MySpecialDevice extends CustomDevice
|
|
180
|
+
{
|
|
181
|
+
constructor() {
|
|
182
|
+
super( "special-device" )
|
|
183
|
+
}
|
|
184
|
+
}
|
|
555
185
|
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
squash: (amount: number, duration: number = 0.3) => Action.sequence([
|
|
559
|
-
Action.scaleTo(amount, 1 / amount, duration / 2).easeOut(),
|
|
560
|
-
Action.scaleTo(1, duration / 2).easeIn()
|
|
561
|
-
]),
|
|
562
|
-
stretch: (amount: number, duration: number = 0.3) => Action.sequence([
|
|
563
|
-
Action.scaleTo(1 / amount, amount, duration / 2).easeOut(),
|
|
564
|
-
Action.scaleTo(1, duration / 2).easeIn()
|
|
565
|
-
]),
|
|
566
|
-
squashAndStretch: (amount: number, duration: number = 0.3) => Action.sequence([
|
|
567
|
-
MyActions.squash(amount, duration / 2),
|
|
568
|
-
MyActions.stretch(amount, duration / 2),
|
|
569
|
-
]),
|
|
570
|
-
};
|
|
571
|
-
|
|
572
|
-
// Small squish!
|
|
573
|
-
mySprite.run(MyActions.squashAndStretch(1.25));
|
|
574
|
-
|
|
575
|
-
// Big squish!
|
|
576
|
-
mySprite.run(MyActions.squashAndStretch(2.0));
|
|
186
|
+
// 2. add the device
|
|
187
|
+
InputDevice.add( new MySpecialDevice() )
|
|
577
188
|
```
|
|
578
189
|
|
|
579
|
-
###
|
|
190
|
+
### Navigation API
|
|
580
191
|
|
|
581
|
-
|
|
192
|
+
By default, any element with `"mousedown"` or `"pointerdown"` handlers will be navigatable.
|
|
582
193
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
const colorB = Math.sin(0.3 * t + 4) * 127 + 128;
|
|
194
|
+
Container properties | type | default | description
|
|
195
|
+
---------------------|------|---------|--------------
|
|
196
|
+
`navigationMode` | `"auto"` \| `"disabled"` \| `"target"` | `"auto"` | When set to `"auto"`, a `Container` can be navigated to if it has a `"pointerdown"` or `"mousedown"` event handler registered.
|
|
197
|
+
`isNavigatable` | `get () => boolean` | `false` | returns `true` if `navigationMode` is `"target"`, or `"auto"` and a `"pointerdown"` or `"mousedown"` event handler is registered.
|
|
198
|
+
`navigationPriority` | `number` | `0` | The priority relative to other navigation items in this group.
|
|
589
199
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
200
|
+
Navigation intent | Keyboard | Gamepads
|
|
201
|
+
------------------|------------------------|-----------------------------------
|
|
202
|
+
`"navigateLeft"` | `ArrowLeft`, `KeyA` | Left Joystick (Left), `DPadLeft`
|
|
203
|
+
`"navigateRight"` | `ArrowRight`, `KeyD` | Left Joystick (Right), `DPadRight`
|
|
204
|
+
`"navigateUp"` | `ArrowUp`, `KeyW` | Left Joystick (Up), `DPadDown`
|
|
205
|
+
`"navigateDown"` | `ArrowDown`, `KeyS` | Left Joystick (Down), `DPadUp`
|
|
206
|
+
`"navigateBack"` | `Escape`, `Backspace` | `B`, `Back`
|
|
207
|
+
`"trigger"` | `Enter,` `Space` | `A`
|
|
593
208
|
|
|
594
|
-
|
|
595
|
-
|
|
209
|
+
Container events | description
|
|
210
|
+
------------------|--------------------------------------------------------
|
|
211
|
+
`focus` | Target became focused.
|
|
212
|
+
`blur` | Target lost focus.
|
|
596
213
|
|
|
597
|
-
// Stop rainbow effect
|
|
598
|
-
mySprite.removeAction('rainbow');
|
|
599
|
-
```
|
|
600
214
|
|
|
601
|
-
>
|
|
602
|
-
>
|
|
603
|
-
> - `t` = Progress of time from 0 to 1, which has been passed through the `timingMode` function.
|
|
604
|
-
> - `dt` = delta/change in `t` since last step. Use for relative actions.
|
|
215
|
+
> [!TIP]
|
|
216
|
+
> Modify `device.options.navigation.binds` to override which keys/buttons are used for navigation.
|
|
605
217
|
>
|
|
606
|
-
>
|
|
607
|
-
|
|
608
|
-
This function will be called as many times as the renderer asks over the course of its duration.
|
|
609
|
-
|
|
610
|
-
### Custom Action (with State)
|
|
611
|
-
|
|
612
|
-
Here is a practical example:
|
|
613
|
-
|
|
614
|
-
```ts
|
|
615
|
-
// Create a custom action that relies on
|
|
616
|
-
// state (radius, inital target position).
|
|
617
|
-
const makeOrbitAction = (
|
|
618
|
-
radius: number,
|
|
619
|
-
duration: number
|
|
620
|
-
): Action => {
|
|
621
|
-
let startPos: PIXI.IPointData;
|
|
622
|
-
|
|
623
|
-
return Action.customAction(duration, (target, t, td) => {
|
|
624
|
-
if (!startPos) {
|
|
625
|
-
// Capture on first run
|
|
626
|
-
startPos = { x: target.x, y: target.y };
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
const angle = Math.PI * 2 * t;
|
|
630
|
-
|
|
631
|
-
target.position.set(
|
|
632
|
-
startPos.x + radius * Math.cos(angle),
|
|
633
|
-
startPos.y + radius * Math.sin(angle)
|
|
634
|
-
);
|
|
635
|
-
});
|
|
636
|
-
};
|
|
637
|
-
|
|
638
|
-
// Run the custom action
|
|
639
|
-
mySprite.run(
|
|
640
|
-
Action.repeatForever(makeOrbitAction(10, 15.0))
|
|
641
|
-
);
|
|
642
|
-
```
|
|
218
|
+
> Or set `device.options.navigation.enabled = false` to disable navigation.
|