Html5 canvas usage example

Source: Internet
Author: User

Comments: This is the first article. There is no technical content, so I will not explain it ..
The Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> HTML5 example </title>
<Style type = "text/css">
# Container {border: 1px solid # ccc; width: 800px; height: 600px; position: relative ;}
Canvas {position: absolute; top: 0px; left: 0px; z-index: 1 ;}
</Style>
</Head>
<Body>
<Select id = "tools">
<Option value = "pen"> pencil </option>
<Option value = "line"> straight line </option>
<Option value = "rect"> rectangle </option>
<Option value = "circle"> circle </option>
<Option value = "ellipse"> elliptic </option>
</Select>
<Div id = "container">
<Canvas id = "canvas" width = "800" height = "600"> </canvas>
<Canvas id = "canvas_temp" style = "z-index: 2;" width = "800" height = "600"> </canvas>
</Div>
<Script type = "text/javascript">
Var canvas = document. getElementById ('canvas ');
Var context = canvas. getContext ('2d ');

Var _ canvas = document. getElementById ('canvas _ temp ');
Var _ context = _ canvas. getContext ('2d ');
Var tools = document. getElementById ('tool ');

Tools. onchange = function (e ){
Tool [this. value] ();
};
Var tool = {
Pen: function (){
This. reset ();
_ Canvas. onmousedown = function (e ){
_ Context. moveTo (e. layerX, e. layerY );
_ Canvas. onmousemove = function (e ){
_ Context. lineTo (e. layerX, e. layerY );
_ Context. stroke ();
};
_ Canvas. onmouseup = function (e ){
_ Canvas. onmousemove = null;
_ Canvas. onmouseup = null;
Tool. updata ();
};
};
},
Line: function (){
This. reset ();
_ Canvas. onmousedown = function (e ){
Var _ e = e;
_ Canvas. onmousemove = function (e ){
_ Context. clearRect (0, 0, canvas. width, canvas. height );
_ Context. beginPath ();
_ Context. moveTo (_ e. layerX, _ e. layerY );
_ Context. lineTo (e. layerX, e. layerY );
_ Context. stroke ();
_ Context. closePath ();
};
_ Canvas. onmouseup = function (e ){
_ Canvas. onmousemove = null;
_ Canvas. onmouseup = null;
Tool. updata ();
};
}
},
Rect: function (){
This. reset ();
_ Canvas. onmousedown = function (e ){
Var _ e = e;
_ Context. strokeStyle = "#000 ";
_ Canvas. onmousemove = function (e ){
_ Context. clearRect (0, 0, canvas. width, canvas. height );
_ Context. strokeRect (_ e. layerX, _ e. layerY, e. layerX-_ e. layerX, e. layerY-_ e. layerY );
};
_ Canvas. onmouseup = function (e ){
_ Canvas. onmousemove = null;
_ Canvas. onmouseup = null;
Tool. updata ();
};
}
},
Circle: function (){
This. reset ();
_ Canvas. onmousedown = function (e ){
Var _ e = e;
_ Canvas. onmousemove = function (e ){
_ Context. clearRect (0, 0, canvas. width, canvas. height );
_ Context. beginPath ();
_ Context. arc (_ e. layerX, _ e. layerY, e. layerX-_ e. layerX, 0, Math. PI * 2, true );
_ Context. stroke ();
_ Context. closePath ();
};
_ Canvas. onmouseup = function (e ){
_ Canvas. onmousemove = null;
_ Canvas. onmouseup = null;
Tool. updata ();
};
}
},
Ellipse: function (){
This. reset ();
_ Canvas. onmousedown = function (e ){
Var _ e = e;
_ Canvas. onmousemove = function (e ){
Var st = 0;
_ Context. clearRect (0, 0, canvas. width, canvas. height );
_ Context. beginPath ();
_ Context. moveTo (_ e. layerX + (e. layerX-_ e. layerX) * Math. cos (st), _ e. layerY + (e. layerX-_ e. layerX) * Math. sin (st ));
St + = 1/180 * Math. PI;
For (var I = 0; I <360; I ++ ){
_ Context. lineTo (_ e. layerX + (e. layerX-_ e. layerX) * Math. cos (st), _ e. layerY + (e. layerY-_ e. layerY) * Math. sin (st ));
St + = 1/180 * Math. PI;
}
_ Context. stroke ();
_ Context. closePath ();
};
_ Canvas. onmouseup = function (e ){
_ Canvas. onmousemove = null;
_ Canvas. onmouseup = null;
Tool. updata ();
};
}
},
Reset: function (){
_ Canvas. onmousedown = null;
_ Canvas. onmouseup = null;
_ Canvas. onmousemove = null;
},
Updata: function (){
Context. drawImage (_ canvas, 0, 0 );
_ Context. clearRect (0, 0, canvas. width, canvas. height );
}
};
Tool ['pen '] ();
</Script>
</Body>
</Html>

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.