Skip to content

Commit

Permalink
Renderer2d: keep own copy of viewport dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffomatic committed Dec 21, 2020
1 parent 866894f commit 1233304
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Immutable } from '~/types/immutable'

export class Camera {
private position: vec2
viewportDimensions: vec2
private viewportDimensions: vec2
minWorldPos: vec2
worldDimensions: vec2
private zoom: number // ratio of view distance to world distance
Expand Down
3 changes: 2 additions & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export class Client {
setViewportDimensions(d: vec2): void {
this.camera.setViewportDimensions(d)
this.renderer3d.setViewportDimensions(d)
this.renderer2d.setViewportDimensions(d)
}

startPlay(): void {
Expand Down Expand Up @@ -435,7 +436,7 @@ export class Client {
this.waitingForServer ? 'WAITING FOR SERVER' : '',
]

const x = this.camera.viewportDimensions[0] - 10
const x = this.renderer2d.getViewportDimensions()[0] - 10
let y = 10
const res: Renderable2d[] = []
for (const t of text) {
Expand Down
11 changes: 11 additions & 0 deletions src/renderer/Renderer2d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { mat2d, vec2 } from 'gl-matrix'

import { Immutable } from '~/types/immutable'
import { transformCircle } from '~/util/math'

export enum Primitive2d {
Expand Down Expand Up @@ -61,10 +62,12 @@ export type Renderable2d = Path | Rect | Circle | Line | Text
export class Renderer2d {
private ctx: CanvasRenderingContext2D
private transform: mat2d
private viewportDimensions: vec2

constructor(canvas: HTMLCanvasElement) {
this.ctx = canvas.getContext('2d')!
this.transform = mat2d.identity(mat2d.create())
this.viewportDimensions = vec2.fromValues(canvas.width, canvas.height)
}

clear(): void {
Expand All @@ -75,6 +78,14 @@ export class Renderer2d {
this.transform = mat2d.clone(t)
}

setViewportDimensions(dimensions: Immutable<vec2>): void {
this.viewportDimensions = vec2.clone(dimensions)
}

getViewportDimensions(): Immutable<vec2> {
return this.viewportDimensions
}

setGlobalOpacity(alpha: number): void {
this.ctx.globalAlpha = alpha
}
Expand Down

0 comments on commit 1233304

Please sign in to comment.