입력
엔진은 캔버스의 마우스와 터치, 문서의 키보드, 게임패드를 받아 InputManager 에 담습니다. 마우스와 터치는 "터치" 한 갈래로 합쳐 씬에 넘기고, 씬은 그것을 노드에 넘깁니다(TouchRaycaster, TouchRecognizer). 이 장은 씬에서 터치 받기, 노드로 넘기기, 손가락 여럿, 마우스 단추와 휠, 키보드, 입력 동작(InputActions), 게임패드, 마우스 커서를 봅니다.
씬에서 터치 받기
씬의 기본 tick 이 매 프레임 입력 상태를 보고 아래 메서드를 부릅니다. 좌표는 모두 뷰 좌표(viewManager.applyViewRect 로 그리는 좌표)입니다.
| 메서드 | 불리는 때 |
|---|---|
touchPress(viewInputPosition) |
누른 프레임 |
touchMove(viewInputPosition) |
누르고 있는 동안 (누른 프레임에는 부르지 않습니다) |
touchRelease(viewInputPosition) |
뗀 프레임 (한 프레임 안에 눌렀다 뗐으면 touchPress 바로 뒤에) |
touchCancel(viewInputPosition) |
터치가 취소된 프레임 (touchcancel) |
touchWheel(viewInputPosition, wheelDelta) |
이번 프레임에 휠을 굴렸을 때 (wheelDelta 는 이번 프레임에 쌓인 deltaX, deltaY) |
마우스는 처음에 어느 단추든 터치로 넘깁니다. 왼쪽 단추만 터치로 넘기려면 inputManager.setTouchFromLeftButtonOnly(true) 를 부릅니다. 손가락이 여럿이면 가장 최근 터치 사건에서 바뀐 첫 손가락의 자리를 씁니다. (여러 손가락은 아래 "손가락 여럿")
import { Engine, EngineConfiguration, Scene, Vector2, Color } from "./build/vanilla.js";
class DragScene extends Scene {
#ballPosition;
#grabOffset;
constructor() {
super();
this.#ballPosition = Vector2.create(640, 360);
this.#grabOffset = null;
}
touchPress(viewInputPosition) {
const deltaX = viewInputPosition.x - this.#ballPosition.x;
const deltaY = viewInputPosition.y - this.#ballPosition.y;
if (deltaX * deltaX + deltaY * deltaY <= 60 * 60) {
this.#grabOffset = Vector2.create(deltaX, deltaY);
}
}
touchMove(viewInputPosition) {
if (this.#grabOffset === null) {
return;
}
this.#ballPosition = Vector2.create(viewInputPosition.x - this.#grabOffset.x, viewInputPosition.y - this.#grabOffset.y);
}
touchRelease(viewInputPosition) {
this.#grabOffset = null;
}
touchCancel(viewInputPosition) {
this.#grabOffset = null;
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
draw(graphic) {
super.draw(graphic);
const isGrabbed = this.#grabOffset !== null;
graphic.setFillColor(isGrabbed ? new Color(1, 0.7, 0.2, 1) : new Color(0.3, 0.7, 1, 1));
graphic.drawCircle(this.#ballPosition, 60);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new DragScene());touchPress 등을 덮어쓸 때 부모 것을 부르지 않아도 됩니다. (Scene 의 기본 구현은 비어 있습니다) tick 을 덮어쓸 때는 super.tick(timeDelta) 를 불러야 이 메서드들이 불립니다.
노드로 터치 넘기기 (TouchRaycaster, TouchRecognizer)
노드가 터치를 받으려면 셋이 필요합니다.
WorldNode.setInteractable(true)- 터치를 받을 메서드(
touchPress,touchMove,touchRelease,touchCancel)를 가진 컴포넌트. 노드는 받은 터치를 그 메서드가 있는 컴포넌트 모두에 넘깁니다. - 그 노드가 씬 루트 아래에 있기. 씬은 씬 꼴과 상관없이
tickTouch에서 씬의 터치 레이캐스터(처음은TouchRaycaster)로 가장 앞의 노드에 먼저 넘기고, 그 뒤에 씬의 터치 메서드를 부릅니다.
TouchRaycaster 는 누를 때 루트부터 그리는 차례로 트리를 돌아, 활성이고 isInteractable() 이 참이며 그 점을 품는(contains) 노드 가운데 가장 나중에 그리는(가장 위에 보이는) 노드를 고릅니다. Mask 가 붙은 노드 밖에 있는 자손은 고르지 않습니다. 고른 노드는 뗄 때까지 손가락이 밖으로 나가도 touchMove, touchRelease 를 받습니다. TouchRecognizer 는 이것을 이어받아, 고른 노드의 조상에 UIScrollView 가 있으면 누름을 0.15 초 늦춰 넘기고, 10 픽셀 넘게 끌면 노드에는 누름을 넘기지 않거나 취소하고 스크롤 칸에 끌기를 넘깁니다. 스크롤 칸 안에 단추가 있으면 setTouchRaycaster(new TouchRecognizer()) 로 씬의 레이캐스터를 바꿉니다. 씬이 터치를 가로채야 하면(막대 끌기 같은) setTouchRaycaster(null) 로 끄고 씬의 터치 메서드에서 직접 넘깁니다. UIScene 과 GameScene 이 그렇게 합니다.
import { Engine, EngineConfiguration, Scene, WorldNode, Component, Paint, TouchRecognizer, Vector2, Color } from "./build/vanilla.js";
class PressHighlight extends Component {
#paint;
#clickCount;
constructor() {
super();
this.#paint = null;
this.#clickCount = 0;
}
attach(node) {
super.attach(node);
this.#paint = node.getComponent(Paint);
}
require() {
return [Paint];
}
touchPress(viewInputPosition) {
this.#paint.setColor("#ffaa33");
}
touchRelease(viewInputPosition) {
this.#paint.setColor("#3366cc");
const node = this.getNode();
const isInside = node.contains(viewInputPosition);
if (isInside) {
this.#clickCount += 1;
console.log("clicked", this.#clickCount);
}
}
touchCancel(viewInputPosition) {
this.#paint.setColor("#3366cc");
}
}
class ButtonScene extends Scene {
create() {
super.create();
this.setTouchRaycaster(new TouchRecognizer());
}
initialize(engine) {
super.initialize(engine);
const root = this.getRoot();
const button = new WorldNode();
button.setContentSize(Vector2.create(240, 90));
button.setLocalPosition(Vector2.create(640, 360));
button.setInteractable(true);
button.addComponent(PressHighlight);
const paint = button.getComponent(Paint);
paint.setColor("#3366cc");
paint.setRoundSize(16);
root.addChild(button);
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new ButtonScene());PressHighlight는require()로Paint를 요구하므로addComponent(PressHighlight)가Paint를 먼저 붙입니다.touchWheel은 그 자리를 덮는 것 가운데 가장 위의 스크롤 칸(UIScrollView같은 것)에 휠을 넘깁니다.UIScene은 이 연결을 스스로 합니다. 버튼, 스크롤 뷰 같은 UI 컴포넌트는 "UI" 장에서 봅니다.
손가락 여럿 (TouchPoint)
씬의 터치 메서드는 한 자리만 받습니다. 손가락 여럿은 InputManager 에서 읽습니다. 터치 화면의 손가락만 들어가고 마우스는 들어가지 않습니다.
getTouchPoints(): 지금 닿아 있는 손가락 목록 (닿은 차례). 단계는began(이번 프레임에 닿음),moved,stationarygetEndedTouchPoints(): 이번 프레임에 떨어진 손가락 (단계ended또는cancelled, 자리는 뗀 자리)findTouchPoint(identifier): 번호로 찾기 (없으면null)
TouchPoint 는 identifier(닿아 있는 동안 같은 번호), phase(TouchPhase 값), canvasNativePosition(캔버스 왼쪽 위 기준 CSS 픽셀), viewPosition(뷰 좌표) 필드를 갖습니다.
import { Engine, EngineConfiguration, Scene, TouchPhase, Vector2, Color } from "./build/vanilla.js";
class MultiTouchScene extends Scene {
#fingerPositions;
constructor() {
super();
this.#fingerPositions = [];
}
tick(timeDelta) {
super.tick(timeDelta);
const engine = this.getEngine();
const inputManager = engine.getInputManager();
const touchPoints = inputManager.getTouchPoints();
this.#fingerPositions = [];
for (const touchPoint of touchPoints) {
const viewPosition = touchPoint.viewPosition;
this.#fingerPositions.push(viewPosition);
if (touchPoint.phase === TouchPhase.began) {
const identifier = touchPoint.identifier;
console.log("finger down", identifier);
}
}
const endedTouchPoints = inputManager.getEndedTouchPoints();
for (const endedTouchPoint of endedTouchPoints) {
const identifier = endedTouchPoint.identifier;
const phase = endedTouchPoint.phase;
console.log("finger up", identifier, phase);
}
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
draw(graphic) {
super.draw(graphic);
graphic.setFillColor(new Color(1, 1, 1, 0.6));
for (const fingerPosition of this.#fingerPositions) {
graphic.drawCircle(fingerPosition, 50);
}
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new MultiTouchScene());마우스 단추와 휠
| 메서드 | 내용 |
|---|---|
isMouseButtonDown(button) |
눌려 있는지. 단추는 MouseButton.left(0), middle(1), right(2) |
isMouseButtonJustPressed(button), isMouseButtonJustReleased(button) |
이번 프레임에 눌렀는지, 뗐는지 (프레임 사이에 눌렀다 뗀 것도 참) |
getViewInputPosition(), getCanvasNativeInputPosition() |
마지막 포인터 자리 (뷰 좌표, 캔버스 CSS 픽셀) |
isTouchDown() |
터치(손가락이나 터치로 넘기는 마우스 단추)가 눌린 채인지 |
hasWheelDelta(), getWheelDeltaX(), getWheelDeltaY() |
이번 프레임에 쌓인 휠 값 (아래로 굴리면 Y 가 양수) |
isWheelWithCtrl() |
마지막 휠을 Ctrl 과 함께 굴렸는지 (노트북 터치패드의 오므리기도 Ctrl 휠로 옵니다) |
setContextMenuPrevented(isPrevented) |
캔버스의 오른쪽 클릭 메뉴를 막을지 (처음 참) |
setTouchFromLeftButtonOnly(isLeftButtonOnly) |
왼쪽 단추만 터치로 넘길지 (처음 거짓) |
키보드
키는 KeyboardEvent.code 글로 묻습니다. 자판 배열과 상관없이 자리로 정해지는 이름입니다. ("KeyA", "Digit1", "ArrowLeft", "Space", "Enter", "Escape", "ShiftLeft", "ControlLeft" 같은 것)
| 메서드 | 내용 |
|---|---|
isKeyPressed(code) |
지금 눌려 있는지 |
isKeyJustPressed(code) |
이번 프레임에 눌렸는지 (프레임 사이에 눌렀다 뗀 키도 참) |
isKeyJustReleased(code) |
이번 프레임에 떼어졌는지 |
setPreventedKeys(codes) |
브라우저 기본 동작을 막을 키 (Tab 의 초점 옮기기, Space 의 쪽 넘기기 같은 것). 글자 입력칸에 쓰는 중이면 막지 않습니다 |
isTypingInTextField() |
초점이 글자 입력칸(input, textarea, contenteditable, select)에 있는지 |
import { Engine, EngineConfiguration, Scene, Vector2, Rect, Color } from "./build/vanilla.js";
class KeyboardScene extends Scene {
#position;
constructor() {
super();
this.#position = Vector2.create(640, 360);
}
initialize(engine) {
super.initialize(engine);
const inputManager = engine.getInputManager();
inputManager.setPreventedKeys(["Space", "ArrowUp", "ArrowDown"]);
}
tick(timeDelta) {
super.tick(timeDelta);
const engine = this.getEngine();
const inputManager = engine.getInputManager();
const speed = 300;
let moveX = 0;
let moveY = 0;
if (inputManager.isKeyPressed("ArrowLeft")) {
moveX -= 1;
}
if (inputManager.isKeyPressed("ArrowRight")) {
moveX += 1;
}
if (inputManager.isKeyPressed("ArrowUp")) {
moveY -= 1;
}
if (inputManager.isKeyPressed("ArrowDown")) {
moveY += 1;
}
this.#position = Vector2.create(this.#position.x + moveX * speed * timeDelta, this.#position.y + moveY * speed * timeDelta);
if (inputManager.isKeyJustPressed("Space")) {
console.log("jump");
}
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
draw(graphic) {
super.draw(graphic);
graphic.setFillColor(new Color(0.4, 0.9, 0.5, 1));
graphic.drawRect(Rect.create(this.#position.x - 40, this.#position.y - 40, 80, 80));
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new KeyboardScene());입력 동작 (InputActions)
게임이 정한 동작 이름("confirm", "jump" 같은 글)에 키, 게임패드 단추, 게임패드 축의 한쪽을 여럿 묶고, 게임은 동작 이름으로 읽습니다. inputManager.getInputActions() 가 엔진이 하나 들고 있는 것을 주고, 엔진이 프레임 처음에 갱신합니다.
| 메서드 | 내용 |
|---|---|
bindKey(action, code) |
키 묶기 |
bindGamepadButton(action, buttonIndex) |
게임패드 단추 묶기 (W3C 표준 배치의 단추 번호, 연결된 패드 모두) |
bindAxis(action, axisIndex, direction) |
축의 한쪽 묶기 (direction 이 1 이면 + 쪽, -1 이면 - 쪽) |
unbindAction(action), getBoundKeys(action) |
묶은 것 모두 풀기, 묶은 키 목록 |
setRepeat(action, delaySeconds, intervalSeconds) |
누르고 있으면 기다린 뒤부터 간격마다 isJustPressed 가 다시 참 (메뉴 고르기) |
isPressed(action), isJustPressed(action), isJustReleased(action) |
상태 |
readValue(action) |
0 ~ 1. 키와 디지털 단추는 1, 트리거와 축은 기운 만큼 |
readAxis(negativeAction, positiveAction) |
두 동작으로 만든 -1 ~ 1 축 |
readMoveAxis() |
WASD 와 방향키, 왼쪽 스틱, 바깥 입력원 가운데 가장 긴 이동 (길이 1 이하, 오른쪽 +x, 아래 +y) |
setDeadZone(value), setAxisPressThreshold(value) |
스틱 가운데 떨림을 거르는 길이 (처음 0.2), 축을 눌린 것으로 보는 기울기 (처음 0.5) |
setKeysIgnoredWhileTyping(isIgnored) |
글자 입력칸에 쓰는 동안 키를 듣지 않을지 (처음 참) |
addSource(source) |
엔진이 모르는 입력원 끼우기 (readActionValue(action) 를 가진 객체) |
게임패드 단추 번호는 W3C 표준 배치를 따릅니다. 0 이 A(크로스), 1 이 B(서클), 2 가 X(스퀘어), 3 이 Y(트라이앵글), 4 와 5 가 L1, R1, 6 과 7 이 L2, R2, 8 이 공유(뷰), 9 가 옵션(메뉴), 12 ~ 15 가 십자키 위, 아래, 왼쪽, 오른쪽입니다. 축 번호는 0 이 왼쪽 스틱 X, 1 이 왼쪽 스틱 Y(아래가 +), 2 와 3 이 오른쪽 스틱 X, Y 입니다.
import { Engine, EngineConfiguration, Scene, Vector2, Rect, Color } from "./build/vanilla.js";
class ActionScene extends Scene {
#position;
#selectedIndex;
constructor() {
super();
this.#position = Vector2.create(640, 360);
this.#selectedIndex = 0;
}
initialize(engine) {
super.initialize(engine);
const inputManager = engine.getInputManager();
const inputActions = inputManager.getInputActions();
inputActions.bindKey("confirm", "Enter");
inputActions.bindKey("confirm", "Space");
inputActions.bindGamepadButton("confirm", 0);
inputActions.bindKey("next", "Tab");
inputActions.bindGamepadButton("next", 15);
inputActions.bindAxis("next", 0, 1);
inputActions.setRepeat("next", 0.4, 0.1);
}
tick(timeDelta) {
super.tick(timeDelta);
const engine = this.getEngine();
const inputManager = engine.getInputManager();
const inputActions = inputManager.getInputActions();
const moveAxis = inputActions.readMoveAxis();
this.#position = Vector2.create(this.#position.x + moveAxis.x * 300 * timeDelta, this.#position.y + moveAxis.y * 300 * timeDelta);
if (inputActions.isJustPressed("next")) {
this.#selectedIndex = (this.#selectedIndex + 1) % 4;
}
if (inputActions.isJustPressed("confirm")) {
console.log("confirm", this.#selectedIndex);
}
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
draw(graphic) {
super.draw(graphic);
for (let index = 0; index < 4; ++index) {
const isSelected = index === this.#selectedIndex;
graphic.setFillColor(isSelected ? "#ffcc33" : "#445066");
graphic.drawRect(Rect.create(40 + index * 140, 40, 120, 60));
}
graphic.setFillColor(new Color(0.4, 0.8, 1, 1));
graphic.drawCircle(this.#position, 40);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new ActionScene());게임패드 (GamepadManager)
inputManager.getGamepadManager() 로 얻습니다. 엔진이 gamepadconnected 와 gamepaddisconnected 를 받아 패드를 등록하고 매 프레임 상태를 읽습니다. 패드는 브라우저가 준 Gamepad.index 번호로 묻습니다.
| 메서드 | 내용 |
|---|---|
getAllConnectedGamepads(), getConnectedGamepadCount() |
연결된 패드 (Gamepad 객체) 목록과 수 |
isButtonPressed(gamepadIndex, buttonIndex) |
단추가 눌려 있는지 |
isButtonJustPressed(gamepadIndex, buttonIndex), isButtonJustReleased(...) |
이번 갱신에 눌렸는지, 떼어졌는지 |
getButtonValue(gamepadIndex, buttonIndex) |
단추와 트리거 값 (0 ~ 1) |
getAxisValue(gamepadIndex, axisIndex) |
스틱 축 값 (-1 ~ 1) |
vibrate(strongMagnitude, weakMagnitude, seconds) |
연결된 패드를 모두 떨기 (세기 0 ~ 1, 진동 장치가 없는 패드와 브라우저는 건너뜀) |
setInputLogEnabled(isEnabled) |
단추와 축이 바뀔 때마다 콘솔에 찍을지 (처음 끔) |
getActiveGamepadId() |
마지막 갱신에 쓴 패드의 이름 (없으면 빈 글) |
readGamepadKind(gamepadId) 는 패드 이름으로 GamepadKind.xbox, playstation, nintendo, other 를 가릅니다. 단추 안내 그림을 패드에 맞출 때 씁니다. inputManager.getLastGamepadKind() 는 마지막으로 쓴 패드의 종류를 줍니다.
import { Engine, EngineConfiguration, Scene, Vector2, GamepadKind, readGamepadKind } from "./build/vanilla.js";
class GamepadScene extends Scene {
tick(timeDelta) {
super.tick(timeDelta);
const engine = this.getEngine();
const inputManager = engine.getInputManager();
const gamepadManager = inputManager.getGamepadManager();
const gamepads = gamepadManager.getAllConnectedGamepads();
for (const gamepad of gamepads) {
const gamepadIndex = gamepad.index;
const leftStickX = gamepadManager.getAxisValue(gamepadIndex, 0);
const leftStickY = gamepadManager.getAxisValue(gamepadIndex, 1);
const rightTrigger = gamepadManager.getButtonValue(gamepadIndex, 7);
if (gamepadManager.isButtonJustPressed(gamepadIndex, 0)) {
const gamepadId = gamepad.id;
const gamepadKind = readGamepadKind(gamepadId);
const buttonName = gamepadKind === GamepadKind.playstation ? "Cross" : "A";
console.log(buttonName, leftStickX, leftStickY, rightTrigger);
gamepadManager.vibrate(0.8, 0.4, 0.2);
}
}
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new GamepadScene());마지막 입력 장치
inputManager.getLastInputDevice() 는 마지막으로 쓴 장치를 InputDevice.keyboard, mouse, touch, gamepad 가운데 하나로 줍니다. 처음 값은 손가락이 주된 기기(CSS pointer: coarse)면 touch, 아니면 keyboard 입니다. 손가락을 뗀 직후 브라우저가 흉내 내 보내는 마우스 사건과 창이 흔들릴 때의 작은 마우스 움직임은 장치를 바꾸지 않습니다. isTouchInput() 은 마지막 입력이 손가락인지 줍니다. 화면에 "탭하세요" 와 "Enter 를 누르세요" 를 바꿔 보일 때 씁니다.
마우스 커서 (PointerCursor)
PointerCursor 는 하나뿐인 객체(PointerCursor.getInstance())입니다. setEnabled(true) 로 켜면 엔진이 프레임 끝마다 캔버스의 CSS 커서를 정합니다. 꺼져 있으면(처음) 아무것도 하지 않습니다.
UIButton위에 마우스가 있으면 단추의 커서(처음"pointer"), 아니면setScreenCursor(name)로 정한 커서입니다.setCursorNames(buttonCursorName, defaultCursorName)로 단추 위 커서와 기본 커서를 바꿉니다.- 쓸 수 있는 CSS 이름은
pointer,crosshair,not-allowed,grab,grabbing,text,move,help,wait,progress,none이고, 모르는 이름이면 브라우저 기본 화살표입니다. registerImageCursor(name, url, url2x, hotspotX, hotspotY, fallbackCursorName)로 그림 커서를 더하고 그 이름을 씁니다. (1 배, 2 배 그림, 1 배 기준 핫스팟, 그림을 못 쓸 때의 CSS 커서)- 손가락이 주된 기기에서는 캔버스를 건드리지 않습니다.
import { Engine, EngineConfiguration, Scene, PointerCursor, Vector2 } from "./build/vanilla.js";
class CursorScene extends Scene {
initialize(engine) {
super.initialize(engine);
const pointerCursor = PointerCursor.getInstance();
pointerCursor.setEnabled(true);
pointerCursor.setCursorNames("pointer", "default");
}
touchPress(viewInputPosition) {
const pointerCursor = PointerCursor.getInstance();
pointerCursor.setScreenCursor("grabbing");
}
touchRelease(viewInputPosition) {
const pointerCursor = PointerCursor.getInstance();
pointerCursor.setScreenCursor("grab");
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new CursorScene());주의할 점
- 씬의 터치 메서드와
getViewInputPosition()은 자리 하나만 봅니다. 핀치처럼 손가락 여럿이 필요하면getTouchPoints()를 씁니다. - 씬은 꼴과 상관없이 노드로 터치를 넘깁니다. 씬의 터치 메서드에서 레이캐스터에 또 넘기면 두 번 넘어가니, 직접 넘길 때는
setTouchRaycaster(null)로 씬의 넘기기를 끕니다. - 키 이름(
KeyCode)과 게임패드 번호(GamepadButtonCode,GamepadAnalogStickCode)의 표는 공개 이름에 없습니다. 키는KeyboardEvent.code글, 패드는 W3C 표준 배치의 번호로 줍니다. - 키는 문서(
document)에서 받으므로 페이지에 초점이 있어야 들어옵니다. iframe 안의 게임은 iframe 을 한 번 눌러야 키를 받습니다. - 창이 초점을 잃으면 엔진은 터치와 마우스 단추를 뗀 것으로 돌리지만 키는 그대로 둡니다. 키 상태는 페이지가 다시 보일 때(
visibilitychange) 지웁니다. 키가 눌린 채 남는 것이 문제면PageEvent.blurred를 받아inputManager.clear()를 부릅니다. - 한 프레임짜리 상태(
isKeyJustPressed,isMouseButtonJustPressed, 휠 값, 떨어진 손가락)는 엔진이 프레임 끝에 지웁니다. 씬의tick안에서 읽습니다.
관련 클래스
InputManager, InputActions, InputDevice, MouseButton, TouchPhase, TouchPoint, TouchRaycaster, TouchRecognizer, GamepadManager, GamepadKind, PointerCursor, Scene, WorldNode, UIScene, PageEvent
Input
The engine receives mouse and touch input on the canvas, keyboard input on the document, and gamepads, and stores them in InputManager. Mouse and touch are merged into a single "touch" stream passed to the scene, and the scene passes it on to nodes (TouchRaycaster, TouchRecognizer). This chapter covers receiving touches in a scene, passing them to nodes, multiple fingers, mouse buttons and the wheel, the keyboard, input actions (InputActions), gamepads, and the mouse cursor.
Receiving touches in a scene
The scene's base tick checks the input state every frame and calls the methods below. All coordinates are view coordinates (the coordinates you draw in after viewManager.applyViewRect).
| Method | When it is called |
|---|---|
touchPress(viewInputPosition) |
The frame of the press |
touchMove(viewInputPosition) |
While held (not on the frame of the press) |
touchRelease(viewInputPosition) |
The frame of the release (right after touchPress if both happened within one frame) |
touchCancel(viewInputPosition) |
The frame a touch was cancelled (touchcancel) |
touchWheel(viewInputPosition, wheelDelta) |
When the wheel was rolled this frame (wheelDelta is the deltaX and deltaY accumulated this frame) |
By default the mouse passes any button as a touch. To pass only the left button, call inputManager.setTouchFromLeftButtonOnly(true). With several fingers, the position of the first changed finger of the latest touch event is used. (see "Multiple fingers" below for several fingers)
import { Engine, EngineConfiguration, Scene, Vector2, Color } from "./build/vanilla.js";
class DragScene extends Scene {
#ballPosition;
#grabOffset;
constructor() {
super();
this.#ballPosition = Vector2.create(640, 360);
this.#grabOffset = null;
}
touchPress(viewInputPosition) {
const deltaX = viewInputPosition.x - this.#ballPosition.x;
const deltaY = viewInputPosition.y - this.#ballPosition.y;
if (deltaX * deltaX + deltaY * deltaY <= 60 * 60) {
this.#grabOffset = Vector2.create(deltaX, deltaY);
}
}
touchMove(viewInputPosition) {
if (this.#grabOffset === null) {
return;
}
this.#ballPosition = Vector2.create(viewInputPosition.x - this.#grabOffset.x, viewInputPosition.y - this.#grabOffset.y);
}
touchRelease(viewInputPosition) {
this.#grabOffset = null;
}
touchCancel(viewInputPosition) {
this.#grabOffset = null;
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
draw(graphic) {
super.draw(graphic);
const isGrabbed = this.#grabOffset !== null;
graphic.setFillColor(isGrabbed ? new Color(1, 0.7, 0.2, 1) : new Color(0.3, 0.7, 1, 1));
graphic.drawCircle(this.#ballPosition, 60);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new DragScene());You do not need to call the parent when overriding touchPress and the others. (the Scene base implementations are empty) When overriding tick, call super.tick(timeDelta) so these methods are called.
Passing touches to nodes (TouchRaycaster, TouchRecognizer)
A node needs three things to receive touches.
WorldNode.setInteractable(true)- A component with touch methods (
touchPress,touchMove,touchRelease,touchCancel). The node passes each touch to every component that has the method. - The node is under the scene root. Whatever the kind of scene,
tickTouchfirst passes the touch to the frontmost node through the scene's touch raycaster (aTouchRaycasterat first), then calls the scene's touch methods.
On a press, TouchRaycaster walks the tree from the root in draw order and picks, among nodes that are active, have isInteractable() true and contain the point (contains), the one drawn last (the one visible on top). Descendants outside a node with a Mask are not picked. The picked node keeps receiving touchMove and touchRelease until release, even if the finger leaves it. TouchRecognizer extends this: if the picked node has a UIScrollView ancestor, it delays the press by 0.15 seconds, and once the drag goes beyond 10 pixels it withholds or cancels the press on the node and hands the drag to the scroll view. If there are buttons inside a scroll view, replace the scene's raycaster with setTouchRaycaster(new TouchRecognizer()). If the scene has to intercept touches (such as dragging a bar), turn it off with setTouchRaycaster(null) and pass touches yourself from the scene's touch methods. UIScene and GameScene do this.
import { Engine, EngineConfiguration, Scene, WorldNode, Component, Paint, TouchRecognizer, Vector2, Color } from "./build/vanilla.js";
class PressHighlight extends Component {
#paint;
#clickCount;
constructor() {
super();
this.#paint = null;
this.#clickCount = 0;
}
attach(node) {
super.attach(node);
this.#paint = node.getComponent(Paint);
}
require() {
return [Paint];
}
touchPress(viewInputPosition) {
this.#paint.setColor("#ffaa33");
}
touchRelease(viewInputPosition) {
this.#paint.setColor("#3366cc");
const node = this.getNode();
const isInside = node.contains(viewInputPosition);
if (isInside) {
this.#clickCount += 1;
console.log("clicked", this.#clickCount);
}
}
touchCancel(viewInputPosition) {
this.#paint.setColor("#3366cc");
}
}
class ButtonScene extends Scene {
create() {
super.create();
this.setTouchRaycaster(new TouchRecognizer());
}
initialize(engine) {
super.initialize(engine);
const root = this.getRoot();
const button = new WorldNode();
button.setContentSize(Vector2.create(240, 90));
button.setLocalPosition(Vector2.create(640, 360));
button.setInteractable(true);
button.addComponent(PressHighlight);
const paint = button.getComponent(Paint);
paint.setColor("#3366cc");
paint.setRoundSize(16);
root.addChild(button);
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new ButtonScene());PressHighlightrequiresPaintthroughrequire(), soaddComponent(PressHighlight)attachesPaintfirst.touchWheelpasses the wheel to the topmost scroll view (such asUIScrollView) covering that point.UIScenemakes this connection by itself. UI components such as buttons and scroll views are covered in the "UI" chapter.
Multiple fingers (TouchPoint)
A scene's touch methods receive only one position. Read multiple fingers from InputManager. Only fingers on a touch screen are listed; the mouse is not.
getTouchPoints(): the fingers touching now (in touch order). The phase isbegan(touched this frame),movedorstationarygetEndedTouchPoints(): fingers lifted this frame (phaseendedorcancelled, position where they were lifted)findTouchPoint(identifier): find by number (nullif none)
A TouchPoint has the fields identifier (the same number while touching), phase (a TouchPhase value), canvasNativePosition (CSS pixels from the canvas's top left) and viewPosition (view coordinates).
import { Engine, EngineConfiguration, Scene, TouchPhase, Vector2, Color } from "./build/vanilla.js";
class MultiTouchScene extends Scene {
#fingerPositions;
constructor() {
super();
this.#fingerPositions = [];
}
tick(timeDelta) {
super.tick(timeDelta);
const engine = this.getEngine();
const inputManager = engine.getInputManager();
const touchPoints = inputManager.getTouchPoints();
this.#fingerPositions = [];
for (const touchPoint of touchPoints) {
const viewPosition = touchPoint.viewPosition;
this.#fingerPositions.push(viewPosition);
if (touchPoint.phase === TouchPhase.began) {
const identifier = touchPoint.identifier;
console.log("finger down", identifier);
}
}
const endedTouchPoints = inputManager.getEndedTouchPoints();
for (const endedTouchPoint of endedTouchPoints) {
const identifier = endedTouchPoint.identifier;
const phase = endedTouchPoint.phase;
console.log("finger up", identifier, phase);
}
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
draw(graphic) {
super.draw(graphic);
graphic.setFillColor(new Color(1, 1, 1, 0.6));
for (const fingerPosition of this.#fingerPositions) {
graphic.drawCircle(fingerPosition, 50);
}
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new MultiTouchScene());Mouse buttons and wheel
| Method | Description |
|---|---|
isMouseButtonDown(button) |
Whether held. Buttons are MouseButton.left (0), middle (1) and right (2) |
isMouseButtonJustPressed(button), isMouseButtonJustReleased(button) |
Whether pressed or released this frame (also true for a press and release between frames) |
getViewInputPosition(), getCanvasNativeInputPosition() |
Last pointer position (view coordinates, canvas CSS pixels) |
isTouchDown() |
Whether a touch (a finger, or a mouse button passed as touch) is held |
hasWheelDelta(), getWheelDeltaX(), getWheelDeltaY() |
Wheel values accumulated this frame (rolling down makes Y positive) |
isWheelWithCtrl() |
Whether the last wheel was rolled with Ctrl (laptop touchpad pinches also arrive as Ctrl wheel) |
setContextMenuPrevented(isPrevented) |
Whether to block the canvas's right click menu (true by default) |
setTouchFromLeftButtonOnly(isLeftButtonOnly) |
Whether to pass only the left button as touch (false by default) |
Keyboard
Keys are queried with KeyboardEvent.code strings. These names are fixed by physical position regardless of keyboard layout. (such as "KeyA", "Digit1", "ArrowLeft", "Space", "Enter", "Escape", "ShiftLeft", "ControlLeft")
| Method | Description |
|---|---|
isKeyPressed(code) |
Whether held now |
isKeyJustPressed(code) |
Whether pressed this frame (also true for a key pressed and released between frames) |
isKeyJustReleased(code) |
Whether released this frame |
setPreventedKeys(codes) |
Keys whose browser default action is blocked (such as Tab moving focus or Space scrolling the page). Not blocked while typing in a text input |
isTypingInTextField() |
Whether focus is in a text input (input, textarea, contenteditable, select) |
import { Engine, EngineConfiguration, Scene, Vector2, Rect, Color } from "./build/vanilla.js";
class KeyboardScene extends Scene {
#position;
constructor() {
super();
this.#position = Vector2.create(640, 360);
}
initialize(engine) {
super.initialize(engine);
const inputManager = engine.getInputManager();
inputManager.setPreventedKeys(["Space", "ArrowUp", "ArrowDown"]);
}
tick(timeDelta) {
super.tick(timeDelta);
const engine = this.getEngine();
const inputManager = engine.getInputManager();
const speed = 300;
let moveX = 0;
let moveY = 0;
if (inputManager.isKeyPressed("ArrowLeft")) {
moveX -= 1;
}
if (inputManager.isKeyPressed("ArrowRight")) {
moveX += 1;
}
if (inputManager.isKeyPressed("ArrowUp")) {
moveY -= 1;
}
if (inputManager.isKeyPressed("ArrowDown")) {
moveY += 1;
}
this.#position = Vector2.create(this.#position.x + moveX * speed * timeDelta, this.#position.y + moveY * speed * timeDelta);
if (inputManager.isKeyJustPressed("Space")) {
console.log("jump");
}
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
draw(graphic) {
super.draw(graphic);
graphic.setFillColor(new Color(0.4, 0.9, 0.5, 1));
graphic.drawRect(Rect.create(this.#position.x - 40, this.#position.y - 40, 80, 80));
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new KeyboardScene());Input actions (InputActions)
Bind several keys, gamepad buttons and gamepad axis directions to action names chosen by the game (strings such as "confirm" or "jump"), and have the game read by action name. inputManager.getInputActions() returns the one the engine holds, and the engine updates it at the start of every frame.
| Method | Description |
|---|---|
bindKey(action, code) |
Bind a key |
bindGamepadButton(action, buttonIndex) |
Bind a gamepad button (button number in the W3C standard mapping, all connected pads) |
bindAxis(action, axisIndex, direction) |
Bind one direction of an axis (direction 1 is the + side, -1 the - side) |
unbindAction(action), getBoundKeys(action) |
Remove every binding, list of bound keys |
setRepeat(action, delaySeconds, intervalSeconds) |
While held, isJustPressed becomes true again every interval after the delay (menu selection) |
isPressed(action), isJustPressed(action), isJustReleased(action) |
State |
readValue(action) |
0 to 1. Keys and digital buttons give 1, triggers and axes give how far they are pushed |
readAxis(negativeAction, positiveAction) |
A -1 to 1 axis made from two actions |
readMoveAxis() |
The longest movement among WASD and arrow keys, the left stick and external sources (length 1 or less, +x right, +y down) |
setDeadZone(value), setAxisPressThreshold(value) |
Length that filters stick center jitter (default 0.2), tilt at which an axis counts as pressed (default 0.5) |
setKeysIgnoredWhileTyping(isIgnored) |
Whether to ignore keys while typing in a text input (true by default) |
addSource(source) |
Plug in an input source the engine does not know (an object with readActionValue(action)) |
Gamepad button numbers follow the W3C standard mapping. 0 is A (Cross), 1 is B (Circle), 2 is X (Square), 3 is Y (Triangle), 4 and 5 are L1 and R1, 6 and 7 are L2 and R2, 8 is Share (View), 9 is Options (Menu), and 12 to 15 are D-pad up, down, left and right. Axis numbers are 0 for left stick X, 1 for left stick Y (down is +), and 2 and 3 for right stick X and Y.
import { Engine, EngineConfiguration, Scene, Vector2, Rect, Color } from "./build/vanilla.js";
class ActionScene extends Scene {
#position;
#selectedIndex;
constructor() {
super();
this.#position = Vector2.create(640, 360);
this.#selectedIndex = 0;
}
initialize(engine) {
super.initialize(engine);
const inputManager = engine.getInputManager();
const inputActions = inputManager.getInputActions();
inputActions.bindKey("confirm", "Enter");
inputActions.bindKey("confirm", "Space");
inputActions.bindGamepadButton("confirm", 0);
inputActions.bindKey("next", "Tab");
inputActions.bindGamepadButton("next", 15);
inputActions.bindAxis("next", 0, 1);
inputActions.setRepeat("next", 0.4, 0.1);
}
tick(timeDelta) {
super.tick(timeDelta);
const engine = this.getEngine();
const inputManager = engine.getInputManager();
const inputActions = inputManager.getInputActions();
const moveAxis = inputActions.readMoveAxis();
this.#position = Vector2.create(this.#position.x + moveAxis.x * 300 * timeDelta, this.#position.y + moveAxis.y * 300 * timeDelta);
if (inputActions.isJustPressed("next")) {
this.#selectedIndex = (this.#selectedIndex + 1) % 4;
}
if (inputActions.isJustPressed("confirm")) {
console.log("confirm", this.#selectedIndex);
}
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
draw(graphic) {
super.draw(graphic);
for (let index = 0; index < 4; ++index) {
const isSelected = index === this.#selectedIndex;
graphic.setFillColor(isSelected ? "#ffcc33" : "#445066");
graphic.drawRect(Rect.create(40 + index * 140, 40, 120, 60));
}
graphic.setFillColor(new Color(0.4, 0.8, 1, 1));
graphic.drawCircle(this.#position, 40);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new ActionScene());Gamepads (GamepadManager)
Get it with inputManager.getGamepadManager(). The engine registers pads on gamepadconnected and gamepaddisconnected and reads their state every frame. Pads are queried with the Gamepad.index number the browser gives.
| Method | Description |
|---|---|
getAllConnectedGamepads(), getConnectedGamepadCount() |
List and count of connected pads (Gamepad objects) |
isButtonPressed(gamepadIndex, buttonIndex) |
Whether a button is held |
isButtonJustPressed(gamepadIndex, buttonIndex), isButtonJustReleased(...) |
Whether pressed or released in this update |
getButtonValue(gamepadIndex, buttonIndex) |
Button and trigger value (0 to 1) |
getAxisValue(gamepadIndex, axisIndex) |
Stick axis value (-1 to 1) |
vibrate(strongMagnitude, weakMagnitude, seconds) |
Vibrates every connected pad (magnitude 0 to 1; pads and browsers without a vibration actuator are skipped) |
setInputLogEnabled(isEnabled) |
Whether to log to the console whenever buttons or axes change (off by default) |
getActiveGamepadId() |
Name of the pad used in the last update (empty string if none) |
readGamepadKind(gamepadId) tells GamepadKind.xbox, playstation, nintendo or other from the pad name. Use it to match button prompt images to the pad. inputManager.getLastGamepadKind() gives the kind of the last used pad.
import { Engine, EngineConfiguration, Scene, Vector2, GamepadKind, readGamepadKind } from "./build/vanilla.js";
class GamepadScene extends Scene {
tick(timeDelta) {
super.tick(timeDelta);
const engine = this.getEngine();
const inputManager = engine.getInputManager();
const gamepadManager = inputManager.getGamepadManager();
const gamepads = gamepadManager.getAllConnectedGamepads();
for (const gamepad of gamepads) {
const gamepadIndex = gamepad.index;
const leftStickX = gamepadManager.getAxisValue(gamepadIndex, 0);
const leftStickY = gamepadManager.getAxisValue(gamepadIndex, 1);
const rightTrigger = gamepadManager.getButtonValue(gamepadIndex, 7);
if (gamepadManager.isButtonJustPressed(gamepadIndex, 0)) {
const gamepadId = gamepad.id;
const gamepadKind = readGamepadKind(gamepadId);
const buttonName = gamepadKind === GamepadKind.playstation ? "Cross" : "A";
console.log(buttonName, leftStickX, leftStickY, rightTrigger);
gamepadManager.vibrate(0.8, 0.4, 0.2);
}
}
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new GamepadScene());Last input device
inputManager.getLastInputDevice() gives the last used device as one of InputDevice.keyboard, mouse, touch and gamepad. The initial value is touch on devices whose primary input is a finger (CSS pointer: coarse), and keyboard otherwise. Mouse events the browser emulates right after a finger lifts, and tiny mouse movements from a shaking window, do not change the device. isTouchInput() tells whether the last input was a finger. Use it to switch on-screen hints between "Tap" and "Press Enter".
Mouse cursor (PointerCursor)
PointerCursor is a single shared object (PointerCursor.getInstance()). Once turned on with setEnabled(true), the engine sets the canvas's CSS cursor at the end of every frame. While off (the default), it does nothing.
- Over a
UIButtonit is the button cursor (default"pointer"), otherwise the cursor set withsetScreenCursor(name). setCursorNames(buttonCursorName, defaultCursorName)changes the cursor over buttons and the default cursor.- Usable CSS names are
pointer,crosshair,not-allowed,grab,grabbing,text,move,help,wait,progressandnone; an unknown name shows the browser's default arrow. registerImageCursor(name, url, url2x, hotspotX, hotspotY, fallbackCursorName)adds an image cursor that you then use by name. (1x and 2x images, hotspot in 1x pixels, the CSS cursor used when the image cannot be)- On devices whose primary input is a finger, the canvas is left alone.
import { Engine, EngineConfiguration, Scene, PointerCursor, Vector2 } from "./build/vanilla.js";
class CursorScene extends Scene {
initialize(engine) {
super.initialize(engine);
const pointerCursor = PointerCursor.getInstance();
pointerCursor.setEnabled(true);
pointerCursor.setCursorNames("pointer", "default");
}
touchPress(viewInputPosition) {
const pointerCursor = PointerCursor.getInstance();
pointerCursor.setScreenCursor("grabbing");
}
touchRelease(viewInputPosition) {
const pointerCursor = PointerCursor.getInstance();
pointerCursor.setScreenCursor("grab");
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new CursorScene());Things to watch
- A scene's touch methods and
getViewInputPosition()see only one position. When you need several fingers, such as for pinching, usegetTouchPoints(). - Every kind of scene passes touches to nodes. If the scene's touch methods also forward to a raycaster, touches are passed twice, so turn off the scene's passing with
setTouchRaycaster(null)when you pass them yourself. - The tables of key names (
KeyCode) and gamepad numbers (GamepadButtonCode,GamepadAnalogStickCode) are not public names. Pass keys asKeyboardEvent.codestrings and pad inputs as W3C standard mapping numbers. - Keys are received on the document, so the page must have focus. A game inside an iframe receives keys only after the iframe is clicked once.
- When the window loses focus, the engine releases touches and mouse buttons but leaves keys as they are. Key state is cleared when the page becomes visible again (
visibilitychange). If keys staying pressed is a problem, listen forPageEvent.blurredand callinputManager.clear(). - One-frame state (
isKeyJustPressed,isMouseButtonJustPressed, wheel values, lifted fingers) is cleared by the engine at the end of the frame. Read it inside the scene'stick.
Related classes
InputManager, InputActions, InputDevice, MouseButton, TouchPhase, TouchPoint, TouchRaycaster, TouchRecognizer, GamepadManager, GamepadKind, PointerCursor, Scene, WorldNode, UIScene, PageEvent