All files / src/drawing webgl.js

0% Statements 0/59
0% Branches 0/12
0% Functions 0/15
0% Lines 0/59
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                                                                                                                                                                                                                                                                                                                                                                                 
/**
* Copyright 2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
 
import DrawingBase from './base';
 
import {hex, keyword} from 'color-convert';
 
class WebGL extends DrawingBase {
 
  constructor(el) {
    super(el);
    this.gl = el.getContext('webgl') || el.getContext('experimental-webgl');
    this.init();
    this.initEl();
  }
 
  initEl() {
    this.gl.viewportWidth = this.el.width;
    this.gl.viewportHeight = this.el.height;
    //this.gl.viewport(0, 0, this.gl.drawingBufferWidth, this.gl.drawingBufferHeight);
    this.gl.viewport(0, 0, this.el.width, this.el.height);
  }
 
  init() {
    this.initBuffers();
    this.initProgram();
    this.bindVariables();
  }
 
  initShaders() {
    const vertexShaderSource = `
      attribute vec2 a_position;
 
      uniform vec2 u_resolution;
 
      void main() {
        // convert the rectangle from pixels to 0.0 to 1.0
        vec2 zeroToOne = a_position / u_resolution;
 
        // convert from 0->1 to 0->2
        vec2 zeroToTwo = zeroToOne * 2.0;
 
        // convert from 0->2 to -1->+1 (clipspace)
        vec2 clipSpace = zeroToTwo - 1.0;
 
        gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);
      }
    `;
    this.vertexShader = this.createShader(this.gl.VERTEX_SHADER, vertexShaderSource);
 
    const fragmentShaderSource = `
      precision mediump float;
      uniform vec4 u_color;
      void main() {
        gl_FragColor = vec4(
          u_color[0] / 255.,
          u_color[1] / 255.,
          u_color[2] / 255.,
          u_color[3]
        );
      }
    `;
    this.fragmentShader = this.createShader(this.gl.FRAGMENT_SHADER, fragmentShaderSource);
  }
 
  createShader(shaderType, shaderSource) {
    const shader = this.gl.createShader(shaderType);
    this.gl.shaderSource(shader, shaderSource);
    this.gl.compileShader(shader);
    if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {
      console.log(this.gl.getShaderInfoLog(shader));
    }
    return shader;
  }
 
  initBuffers() {
    this.squareBuffer = this.gl.createBuffer();
    this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.squareBuffer);
  }
 
  initProgram() {
    this.initShaders();
    // setup a GLSL program
    this.program = this.gl.createProgram(this.gl);
    this.gl.attachShader(this.program, this.vertexShader);
    this.gl.attachShader(this.program, this.fragmentShader);
    this.gl.linkProgram(this.program);
    if (!this.gl.getProgramParameter(this.program, this.gl.LINK_STATUS)) {
      console.log("Could not initialize shaders");
    }
    this.gl.useProgram(this.program);
  }
 
  bindVariables() {
    this.resolutionUniformLocation = this.gl.getUniformLocation(this.program, "u_resolution");
    this.gl.uniform2f(this.resolutionUniformLocation, this.gl.canvas.width, this.gl.canvas.height);
 
    // TODO: difference between uniform and attrib location
    this.colorUniformLocation = this.gl.getUniformLocation(this.program, "u_color");
    this.positionLocation = this.gl.getAttribLocation(this.program, "a_position");
  }
 
  static isSupported(el) {
    try {
      return el.getContext("webgl") || el.getContext("experimental-webgl");
    } catch (e) {
      return false;
    }
  }
 
  clear() {
    this.gl.clearColor(1, 1, 1, 1);
    this.gl.clear(this.gl.COLOR_BUFFER_BIT);
  }
 
  startDrawingFrame() {
    super.startDrawingFrame();
    this.bufferTile();
  }
 
  fillStyle(fillStyle) {
    if (!fillStyle)
      return;
    if (fillStyle[0] === "#") {
      this.state.fillStyle = hex.rgb(fillStyle.slice(1));
    } else {
      this.state.fillStyle = keyword.rgb(fillStyle);
    }
  }
 
  bufferTile(x, y, width, height) {
    //const x = 0, y = 0;
    //const width = 20, height = 20;
    const x1 = x;
    const x2 = x + width;
    const y1 = y;
    const y2 = y + height;
 
    this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.squareBuffer);
    this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array([
       x1, y1,
       x2, y1,
       x1, y2,
       x1, y2,
       x2, y1,
       x2, y2,
    ]), this.gl.STATIC_DRAW);
  }
 
  fillRect(x, y, width, height) {
 
    // set a color
    this.gl.uniform4f(this.colorUniformLocation,
      this.state.fillStyle[0],
      this.state.fillStyle[1],
      this.state.fillStyle[2],
      1);
      //this.state.globalAlpha);
 
    this.gl.enableVertexAttribArray(this.positionLocation);
    this.gl.vertexAttribPointer(this.positionLocation, 2, this.gl.FLOAT, false, 0, 0);
 
    this.bufferTile(x, y, width, height);
    const offset = 0;
    const count = 6; // number of vertices
    this.gl.drawArrays(this.gl.TRIANGLES, offset, count);
  }
 
  fillText(text, x, y) {
    // TODO
    //this.ctx.fillText(text, x, y);
  }
 
  // TODO: destroy canvas
  // TODO: resize events
}
 
export {WebGL};
export default WebGL;