All files BasicGLProvider.ts

62.96% Statements 51/81
44.74% Branches 17/38
61.9% Functions 13/21
62.96% Lines 51/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229          1x                       1x     1x   1x 1x   1x     1x     1x     1x         1x                 1x               1x             1x   1x 1x 1x                                                       2x 1x         1x         1x 1x 1x 1x           3x                     1x 1x     1x       1x 1x 1x       2x       1x       2x     1x             3x                           1x             1x 1x 1x                         1x   1x 1x       1x                 1x 1x   1x         1x 1x     1x 1x   1x      
import GLProvider from './GLProvider';
import checkGLError from 'parsegraph-checkglerror';
import Color from 'parsegraph-color';
import Rect from 'parsegraph-rect';
 
let providerCount = 0;
export default class BasicGLProvider implements GLProvider {
  _shaders:{ [shaderName:string]: WebGLProgram};
  _id:string;
  _gl:WebGLRenderingContext;
  _container:HTMLDivElement;
  _canvas:HTMLCanvasElement;
  _backgroundColor:Color;
  _explicitWidth:number;
  _explicitHeight:number;
 
  constructor(id:string, background:Color) {
    Iif (id) {
      this._id = id;
    } else {
      this._id = "" + (++providerCount);
    }
    this._shaders = {};
    this._gl = null;
 
    Iif (background) {
      this._backgroundColor = background;
    } else {
      this._backgroundColor = new Color(0, 0, 0, 1);
    }
 
    this._container = document.createElement('div');
 
    // The 3D canvas that will be drawn to.
    this._canvas = document.createElement('canvas');
    /* if(WebGLDebugUtils) {
      this._canvas = WebGLDebugUtils.makeLostContextSimulatingCanvas(
      this._canvas);
    }*/
    this._canvas.addEventListener(
        'webglcontextlost',
        (event)=>{
          // console.log('Context lost');
          event.preventDefault();
          this.onContextChanged(true);
        },
        false,
    );
    this._canvas.addEventListener(
        'webglcontextrestored',
        ()=>{
          console.log('Context restored');
          this.onContextChanged(false);
        },
        false,
    );
    this._canvas.addEventListener(
        'contextmenu',
        (e)=>{
          e.preventDefault();
        },
        false,
    );
    this._canvas.style.display = 'block';
 
    this._container.appendChild(this._canvas);
    this._explicitWidth = null;
    this._explicitHeight = null;
  }
 
  onContextChanged(isLost:boolean):void {
    if (isLost) {
      const keys = [];
      for (const k in this._shaders) {
        if (Object.prototype.hasOwnProperty.call(this._shaders, k)) {
          keys.push(k);
        }
      }
      for (const i in keys) {
        if (Object.prototype.hasOwnProperty.call(keys, i)) {
          delete this._shaders[keys[i]];
        }
      }
    }
  }
 
  shaders():{ [shaderName:string]:WebGLProgram } {
    return this._shaders;
  }
 
  id():string {
    return this._id;
  }
 
  gl():WebGLRenderingContext {
    if (this._gl) {
      return this._gl;
    }
    // this._gl = this._canvas.getContext("webgl2", {
    // antialias:true
    // });
    Iif (this._gl) {
      // this.render = this.renderWebgl2;
      checkGLError(this._gl, 'WebGL2 creation');
      return this._gl;
    }
    this._gl = this._canvas.getContext('webgl');
    Eif (this._gl) {
      checkGLError(this._gl, 'WebGL creation');
      return this._gl;
    }
    throw new Error('GL context is not supported');
  };
 
  canvas():HTMLCanvasElement {
    return this._canvas;
  }
 
  getSize(sizeOut?:Rect) {
    sizeOut.setX(0);
    sizeOut.setY(0);
    sizeOut.setWidth(this.width());
    sizeOut.setHeight(this.height());
  };
 
  resize(w:number, h:number):void {
    this.container().style.width = typeof w === 'number' ? w + 'px' : w;
    Iif (arguments.length === 1) {
      h = w;
    }
    this.container().style.height = typeof h === 'number' ? h + 'px' : h;
  };
 
  setExplicitSize(w:number, h:number):void {
    this._explicitWidth = w;
    this._explicitHeight = h;
    this.resize(w, h);
  };
 
  getWidth():number  {
    return this._explicitWidth || this.container().clientWidth;
  };
 
  width():number {
    return this.getWidth();
  }
 
  getHeight():number {
    return this._explicitHeight || this.container().clientHeight;
  };
  height() {
    return this.getHeight();
  }
 
  /**
   * @return {HTMLElement} Returns the container that holds the canvas for this graph.
   */
  container():HTMLElement {
    return this._container;
  };
 
  setBackground(...args:Array<number>):void {
    if (args.length > 1) {
      return this.setBackground(new Color(...args));
    }
    this._backgroundColor = args[0];
  };
 
  /**
   * @return {Color} Retrieves the current background color.
   */
  backgroundColor():Color {
    return this._backgroundColor;
  };
 
  /**
   * @return {boolean} Returns whether the window has a nonzero client width and height.
   */
  canProject():boolean {
    const displayWidth = this.getWidth();
    const displayHeight = this.getHeight();
    return displayWidth != 0 && displayHeight != 0;
  };
 
  /**
   * Renders this provider's content, resizing the canvas to its container.
   *
   * The default implementation only sets the WebGL's clear color to the
   * background but does not clear the canvas.
   *
   * @return {boolean} true if this provider needs an additional call to complete rendering its content.
   * @throws if the scene cannot be projected to, as determined by canProject
   */
  render():boolean {
    this._container.style.backgroundColor = this._backgroundColor.asRGB();
 
    const gl = this.gl();
    Iif (this.gl().isContextLost()) {
      return false;
    }
    // console.log("Rendering window");
    Iif (!this.canProject()) {
      throw new Error(
          'Refusing to render to an unprojectable window.' +
          ' Use canProject() to handle, and parent this' +
          ' window\'s container to fix.',
      );
    }
 
    // Lookup the size the browser is displaying the canvas.
    const displayWidth = this.width();
    const displayHeight = this.height();
    // Check if the canvas is not the same size.
    Eif (
      this.canvas().width != displayWidth ||
      this.canvas().height != displayHeight
    ) {
      // Make the canvas the same size
      this.canvas().width = displayWidth;
      this.canvas().height = displayHeight;
    }
 
    const bg = this.backgroundColor();
    gl.clearColor(bg.r(), bg.g(), bg.b(), bg.a());
 
    return false;
  }
}