1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 20:46:39 +02:00

Big linting

This commit is contained in:
kkapsner 2019-11-28 01:26:35 +01:00
parent b5e6d049ce
commit aef6bd3d59
58 changed files with 2074 additions and 1856 deletions

View file

@ -4,9 +4,9 @@
(function(){
"use strict";
var logging = require("./logging");
const logging = require("./logging");
var scope;
let scope;
if ((typeof exports) !== "undefined"){
scope = exports;
}
@ -15,9 +15,8 @@
}
scope.copyCanvasToWebgl = function copyCanvasToWebgl(window, canvas, webGLVersion = "webgl"){
var webGlCanvas = canvas.cloneNode(true);
var success;
var context =
const webGlCanvas = canvas.cloneNode(true);
const context =
window.HTMLCanvasElement.prototype.getContext.call(webGlCanvas, webGLVersion) ||
window.HTMLCanvasElement.prototype.getContext.call(webGlCanvas, "experimental-" + webGLVersion);
if (!context){
@ -27,38 +26,35 @@
}
context.viewport(0, 0, webGlCanvas.width, webGlCanvas.height);
var program = context.createProgram();
var shader = context.createShader(context.VERTEX_SHADER);
var vertex = "attribute vec4 a_position;\nattribute vec2 a_texCoord;\nvarying vec2 v_texCoord;\n" +
const program = context.createProgram();
const vertexShader = context.createShader(context.VERTEX_SHADER);
const vertex = "attribute vec4 a_position;\nattribute vec2 a_texCoord;\nvarying vec2 v_texCoord;\n" +
"void main(){\n\tgl_Position = a_position;\n\tv_texCoord = a_texCoord;\n}";
context.shaderSource(shader, vertex);
context.compileShader(shader);
success = context.getShaderParameter(shader, context.COMPILE_STATUS);
if (!success){
context.deleteShader(shader);
context.shaderSource(vertexShader, vertex);
context.compileShader(vertexShader);
if (!context.getShaderParameter(vertexShader, context.COMPILE_STATUS)){
context.deleteShader(vertexShader);
logging.warning("webgl: failed to compile vertex shader.");
return {canvas: false, context: false};
}
context.attachShader(program, shader);
context.attachShader(program, vertexShader);
shader = context.createShader(context.FRAGMENT_SHADER);
var fragmenter = "precision mediump float;\nuniform sampler2D u_image;\nvarying vec2 v_texCoord;\n" +
const fragmentShader = context.createShader(context.FRAGMENT_SHADER);
const fragmenter = "precision mediump float;\nuniform sampler2D u_image;\nvarying vec2 v_texCoord;\n" +
"void main(){\n\tgl_FragColor = texture2D(u_image, v_texCoord);\n}";
context.shaderSource(shader, fragmenter);
context.compileShader(shader);
success = context.getShaderParameter(shader, context.COMPILE_STATUS);
if (!success){
context.deleteShader(shader);
context.shaderSource(fragmentShader, fragmenter);
context.compileShader(fragmentShader);
if (!context.getShaderParameter(fragmentShader, context.COMPILE_STATUS)){
context.deleteShader(fragmentShader);
logging.warning("webgl: failed to compile fragmenter shader.");
return {canvas: false, context: false};
}
context.attachShader(program, shader);
context.attachShader(program, fragmentShader);
context.linkProgram(program);
success = context.getProgramParameter(program, context.LINK_STATUS);
if (!success){
if (!context.getProgramParameter(program, context.LINK_STATUS)){
context.deleteProgram(program);
logging.warning("webgl: failed to link program.");
return {canvas: false, context: false};
@ -66,8 +62,7 @@
context.useProgram(program);
var positionAttributeLocation = context.getAttribLocation(program, "a_position");
var positionBuffer = context.createBuffer();
const positionBuffer = context.createBuffer();
context.bindBuffer(context.ARRAY_BUFFER, positionBuffer);
context.bufferData(context.ARRAY_BUFFER, new Float32Array([
-1, -1,
@ -76,21 +71,21 @@
1, 1,
-1, 1,
1, -1
]), context.STATIC_DRAW);
const positionAttributeLocation = context.getAttribLocation(program, "a_position");
context.enableVertexAttribArray(positionAttributeLocation);
var size = 2; // 2 components per iteration
var type = context.FLOAT; // the data is 32bit floats
var normalize = false; // don't normalize the data
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
var offset = 0; // start at the beginning of the buffer
const size = 2; // 2 components per iteration
const type = context.FLOAT; // the data is 32bit floats
const normalize = false; // don't normalize the data
const stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
const offset = 0; // start at the beginning of the buffer
context.vertexAttribPointer(positionAttributeLocation, size, type, normalize, stride, offset);
var texCoordLocation = context.getAttribLocation(program, "a_texCoord");
const texCoordLocation = context.getAttribLocation(program, "a_texCoord");
// provide texture coordinates for the rectangle.
var texCoordBuffer = context.createBuffer();
const texCoordBuffer = context.createBuffer();
context.bindBuffer(context.ARRAY_BUFFER, texCoordBuffer);
context.bufferData(context.ARRAY_BUFFER, new Float32Array([
0, 1,
@ -102,19 +97,15 @@
]), context.STATIC_DRAW);
context.enableVertexAttribArray(texCoordLocation);
context.vertexAttribPointer(texCoordLocation, 2, context.FLOAT, false, 0, 0);
var texture = context.createTexture();
context.bindTexture(context.TEXTURE_2D, texture);
context.bindTexture(context.TEXTURE_2D, context.createTexture());
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.NEAREST);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, context.NEAREST);
context.texImage2D(context.TEXTURE_2D, 0, context.RGBA, context.RGBA, context.UNSIGNED_BYTE, canvas);
var primitiveType = context.TRIANGLES;
var triangleOffset = 0;
var count = 6;
context.drawArrays(primitiveType, triangleOffset, count);
context.drawArrays(context.TRIANGLES /*primitiveType*/, 0 /*triangleOffset*/, 6 /*count*/);
return {webGlCanvas, context};
};