Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8024153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:46:46+00:00 2026-06-04T22:46:46+00:00

To implement mouse gestures in webgl, i would like to allow the users to

  • 0

To implement mouse gestures in webgl, i would like to allow the users to “draw on screen” freehand mode. Doing it in 3D webgl would allow to have nice shader effects on the brush being used such as fire effects, glows or other cool graphical candy.

what is the current recommend way to draw on screen in webgl efficiently?

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:46:48+00:00Added an answer on June 4, 2026 at 10:46 pm

    You have 2 options.

    You could draw to an fbo and then draw the fbo to the canvas

    or

    You could request ‘preserveDrawingBuffer: true’ when creating the webgl context.

    var canvas = document.getElementById("canvas");
    var gl = canvas.getContext("webgl", {preserveDrawingBuffer:true});
    
    program = twgl.createProgramFromScripts(gl, ["2d-vertex-shader", "2d-fragment-shader"]);
    gl.useProgram(program);
    
    var positionLoc = gl.getAttribLocation(program, "v_position");
    var offsetLoc = gl.getUniformLocation(program, "u_offset");
    
    var res = 1;
    setupQuad(gl, res, positionLoc);
    
    canvas.addEventListener('mousemove', draw, false);
    
    function draw(event) {
      var m = getNoPaddingNoBorderCanvasRelativeMousePosition(event);
      // convert mouse coords to clip space since that's what this
      // particular shader is using. See 
      // https://webglfundamentals.org
      // for how to use pixels instead of clip space. 
      var x = m.x / gl.canvas.width * 2 - 1;
      var y = m.y / gl.canvas.height * -2 + 1;  // flip y
    
      drawBrush(x, y);
    }
    
    function drawBrush(x, y) {
      gl.uniform2f(offsetLoc, x, y);
    
      gl.drawElements(gl.TRIANGLES, res * res * 6, gl.UNSIGNED_SHORT, 0);
    }
    
    drawBrush(0, 0);
    
    
    
    function setupQuad(gl, gridRes, positionLoc) {
      var scale = 0.05;
      var objects = [];
    
      var vertsAcross = gridRes + 1;
      var numVerts = vertsAcross * vertsAcross;
      var positions = new Float32Array(numVerts * 2);
      var indices = new Uint16Array(6 * gridRes * gridRes);
      var poffset = 0;
    
      for (var zz = 0; zz <= gridRes; ++zz) {
        for (var xx = 0; xx <= gridRes; ++xx) {
          var u = xx / gridRes;
          var v = zz / gridRes;
          positions[poffset + 0] = (-1 + 2 * u) * scale;
          positions[poffset + 1] = (-1 + 2 * v) * scale;
          poffset += 2;
        }
      }
        
      var tbase = 0;
      for (var zz = 0; zz < gridRes; ++zz) {
        var index = zz * vertsAcross;
        for (var xx = 0; xx < gridRes; ++xx) {
          indices[tbase + 0] = index + 0;
          indices[tbase + 1] = index + 1;
          indices[tbase + 2] = index + vertsAcross;
          indices[tbase + 3] = index + vertsAcross;
          indices[tbase + 4] = index + 1;
          indices[tbase + 5] = index + vertsAcross + 1;
          index += 1;
          tbase += 6;
        }
      }
        
      function makeBuffer(data, type, size, loc) {
        var buf = gl.createBuffer();
        gl.bindBuffer(type, buf);
        gl.bufferData(type, data, gl.STATIC_DRAW);
        if (type == gl.ARRAY_BUFFER) {
          gl.enableVertexAttribArray(loc);
          gl.vertexAttribPointer(loc, size, gl.FLOAT, false, 0, 0);
        }
        return buf;
      }
        
      objects.push(makeBuffer(positions, gl.ARRAY_BUFFER, 2, positionLoc));
      objects.push(makeBuffer(indices, gl.ELEMENT_ARRAY_BUFFER));
        
      return objects;
    };
    
    function getRelativeMousePosition(event, target) {
      target = target || event.target;
      var rect = target.getBoundingClientRect();
    
      return {
        x: event.clientX - rect.left,
        y: event.clientY - rect.top,
      }
    }
    
    // assumes target or event.target is canvas
    function getNoPaddingNoBorderCanvasRelativeMousePosition(event, target) {
      target = target || event.target;
      var pos = getRelativeMousePosition(event, target);
    
      pos.x = pos.x * target.width  / canvas.clientWidth;
      pos.y = pos.y * target.height / canvas.clientHeight;
    
      return pos;  
    }
    canvas { border: 1px solid black; }
    <script src="https://twgljs.org/dist/twgl.min.js"></script>
    <script id="2d-vertex-shader" type="x-shader/x-vertex">
    attribute vec2 v_position;
    uniform vec2 u_offset;
        
    void main() {
        gl_Position = vec4(v_position + u_offset, 0, 1);
    }
    </script>
    <script id="2d-fragment-shader" type="x-shader/x-fragment">
    precision mediump float;
    void main() {
        gl_FragColor = vec4(0, 1, 0, 1);  
    }
    </script>
    <canvas id="canvas" width="400" height="300"></canvas>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a modal dialog that I would like to implement a right mouse
I have some custom, CoreAnimation based UI. I'd like to implement mouse dragging of
I have a need to implement a rating feature and would like to do
I would like to implement session timeout kind of thing for a JFrame .
I need to implement mouse drag events which look something like this: class MouseDragEvent
I would implement the view moving, I just need to know if the mouse
I want to implement slider/progressbar like YouTube style. That means when mouse on the
I was wondering how to implement the debuggger mouse over display functionality, U know
i'm attempting to implement a mouse event where Mouse_Move can only occur if Mouse_Down.
I want to implement a panning pictureBox in C# winforms. I have a panel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.