<body><canvasid="myCanvas"width="578"height="200"></canvas><script>var canvas = document.getElementById('myCanvas');var context = canvas.getContext('2d');// do cool things with the context
context.font ='40pt Calibri';
context.fillStyle ='blue';
context.fillText('Hello World!',150,100);</script></body>
五角星
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>五角星</title></head><body><canvasid="canvas"width="400"height="400"></canvas><script>// 使用id来寻找canvas元素var myCanvas = document.getElementById('canvas');if(myCanvas.getContext){
// 创建context对象// getContext("2d") 对象是内建的 HTML5 对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法var ctx = myCanvas.getContext("2d");// beginPath:开始绘制一段新的路径
ctx.beginPath();var horn =5;// 画5个角var angle =360/ horn;// 五个角的度数// 两个圆的半径varR=200;var r =80;// 坐标var x =200;var y =200;for(var i =0; i < horn; i++){
// 角度转弧度:角度/180*Math.PI// 外圆顶点坐标
ctx.lineTo(Math.cos((18+ i * angle)/180* Math.PI)*R+ x,-Math.sin((18+ i * angle)/180* Math
.PI)*R+ y);// 內圆顶点坐标
ctx.lineTo(Math.cos((54+ i * angle)/180* Math.PI)* r + x,-Math.sin((54+ i * angle)/180* Math
.PI)* r + y);}// closePath:关闭路径,将路径的终点与起点相连
ctx.closePath();
ctx.lineWidth ="3";
ctx.fillStyle ='#E4EF00';//填充颜色
ctx.strokeStyle ="red";//边框颜色
ctx.fill();
ctx.stroke();}</script></body></html>