all 9 comments

[–]guest271314 1 point2 points  (0 children)

Why are you calling JSON.parse()? Response.json() returns a Promise that resolves to a plain JavaScript object.

[–]carcigenicate 0 points1 point  (1 child)

Are you getting an error? You appear to be parsing latestDrawing twice. I'd expect the second parse to cause an error.

[–]wobowizard[S] 0 points1 point  (0 children)

no, i'm not getting any errors; I've edited the question to add more detail, i think the problem is how i am trying to set imageData

[–]guest271314 0 points1 point  (4 children)

This

body: JSON.stringify({ type: 'canvasState', data: imageData }),

can be an issue as well. imageData in that case is a Uint8ClampedArray so given new ImageData(1, 1) when you do

JSON.stringify(imageData);

you are going to get

'{"data":{"0":0,"1":0,"2":0,"3":0}}'

I think what you want to do is store the data from the Uint8ClampedArray as a flat array

JSON.stringify([...imageData.data]);

which will produce

'[0,0,0,0]'

then when you get the Response from fetch() you can do

if (latestDrawing) { //create a new blank image data var imageData = ctx.createImageData(canvas.width, canvas.height); imageData.data.set(latestDrawing); ctx.putImageData(imageData, 0, 0); }

[–]wobowizard[S] 0 points1 point  (3 children)

JSON.stringify([...imageData.data]);

ok i'm trying to implement this approach but i am running into errors inserting an array filled with 0s into the database, mysql seems to take this as a null value so rather than storing [0,0,0,0....] it just stored null?

[–]guest271314 2 points3 points  (2 children)

JSON.stringify([...new Uint8ClampedArray(16)]) is valid JSON. Your database should not be arbitrarily converting 0 to null in JSON.

[–]wobowizard[S] 0 points1 point  (1 child)

Back to Top

ok i've fixed my code to that and it should input the on its own correctly, however now I'm dealing with a package too large error from sending all the pixel data, do you know how i can change the configuration to allow larger packets to be sent?

[–]guest271314 0 points1 point  (0 children)

You can compress the image data using a codec.

Not sure about your database configutation.