aboutsummaryrefslogtreecommitdiff
path: root/pointless/viewer/screen.js
diff options
context:
space:
mode:
Diffstat (limited to 'pointless/viewer/screen.js')
-rw-r--r--pointless/viewer/screen.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/pointless/viewer/screen.js b/pointless/viewer/screen.js
new file mode 100644
index 0000000..8a6891c
--- /dev/null
+++ b/pointless/viewer/screen.js
@@ -0,0 +1,56 @@
1/*
2 * screen.js
3 * Part of Pointless Viewer, a Beamer presentation viewer
4 * Copyright 2018 Pacien TRAN-GIRARD
5 * License: GNU GPL v3
6 */
7
8"use strict";
9
10class Screen {
11 constructor(window, secondary=false) {
12 this.window = window;
13 this.secondary = secondary;
14 this.canvas = window.document.getElementById("screen");
15 this.context = this.canvas.getContext("2d");
16 this.page = null;
17
18 var self = this;
19 this.window.addEventListener("resize", function() {
20 self._refreshPage();
21 });
22 }
23
24 setPage(page) {
25 this.page = page;
26 this._refreshPage();
27 }
28
29 _resizeScreen(ratio) {
30 var windowRatio = this.window.innerWidth / this.window.innerHeight;
31 var scaleFactor = ratio / windowRatio;
32 this.canvas.width = this.window.innerWidth * Math.min(scaleFactor, 1);
33 this.canvas.height = this.window.innerHeight / Math.max(scaleFactor, 1);
34 }
35
36 _setOffset() {
37 var xOffset = this.secondary ? -this.canvas.width : 0;
38 this.context.transform(1, 0, 0, 1, xOffset, 0);
39 }
40
41 _paintPage() {
42 var renderRatio = this.canvas.height / this.page.getViewport(1).height;
43 var renderViewport = this.page.getViewport(renderRatio);
44 var renderContext = { canvasContext: this.context, viewport: renderViewport };
45 this.page.render(renderContext);
46 }
47
48 _refreshPage() {
49 if (this.page == null) return;
50 var viewport = this.page.getViewport(1);
51 var ratio = (viewport.width / 2) / viewport.height;
52 this._resizeScreen(ratio);
53 this._setOffset();
54 this._paintPage();
55 }
56}