html5之canvas实例(二)

时间:2022-01-16 05:29:34

点击页面会自动随机画出一个三角形!!!

 

<!DOCTYPE HTML>

<html>
<head><meta name="viewport" content="width=320,user-scalable=no"></head>
<body>

<canvas id="canvas" width="3000" height="3000"></canvas>
<script type="text/javascript">

var canvas=document.getElementById("canvas");
var cxt=canvas.getContext("2d");


canvas.onmousedown=function (event){

var x=event.x;
var y=event.y;
var r=Math.random()*5+5;
cxt.beginPath();
cxt.moveTo(x,y);
cxt.lineTo(x,y+r);
cxt.lineTo(x+r,y+r);
cxt.lineTo(x,y)
cxt.strokeStyle="black";
cxt.stroke();


}

</script>




</body>


</html>

 

 

 

 

截图如下:

 

 

 

 

html5之canvas实例(二)