1

I have a series of functions that builds a rgba sequence array, and I'm planning to apply this data to a canvas object. I'm not sure how to convert the rgba (already in 0-255 values) into a proper imageData object that I can use .putImageData with.

var new_canvas = document.createElement('canvas');
var mc_ctx = new_canvas.getContext('2d');
new_canvas.width = 50;
new_canvas.height = 50;
var mc_imageData = mc_ctx.getImageData(0,0, 50, 50);
var mc_px_data = mc_imageData.data;

for ( var i = 0; i <= rgba_array.length; i++ ) {
    mc_px_data[i] = rgba_array[i];
}

mc_ctx.putImageData(mc_imageData, 0, 0);

All i get is white, even though an output of the rgba_array values shows the proper stream of values.

7
  • where did rgba_array get defined? are you SURE it contains an ImageData object? what does console.log(rgba_array) show? Commented Aug 12, 2016 at 21:04
  • @MarcB - rgba_array is probably not an ImageData object then, it's just a normal array with rgba values as the entries, such as 255,0,0,255... (red) etc. Commented Aug 13, 2016 at 0:03
  • You can only use putImagedata with an ImageData object. You could fill its data property with you array's values, but you need the ImageData object. Commented Aug 13, 2016 at 4:47
  • @Kaiido - Would you be able to post this as an answer with example? I'm still kinda new to the canvas element. Commented Aug 13, 2016 at 12:05
  • 1
    Still an hard to answer one... First, an advice : to create an empty ImageData, use the context.createImageData(width, height) method, it's way less heavier than the getImageData()one. Second, in order to be able t elp you, we'll need some info about this rgba_array. Are you sure it does only contain the 50*50 px rgba values (if so rgba_array.length should be 10000). Then, are you sure all its 10000 firsts values are not set to 255 ? If you still don't find your problem, then check that you actually append your canvas and that it should be visible in your doc. Commented Aug 15, 2016 at 13:51

1 Answer 1

3

After you have set the values in mc_px_data, you must convert it to a UInt8ClampedArray and set the data properly or it won't write. The following should work after your for loop (untested)

var new_canvas = document.createElement('canvas');
var mc_ctx = new_canvas.getContext('2d');
new_canvas.width = 50;
new_canvas.height = 50;
var mc_imageData = mc_ctx.getImageData(0,0, 50, 50);
var mc_px_data = mc_imageData.data;

for ( var i = 0; i <= rgba_array.length; i++ ) {
    mc_px_data[i] = rgba_array[i];
}

// New code here:
mc_px_data = new Uint8ClampedArray(mc_px_data);
mc_imageData.data.set(mc_px_data);
mc_ctx.putImageData(mc_imageData, 0, 0);
Sign up to request clarification or add additional context in comments.

2 Comments

you should have posted the complete code with your changes, its really hard to read this way
@moeiscool I've added the original code for completeness as per your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.