Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client or server-side HTML5 canvas rendering for a node.js whiteboard application?

I was thinking a little whiteboard web app would be a nice way to improve my node.js and JavaScript skills. I've seen a few on the web, which makes sense as it seems ideal for this kind of stack.

Just taking a moment to think, however, I was wondering about the roles of both client and server in this kind of web application. Stumbling upon node-canvas, I became even more confused. What, specifically, should the client and server be responsible for?

If the server is capable of rendering to a canvas, should it accept and validate input from the clients and then broadcast it to all other connected users via socket.io? This way, the server keeps a master-canvas element of sorts. Once a new user connects, the server just has to push out its canvas that client - bringing it up to pace with whatever has been drawn.

Any guidance on implementation - specific or philosophical - is appreciated.

Thanks!

like image 324
Qcom Avatar asked Aug 31 '11 02:08

Qcom


3 Answers

I wrote http://draw.2x.io, which uses node-canvas (previously node-cairo, which I wrote myself) along with socket.io.

The way I've designed my application, the client essentially does all the stroke generation from user input. These are in turn processed by a canvas abstraction, which supports a subset of operations and parameters which I've defined myself. If this layer accepts whatever input the painting modules produce, they are also shipped, via socket.io, to the server.

On the server I've got the same kind of canvas layer wrapping node-canvas. This will thus replicate the input from the user in memory there, eventually making it possible to send a state image to new clients. Following this, the strokes will -- pending parameter / context validation by the server application -- be published to other connected clients, which will repeat the same procedure as above.

like image 93
einaros Avatar answered Oct 06 '22 23:10

einaros


A company I work for implemented a whiteboard app with node.js (but did not use node-canvas) and socket.io. Unfortunately, I cannot give you code or even a website since it has not been released.

Your implementation seems very similar. Clients connect to our server and update the server whenever the whiteboard is drawn to (JSON data w/(x,y) coordinates) through socket.io. The server then updates the rest of the clients and keeps a copy of all the (x,y) coordinates so that new clients who join can see what has already been drawn.

Good luck with your app. I've been programming with node.js a lot lately and boy do I love it.

like image 2
jtsao22 Avatar answered Oct 06 '22 23:10

jtsao22


here's a multiuser whiteboard tutorial written in javascript/html5, all source available: http://www.unionplatform.com/?page_id=2762

it's not node on the server-side, but the client-side code should still be useful if you want to adapt it to a node backend.

like image 1
colin moock Avatar answered Oct 06 '22 22:10

colin moock