本文へスキップ
Skillpedia
一般的なプログラミング6.4k · #14217

melonjs-ui-and-text

できること

このガイドは、HUDの要素、ボタン、メニュー、パネル、テキストをmelonJSのゲームに追加する方法を説明します。メニュー、スコア、インターフェースの要素を作るゲーム開発者向けです。

使う場面

  • ✓開発者が、ゲーム画面にクリックできるボタンやメニューが欲しいとき。
  • ✓開発者が、ゲームに体力バーや進行バーを入れたいとき。
  • ✓開発者の縁取りのテキストが、切れて見えたり細すぎたりするとき。

スキルのリンク

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

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-ui-and-text/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“私のmelonJSのメニュー画面に、スコアの表示とクリックできる開始ボタンを追加してください。”

毎週5つの新しいスキル。1分で読めます。

仕組み

このガイドは、テキスト、ボタン、スライダー、パネルの作り方を示し、ゲームのワールドが動いても固定されたままの画面にそれらを配置する方法を示します。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

UI, HUD and text

Outlined text: behind the glyphs, or over them

An outline is centred on the glyph's edge, so half of it lies inside the glyph. Text paints it behind the glyphs by default (paintOrder: "stroke"): the letters stay whole and the outline shows only around them, so a thick outline works on any face. What shows is half of lineWidth: ask for twice the width you want to see.

paintOrder: "fill" paints the glyphs first and the outline over them, as a plain canvas does with fillText then strokeText, and as melonJS did before 20.13. Its inner half then covers the letters: on a small or thin face a lineWidth of 3 already leaves them solid outline colour.

const style = {
    font: "sans-serif", size: 32,
    fillStyle: "#ffd23f", strokeStyle: "#1b1b1b", lineWidth: 6,
};
new Text(x, y, { ...style, text: "BEHIND" });                     // the default
new Text(x, y, { ...style, paintOrder: "fill", text: "OVER" });   // the old look

The stroke used to cost you the top row of pixels as well, and a display face whose glyphs overshoot the nominal ascent did the same on its own: the render box was sized from the line height alone, with no allowance for either. That is fixed — the bake is padded by the ink's real extent and the blit shifts back by the same amount, so nothing is clipped and the reported bounds are unchanged. Write the string you mean:

new Text(x, y, { font: "Display", lineWidth: 1, lineHeight: 1.45, text: "" });
hud.setText("SCORE 100");

If you have a label carrying a leading \n to buy headroom, that workaround is now dead weight — drop it, and take the line height back off the position you shifted it by.

The outline's corners are set by lineJoin, the same property a canvas context has, and miterLimit:

setting corners use it for
lineJoin: "round" (default) rounded: the outline is the same width all around almost everything: HUDs, buttons, titles
lineJoin: "bevel" cut flat a harder edge with no spikes
lineJoin: "miter" pointed, as a plain canvas strokes text a sharp, angular look
miterLimit: n (default 10) with "miter" only: a corner pointing further than n half line widths is cut flat instead taming the spikes "miter" grows on "A", "V", "M"

At the canvas default of 10, a thick "miter" outline grows spikes several times its width from sharp glyphs; 2 to 4 keeps the corners pointed and the spikes short:

const outline = {
    font: "sans-serif", size: 48,
    fillStyle: "#ffffff", strokeStyle: "#000000", lineWidth: 6,
};
new Text(x, y, { ...outline, text: "ROUND" });                    // the default
new Text(x, y, { ...outline, lineJoin: "bevel", text: "BEVEL" });
new Text(x, y, { ...outline, lineJoin: "miter", text: "MITER" }); // long spikes
new Text(x, y, { ...outline, lineJoin: "miter", miterLimit: 3, text: "MITER 3" });

Whichever you pick, the bake leaves room for it on all four sides, so no corner is cut and the label's position and bounds do not move; pointed corners just cost a bigger texture. Changed on a live label, they show from the next setText().

Gradient text

fillStyle takes a Gradient as well as a colour — the same object Renderer#setColor accepts, built the way the canvas API builds one:

const ramp = renderer.createLinearGradient(0, 0, 0, 24);  // top to bottom
ramp.addColorStop(0, "#fffdf0");
ramp.addColorStop(1, "#f0a020");

new Text(x, y, { font: "Display", size: 24, fillStyle: ramp });

Coordinates are the label's own bake: (0, 0) is the top-left of the render box, so the ramp above runs down one line. A multi-line label restarts it on every line, reading like one Text per line — you do not have to author the gradient over the block height. Pass gradientPerLine: false for a single ramp spanning the whole block, which is what a plain canvas does and what a deliberate fade across a two-line title wants.

The ramp colours the fill only — Text strokes in a separate pass, so an outline keeps its own colour without any luminance trickery — and it works on Canvas2D, which a post effect does not.

The gradient does not live in fillStyle

fillStyle ACCEPTS a Gradient; it never HOLDS one. The constructor routes it to fillGradient and leaves fillStyle as the pooled Color it always is, which is what keeps fillStyle.alpha gating the fill and keeps the colour owned by the pool.

So changing the ramp la

よくある質問

縁取りのテキストが正しく見えないのはなぜですか?

縁取りは文字の輪郭の中心に描かれるため、幅の半分しか見えないとガイドは説明しています。

2つの要素が重なると、どちらがクリックを受け取りますか?

重なったパネルでは、ポインターはそのうち1つに届くとスキルは述べており、その扱い方を説明しています。

カスタムのWebフォントを使えますか?

はい。テキストの要素にWebフォントを読み込むことに触れています。

詳細

作者による元の説明

Use this skill for HUDs, buttons, menus, options screens, dialogue panels, progress bars and on-screen text in melonJS. Covers UIBaseElement/UISpriteElement/UITextButton, UICheckbox, UISlider, ProgressBar, UIPanel (skins, layout, scrolling), Draggable and DropTarget, the floating screen-space container pattern, which of two overlapping panels gets the pointer, Text and BitmapText, web font loading, and NineSliceSprite panels. Triggers on: UI, HUD, button, checkbox, radio button, UICheckbox, slider, UISlider, volume slider, options menu, settings screen, scrolling list, scroll panel, layout, co

repo: melonjs/melonJS

人気

GitHubのスター
6.4k
ランキング
#14217
ライセンス
MIT
更新日
2026/11/2

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル