r/javaScriptStudyGroup • u/ForScale • May 15 '16
Check out my latest CodPen!
http://codepen.io/ForScale/full/rebPvL1
u/ForScale May 15 '16
Just a fun idea to start playing around with!
Definitely needs some work on the ux side of things...
2
u/Volv May 15 '16
lol. Codpen, So thats a bit random :)
1
u/ForScale May 15 '16
Lol! I think I saw somebody write that once, CodPen; they just forgot the e, obviously. But upon reading it I had the funny idea of a cod being used as a pen. So... today I implemented it!
I'm gonna build it up as a drawing application. I'll add in the ability to change pen size and color and some other goodies!
2
u/Volv May 16 '16
Looking forward to seeing what you add.
And at the risk of being annoying :) - My approach1
u/ForScale May 17 '16 edited May 17 '16
Lol! NOT ANNOYING! :)
But... I don't think it's working for me... The code makes sense and on a quick scan I can see what it's supposed to be doing, but I'm just getting a black canvas/screen. It's never drawing a blue line for me... I'll take an in depth look later on to see if I can get it working!Not at all sure what was wrong the first time I was playing with it, but I threw in a few alerts to make sure all the handler functions were working and now the drawing is working! I have no way to explain that...
I like the outsideCanvas thing. That's a great idea!
Using Chrome, btw.*and lol, you didn't steal mousemove from me. It's like saying you stole var from me! :)
2
u/Volv May 17 '16 edited May 17 '16
Hmm. I had it not work for me once or twice, couldn't work out why. Refresh always fixed it, will need to try harder to work it out lol.
I would usually have tracked a drawing variable or something rather than add and remove eventlisteners. So trying a new approach :)1
2
u/Volv May 17 '16 edited May 17 '16
So it only seems to fail for me after a resize. Even when I try to reattatch the listener after resize. Don't know why.
Comment out resize function = fixed :)
Can now change colours. Also pressing Ctrl and clicking will draw a single pixel atm.1
u/ForScale May 17 '16
Nice! Yep, I resized it the first time before trying it. Interesting that that breaks it...
1
u/Volv May 17 '16
Now has (very) rudimentary fill function.
Was able to make this - Pic
Ctrl-click to fill colour. It will cause a stack overflow and fail disastrously on any regions bigger than about 10% of canvas :)
Need to come up with a less recursive flat function I think. Make it work first... time for optimization.
Codepen2
u/ForScale May 17 '16
Ah! That's awesome!
I am having trouble getting the fill to work though... If I understand correctly, I'm supposed to hold ctrl and click the left mouse button within a closed off shape? I got it to work once, but I can't seem to get it to work again...
2
u/Volv May 18 '16
No longer breaks on big areas.
Next up - efficiency.
Codepen2
u/ForScale May 18 '16
Ah, nice! Fill is working well now!
If you have a moment, would you mind explaining at a high level... like even higher than pseudo code; just kind of layman's terms... how the fill function works? I see the whole getImageData deal going on but I'm not too familiar with it. And I see the coordinates deal in there, but I'm having trouble putting it all together conceptually...
Irrespective of my lack of understanding, nice work!! :)
2
u/Volv May 18 '16 edited May 18 '16
Thanks. Was fun (if a bit tricky) working on a bigger problem.
I've added more comments throughout the fill part.
Algorithm:
Looks at the pixel you clicked. Say it was black.
Check pixels at all 8 surrounding points. top left, top, top right etc
If they match original colour (black) change their colour to fill colour
Add them to array to check next
Use result array to start next round of checking.
Uses mem object so it doesn't check previously checked
So spreads out from clicked point until it hits a significantly different colour then stops.→ More replies (0)1
u/Volv May 18 '16
Zoom! - Codepen
2
u/ForScale May 19 '16
Ah... that's awesome! I looked through the code... The zoom effect is a brand new canvas, right?
2
u/Volv May 19 '16
yeh, little piece stretched onto a new canvas
1
u/ForScale May 19 '16
Really cool!
My challenge to you now... Add some kind of functionality that you don't typically see in simple drawing applications like MS Paint...
What that atypical feature is is up to you! :)
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 = 12So 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!
2
u/ShortSynapse May 15 '16
No touch support :(