Make a drawing board with HTML5 Canvas

Source: Internet
Author: User

Using HTML5, you can easily implement a paint application on a canvas, use a browser that supports HTML5 to paint in the area below, and to see how it works, make sure your browser supports HTML5:

The function is very simple, the principle and drag and drop is similar, mainly three events:

    1. Bind the MouseDown event to the beginning of the painting (call MoveTo move brush) in the canvas casino Macau
    2. Behavior when binding a MouseMove event on document to handle painting (call LineTo and stroke for painting)
    3. Binding the MouseUp event in document to the end of the mark painting (two events on document)

One of the special considerations to implement is how to pass the correct coordinate values when calling MoveTo and the LineTo method, which should be the offset of the cursor relative to the upper-left corner of the canvas, and take into account the position of the canvas in relation to the current viewport. The Getboundingclientrect method comes in handy (the browser that supports HTML5 should implement this method), and finally uses the event object's Clientx, ClientY minus Getboundingclientrect method returns the left, top value.

Here is the implementation code:

View Source print?
01 // <![CDATA[
02 functionDraw(arg) {
03     if(arg.nodeType) {
04         this.canvas = arg;
05     } elseif (typeofarg == ‘string‘) {
06         this.canvas = document.getElementById(arg);
07     } else{
08         return;
09     }
10     this.init();
11 }
12 Draw.prototype = {
13     init: function() {
14         varthat = this;
15         if(!this.canvas.getContext) {
16             return;
17         }
18         this.context = this.canvas.getContext(‘2d‘);
19         this.canvas.onselectstart = function() {
20             returnfalse//修复chrome下光标样式的问题
21         };
22         this.canvas.onmousedown = function(event) {
23             that.drawBegin(event);
24         };
25     },
26     drawBegin: function(e) {
27         varthat = this,
28             stage_info = this.canvas.getBoundingClientRect();
29         window.getSelection ? window.getSelection().removeAllRanges() :
30                                 document.selection.empty();  //清除文本的选中
31         this.context.moveTo(
32             e.clientX - stage_info.left,
33             e.clientY - stage_info.top
34         );
35         document.onmousemove = function(event) {
36             that.drawing(event);
37         };
38         document.onmouseup = this.drawEnd;
39     },
40     drawing: function(e) {
41         varstage_info = this.canvas.getBoundingClientRect();
42         this.context.lineTo(
43             e.clientX - stage_info.left,
44             e.clientY - stage_info.top
45         );
46         this.context.stroke();
47     },
48     drawEnd: function() {
49         document.onmousemove = document.onmouseup = null;
50     }
51 };
52 vardraw = newDraw(‘the_stage‘);
53 // ]]>

Just such a simple mouse painting function is completed, the shortcomings are also, for example, can not draw a point ... I personally think that using canvas to do drawing or relatively weak, some of the complex features are not very good to achieve, but you can also try to, for example, to add a method to save the picture, define Draw.prototype.save = function () {...}, Where the Todataurl method implementation can be called.

Make a drawing board with HTML5 Canvas

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.