Fix lint, reduce duplication, don't scale ctx

Scaling ctx was breaking the scale when setOption was called, not sure
it was doing anything.
This commit is contained in:
Daniel Imms
2018-01-28 14:13:25 -08:00
parent 106704b5d8
commit fc2af1d806
+19 -17
View File
@@ -24,7 +24,7 @@ export abstract class BaseRenderLayer implements IRenderLayer {
private _charAtlas: HTMLCanvasElement | ImageBitmap; private _charAtlas: HTMLCanvasElement | ImageBitmap;
constructor( constructor(
container: HTMLElement, private _container: HTMLElement,
id: string, id: string,
zIndex: number, zIndex: number,
private _alpha: boolean, private _alpha: boolean,
@@ -33,13 +33,16 @@ export abstract class BaseRenderLayer implements IRenderLayer {
this._canvas = document.createElement('canvas'); this._canvas = document.createElement('canvas');
this._canvas.classList.add(`xterm-${id}-layer`); this._canvas.classList.add(`xterm-${id}-layer`);
this._canvas.style.zIndex = zIndex.toString(); this._canvas.style.zIndex = zIndex.toString();
this._ctx = this._canvas.getContext('2d', {alpha: _alpha}); this._initCanvas();
this._ctx.scale(window.devicePixelRatio, window.devicePixelRatio); this._container.appendChild(this._canvas);
}
private _initCanvas(): void {
this._ctx = this._canvas.getContext('2d', {alpha: this._alpha});
// Draw the background if this is an opaque layer // Draw the background if this is an opaque layer
if (!_alpha) { if (!this._alpha) {
this.clearAll(); this.clearAll();
} }
container.appendChild(this._canvas);
} }
public onOptionsChanged(terminal: ITerminal): void {} public onOptionsChanged(terminal: ITerminal): void {}
@@ -54,23 +57,22 @@ export abstract class BaseRenderLayer implements IRenderLayer {
} }
protected setTransparency(terminal: ITerminal, alpha: boolean): void { protected setTransparency(terminal: ITerminal, alpha: boolean): void {
// Do nothing when alpha doesn't change
if (alpha === this._alpha) { if (alpha === this._alpha) {
return; return;
} }
this._alpha = alpha
const oldCanvas = this._canvas // Create new canvas and replace old one
// Cloning preserves canvas properties and permits to acquire a new context const oldCanvas = this._canvas;
this._alpha = alpha;
// Closing preserves properties
this._canvas = <HTMLCanvasElement>this._canvas.cloneNode(); this._canvas = <HTMLCanvasElement>this._canvas.cloneNode();
this._ctx = this._canvas.getContext('2d', {alpha: this._alpha}); this._initCanvas();
this._ctx.scale(window.devicePixelRatio, window.devicePixelRatio); this._container.replaceChild(this._canvas, oldCanvas);
// Draw the background if this is an opaque layer
if (!this._alpha) { // Regenerate char atlas and force a full redraw
this.clearAll();
}
oldCanvas.parentNode.replaceChild(this._canvas, oldCanvas);
this._refreshCharAtlas(terminal, this._colors); this._refreshCharAtlas(terminal, this._colors);
// Force a full redraw this.onGridChanged(terminal, 0, terminal.rows - 1);
this.onGridChanged(terminal, 0, terminal.rows - 1)
} }
/** /**