melonjs-camera-and-drawing
Fungsinya
Panduan ini menjelaskan cara membuat kamera mengikuti karakter, bergetar atau memudar, dan menggambar bentuk serta gradien secara manual. Panduan ini untuk pengembang game yang menginginkan kontrol kamera dan penggambaran yang presisi.
Kapan dipakai
- βSaat seorang pengembang ingin kamera mengikuti pemain dengan halus dalam game melonJS.
- βSaat seorang pengembang ingin menggambar bentuk atau garis kustom di dalam layar game.
- βSaat seorang pengembang perlu mengubah posisi mouse menjadi koordinat dunia game.
Link skill
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-camera-and-drawing/SKILL.mdSalin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.
Cara pakai
Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.
Pesan siap tempel
Untuk Claude Code, Codex, atau Cursor: jalankan di folder proyek, lalu skill langsung terpasang.
Unduh SKILL.md lalu taruh di folder skill agen Anda.
Contoh permintaan ke AI
βBuat kamera mengikuti pemain saya dengan halus, dan jaga tampilan sedikit di atas kaki pemain.β
5 skill baru setiap minggu. 1 menit baca.
Cara kerjanya
Skill meminta AI memberikan entitas itu sendiri ke pemanggilan follow, mengatur nilai penghalusan, dan memakai offset untuk menggeser tampilan tanpa memindahkan kamera.
Konten teknis (SKILL.md asli)
Teks asli dari pembuat, dalam bahasa Inggris, langsung dari 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));
- 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.
- 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.
slerpworks 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
Pertanyaan umum
Mengapa pemanggilan follow menghasilkan error?
Skill menyatakan berikan renderable itu sendiri, bukan properti posisinya.
Bagaimana cara menggeser tampilan?
Atur offset viewport, yang menggeser tampilan dalam satuan dunia tanpa memindahkan kamera.
Bisakah saya menggambar bentuk sendiri?
Ya. Skill mencakup bentuk, garis, dan gradien yang digambar di dalam fungsi gambar kustom.
Detail
Deskripsi asli dari pembuat
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
Popularitas
- Bintang GitHub
- 6.4k
- Peringkat
- #14198
- Lisensi
- MIT
- Diperbarui
- 2/11/2026
Komentar
- β¦
Skill lain dari repositori ini
melonjs-20-migration
6.4kMembantu agent Anda menulis kode game melonJS versi 20 dan menghindari metode yang usang dari versi lama, seperti startup async yang wajib.
melonjs-3d-assets
6.4kMembantu agent Anda memuat model 3D seperti berkas glTF, GLB, dan OBJ ke dalam game browser melonJS, dengan opsi skala dan pencahayaan.
melonjs-3d
6.4kMembantu agent Anda membangun adegan 3D atau 2.5D dalam game browser melonJS, mencakup kamera, mesh, lampu, dan kedalaman, dengan aturan sumbu mesin.
melonjs-audio
6.4kMembantu agent Anda menambahkan efek suara dan musik ke game melonJS, termasuk pengaturan audio, musik latar, kontrol volume, dan suara spasial.
Skill serupa
diffs
394kMemungkinkan agent Anda menghasilkan diff perubahan yang nyata dan dapat dibagikan sebagai tautan viewer atau file gambar, bukan ringkasan yang ditulis manual.
coding-agent
394kMembantu agent Anda menyerahkan pekerjaan pemrograman besar ke Codex, Claude Code, atau OpenCode di latar belakang, lalu memeriksa kemajuannya.
gh-issues
394kMembantu agent Anda menemukan issue GitHub, memilih yang dikerjakan, dan membuka pull request perbaikan, termasuk tindak lanjut komentar tinjauan.