aboutsummaryrefslogtreecommitdiff
path: root/pointless/viewer/screen.js
blob: 8a6891cdc273694557935c91c807b255c95b6635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * screen.js
 * Part of Pointless Viewer, a Beamer presentation viewer
 * Copyright 2018 Pacien TRAN-GIRARD
 * License: GNU GPL v3
 */

"use strict";

class Screen {
  constructor(window, secondary=false) {
    this.window = window;
    this.secondary = secondary;
    this.canvas = window.document.getElementById("screen");
    this.context = this.canvas.getContext("2d");
    this.page = null;

    var self = this;
    this.window.addEventListener("resize", function() {
      self._refreshPage();
    });
  }

  setPage(page) {
    this.page = page;
    this._refreshPage();
  }

  _resizeScreen(ratio) {
    var windowRatio = this.window.innerWidth / this.window.innerHeight;
    var scaleFactor = ratio / windowRatio;
    this.canvas.width = this.window.innerWidth * Math.min(scaleFactor, 1);
    this.canvas.height = this.window.innerHeight / Math.max(scaleFactor, 1);
  }

  _setOffset() {
    var xOffset = this.secondary ? -this.canvas.width : 0;
    this.context.transform(1, 0, 0, 1, xOffset, 0);
  }

  _paintPage() {
    var renderRatio = this.canvas.height / this.page.getViewport(1).height;
    var renderViewport = this.page.getViewport(renderRatio);
    var renderContext = { canvasContext: this.context, viewport: renderViewport };
    this.page.render(renderContext);
  }

  _refreshPage() {
    if (this.page == null) return;
    var viewport = this.page.getViewport(1);
    var ratio = (viewport.width / 2) / viewport.height;
    this._resizeScreen(ratio);
    this._setOffset();
    this._paintPage();
  }
}