Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

canvas resize canvas

(window.onresize = () => {
	canvas.width = innerWidth, canvas.height = innerHeight;
})();

// Condensed
(window.onresize=e=>{canvas.width=innerWidth,canvas.height=innerHeight})()
Comment

resize canvas javascript

const canvas = document.getElementById('yourCanvas');
canvas.width = 200; // Use document.documentElement.clientWidth for full page width!
canvas.height = 200; // Use document.documentElement.clientHeight for full page height!
Comment

resize canvas

"use strict";

function main() {
  // Get A WebGL context
  /** @type {HTMLCanvasElement} */
  var canvas = document.querySelector("#canvas");
  var gl = canvas.getContext("webgl");
  if (!gl) {
    return;
  }

  // setup GLSL program
  var program = webglUtils.createProgramFromScripts(gl, ["vertex-shader-2d", "fragment-shader-2d"]);
  gl.useProgram(program);

  // look up where the vertex data needs to go.
  var positionAttributeLocation = gl.getAttribLocation(program, "a_position");

  // lookup uniforms
  var colorLocation = gl.getUniformLocation(program, "u_color");
  var matrixLocation = gl.getUniformLocation(program, "u_matrix");

  // Create a buffer to put three 2d clip space points in
  var positionBuffer = gl.createBuffer();

  // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
  gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

  requestAnimationFrame(drawScene);

  // Draw the scene.
  function drawScene(now) {
    now *= 0.001;  // convert to seconds

    // Tell WebGL how to convert from clip space to pixels
    gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

    // Clear the canvas.
    gl.clear(gl.COLOR_BUFFER_BIT);

    // Tell it to use our program (pair of shaders)
    gl.useProgram(program);

    // Turn on the attribute
    gl.enableVertexAttribArray(positionAttributeLocation);

    // Bind the position buffer.
    gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

    // Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
    var size = 2;          // 2 components per iteration
    var type = gl.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
    gl.vertexAttribPointer(
        positionAttributeLocation, size, type, normalize, stride, offset);

    // Set Geometry.
    var radius = Math.sqrt(gl.canvas.width * gl.canvas.width + gl.canvas.height * gl.canvas.height) * 0.5;
    var angle = now;
    var x = Math.cos(angle) * radius;
    var y = Math.sin(angle) * radius;
    var centerX = gl.canvas.width  / 2;
    var centerY = gl.canvas.height / 2;
    setGeometry(gl, centerX + x, centerY + y, centerX - x, centerY - y);

    // Compute the matrices
    var projectionMatrix = m3.projection(gl.canvas.width, gl.canvas.height);

    // Set the matrix.
    gl.uniformMatrix3fv(matrixLocation, false, projectionMatrix);

    // Draw in red
    gl.uniform4fv(colorLocation, [1, 0, 0, 1]);

    // Draw the geometry.
    var primitiveType = gl.LINES;
    var offset = 0;
    var count = 2;
    gl.drawArrays(primitiveType, offset, count);

    requestAnimationFrame(drawScene);
  }
}

// Fill the buffer with a line
function setGeometry(gl, x1, y1, x2, y2) {
  gl.bufferData(
      gl.ARRAY_BUFFER,
      new Float32Array([
          x1, y1,
          x2, y2]),
      gl.STATIC_DRAW);
}

main();
Comment

PREVIOUS NEXT
Code Example
Javascript :: passport js local strategy response handling 
Javascript :: sanitize data within an Express application 
Javascript :: javascript closure function example 
Javascript :: how to build jquery post data 
Javascript :: null value check in react js 
Javascript :: regex not js 
Javascript :: js get the filename you uploaded 
Javascript :: parse Color to json flutter 
Javascript :: jwt strategy 
Javascript :: sort by attribute in reactjs 
Javascript :: this.setstate prevstate 
Javascript :: how to make a alert popup message in javascript 
Javascript :: signed and unsigned integers in JavaScript 
Javascript :: sort by ascending javascript 
Javascript :: json to string 
Javascript :: Async return values 
Javascript :: variable javascript 
Javascript :: mail 
Javascript :: function declaration and function definition in javascript 
Javascript :: browseranimationsmodule browsermodule has already been loaded 
Javascript :: tinymce return text and html 
Javascript :: javascript check if string ends with space 
Javascript :: how to get the value of textarea in react 
Javascript :: getmonth js 
Javascript :: how to convert decimal to roman in javascript 
Javascript :: react-moralis 
Javascript :: replacing a value in string using aregular expression pyhton 
Javascript :: how to get the uppert triangular matrix out of a matrix matlab 
Javascript :: toast info 
Javascript :: change module name react native android studio 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =