25. user: recieve
socket.on(‘draw:progress’, function( a, d ) {
if ( a !== uid && d ) {
progress_external_path( d, a )
}
}
socket.on(‘draw:end’, function( a, d ) {
if ( a !== uid && d ) {
end_external_path( d, a )
}
}
26. user: recieve
progress_external_path = function( paths , a ) {
var path = external_paths[a];
if (!path) {
// create new path
// store using external UID in object
}
for ( i = 0; i<paths.length; i++ ) {
// add point to path
}
}
* Hi, Im Richard\n* web designer, alot of time writing Javascript\n* Work for Thap, cool company\n* Give jQuery workshops\n
* Real-Time Collaborative Drawing tool\n* Lets check it out\n
* How many people here have installed nodejs?\n* Would love it if you could, can follow some of the slides & run your first nodejs App\n* Can help at the end\n* Please interrupt if you have questions.\n
* How do you make a real-time collaborative drawing thing?\n* Simpler than you might think\n
* Its all about events\n* When user A draws, they send their drawing to the server.\n* Server then sends the drawing onto all other users \n
* HTML5 Canvas: Element, Drawing Context, Bitmap using javascript.\n* PaperJS: covers key concepts: points, paths, shapes, text, animations\n* Websockets: Continuous 2 way connection between 2 points. Better than AJAX\n* Nodejs: How many people know of this?\n* Express makes it easy, will try and install both during presentation\n
* Try and code-along. Will offer help at end.\n
* Hopefully you&#x2019;ve already done this\n
* Designers: Dont be scared of terminal\n* NPM: handles javascript packages. Like dealing with Ruby gems\n* Nodemon, runs your app but restarts when you make changes\n* I&#x2019;ve wasted a few hours by not using nodemon\n* This command means you can use nodemon wherever you want\n
* You can now create new Express applications\n* Express is a great little framework\n
* Cd means change directory\n* Im going to create my app in a folder called jsnortheast\n
* Creates a new Express application\n
* Express has dependencies; jade, for example.\n* Jade is a templating engine for HTML, think HAML for javascript\n* this command installs those dependencies so that Express can run\n
* Use node monitor to run the newly created express application\n
* You should see &#x201C;Welcome to Express&#x201D;\n\n
* Follows familiar MVC structure\n* node_modules - dependencies reside here: jade, socket.io etc\n* public: images, css, client-side javascript\n* routes: the URL structure of your application\n* views: the HTML (Jade) of your app\n* app.js: runs your application\n* package.json: dependencies list & versions, dont need to edit\n
* Im using a very small part of paper.js\n* Honestly, its all on the paperjs website.\n
* 1. Include paperJS on the page. This is Jade which is converted into a HTML script tag\n* 2. Include a Canvas element & tell paperJS to automatically re-size it to the window dimensions\n* 3. A file our javascript. Type tells paperJS to run it, canvas tells paperJS which canvas to use \n
* on mouse down we create a new path at the cursors poistion and give it a color\n* on mouse up we close the path & smooth it\n
* make path wider/narrower depending on speed of mouse movement\n* Add new points to the path at the cursors position\n* Paths are drawn as the user drags & closed as they mouse up\n
* Socket io passes draw data from user to server to other users in real time\n
* First install a version of socket.io just for express\n* Include socket.io in the HTML\n* Connect to the server in the client side javascript\n
* on mouse drag we add points to an array\n* Every 100ms we send that array to the server with a UID and the event name: &#x2018;draw:progress&#x2019;\n* on mouse up we send the the array and UID to the server\n* this time we send it with a different event name.\n
* Rugby ball pass, recieves data, passes on to users as quickly as possible\n
* On both types of draw event, user checks &#x2018; is this my UID ? &#x2018;\n* if not it draws that path.\n
* first we check to see if a path has already been created using that UID (a)\n* if not we create it\n* Then we add each point in the paths array to the path\n
* This was a hack day project, you can achieve a lot in 8 hours\n* This is really fun technology\n* Very popular tech: 1,500 visits in 5 days.\n* Real-time good to be playing with\n\n \n
* Please do download the source code & create your own &#x2018;things&#x2019;\n* I wrote a blog post about draw stuff, check it out\n* I tweet a lot, feel free to tweet me questions or ask for help, or email me.\n* Any questions?\n\n