Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setPixel? #8

Open
codemonkeynorth opened this issue Oct 29, 2020 · 2 comments
Open

setPixel? #8

codemonkeynorth opened this issue Oct 29, 2020 · 2 comments

Comments

@codemonkeynorth
Copy link

codemonkeynorth commented Oct 29, 2020

Hi,

I was just looking at the docs and I couldn't see a way to alter the image data? (eg setPixel(x,y, rgba) etc)

Was wondering if I missed something?

Essentially in one example we want to be able to take a greyscale image, read each pixel's grey level and map it to another 256-colour palette

pseudocode:

var rgba = getPixel(x,y)
var avg = Math.floor((rgba.r + rgba.g + rgba.b)/3);
rgba.r = lut[avg].r;
rgba.g = lut[avg].g;
rgba.b = lut[avg].b;
setPixel(x,y, rgba);

alternatively eg using getPixels, modifying the array and calling setPixels(modifiedArray)

admittedly this can be done with

var canvas = new CanvasPlus();
// ...
var context = canvas.getContext();
var imageData = context.getImageData(0,0, width, height);

// ...modify imageData

context.putImageData(imageData,0,0);  

// this now would also get the modified pixels
var data = canvas.getPixels();

thanks
J.

@jhuckaby
Copy link
Owner

Hello there,

Yeah, currently the only way to access the raw pixels is through getPixels(). This will give you a Uint8ClampedArray of RGBA pixels, which you can manipulate. The problem is, you'd then have to use getContext() to put the image data back, using the HTML5 Canvas API.

Sorry there isn't a better API. I'll add this to the TODO list for a future release.

@codemonkeynorth
Copy link
Author

@jhuckaby thanks for the update, just wanted to check I wasn’t missing anything obvious

Regards
J

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants