Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // we have access to the DOM element
  2. var canvas = document.querySelector('canvas');
  3. // we need to get the correct context type, or it will return null
  4. var gl = canvas.getContext('webgl') || canvas.getContext('webgl2');
  5. // where we'll store our pixels info
  6. var pixels = new Uint8Array(4);
  7.  
  8. canvas.addEventListener('click', function(e) {
  9.   var x = e.clientX - canvas.offsetLeft;
  10.   var y = canvas.clientHeight - (e.clientY - canvas.offsetTop);
  11. console.log(x); console.log(y);
  12.   // we need to call it in the same execution flow as 'render' because webgl erase the drawing buffer by default
  13.   // this can be done by stacking our code in the next frame.
  14.   requestAnimationFrame(function() {
  15.     gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
  16.     console.log(pixels);
  17.   });
  18. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement