본문으로 건너뛰기
Skillpedia
일반 코딩6.4k · #14198

melonjs-camera-and-drawing

하는 일

카메라가 캐릭터를 따라가거나 흔들리고 페이드되는 방법과, 도형과 그라데이션을 직접 그리는 방법을 설명합니다. 정밀한 카메라와 그리기 제어를 원하는 게임 개발자를 위한 스킬입니다.

사용하는 경우

  • ✓개발자가 melonJS 게임에서 카메라가 플레이어를 부드럽게 따라가게 하려 할 때.
  • ✓개발자가 게임 화면 안에 사용자 정의 도형이나 선을 그리려 할 때.
  • ✓개발자가 마우스 위치를 게임 월드 좌표로 변환해야 할 때.

스킬 링크

https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-camera-and-drawing/SKILL.md

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-camera-and-drawing/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“카메라가 제 플레이어를 부드럽게 따라가게 하고, 시점은 발보다 조금 위에 유지해 주세요.”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

스킬은 AI에게 엔티티 자체를 follow 호출에 전달하고, 부드러움 값을 설정하며, 카메라를 움직이지 않고 오프셋으로 시점만 이동하도록 안내합니다.

기술 내용 (원본 SKILL.md)

작성자의 영어 원문을 GitHub에서 그대로 가져왔습니다.

Camera and immediate-mode drawing

Following a target

app.viewport.follow(player, app.viewport.AXIS.BOTH, 0.1);

Pass the renderable, not renderable.pos. follow() accepts a Renderable, a Vector2d or a Vector3d and throws invalid target for me.Camera2d.follow for anything else. A renderable's pos is an ObservableVector3d, which is not a Vector3d subclass, so follow(player.pos, …) throws.

The third argument is a damping factor in [0 .. 1] — lower is smoother and laggier, 1 snaps. Omit it for a hard lock (it defaults to 1). It is frame-rate independent since 19.7. AXIS.HORIZONTAL / AXIS.VERTICAL / AXIS.BOTH / AXIS.NONE.

Call it from the followed entity's onActivateEvent, using this.parentApp.

To frame the target off centre, set offset; do not follow a helper object. It shifts the view without moving the camera, in world units, and is applied after following and the bounds, so the view can show past them:

app.viewport.follow(player, app.viewport.AXIS.BOTH, 0.1);
app.viewport.offset.set(0, -70);   // look a little above the feet

// or ahead of where the player faces, eased (see math.damp below)
const ahead = facingLeft ? -80 : 80;
app.viewport.offset.x = math.damp(app.viewport.offset.x, ahead, 4, dt / 1000);

Culling, tile layers, parallax ImageLayers, lights and the pointer (localToWorld, worldToLocal) all count it. shake() adds its jitter on top and gives the value back when it ends, so setting it before or during a shake is fine. reset() clears it.

For anything that is NOT the camera, the same smoothing is math.damp. follow() is frame-rate independent for you; a value you smooth yourself is not, and lerp(current, target, alpha) once per frame moves further on a slow frame than on a fast one, so the feel changes with the frame rate:

import { math } from "melonjs";

// dt is milliseconds, damp wants seconds
this.speed = math.damp(this.speed, targetSpeed, 5, dt / 1000);
this.turret.angle = math.damp(this.turret.angle, wanted, 8, dt / 1000);

damp(current, target, lambda, dt). Higher lambda converges faster; it is the rate, not a per-frame fraction.

For a DIRECTION rather than a scalar, the tool is slerp, on Vector2d and Vector3d alike. Two reasons, and the second is the one that bites:

// a turret, a ship, a camera: a facing that chases another facing
facing.slerp(toTarget, 1 - Math.exp((-6 * dt) / 1000));
  1. Damping a facing component by component drags it through the inside of the circle, so it slows at the ends, rushes the middle and comes out shorter than it went in.
  2. Damping a scalar heading goes the wrong way round the wrap. From 170 degrees to -170 degrees is 20 degrees apart, and the numbers are 340 apart, so the turret sweeps all the way round the back. slerp works on the directions, takes the shorter arc by construction, and has no wrap to cross.

See melonjs-3d for the rest, including what happens between two exactly opposite directions. Use it for camera pans you drive yourself, turret tracking, zoom, input smoothing and any value that should chase another one — it is the tool that is easy to miss and easy to hand-write slightly wrong.

Bounds are the thing people forget

The camera's bounds default to the renderer size, so it cannot scroll until you widen them to the world:

app.viewport.setBounds(0, 0, mapWidth, mapHeight);

Symptom: follow() is called, looks right, and the view never moves. Note a canvas resize resets the default camera's bounds to (0, 0, w, h), so re-apply them from a CANVAS_ONRESIZE handler if your game resizes. Loading a Tiled map sets them for you from the map size.

Camera effects

app.viewport.shake(4, 200);                       // intensity, ms
app.viewport.fadeIn("#000000", 500);              // fade TO black
app.viewport.fadeOut("#000000", 500);             // start black, fade back to normal
app.viewport.colorMatrix.contrast(1.1).saturate(1.1);
app.viewport.addPostEffect(new VignetteEffect(app.renderer));

The fade pair is named from the colour's point of view, not the scene's: fadeIn fades the colour in (screen goes to color), fadeOut fades it out (screen starts at color and clears). Both default to a 1000 ms duration and take an optional completion callback.

fadeIn does not HOLD the colour. The effect completes, the camera's own update destroys it, and the scree

자주 묻는 질문

follow 호출에서 왜 오류가 나나요?

스킬은 위치 속성이 아니라 렌더러블 자체를 전달하라고 말합니다.

시점을 어떻게 오프셋하나요?

뷰포트 오프셋을 설정하면, 카메라를 움직이지 않고 월드 단위로 시점을 이동합니다.

직접 도형을 그릴 수 있나요?

네. 사용자 정의 그리기 함수 안에서 그리는 도형, 선, 그라데이션을 다룹니다.

상세 정보

작성자의 원본 설명

Use this skill for camera control and immediate-mode drawing in melonJS — following a target, viewport bounds, shake and fade, secondary cameras and what each one draws, and drawing shapes, lines and gradients inside a custom draw(). Covers world versus screen coordinate conversion, clipping and masking, baking with CanvasRenderTarget, and reading pixels back. Triggers on: Camera2d, viewport, follow, offset, look ahead, setBounds, minimap, second camera, split screen, cullMask, visibilityLayer, hide from a camera, shake, fadeIn, fadeOut, worldToLocal, localToWorld, colorMatrix, renderer.fill,

repo: melonjs/melonJS

인기도

GitHub 스타
6.4k
순위
#14198
라이선스
MIT
업데이트
2026. 11. 2.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