Jcanvas plug-in for drawing and saving the image as an HTML5 canvas

Source: Internet
Author: User

Comments: HTML5 canvas draws a picture and saves it as an image. The jcanvas plug-in is used. For more information, see the following example.

The Code is as follows:
<Head>
<Script src = 'jquery-1.9.1.js'> </script>
<Script src = 'jcanvas. min. js'> </script>
<! -- <Script src = 'js/jquery. mobile-1.2.0.min.js '> </script> -->
<Script>
Var maxX =-1;
Var maxY =-1;
Var minX = 99999;
Var minY = 99999;
Function checkData (event ){
Var x = event. pageX-$ ('canvas '). offset (). left;
Var y = event. pageY-$ ('canvas '). offset (). top;
If (x> maxX ){
MaxX = x;
} Else if (x <minX ){
MinX = x;
}
If (y> maxY ){
MaxY = y;
} Else if (y <minY ){
MinY = y;
}
}
$ (Function (){
Var obj = $ ('canvas ');
Var temp_e;
Var temp_draw = false;

Obj. on ({
Mousedown: function (e ){
Temp_e = e;
Temp_draw = true;
CheckData (e );
},
Mousemove: function (e ){
If (temp_draw ){
Obj. drawLine ({
StrokeStyle: '#000 ',
StrokeWidth: 3,
X1: temp_e.pageX-$ ('canvas '). offset (). left, y1: temp_e.pageY-$ ('canvas'). offset (). top,
X2: e. pageX-$ ('canvas '). offset (). left, y2: e. pageY-$ ('canvas'). offset (). top,
});
}
Temp_e = e;
CheckData (e );
},
Mouseup: function (e ){
Temp_e = null;
Temp_draw = false;
CheckData (e );
},
Mouseout: function (){
Temp_e = null;
Temp_draw = false;
}
});
$ ("# Clean"). on ("click", function (){
MaxX =-1;
MaxY =-1;
MinX = 99999;
MinY = 99999;
Obj. clearCanvas ();
});
$ ("# Save"). on ("click", function (){
Var image = obj. getCanvasImage ("png ");
Alert (image );
});

});
</Script>
</Head>
<Body>
<Input type = "button" id = "save" value = "save"/>
<Input type = "button" id = "clean" value = "clear"/>
<Br/>
<Canvas width = '000000' height = '000000' style = "background: # f00"> </canvas>
<Div id = "points"> </div>
</Body>

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.