Each client needs to send drawing commands to the server. The server must send commands to all clients. You need a method that receives messages and draws them.
eg. if the user can set attributes such as, for example, โstroke sizeโ and color, you also need to pass these changes.
You need to register mouse listeners for users. They should call drawing methods, for example. drawMoveTo and drawLineTo , as well as buffer commands along the same path so that you can translate the full path in messages to other clients.
Examples of messages may include:
{"clientId": 36, "penSize": 8, "color": "blue"} {"clientId": 36, "command": {"moveTo", "x": 48, "y": 12}, "path": [{"moveTo", "x": 48, "y": 12}]} {"clientId": 36, "command": {"lineTo", "x": 52, "y": 24}, "path": [{"lineTo", "x": 52, "y": 24}, {"moveTo", "x": 48, "y": 12}]} {"clientId": 36, "command": {"lineTo", "x": 47, "y": 36}, "path": [{"lineTo", "x": 47, "y": 36}, {"lineTo", "x": 52, "y": 24}, {"moveTo", "x": 48, "y": 12}]}
You can have a data structure to track "strokeAttributes" for each user. Then, when you get a message like moveTo or lineTo , you look at clientId to get the stroke attributes, for example. penSize and color , then you call the same methods that call when the local user draws, for example. drawMoveTo and drawLineTo .
The draw methods must use different strokes and attributes depending on which client the message comes from (or the mouselistener). This will change a lot if multiple users draw at the same time.
I would recommend that you do communication using WebSockets or perhaps socket.io .
Here is a good article with code for an application similar to what you are asking for A multi-user drawing panel built in with Pure JavaScript / HTML5 / Canvas