I am trying to create a collaborative whiteboard with socket.io and fabric.js.
When a user draws something I send the path to other users as a JSON format:
canvas.on('path:created', function(e) {
canvas.remove(fabric.Path.fromObject(JSON.stringify(e.path)));
socket.emit('add path', e.path.toJSON());
});
How can I recreate this object on the canvas here?
socket.on('add path', function(path) {
canvas.add(path); // doesn't work
});
try to use fabric.util.enlinvenObjects
The purpouse of that function is to switch from object form to istance.
socket.on('add path', function(path) {
fabric.util.enlivenObjects([path], function(objects) {
objects.forEach(function(o) {
canvas.add(o);
});
});
});