vanilladocs
vanillapunch.com
API / misc/cameragestures

misc/cameragestures

카메라 손짓. (3D 판이나 지도를 돌리고 밀고 키우는 손짓, 마우스 오른쪽과 가운데 단추 끌기, 두 손가락)

Camera gestures. (gestures that rotate, pan and zoom a 3D board or map, mouse right and middle button drag, two fingers)

CameraGestures

짝Counterpart: vanilla-engine CameraGestures

카메라 손짓. (3D 판이나 지도를 돌리고 밀고 키우는 손짓, 마우스 오른쪽과 가운데 단추 끌기, 두 손가락)

  • 엔진의 입력은 손가락 하나(또는 마우스의 아무 단추)를 누름, 끌기, 뗌으로만 넘긴다. 돌리고 밀고 키우는 손짓은 그 밖의 것이라 캔버스의 DOM 사건을 따로 읽는다. (엔진의 동작은 그대로 둔다)
  • 마우스 오른쪽 단추를 누른 채 끌기: 가로로 끈 만큼 돌리고(화면 너비만큼이 한 바퀴), 세로로 끈 만큼 기울인다. 끌지 않고 떼면 오른쪽 클릭이다.
  • 마우스 가운데 단추를 누른 채 끌기: 민다.
  • 두 손가락: 같은 쪽으로 끌면 밀고, 오므리고 벌리면 두 손가락 가운데를 두고 키우고 줄이고, 비틀기 문턱을 넘게 비틀면 돌린다. (오므려 키우다가 조금씩 도는 것을 막는다)
  • 휠은 엔진이 넘긴다. 여기서는 Ctrl 을 눌렀는지만 적어 둔다. (노트북 터치패드의 오므리기도 Ctrl 휠로 온다)
  • 손짓 중에는 엔진이 넘기는 누름과 뗌을 놀이에 쓰지 않도록 isGestureActive 로 알린다.
  • 모인 양(밀기, 돌리기, 기울이기, 키우기)은 매 프레임 take 로 꺼내 간다. 밀기는 뷰 단위, 돌리기와 기울이기는 라디안, 키우기는 곱할 비다.
  • 마지막 손이 손가락인지 마우스인지도 읽는다. 손가락을 뗀 바로 뒤에 브라우저가 흉내 내 보내는 마우스 사건은 무시한다.
  • 생성하면 엔진의 캔버스에 사건을 붙인다. 다 쓰면 dispose().

Camera gestures. (gestures that rotate, pan and zoom a 3D board or map, mouse right and middle button drag, two fingers)

  • The engine's input only passes press, drag and release of one finger (or any mouse button). Rotate, pan and zoom gestures are outside that, so the canvas DOM events are read separately. (the engine's behavior is left as is)
  • Dragging with the right mouse button held: rotates by the horizontal drag (the screen width is one full turn) and tilts by the vertical drag. Releasing without dragging is a right click.
  • Dragging with the middle mouse button held: pans.
  • Two fingers: dragging in the same direction pans, pinching in and out zooms in and out around the midpoint of the two fingers, and twisting past the twist threshold rotates. (prevents slight rotation while pinch-zooming)
  • The wheel is passed by the engine. Here it only records whether Ctrl was held. (laptop touchpad pinches also arrive as Ctrl wheel)
  • During a gesture, isGestureActive signals not to use the presses and releases passed by the engine for gameplay.
  • The accumulated amounts (pan, rotate, tilt, zoom) are taken out every frame with take. Pan is in view units, rotate and tilt in radians, zoom is a ratio to multiply.
  • Also reads whether the last input was a finger or a mouse. Mouse events the browser emulates right after a finger is lifted are ignored.
  • Constructing attaches events to the engine's canvas. When done, dispose().
예제Example두 손가락과 마우스 손짓으로 2D 카메라 돌리고 밀고 키우기Rotate, pan and zoom a 2D camera with two-finger and mouse gestures
import { Engine, EngineConfiguration, Scene, CameraGestures, Camera2D, Rect, Colors } from "./build/vanilla.js";

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const cameraGestures = new CameraGestures();
const camera = new Camera2D();

class MainScene extends Scene {
	tick(timeDelta) {
		super.tick(timeDelta);
		const viewManager = engine.getViewManager();
		const viewSize = viewManager.getViewSize();
		camera.setViewSize(viewSize);
		const pan = cameraGestures.takePan();
		camera.panByScreen(pan.x, pan.y);
		const rotation = cameraGestures.takeRotation();
		const currentRotation = camera.getRotation();
		camera.setRotation(currentRotation + rotation * 180 / Math.PI);
		const zoom = cameraGestures.takeZoom();
		const zoomAnchor = cameraGestures.getZoomAnchor();
		if (zoom !== 1 && zoomAnchor !== null) {
			camera.zoomAt(zoomAnchor, zoom);
		}
		camera.tick(timeDelta);
	}

	draw(graphic) {
		camera.begin(graphic);
		graphic.setFillColor(Colors.white);
		graphic.drawRect(Rect.create(-100, -100, 200, 200));
		camera.end(graphic);
		super.draw(graphic);
	}

	touchPress(viewInputPosition) {
		super.touchPress(viewInputPosition);
		if (cameraGestures.isGestureActive()) {
			return;
		}
		console.log("tap", viewInputPosition.x, viewInputPosition.y);
	}
}

engine.run(new MainScene());
생성자constructorconstructor()#0.4.0 부터since 0.4.0소스source

생성. (캔버스에 사건을 붙인다, 다 쓰면 dispose)

Constructor. (attaches events to the canvas, dispose when done)

짝Counterpart: vanilla-engine CameraGestures.new

메서드methodaddListener(target, type, handler, options)#0.4.0 부터since 0.4.0소스source

DOM 사건 붙이기. (dispose 가 뗍다)

Attaches DOM events. (dispose detaches them)

targetEventTarget
사건을 붙일 대상.Target to attach the event to.
typestring
사건 이름.Event name.
handlerFunction
사건 처리 함수.Event handler function.
optionsobject | boolean
addEventListener 의 options. (dispose 가 뗄 때도 같은 것을 넘긴다)options of addEventListener. (dispose passes the same when removing it)
메서드methoddispose()#0.4.0 부터since 0.4.0소스source

다 씀. (붙인 사건을 뗍다)

Disposes. (detaches the attached events)

메서드methodisGestureActive()#0.4.0 부터since 0.4.0소스source

손짓 중인지. (오른쪽이나 가운데 단추를 누르고 있거나 두 손가락, 이때 엔진의 누름은 놀이에 쓰지 않는다)

