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

  • SEARCH
  • Home
  • 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 8239741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:14:55+00:00 2026-06-07T20:14:55+00:00

I am very new to WebGL, but not Javascript or Canvas. Essentially, I need

  • 0

I am very new to WebGL, but not Javascript or Canvas. Essentially, I need a very fast way to be able to change the color of a pixel on a canvas at any coordinate. I feel like this could be done using WebGL fragment shaders. Is this possible, and is there a better way? How would I go about doing this?

  • 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-07T20:14:57+00:00Added an answer on June 7, 2026 at 8:14 pm

    This isn’t an exhaustive guide to rendering because it doesn’t include the bits about shader management; instead it focuses on how drawing of WebGL primitives is actually performed, and how to use orthographic projection to precisely position vertices on the screen.

    For more information, I highly recommend these sources:

    • Learning WebGL – tutorials
    • The WebGL specification
    • The Typed Arrays specification

    WebGL does all of its drawing with Vertex Buffer Objects (VBOs), so you need to compile all your data into 3D vertices and send them down to the video card in as few VBOs as possible (to maximize performance).

    A VBO can be created in WebGL like so:

    var vbo = gl.createBuffer();
    

    Then you need to send the data down to the buffer, usually in the form of a Float32Array. If you already have your data in the form of a regular JavaScript array, then you can use new Float32Array(jsArray) to initialize the Float32Array from the jsArray‘s contents. Try to do this rarely though. Typed arrays are very fast, but initialize very slowly. It’s best to create them once and reuse them whenever possible.

    Once you have a Float32Array, you can pass the data down to the buffer like so:

    gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
    gl.bufferData(gl.ARRAY_BUFFER, float32Data, gl.DYNAMIC_DRAW);
    

    You’ll need to perform a bufferData or bufferSubData call every time the data changes.

    By this time the data is on the graphics card, so you only need to actually draw it:

    // bind the VBO to be drawn if you haven't already
    gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
    // send the vertex data to the shader program
    gl.vertexAttribPointer(vertexAttributeLocation, 3, gl.FLOAT, false, 0, 0);
    // draw the buffer
    gl.drawArrays(gl.POINTS, 0, float32Data.length / 3);
    

    Note that although the graphics card can hold quite a few VBOs, you should use as few of them as possible, because the more drawArrays calls you have to perform, the slower things are going to get. Indeed, if you render only a single pixel at a time in a given VBO, it’s going to be too slow to run.

    The reason the length of the Float32Array is divided by 3 is because each single data element is a 3D (X, Y, Z) coordinate, so each data element consists of 3 float components. Note that the first argument to gl.drawArrays was gl.POINTS. This instructs the graphics card to draw a single point (by default, a single pixel in size) for each item in the array. There are other ways to draw, and if you need to fill a group of pixels, one of the other draw modes (e.g. gl.TRIANGLES) may be more to your liking.

    As for lighting up specific pixels, it depends on how the shader is written, but most likely you’re making use of a modelview matrix and a projection matrix. The modelview matrix represents the orientation of the camera relative to the points being drawn, and the projection matrix represents the camera dimensions (width and height, field of view, nearest and furthest visible ranges, etc). So if you want to light specific pixels, your best bet is to apply an orthographic projection matrix with a width and height equal to the width and height of the canvas; and a modelview matrix set to the identity (no transformation). In an orthographic projection, objects don’t shrink as they get further away from the camera, so they’re very helpful for specifying exact positions relative to the screen. Also, if you give them the proper dimensions, you can position vertices very precisely — at specific pixels, if you wish.

    Different matrix libraries work in different ways, but for example, to set up an orthographic matrix in gl-matrix, you can do so like this:

    var ortho = mat4.ortho(left, right, bottom, top, near, far);
    

    The exact numbers depend on your preference; if you’d like to place the origin (0, 0) at the bottom-left of the canvas, you’d do so like this:

    var ortho = mat4.ortho(0, canvas.width, 0, canvas.height, 0.01, 200);
    

    Note that the Z value of each point still has to lie between near and far in order to be rendered, however, and the near value cannot be set to 0.

    Let me know if any of this needs clarifying.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very new to XSL (and XML for that matter), but I need to step
Im very new to SQL but need to write a query to do the
Im very new in php and try to use cookie but it is not
Very new to C++ and Cocos2d-x but I was just toying around with CCArray
Very new to javascript and html-type stuff. I wanted to just make a quick
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
Very new to using Raphael.js I'm looking at the tutorials and I am able
Very new to Objective-C and Xcode and working my way through modifying the DrillDownApp
Very new to Business Objects, but I have a .NET background and PL/SQL background.
The difference between a JavaScript Array , and Object is not very big. In

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.