r/javaScriptStudyGroup • • May 15 '16

Check out my latest CodPen!

http://codepen.io/ForScale/full/rebPvL
2 Upvotes

28 comments sorted by

View all comments

1

u/Volv May 18 '16

You can solve the odd fish sized cursor problem when you update the cursor
Codpen
 
div.style.top = e.y + "px";

div.style.left = e.x + "px"; document.body.style.cursor = "url('http://i63.tinypic.com/2cgbitt_th.jpg') 70 120, default";

1

u/ForScale May 18 '16

Right out of the mouth: http://codepen.io/anon/pen/pymmrB?editors=0010 55 50

Questions:

What do you think the load on the page is like with adding div elements vs canvas strokes? How could we measure?

How are canvas strokes and fills represented in the DOM?

How did you select an individual pixel in yours?

2

u/Volv May 18 '16 edited May 18 '16

Nice :)
 
I expect DOM to be very much slower - usually suggested to limit DOM manipulation.
 

Could try drawing a load of 1px by 1px squares. Maybe in a 100x100 Area. And benchmarking. Average 3 runs. Then further test - change colours of them all 1 by 1. Average 3 runs.
Will write one on my side later if you'd like.
 

  // imgData.data contains huge array with rgba values for each pixel
  const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);   

So for a 2 by 2px canvas imgData.data contains

imgData.data = [r, g, b, a,
                r, g, b, a,
                r, g, b, a,
                r, g, b, a]  

 
First pixel data lives at index 0-3 Second at 4-7 etc .

Thats what this does

function convertCoord(x, y) {
  return 4*(y*canvas.width + x);
}  
// 0, 0 = 0
// 0, 1 = 4
// 1, 0 = 8 
// 1, 1 = 12  

So I just take result from this and read

imgData.data(result)   // Red 
imgData.data(result+1) // Green
imgData.data(result+2) // Blue
imgData.data(result+3) // Alpha (0-1)

1

u/ForScale May 18 '16

Oh... that's fascinating! That imgData does stores a whole pixel by pixel analysis, huh? That seems really powerful!

Thanks!