Whether a gesture is in progress. (the right or middle button is held or two fingers are down, during which the engine's presses are not used for gameplay)

반환Returnsboolean 손짓 중이면 참.True during a gesture.

짝Counterpart: vanilla-engine CameraGestures.is_gesture_active

메서드methodisTouchInput()#0.4.0 부터since 0.4.0소스source

마지막 손이 손가락인지. (처음에는 기기가 손가락이 주된 입력인지로)

Whether the last input was a finger. (initially, whether the device's primary input is touch)

반환Returnsboolean 마지막 손이 손가락이면 참.True if the last input was a finger.

짝Counterpart: vanilla-engine CameraGestures.is_touch_input

메서드methodisCoarsePointer()#0.4.0 부터since 0.4.0소스source

기기가 손가락이 주된 입력인지. (폰과 태블릿)

Whether the device's primary input is touch. (phones and tablets)

반환Returnsboolean 손가락이 주된 입력인 기기면 참. (matchMedia 가 없으면 거짓)True on a device whose primary input is a finger. (false if matchMedia is unavailable)

짝Counterpart: vanilla-engine CameraGestures.is_coarse_pointer

메서드methodisLastWheelCtrl()#0.4.0 부터since 0.4.0소스source

마지막 휠이 Ctrl 을 누른 휠이었는지. (휠로 밀기를 켰어도 그 휠은 키우기입다)

Whether the last wheel was a wheel with Ctrl held. (even with wheel panning enabled, that wheel zooms)

반환Returnsboolean 마지막 휠이 Ctrl 을 누른 휠이면 참.True if the last wheel was a wheel with Ctrl held.

짝Counterpart: vanilla-engine CameraGestures.is_last_wheel_ctrl

메서드methodtakePan()#0.4.0 부터since 0.4.0소스source

모인 밀기 꺼내기. (Vector2 뷰 단위, 꺼내면 비웁다)

Take the gathered pan. (Vector2 in view units, emptied once taken)

반환ReturnsVector2 모인 밀기. (x, y 가 뷰 단위)The gathered pan. (x and y are in view units)

짝Counterpart: vanilla-engine CameraGestures.take_pan

메서드methodtakeRotation()#0.4.0 부터since 0.4.0소스source

모인 돌리기 꺼내기. (라디안, 꺼내면 비웁다)

Takes the accumulated rotation. (radians, cleared when taken)

반환Returnsnumber 모인 돌리기. (라디안)Accumulated rotation. (radians)

짝Counterpart: vanilla-engine CameraGestures.take_rotation

메서드methodtakeTilt()#0.4.0 부터since 0.4.0소스source

모인 기울이기 꺼내기. (라디안, 꺼내면 비웁다)

Takes the accumulated tilt. (radians, cleared when taken)

반환Returnsnumber 모인 기울이기. (라디안, + 가 위에서 보는 쪽)Accumulated tilt. (radians, + is toward looking from above)

짝Counterpart: vanilla-engine CameraGestures.take_tilt

메서드methodtakeZoom()#0.4.0 부터since 0.4.0소스source

모인 키우기 꺼내기. (곱할 비, 꺼내면 1)

Takes the accumulated zoom. (ratio to multiply, reset to 1 when taken)

반환Returnsnumber 모인 키우기. (곱할 비, 1 이면 그대로)Accumulated zoom. (ratio to multiply, 1 means unchanged)

짝Counterpart: vanilla-engine CameraGestures.take_zoom

메서드methodgetZoomAnchor()#0.4.0 부터since 0.4.0소스source

키우기의 가운데. (뷰 좌표, 두 손가락 가운데, 없으면 null)

Zoom center. (view coordinates, midpoint of the two fingers, null if none)

반환ReturnsVector2 | null 키우기의 가운데. (뷰 좌표, 두 손가락으로 키운 적이 없으면 null)Center of the zoom. (view coordinates, null if never zoomed with two fingers)

짝Counterpart: vanilla-engine CameraGestures.get_zoom_anchor

메서드methodtakeRightClick()#0.4.0 부터since 0.4.0소스source

오른쪽 클릭이 있었는지 꺼내기. (끌지 않고 뗀 오른쪽 단추, 꺼내면 지웁다)

Takes whether a right click occurred. (right button released without dragging, cleared when taken)

반환Returnsboolean 오른쪽 클릭이 있었으면 참.True if there was a right click.

짝Counterpart: vanilla-engine CameraGestures.take_right_click

메서드methodtakeRotationReleased()#0.4.0 부터since 0.4.0소스source

돌리기를 놓았는지 꺼내기. (꺼내면 지웁다)

Takes whether rotation was released. (cleared when taken)

반환Returnsboolean 돌리기를 놓았으면 참. (오른쪽 단추를 떼거나 두 손가락을 모두 뗐을 때)True if the rotation was released. (when the right button is released or both fingers are lifted)

짝Counterpart: vanilla-engine CameraGestures.take_rotation_released

메서드methodreadUnitsPerCssPixel()#0.4.0 부터since 0.4.0소스source

CSS 픽셀 하나가 뷰 단위로 얼마인지. (폰의 pt 와 같습다)

How many view units one CSS pixel is. (same as a pt on phones)

반환Returnsnumber CSS 픽셀 하나의 뷰 단위 크기. (엔진이 없으면 1)Size of one CSS pixel in view units. (1 if there is no engine)

메서드methodreadViewPosition(clientX, clientY)#0.4.0 부터since 0.4.0소스source

브라우저 창의 자리를 뷰 좌표로. (엔진이 누름의 자리를 바꾸는 것과 같게, 엔진이 없으면 null)

Browser window position to view coordinates. (same as how the engine converts press positions, null if there is no engine)

clientXnumber
브라우저 창 기준 X. (CSS 픽셀)X relative to the browser window. (CSS pixels)
clientYnumber
브라우저 창 기준 Y. (CSS 픽셀)Y relative to the browser window. (CSS pixels)

반환ReturnsVector2 | null 뷰 좌표. (엔진이나 캔버스가 없으면 null)View coordinates. (null if there is no engine or canvas)

메서드methodisCompatibilityMouse()#0.4.0 부터since 0.4.0소스source

손가락을 뗀 바로 뒤의 마우스 사건인지. (브라우저가 흉내 내 보내는 것)

Whether it is a mouse event right after a finger was lifted. (emulated by the browser)

반환Returnsboolean 마지막 손가락 사건 뒤 무시하는 시간 안이면 참.True if within the ignore time after the last finger event.

메서드methodhandleMouseDown(mouseEvent)#0.4.0 부터since 0.4.0소스source

마우스 누름. (오른쪽 단추면 돌리기, 가운데 단추면 밀기 시작)

Mouse press. (the right button starts rotating, the middle button starts panning)

mouseEventMouseEvent
브라우저 마우스 사건.Browser mouse event.
메서드methodreadMouseDragCursor()#0.4.0 부터since 0.4.0소스source

마우스로 끄는 커서. (오른쪽 단추로 돌리거나 가운데 단추로 미는 중이면 grabbing, 오른쪽 단추를 누르고 아직 끌지 않았으면 grab, 아니면 빈 글)

Mouse drag cursor. (grabbing while rotating with the right button or panning with the middle button, grab if the right button is held but not yet dragged, otherwise an empty string)

반환Returnsstring CSS 커서 이름. ("grabbing", "grab", 빈 글)CSS cursor name. ("grabbing", "grab", empty string)

짝Counterpart: vanilla-engine CameraGestures.read_mouse_drag_cursor

메서드methodhandleMouseMove(mouseEvent)#0.4.0 부터since 0.4.0소스source

마우스 움직임. (오른쪽 단추를 누른 채면 가로로 움직인 만큼 돌리고 세로로 움직인 만큼 기울이고, 가운데 단추면 밉다)

Mouse move. (with the right button held, rotates by the horizontal movement and tilts by the vertical movement, with the middle button pans)

mouseEventMouseEvent
브라우저 마우스 사건.Browser mouse event.
메서드methodhandleMouseUp(mouseEvent)#0.4.0 부터since 0.4.0소스source

마우스 뗌. (오른쪽 단추면 돌리기 끝, 끌지 않았으면 오른쪽 클릭, 가운데 단추면 밀기 끝)

Mouse release. (the right button ends rotating, a right click if not dragged, the middle button ends panning)

mouseEventMouseEvent
브라우저 마우스 사건.Browser mouse event.
메서드methodhandleWheel(wheelEvent)#0.4.0 부터since 0.4.0소스source

휠. (Ctrl 을 눌렀는지만 적는다, 키우기와 밀기는 엔진이 넘긴 휠로 화면이 합다)

Wheel. (only records whether Ctrl was held, zoom and pan are done by the screen from the wheel passed by the engine)

wheelEventWheelEvent
브라우저 휠 사건.Browser wheel event.
메서드methodhandleTouchChange(touchEvent)#0.4.0 부터since 0.4.0소스source

손가락 수가 바뀜. (둘 이상이면 손짓을 시작하고, 모두 떼면 끝냅다)

Finger count changed. (two or more starts a gesture, lifting all ends it)

touchEventTouchEvent
브라우저 손가락 사건.Browser touch event.
메서드methodcaptureTouchPose(touches)#0.4.0 부터since 0.4.0소스source

두 손가락의 가운데, 각도, 거리를 기준으로 잡기.

Captures the midpoint, angle and distance of the two fingers as the reference.

touchesTouchList
지금 닿은 손가락 목록. (앞의 둘을 쓴다)List of fingers currently touching. (uses the first two)
메서드methodhandleTouchMove(touchEvent)#0.4.0 부터since 0.4.0소스source

두 손가락이 움직임. (가운데가 움직인 만큼 밀고, 거리가 바뀐 만큼 키우고, 문턱을 넘게 비틀면 돌립다)

Two fingers moved. (pans by how much the midpoint moved, zooms by how much the distance changed, rotates when twisted past the threshold)

touchEventTouchEvent
브라우저 손가락 사건.Browser touch event.
메서드methodsetThresholds(twistStartRadians, tiltPerHalfHeightRadians, rightClickDistance, compatibilityMouseMilliseconds)#0.4.0 부터since 0.4.0소스source

문턱 설정. (비틀기를 시작하는 각도, 화면 높이의 반만큼 끌 때 기울어지는 각도, 오른쪽 클릭으로 보는 거리, 흉내 낸 마우스를 무시하는 밀리초)

Sets the thresholds. (angle to start twisting, tilt angle when dragging half the screen height, distance counted as a right click, milliseconds to ignore emulated mouse)

twistStartRadiansnumber
두 손가락 비틀기가 돌리기를 시작하는 각도. (라디안, 기본 15 도)Angle at which a two-finger twist starts rotating. (radians, default 15 degrees)
tiltPerHalfHeightRadiansnumber
오른쪽 끌기를 화면 높이의 반만큼 세로로 끌면 기울어지는 각도. (라디안, 기본 35 도)Tilt angle when a right drag moves vertically by half the screen height. (radians, default 35 degrees)
rightClickDistancenumber
오른쪽 단추를 이만큼 안에서 떼면 오른쪽 클릭으로 본다. (CSS 픽셀, 기본 8)Releasing the right button within this distance counts as a right click. (CSS pixels, default 8)
compatibilityMouseMillisecondsnumber
손가락을 뗀 뒤 흉내 낸 마우스 사건을 무시하는 시간. (밀리초, 기본 800)Time to ignore emulated mouse events after a finger is lifted. (milliseconds, default 800)

짝Counterpart: vanilla-engine CameraGestures.set_thresholds

예제Example문턱을 도 단위로 바꿔 넣기Set thresholds converted from degrees
import { CameraGestures } from "./build/vanilla.js";

const cameraGestures = new CameraGestures();
const twistStartRadians = 10 * Math.PI / 180;
const tiltPerHalfHeightRadians = 45 * Math.PI / 180;
cameraGestures.setThresholds(twistStartRadians, tiltPerHalfHeightRadians, 6, 600);
console.log(cameraGestures.isGestureActive(), cameraGestures.takeZoom());
cameraGestures.dispose();