Skip to content

Commit

Permalink
fix: [#2490] Circle lineWidth now factored (#2662)
Browse files Browse the repository at this point in the history
Closes #2490 

The PR adjusts the underlying padding dynamically to account for lineWidth in the `Circle` constructor
  • Loading branch information
eonarheim authored Jun 15, 2023
1 parent 4ebacc7 commit 0d5a41f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ are returned

### Fixed

- Fixed issue where setting lineWidth on `ex.Circle` was not accounted for in the bitmap
- Fixed issue in macos where the meta key would prevent keyup's from firing correctly
- Fixed issue when excalibur was hosted in a x-origin iframe, the engine will grab window focus by default if in an iframe. This can be suppressed with `new ex.Engine({grabWindowFocus: false})`
- Fixed issue where `ex.Camera.rotation = ...` did not work to rotate the camera, also addressed offscreen culling issues that were revealed by this fix.
Expand Down
2 changes: 1 addition & 1 deletion sandbox/tests/graphics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Game extends ex.Engine {
radius: 30,
strokeColor: ex.Color.DarkGray,
lineWidth: 10,
padding: 20,
// padding: 20,
smoothing: true,

});
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Graphics/Circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export class Circle extends Raster {
}
constructor(options: RasterOptions & CircleOptions) {
super(options);
this.padding = options.padding ?? 2; // default 2 padding for circles looks nice
const lineWidth = options.lineWidth ?? (options.strokeColor ? 1 : 0); // default lineWidth in canvas is 1px
this.padding = options.padding ?? 2 + (lineWidth / 2); // default 2 padding for circles looks nice
this.radius = options.radius;
this.filtering = options.filtering ?? ImageFiltering.Blended;
this.rasterize();
Expand Down
19 changes: 19 additions & 0 deletions src/spec/CircleSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ describe('A Circle Graphic', () => {
await expectAsync(canvasElement).toEqualImage('src/spec/images/GraphicsCircleSpec/circle.png');
});

it('can set a lineWidth', async () => {
const sut = new ex.Circle({
radius: 30,
lineWidth: 15,
color: ex.Color.Green,
strokeColor: ex.Color.Black
});

const canvasElement = document.createElement('canvas');
canvasElement.width = 100;
canvasElement.height = 100;
const ctx = new ex.ExcaliburGraphicsContext2DCanvas({ canvasElement });

ctx.clear();
sut.draw(ctx, 0, 0);

await expectAsync(canvasElement).toEqualImage('src/spec/images/GraphicsCircleSpec/line-width.png');
});

it('can be cloned', () => {
const sut = new ex.Circle({
radius: 10,
Expand Down
Binary file added src/spec/images/GraphicsCircleSpec/line-width.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0d5a41f

Please sign in to comment.