Loading...
Loading...
Use this skill when drawing shapes and graphics in Phaser 4. Covers the Graphics game object, lines, rectangles, circles, arcs, polygons, gradients, fill, stroke, and generated textures. Triggers on: Graphics, draw shape, fillRect, lineStyle, polygon, arc.
npx skill4agent add phaserjs/phaser graphics-and-shapesDrawing primitives with the Graphics game object, and using Shape game objects (Arc, Curve, Ellipse, Grid, IsoBox, IsoTriangle, Line, Polygon, Rectangle, Star, Triangle).
// Draw a filled rectangle and stroked circle with Graphics
const gfx = this.add.graphics();
gfx.fillStyle(0x00aa00, 1);
gfx.fillRect(50, 50, 200, 100);
gfx.lineStyle(3, 0xff0000, 1);
gfx.strokeCircle(400, 150, 60);// Same shapes as standalone Shape game objects
const rect = this.add.rectangle(150, 100, 200, 100, 0x00aa00);
const circle = this.add.circle(400, 150, 60);
circle.setStrokeStyle(3, 0xff0000);this.add.graphics()fillRectstrokeCirclebeginPathlineTostrokePaththis.add.graphics(config?){ x?, y?, lineStyle?, fillStyle? }translateCanvasscaleCanvasrotateCanvassaverestoregenerateTexture(key, width, height)setPositionShapethis.add.rectangle(...)this.add.circle(...)setFillStyle(color, alpha)setStrokeStyle(lineWidth, color, alpha)const gfx = this.add.graphics();
// Solid fill — call before any fill* method
gfx.fillStyle(0xff0000, 1); // (color, alpha)
// Line style — call before any stroke* method
gfx.lineStyle(4, 0x00ff00, 1); // (width, color, alpha)
// Gradient fill (WebGL only) — 4 corner colors
gfx.fillGradientStyle(
0xff0000, 0x00ff00, 0x0000ff, 0xffff00, // tl, tr, bl, br colors
1, 1, 1, 1 // tl, tr, bl, br alphas
);
gfx.fillRect(0, 0, 300, 200);
// Gradient line style (WebGL only)
gfx.lineGradientStyle(2, 0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 1);const gfx = this.add.graphics();
// Rectangles
gfx.fillStyle(0x0000ff);
gfx.fillRect(x, y, width, height);
gfx.lineStyle(2, 0xffffff);
gfx.strokeRect(x, y, width, height);
// Circles
gfx.fillCircle(x, y, radius);
gfx.strokeCircle(x, y, radius);
// Ellipses
gfx.fillEllipse(x, y, width, height, smoothness);
gfx.strokeEllipse(x, y, width, height, smoothness); // smoothness defaults to 32
// Triangles
gfx.fillTriangle(x0, y0, x1, y1, x2, y2);
gfx.strokeTriangle(x0, y0, x1, y1, x2, y2);
// Points (draws a small square)
gfx.fillPoint(x, y, size); // size defaults to 1
// Lines
gfx.lineBetween(x1, y1, x2, y2);const gfx = this.add.graphics();
// Uniform radius (default 20)
gfx.fillStyle(0x333333);
gfx.fillRoundedRect(50, 50, 300, 200, 16);
gfx.lineStyle(2, 0xffffff);
gfx.strokeRoundedRect(50, 50, 300, 200, 16);
// Per-corner radius
gfx.fillRoundedRect(50, 50, 300, 200, {
tl: 20, tr: 20, bl: 0, br: 0
});
// Concave corners (negative values)
gfx.fillRoundedRect(50, 50, 300, 200, { tl: -10, tr: -10, bl: -10, br: -10 });const gfx = this.add.graphics();
// Manual path
gfx.lineStyle(3, 0xffff00);
gfx.beginPath();
gfx.moveTo(100, 100);
gfx.lineTo(200, 50);
gfx.lineTo(300, 100);
gfx.lineTo(250, 200);
gfx.closePath();
gfx.strokePath(); // or gfx.stroke() — alias for strokePath
// Fill a path
gfx.fillStyle(0x00aaff);
gfx.beginPath();
gfx.moveTo(100, 100);
gfx.lineTo(200, 50);
gfx.lineTo(300, 100);
gfx.closePath();
gfx.fillPath(); // or gfx.fill() — alias for fillPath
// Arc within a path (angles in radians)
gfx.beginPath();
gfx.arc(200, 200, 80, 0, Math.PI / 2, false, 0); // (x, y, radius, startAngle, endAngle, anticlockwise, overshoot)
gfx.strokePath();
// Pie slice
gfx.slice(200, 200, 80, 0, Math.PI / 3, false); // auto begins/closes path
gfx.fillPath();
// Stroke/fill from point arrays
gfx.strokePoints(points, closeShape, closePath, endIndex);
gfx.fillPoints(points, closeShape, closePath, endIndex);const circle = new Phaser.Geom.Circle(200, 200, 50);
const rect = new Phaser.Geom.Rectangle(50, 50, 100, 80);
gfx.fillCircleShape(circle);
gfx.strokeCircleShape(circle);
gfx.fillRectShape(rect);
gfx.strokeRectShape(rect);
gfx.fillTriangleShape(triangle);
gfx.strokeTriangleShape(triangle);
gfx.strokeLineShape(line);
gfx.fillEllipseShape(ellipse, smoothness);
gfx.strokeEllipseShape(ellipse, smoothness);const gfx = this.add.graphics();
gfx.save();
gfx.translateCanvas(100, 100);
gfx.rotateCanvas(0.5); // radians
gfx.scaleCanvas(2, 2);
gfx.fillStyle(0xff0000);
gfx.fillRect(0, 0, 50, 50); // draws at translated/rotated/scaled position
gfx.restore();const gfx = this.add.graphics();
gfx.fillStyle(0xff0000);
gfx.fillCircle(32, 32, 32);
gfx.generateTexture('redCircle', 64, 64);
gfx.destroy();
// Now use as a regular texture
this.add.image(400, 300, 'redCircle');fillGradientStyle// Shapes set fill via constructor or setFillStyle
const rect = this.add.rectangle(200, 150, 100, 80, 0xff0000, 1);
// Change fill later
rect.setFillStyle(0x00ff00, 0.8);
// Add stroke (not set by default)
rect.setStrokeStyle(3, 0xffffff, 1); // (lineWidth, color, alpha)
// Remove fill or stroke
rect.setFillStyle(); // no args = isFilled becomes false
rect.setStrokeStyle(); // no args = isStroked becomes false
// Direct property access
rect.fillColor = 0x0000ff;
rect.fillAlpha = 0.5;
rect.strokeColor = 0xffffff;
rect.strokeAlpha = 1;
rect.lineWidth = 2;
rect.isFilled = true;
rect.isStroked = true;
rect.closePath = true; // close stroke path (default true)| Factory Method | Class | Parameters (after x, y) | Fill | Stroke | Notes |
|---|---|---|---|---|---|
| Arc | radius=128, startAngle=0, endAngle=360 (degrees), anticlockwise=false | Yes | Yes | Angles in degrees. Full circle by default. |
| Arc | radius=128 | Yes | Yes | Alias for Arc with 0-360 angles. |
| Curve | Phaser.Curves.Curve object | Yes | Yes | Has |
| Ellipse | width=128, height=128 | Yes | Yes | Equal w/h renders as circle. Has |
| Grid | width=128, height=128, cellWidth=32, cellHeight=32 | Yes | No | Has |
| IsoBox | size=48, height=32, fillTop=0xeeeeee, fillLeft=0x999999, fillRight=0xcccccc | Yes | No | Isometric box. |
| IsoTriangle | size=48, height=32, reversed=false | Yes | No | Isometric pyramid. |
| Line | x1=0, y1=0, x2=128, y2=0 | No | Yes | Stroke only. Constructor takes stroke color (not fill). |
| Polygon | points (various formats) | Yes | Yes | Points: array of Vec2, |
| Rectangle | width=128, height=128 | Yes | Yes | Change size via |
| Star | points=5, innerRadius=32, outerRadius=64 | Yes | Yes | 4 points = diamond. More points = spikier. |
| Triangle | x1=0,y1=128, x2=64,y2=0, x3=128,y3=128 | Yes | Yes | Always closed. Use Polygon for open shapes. |
| Method | Signature | Notes |
|---|---|---|
| | Set fill for subsequent fill calls |
| | Set stroke for subsequent stroke calls |
| | WebGL only. 4 corner colors. |
| | WebGL only. |
| | Set via |
| Method | Signature | Notes |
|---|---|---|
| | Start a new path |
| | Move draw position |
| | Line to position |
| | Angles in radians |
| | Close current path |
| | Fill the current path |
| | Stroke the current path |
| | Pie slice. Auto begins/closes path. Angles in radians. |
| Method | Signature |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Method | Signature |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Method | Signature | Notes |
|---|---|---|
| | Translate subsequent draws |
| | Scale subsequent draws |
| | Rotate subsequent draws |
| | Push state to stack |
| | Pop state from stack |
| | Clear command buffer, reset to default styles |
| | Bake to a Texture (Canvas API) |
| Method | Signature | Notes |
|---|---|---|
| | No args = disable fill |
| | No args = disable stroke |
arcthis.add.arc()fillStylelineStylesetPosition(x, y)displayOriginXdisplayOriginYfillColorisFilled = trueisStrokedfalsesetStrokeStyle()strokeColorstrokeAlphaoutlineFillColoroutlineFillAlphafillGradientStylegenerateTexturegenerateTexturepathDetailThresholdrender.pathDetailThreshold{ tl, tr, bl, br }closePathtruefalsesetTint()tintsetFillStyle(color, alpha)setStrokeStyle(lineWidth, color, alpha)getBounds()Phaser.Geom.Polygon.GetAABB(polygon.geom)const star = this.add.star(400, 300, 5, 32, 64, 0xffff00);
star.setPoints(8); // change number of points
star.setInnerRadius(20); // change inner radius
star.setOuterRadius(80); // change outer radiusconst line = this.add.line(400, 300, 0, 0, 200, 100, 0xffffff);
// setLineWidth(startWidth, endWidth) — endWidth is WebGL only (tapering effect)
line.setLineWidth(4, 1); // tapers from 4px to 1px (Canvas uses startWidth only)const grid = this.add.grid(400, 300, 320, 240, 32, 32, 0x222222);
// Alternating cell color (checkerboard)
grid.setAltFillStyle(0x444444, 1); // no args = disable alternating cells
// Grid uses setStrokeStyle for cell outlines (inherited from Shape)
grid.setStrokeStyle(1, 0x666666, 1);
// v4: control outside edge stroke
grid.strokeOutside = true; // stroke the outer border
grid.strokeOutsideIncomplete = false; // skip partial cells on right/bottom edges
// Cell padding (default 0.5) — gutter between cells is 2x this value
grid.cellPadding = 1;const box = this.add.isobox(200, 200, 48, 32, 0xeeeeee, 0x999999, 0xcccccc);
box.setFaces(true, true, false); // showTop, showLeft, showRight
box.setProjection(4); // isometric projection value
const tri = this.add.isotriangle(400, 200, 48, 32, false, 0xeeeeee, 0x999999, 0xcccccc);
tri.setReversed(true); // flip upside down
tri.setFaces(true, true, true);
tri.setProjection(4);| File | Purpose |
|---|---|
| Graphics class — all drawing methods |
| |
| Internal command constants for the command buffer |
| Render dispatch |
| Base Shape class (fill/stroke/path data, setFillStyle, setStrokeStyle) |
| Arc shape (also used for circle) |
| |
| Curve shape |
| |
| Ellipse shape |
| |
| Grid shape |
| |
| IsoBox shape |
| |
| IsoTriangle shape |
| |
| Line shape |
| |
| Polygon shape |
| |
| Rectangle shape |
| |
| Star shape |
| |
| Triangle shape |
| |