Jump to content

Can I convert an Image to an Array of Bits?


Recommended Posts

I recommend against using pixel-perfect collision detection, it is rarely needed.


That being said you can extract the pixels of an image using the canvas api:

// assume "img" is your image elementvar canvas = document.getElementById('mycanvas');var ctx = canvas.getContext('2d');canvas.width = img.width;canvas.height = img.height;ctx.drawImage(img, 0, 0, img.width, img.height);var pixelData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
That will return a Float32Array of the pixels in the format [R,G,B,A, R,G,B,A, ...]
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...