Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#2440] Multiply current gfx ctx opacity #2449

Merged
merged 4 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fixed issue where context opacity was not respected when set in a `preDraw`
- Fixed issue where `ex.Sound.loop` was not working, and switching tab visibility would cause odd behavior with looping `ex.Sound`
- Fixed issue where screenshots from `ex.Engine.screenshot()` did not match the smoothing set on the engine.
- Fixed incorrect event type returned when `ex.Actor.on('postupdate', (event) => {...})`.
Expand Down
14 changes: 14 additions & 0 deletions sandbox/tests/scenepredraw/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scene Predraw Opacity</title>
</head>
<body>
<p>All rectangles should have some transparency</p>
<script src="../../lib/excalibur.js"></script>
<script src="index.js"></script>
</body>
</html>
74 changes: 74 additions & 0 deletions sandbox/tests/scenepredraw/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var paddle = new ex.Actor({
x: 150, y: 150,
width: 200, height: 200,
color: ex.Color.Chartreuse
});

var paddle2 = new ex.Actor({
x: 170, y: 170,
width: 200, height: 200,
color: ex.Color.White
});


class MyScene extends ex.Scene {

public onPreDraw(ctx: ex.ExcaliburGraphicsContext) {
ctx.save();
ctx.opacity = 0.2;
}

public onPostDraw() {
this.engine.graphicsContext.restore();
}
}


class CustomDraw extends ex.System<ex.GraphicsComponent> {
public readonly types = ['ex.graphics'] as const;
public readonly systemType = ex.SystemType.Draw;

private _graphicsContext?: ex.ExcaliburGraphicsContext;
private _engine?: ex.Engine;

public initialize(scene: ex.Scene): void {
this._graphicsContext = scene.engine.graphicsContext;
this._engine = scene.engine;
}

public preupdate(): void {
// Graphics context could be switched to fallback in a new frame
if (this._engine == null) {
throw new Error("Uninitialized ObjectSystem");
}
this._graphicsContext = this._engine.graphicsContext;
}

public update(entities: ex.Entity[], delta: number) {
if (this._graphicsContext == null) {
throw new Error("Uninitialized ObjectSystem");
}
const ctx = this._graphicsContext;

ctx.save();
ctx.translate(0, 0);
ctx.drawRectangle(ex.vec(0, 0), 100, 100, ex.Color.White);
ctx.restore();
}

}

var game = new ex.Engine({
width: 800,
height: 600,
});

var thescene = new MyScene();
thescene.world.add(new CustomDraw());
game.addScene('test', thescene);
game.goToScene('test');

game.add(paddle);
game.add(paddle2);

game.start();
2 changes: 1 addition & 1 deletion src/engine/Graphics/GraphicsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class GraphicsSystem extends System<TransformComponent | GraphicsComponen

// TODO remove this hack on the particle redo
const particleOpacity = (entity instanceof Particle) ? entity.opacity : 1;
this._graphicsContext.opacity = graphics.opacity * particleOpacity;
this._graphicsContext.opacity *= graphics.opacity * particleOpacity;

// Draw the graphics component
this._drawGraphicsComponent(graphics);
Expand Down
34 changes: 34 additions & 0 deletions src/spec/GraphicsSystemSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,38 @@ describe('A Graphics ECS System', () => {

expect(game.graphicsContext.translate).toHaveBeenCalledWith(100, 100);
});

it('will multiply the opacity set on the context', async () => {
const sut = new ex.GraphicsSystem();
const offscreenSystem = new ex.OffscreenSystem();
engine.currentScene.camera.update(engine, 1);
engine.currentScene._initialize(engine);
offscreenSystem.initialize(engine.currentScene);
sut.initialize(engine.currentScene);



engine.graphicsContext.opacity = .5;

const actor = new ex.Actor({
x: 10,
y: 10,
height: 10,
width: 10,
color: ex.Color.Red
});
actor.graphics.opacity = .5;

sut.notify(new ex.AddedEntity(actor));

offscreenSystem.update([actor]);

engine.graphicsContext.clear();
sut.preupdate();
sut.update([actor], 1);

engine.graphicsContext.flush();
await expectAsync(TestUtils.flushWebGLCanvasTo2D(engine.canvas))
.toEqualImage('src/spec/images/GraphicsSystemSpec/graphics-context-opacity.png');
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.