Use Web socket and Node. js to implement real-time drawing of the HTML 5 canvas (1)

Source: Internet
Author: User
Tags node server

Web socket and Canvas are two cool features currently implemented in browsers. This tutorial briefly describes how the two work, and creates a real-time canvas Based on Node. js and Web socket. For simplicity, I will use CoffeeScript to write all the code. If you prefer to use traditional JavaScript, you just need to take a look at the corresponding. js file. For the same reason, I also discarded CSS.

Download Code (https://github.com/wesbos/websocket-canvas-draw) from GitHub)

Introduction to detailed screen playback speed-up tutorials

Http://www.youtube.com/watch? Feature = player_embedded & v = n7wQsLu_k00

Cross-device/browser compatibility

Http://www.youtube.com/watch? V = oI9AahO9vDY & feature = player_embedded

Server

The first thing we need to do is to create a Web socket server. To this end, we need to use Node. js and module Socket. io. Socket. io makes it easy to build and run Web Socket server tasks. It even provides the Flash fallback function for browsers that do not support native Web sockets ). In this tutorial, we will only use browsers that support elements.

If you have not installed Socket. io before, make sure that it has been installed. Therefore, enter npm install socket. io in your terminal.

Now, we may wish to establish a Web socket server. Create the server. coffee file and use the following configuration.

Io = require ('socket. io '). listen (4000)

Io. sockets. on 'connection', (socket)->

Compile your coffeescript, return to your terminal, and enter node server. js .. Now you have a Web socket server running through port 4000.

If you enter the local host: 4000, the following results are displayed:

Client

First, we will not immediately let the index.html file be set up and run. In addition to some basic tags, I also added jQuery, the Socket currently provided from our server. io JS file, jQuery plug-in for drag events, and our own scripts. js file.

 
 
  1. <! Doctype html>
  2. <Html>
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
  6. <Script type = "text/javascript" src = "js/jquery. event. drag-2.0.js"> </script>
  7. <Script src = "http: // localhost: 4000/socket. io/socket. io. js"> </script>
  8. <Script type = "text/javascript" src = "scripts. js"> </script>
  9. <Link rel = "stylesheet" href = "style.css"/>
  10.  
  11. <Title> HTML5 Canvas + Node. JS Socket. io </title>
  12. </Head>
  13. <Body>
  14. <Article> <! -Our canvas will be inserted here --> </article>
  15.  
  16. <! -- Scripts required -->
  17. <Script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
  18. <Script type = "text/javascript" src = "js/jquery. event. drag-2.0.js"> </script>
  19. <Script src = "http: // localhost: 4000/socket. io/socket. io. js"> </script>
  20. <Script type = "text/javascript" src = "scripts. js"> </script>
  21. </Body>

Since our servers have been built and run, we can write some code for drawing the canvas. Create a new file named scripts. coffee. All the following code is executed in the App. init () method. Once the jQuery document is ready, we will trigger this method.

Create a Canvas Element

# Build our application with its own namespace

 
 
  1. App = {}
  2.  
  3. ###
  4. Initialization
  5. ###
  6. App. init =->
  7. App. canvas = document. createElement 'canvas '# create a <canvas> element
  8. App. canvas. height = 400
  9. App. canvas. width = 800 # increase the size
  10. Document. getElementsByTagName ('Article') [0]. appendChild (App. canvas) # attach it to the DOM
  11.  
  12. AppApp. ctx = App. canvas. getContext ("2d") # store the context
  13.  
  14. # Set some parameters for our line chart
  15. App. ctx. fillStyle = "solid"
  16. App. ctx. strokeStyle = "# bada55"
  17. App. ctx. lineWidth = 5
  18. App. ctx. lineCap = "round"
  19.  
  20. # Plotting Functions
  21. App. draw = (x, y, type)->
  22. If type is "dragstart"
  23. App. ctx. beginPath ()
  24. App. ctx. moveTo (x, y)
  25. Else if type is "drag"
  26. App. ctx. lineTo (x, y)
  27. App. ctx. stroke ()
  28. Else
  29. App. ctx. closePath ()
  30. Return


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.