sigma
Version:
A JavaScript library dedicated to graph drawing.
113 lines (112 loc) • 5.72 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../../../utils");
var edge_quadraticBezier_vert_glsl_1 = __importDefault(require("../shaders/edge.quadraticBezier.vert.glsl.js"));
var edge_quadraticBezier_frag_glsl_1 = __importDefault(require("../shaders/edge.quadraticBezier.frag.glsl.js"));
var edge_1 = require("./common/edge");
var POINTS = 3, ATTRIBUTES = 5;
var EdgeQuadraticBezierProgram = /** @class */ (function (_super) {
__extends(EdgeQuadraticBezierProgram, _super);
function EdgeQuadraticBezierProgram(gl) {
var _this = _super.call(this, gl, edge_quadraticBezier_vert_glsl_1.default, edge_quadraticBezier_frag_glsl_1.default, POINTS, ATTRIBUTES) || this;
// Locations
_this.positionLocation = gl.getAttribLocation(_this.program, "a_position");
_this.colorLocation = gl.getAttribLocation(_this.program, "a_color");
_this.coordLocation = gl.getAttribLocation(_this.program, "a_coord");
// Uniform locations:
var matrixLocation = gl.getUniformLocation(_this.program, "u_matrix");
if (matrixLocation === null)
throw new Error("sigma/renderers/webgl/program/edge.EdgeQuadraticBezierProgram: error while getting matrixLocation");
_this.matrixLocation = matrixLocation;
var resolutionLocation = gl.getUniformLocation(_this.program, "u_resolution");
if (resolutionLocation === null)
throw new Error("sigma/renderers/webgl/program/edge.EdgeQuadraticBezierProgram: error while getting resolutionLocation");
_this.resolutionLocation = resolutionLocation;
var ratioLocation = gl.getUniformLocation(_this.program, "u_ratio");
if (ratioLocation === null)
throw new Error("sigma/renderers/webgl/program/edge.EdgeQuadraticBezierProgram: error while getting ratioLocation");
_this.ratioLocation = ratioLocation;
var scaleLocation = gl.getUniformLocation(_this.program, "u_scale");
if (scaleLocation === null)
throw new Error("sigma/renderers/webgl/program/edge.EdgeQuadraticBezierProgram: error while getting scaleLocation");
_this.scaleLocation = scaleLocation;
_this.bind();
return _this;
}
EdgeQuadraticBezierProgram.prototype.bind = function () {
var gl = this.gl;
// Bindings
gl.enableVertexAttribArray(this.positionLocation);
gl.enableVertexAttribArray(this.colorLocation);
gl.enableVertexAttribArray(this.coordLocation);
gl.vertexAttribPointer(this.positionLocation, 2, gl.FLOAT, false, this.attributes * Float32Array.BYTES_PER_ELEMENT, 0);
gl.vertexAttribPointer(this.colorLocation, 1, gl.FLOAT, false, this.attributes * Float32Array.BYTES_PER_ELEMENT, 8);
gl.vertexAttribPointer(this.coordLocation, 2, gl.FLOAT, false, ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT, 12);
};
EdgeQuadraticBezierProgram.prototype.computeIndices = function () {
// nothing to do
};
EdgeQuadraticBezierProgram.prototype.process = function (sourceData, targetData, data, hidden, offset) {
var i = 0;
if (hidden) {
for (var l = i + POINTS * ATTRIBUTES; i < l; i++)
this.array[i] = 0;
}
var x1 = sourceData.x, y1 = sourceData.y, x2 = targetData.x, y2 = targetData.y, color = utils_1.floatColor(data.color);
i = POINTS * ATTRIBUTES * offset;
var array = this.array;
// Control point
array[i++] = (x1 + x2) / 2 + (y2 - y1) / 4;
array[i++] = (y1 + y2) / 2 + (x1 - x2) / 4;
// array[i++] = thickness;
array[i++] = color;
array[i++] = 0.5;
array[i++] = 0;
// First point
array[i++] = x1;
array[i++] = y1;
// array[i++] = thickness;
array[i++] = color;
array[i++] = 0;
array[i++] = 0;
// Second point
array[i++] = x2;
array[i++] = y2;
// array[i++] = thickness;
array[i++] = color;
array[i++] = 1;
array[i] = 1;
};
EdgeQuadraticBezierProgram.prototype.render = function (params) {
var gl = this.gl;
var program = this.program;
gl.useProgram(program);
// Binding uniforms
gl.uniform2f(this.resolutionLocation, params.width, params.height);
gl.uniform1f(this.ratioLocation, params.ratio / Math.pow(params.ratio, params.edgesPowRatio));
gl.uniformMatrix3fv(this.matrixLocation, false, params.matrix);
gl.uniform1f(this.scaleLocation, params.ratio);
// Drawing:
gl.drawArrays(gl.TRIANGLES, 0, this.array.length / ATTRIBUTES);
};
return EdgeQuadraticBezierProgram;
}(edge_1.AbstractEdgeProgram));
exports.default = EdgeQuadraticBezierProgram;