Thanks Telinc1! That worked.
I’m using the Canvas API directly and it’s working great. To bridge the gap between Phaser and the ContextAPI, I’m creating my paths in Phaser but then iterating through the points to draw it on the canvas.
const cols = 5;
const rows = 5;
const circleRadius = 20;
const padding = 10;
const frameSize = circleRadius * 2 + padding * 2;
const canvas = scene.textures.createCanvas(
'puzzlePieces',
cols * frameSize,
rows * frameSize,
);
const ctx = canvas.context;
for (let x = 0; x < cols; x++) {
for (let y = 0; y < rows; y++) {
const xOffset = x * frameSize;
const yOffset = y * frameSize;
// Fill the entire frame with a color so we see it's area.
ctx.fillStyle = '#' + [0, 0, 0, 0, 0, 0]
.map(() => (2 + Math.round(Math.random() * 12)).toString(16))
.join('');
ctx.fillRect(xOffset, yOffset, frameSize, frameSize);
const points = new Phaser.Curves.Path(
xOffset + padding + circleRadius * 2,
yOffset + padding + circleRadius,
)
.circleTo(circleRadius)
.getPoints();
ctx.save();
ctx.moveTo(points[0].x, points[0].y);
ctx.beginPath();
for (let i = 1; i < points.length; i++) {
ctx.lineTo(points[i].x, points[i].y);
}
ctx.closePath();
ctx.clip();
// Draw the rectangle again, but this time it will be clipped to the circle.
ctx.fillStyle = '#' + [0, 0, 0, 0, 0, 0]
.map(() => (2 + Math.round(Math.random() * 12)).toString(16))
.join('');
ctx.fillRect(xOffset, yOffset, frameSize, frameSize);
// If drawing an image, you can use this:
// const puzzleImg = scene.textures.get('puzzle').getSourceImage() as HTMLImageElement;
// ctx.drawImage(puzzleImg, 0, 0);
ctx.restore();
// Add the texture frame.
canvas.add(
`${x}x${y}`,
0,
x * frameSize,
y * frameSize,
frameSize,
frameSize,
);
}
}
canvas.refresh();
for (let x = 0; x < cols; x++) {
for (let y = 0; y < rows; y++) {
scene.add.image(x * frameSize, y * frameSize, 'puzzlePieces', `${x}x${y}`)
.setOrigin(0, 0);
}
}
Here is the image that was displayed:
