mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-03 16:50:54 +00:00
6ce6e16837
## Description: Make the terrain backing canvas explicitly opaque by requesting a 2D context with alpha: false. Remove confusing latent alpha support in TerrainLayer Current themes always generate opaque terrain colors, but the code previously looked like it supported per-tile alpha (via terrainColor.rgba.a), which is misleading. Being explicit about opacity can avoid unnecessary alpha compositing work and clarifies intent. No visual change expected with current themes (terrain was already effectively opaque). ## Please complete the following: - [ ] I have added screenshots for all UI updates - [ ] I process any text displayed to the user through translateText() and I've added it to the en.json file - [ ] I have added relevant tests to the test directory - [ ] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: DISCORD_USERNAME
78 lines
2.2 KiB
TypeScript
78 lines
2.2 KiB
TypeScript
import { Theme } from "../../../core/configuration/Config";
|
|
import { GameView } from "../../../core/game/GameView";
|
|
import { TransformHandler } from "../TransformHandler";
|
|
import { Layer } from "./Layer";
|
|
|
|
export class TerrainLayer implements Layer {
|
|
private canvas: HTMLCanvasElement;
|
|
private context: CanvasRenderingContext2D;
|
|
private imageData: ImageData;
|
|
private theme: Theme;
|
|
|
|
constructor(
|
|
private game: GameView,
|
|
private transformHandler: TransformHandler,
|
|
) {}
|
|
shouldTransform(): boolean {
|
|
return true;
|
|
}
|
|
tick() {
|
|
if (this.game.config().theme() !== this.theme) {
|
|
this.redraw();
|
|
}
|
|
}
|
|
|
|
init() {
|
|
console.log("redrew terrain layer");
|
|
this.redraw();
|
|
}
|
|
|
|
redraw(): void {
|
|
this.canvas = document.createElement("canvas");
|
|
this.canvas.width = this.game.width();
|
|
this.canvas.height = this.game.height();
|
|
|
|
const context = this.canvas.getContext("2d", { alpha: false });
|
|
if (context === null) throw new Error("2d context not supported");
|
|
this.context = context;
|
|
|
|
this.imageData = this.context.createImageData(
|
|
this.canvas.width,
|
|
this.canvas.height,
|
|
);
|
|
|
|
this.initImageData();
|
|
this.context.putImageData(this.imageData, 0, 0);
|
|
}
|
|
|
|
initImageData() {
|
|
this.theme = this.game.config().theme();
|
|
this.game.forEachTile((tile) => {
|
|
const terrainColor = this.theme.terrainColor(this.game, tile);
|
|
// TODO: isn't tileref and index the same?
|
|
const index = this.game.y(tile) * this.game.width() + this.game.x(tile);
|
|
const offset = index * 4;
|
|
this.imageData.data[offset] = terrainColor.rgba.r;
|
|
this.imageData.data[offset + 1] = terrainColor.rgba.g;
|
|
this.imageData.data[offset + 2] = terrainColor.rgba.b;
|
|
this.imageData.data[offset + 3] = 255;
|
|
});
|
|
}
|
|
|
|
renderLayer(context: CanvasRenderingContext2D) {
|
|
if (this.transformHandler.scale < 1) {
|
|
context.imageSmoothingEnabled = true;
|
|
context.imageSmoothingQuality = "low";
|
|
} else {
|
|
context.imageSmoothingEnabled = false;
|
|
}
|
|
context.drawImage(
|
|
this.canvas,
|
|
-this.game.width() / 2,
|
|
-this.game.height() / 2,
|
|
this.game.width(),
|
|
this.game.height(),
|
|
);
|
|
}
|
|
}
|