Wednesday, September 5, 2012

Draw Something JS - Part #1

This is my first post, so please be gentle with your comments :) 
Today, You are going to see the power of HTML5 and Javascript and I’m going to show you, step by step, how to create the Great DrawSomething application in Javascript.

DrawSomething JS



Ready?!?! so lets go.

The first thing we’re going to learn is how to draw inside your browser:

HTML5 has a new element called Canvas, to add a canvas inside your html, you just need to copy this code inside the html page:

<canvas id=”board” class=”board” ></canvas>

To draw inside this canvas with the mouse, we need to listen to three mouse events:
  • mousedown - to start drawing the line
  • mousemove - to continue drawing the line
  • mouseup - to stop drawing the line
var board = document.getElementById(“board”);
board.addEventListener(“mousedown”,startPaintWithMouse,false);
board.addEventListener(“mousemove”,movePaintWithMouse,false);
board.addEventListener(“mouseup”,stopPaintWithMouseUp,false);
Here are the methods implementation:
var startPaintWithMouse = function(event) {
    brushCtx = board.getContext(‘2d’); // get the context of the canvas
    brushCtx.beginPath();
    brushCtx.lineWidth = lineWidth; // set line width from variable
    brushCtx.strokeStyle = color; // set line color from variable
    brushCtx.moveTo(event.offsetX,event.offsetY); // start drawing from this point
};
var movePaintWithMouse = function(event) {
    if (mouseState == 1) {
        event.preventDefault();
        brushCtx = board.getContext(‘2d’);
        brushCtx.lineTo(event.offsetX,event.offsetY); // go to this point and draw
        brushCtx.stroke(); // stroke the line
    }
};
var stopPaintWithMouseUp = function(evet) {
    mouseState = 0; // set mouse state to 0 to stop the draw
};

Now, just add the buttons that change the line width and color and start to draw inside the html5 canvas element.
In the next post, We will learn how to record the user movements and to play it, just like the DrawSomething app.

51 comments:

  1. That is a good thing. I hope I could use it freely, nevertheless, its modifications are too complicated to me.

    ReplyDelete
  2. Each element is important in this structure of the code. We need to draw the clean lines to create the desired application.

    ReplyDelete
  3. I would like to learn how to record the user movements and to play this game. It must be exciting!

    ReplyDelete
  4. Vest Nice blog for learning new things,thanks for such beautiful blog.
    below some new idea plz check once.
    kajal agarwal hot

    ReplyDelete
  5. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Advanced AWS Training in Bangalore | Best Amazon Web Services Training Institute in Bangalore
    Advanced AWS Training Institute in Pune | Best Amazon Web Services Training Institute in Pune
    Advanced AWS Online Training Institute in india | Best Online AWS Certification Course in india
    AWS training in bangalore | Best aws training in bangalore

    ReplyDelete
  6. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    microsoft azure training in bangalore
    rpa training in bangalore
    rpa training in pune
    best rpa training in bangalore

    ReplyDelete
  7. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  8. The post was really very good.Thanks for sharing
    prestige elysian

    ReplyDelete
  9. I’ve found extensive lists before, but none this informative. Thanks for sharing!

    For more details Click Here- SAP Training in Bangalore

    ReplyDelete
  10. Such a wonderful article and I feel that it is best to write more on this topic. Thank you so much because i learn a lot of ideas about it. Keep posting...
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata
    SEO Course In Kolkata

    ReplyDelete
  11. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.

    Correlation vs Covariance

    ReplyDelete
  12. I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.

    Data Science Course

    ReplyDelete
  13. It has fully emerged to crown Singapore's southern shores and undoubtedly placed her on the global map of residential landmarks. I still scored the more points than I ever have in a season for GS. I think you would be hard pressed to find somebody with the same consistency I have had over the years so I am happy with that.

    Data Science Training

    ReplyDelete
  14. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.

    Simple Linear Regression

    Correlation vs covariance

    KNN Algorithm

    Logistic Regression explained

    ReplyDelete
  15. Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data scientist courses

    ReplyDelete
  16. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.Best data science courses in hyerabad

    ReplyDelete
  17. Terrific post thoroughly enjoyed reading the blog and more over found to be the tremendous one. In fact, educating the participants with it's amazing content. Hope you share the similar content consecutively.
    Data Analytics Course in Raipur

    ReplyDelete
  18. Thanks for posting the best information and the blog is very helpful.data science interview questions and answers

    ReplyDelete
  19. Thanks for sharing information to our knowledge, it helps me plenty keep sharing…

    Tableau Training in Pune
    Tableau Training Institutes in Pune

    ReplyDelete
  20. Truly mind blowing blog went amazed with the subject they have developed the content. These kind of posts really helpful to gain the knowledge of unknown things which surely triggers to motivate and learn the new innovative contents. Hope you deliver the similar successive contents forthcoming as well.

    data science in bangalore

    ReplyDelete
  21. This is a fabulous post I seen because of offer it. It is really what I expected to see trust in future you will continue in sharing such a mind boggling post
    data scientist training and placement

    ReplyDelete

  22. Hello! I just wish to give an enormous thumbs up for the nice info you've got right here on this post. I will probably be coming back to your weblog for more soon!
    data scientist training in hyderabad

    ReplyDelete
  23. Thanks for posting the best information and the blog is very important.artificial intelligence course in hyderabad

    ReplyDelete




  24. I was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra. data science course in jaipur

    ReplyDelete
  25. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing. data analytics course in delhi

    ReplyDelete
  26. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing. data scientist course in delhi

    ReplyDelete
  27. Die Technologie entwickelt sich schneller denn je und schneller als Sie denken. Diese aufkommende Technologie wird unsere Lebensweise verändern.

    ReplyDelete
  28. Thanks for posting the best information and the blog is very good. ethical hacking training in kolkata

    ReplyDelete
  29. Stupendous blog huge applause to the blogger and hoping you to come up with such an extraordinary content in future. Surely, this post will inspire many aspirants who are very keen in gaining the knowledge. Expecting many more contents with lot more curiosity further.

    Data Science Certification in Bhilai

    ReplyDelete
  30. Thanks for posting the best information and the blog is very good.cloud computing training in kolkata

    ReplyDelete
  31. I want you to thank for your time of this wonderful read!!! I definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog!
    business analytics course in hyderabad

    ReplyDelete
  32. This post is very simple to read and appreciate without leaving any details out. Great work!
    business analytics training in hyderabad

    ReplyDelete
  33. CBD products and oils are ideal for treating various conditions. Regardless, different people may not know how to use them to acquire the results. Accepting that you have endeavored CBD ahead of time and dropped by no outcome, read on to learn about the normal blunders you should maintain away from to exploit your product. CBD based products

    ReplyDelete
  34.  
    Search engines like fresh content and a blog site like Bcss8 is an excellent way to provide this. Blogs can be used to discuss your products or services and keep your readers up to date on industry trends. They can also be used to engage your customers and build strong relationships. Besides offering information, blogs can also show authority. A blog provides a more personal way to communicate with your audience and can be a good tool for improving your SEO. bcss8.com

    ReplyDelete
  35. Weekly Silicon Valley revolutionize the traditional blogging landscape by offering a comprehensive and visually appealing format. They enable content creators to consolidate their work, establish authority, and build a loyal readership. Weekly Silicon Valley also leverage SEO optimization, track analytics, and provide valuable insights into reader behavior, helping creators refine their strategies and increase their online visibility. weeklysiliconvalley

    ReplyDelete