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>