Simple implementation of graffiti Html5 write your own canvas, html5 canvas

Source: Internet
Author: User

Simple implementation of graffiti Html5 write your own canvas, html5 canvas

I recently learned that I was amazed by html5's powerful drawing function. So I wrote a little theme: Graffiti board. The following functions can be implemented: painting, color change, and paint brush size adjustment.

Html5 plotting can be divided into points, lines, faces, circles, images, points, and lines. This is the basis of all the plane effects, only unexpected algorithms are supported.

Code first:

Html

Copy XML/HTML Code to clipboard
  1. <Body style = "cursor: pointer">
  2. <Canvas id = "mycavas" width = "1024" height = "400" style = "border: solid 4px #000000"> </canvas> <! -- Canvas -->
  3. <Input type = "color" id = "color1" name = "color1"/> <! -- Color generator -->
  4. <Output name = "a" for = "color1" onforminput = "innerHTML = color1.value"> </output>
  5. <Input type = "range" name = "points" id = "size" min = "5" max = "20"/> <! -- Drag a bar -->
  6. </Body>

Effect:

Well, a simple drawing interface is ready. Below we start to write some code for drawing lines.

Copy the content to the clipboard using JavaScript Code
  1. $. Draw = {};
  2. $. Extend ($. Draw ,{
  3. D2 :"",
  4. CX :"",
  5. Box: "mycavas", // canvas id
  6. BoxObj: function () {// canvas object
  7. This. CX = document. getElementById (this. Box );
  8. },
  9. D2: function () {// 2d drawing object
  10. This. D2 = this. CX. getContext ("2d ");
  11. },
  12. Cricle: function (x, y, r, color) {// circle
  13. If (this. D2 ){
  14. This. D2.beginPath ();
  15. This. D2.arc (x, y, r, 0, Math. PI * 2, true );
  16. This. D2.closePath ();
  17. If (color ){
  18. This. D2.fillStyle = color;
  19. }
  20. This. D2.fill ();
  21. }
  22. },
  23. Init: function () {// Initialization
  24. This. BoxObj ();
  25. This. D2 ();
  26. }
  27. })

I believe you can understand the simple code here. It mainly involves creating an object, including creating a canvas, creating a 2d Object, the circle method, and the object initialization method.

Next, call this object on the front-end html page/p>

Check the Code:

Copy the content to the clipboard using JavaScript Code
  1. Var color = "#000000"; // initialization color
  2. Var size = 5; // initialization size
  3. Document. getElementById ('color1'). onchange = function (){
  4. Color = this. value;
  5. };
  6. Document. getElementById ('SIZE'). onchange = function (){
  7. Size = this. value;
  8. };
  9. $. Draw. init (); // Initialization
  10. Var tag = false; // controls the current state of the mouse and enables the ink switch.
  11. Var current ={}; // stores the point when the mouse is pressed.
  12. Document. onmousedown = function (option) {// click the event
  13. Current. x = option. x;
  14. Current. y = option. y;
  15. $. Draw. Cricle (option. x, option. y, size, color );
  16. Tag = true;
  17. }
  18. Document. onmouseup = function () {// mouse lift event
  19. Tag = false;
  20. }
  21. Document. onmousemove = function (option) {// move the cursor
  22. If (tag ){
  23. If (size> = 0 ){
  24. $. Draw. Cricle (option. x, option. y, size, color );
  25. }
  26. }
  27. }

This code mainly has the following meanings:

1. Capture the change event of the Color Space and the drag bar control to obtain the corresponding color and size values and store them for the following draw lines.

2. initialize the Drawing Object

3. Capture the mouse press, lift and move the event, the key is that a switch can control the ink

Okay, just a simple graffiti sheet. Just upload my calligraphy:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.