1010var horizon = document . getElementById ( "horizon" ) ;
1111var ctx = horizon . getContext ( '2d' ) ;
1212
13- function Graphics ( element ) {
13+ /**
14+ * @constructor
15+ * @param {HTMLCanvasElement | string | null } element
16+ */
17+ function GraphicsBase ( element ) {
1418 /** @type {HTMLCanvasElement? } */
1519 this . canvas = (
1620 element instanceof HTMLCanvasElement
@@ -20,25 +24,42 @@ function Graphics(element) {
2024 ? document . getElementById ( element )
2125 : document . createElement ( "canvas" )
2226 ) ) ;
27+
28+ this . canvas . onresize = function ( ) {
29+ var bounds = canvas . getBoundingClientRect ( ) ;
30+ graphics . resize ( bounds . width , bounds . height ) ;
31+ }
32+ }
33+
34+ GraphicsBase . prototype . resize = function ( width , height ) {
35+ this . canvas . width = width ;
36+ this . canvas . height = height ;
37+ }
38+
39+ GraphicsBase . prototype . getContext = function ( type ) {
40+ return this . canvas . getContext ( ) ;
2341}
2442
2543/**
26- * @param {string | HTMLCanvasElement } elementId
44+ * @constructor
45+ * @extends {GraphicsBase }
46+ * @param {string | HTMLCanvasElement | null } element
2747 */
2848function Graphics2D ( element ) {
29- Graphics . call ( this , element ) ;
30- this . ctx = this . canvas . getContext ( "2d" ) ;
49+ GraphicsBase . call ( this , element ) ;
50+ this . ctx = this . getContext ( "2d" ) ;
3151}
3252
3353// Graphics2D.prototype.__proto__ = Graphics.prototype;
3454// Graphics2D.constru
35- Graphics2D . prototype = Object . create ( Graphics . prototype ) ;
36- Graphics2D . prototype . constructor = Graphics ;
55+ Graphics2D . prototype = Object . create ( GraphicsBase . prototype ) ;
56+ Graphics2D . prototype . constructor = GraphicsBase ;
3757
3858var graphics = new Graphics2D ( "horizon" ) ;
3959
4060function drawHorizon ( roll , pitch ) {
4161 var w = horizon . width , h = horizon . height ;
62+ var ctx = graphics . ctx ;
4263
4364 const pixelOffset = pitch * 5 ;
4465
0 commit comments