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