1.Canvas画直线
1.1 步骤
1.获取画布
2.获取画布的上下文
3.开始一条路径
4.确定起始点
5.确定结束点
6.着色
7.结束路径
1.2 代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> //获取canvas标签 var canvas =document.getElementById("canvas"); //获取上下文对象 var c=canvas.getContext('2d') //开启一条路径 c.beginPath(); //确定起始点 c.moveTo(100,100) //到哪去,确定结束点 c.lineTo(300,300) /* 在着色之前设置颜色和线宽 */ //设置颜色 c.strokeStyle ='green'; //设置线宽 c.lineWidth = 3; //进行上色 c.stroke() //关闭路径 c.closePath() </script> </body> </html>
getContext("2d")对象是内建HTML5对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。这里只有”2d“这个属性,没有”3d“!!!
2. 直线连用和画虚线
2.1 代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') c.beginPath(); c.moveTo(100,100) c.lineTo(400,100) c.strokeStyle ='green'; c.lineWidth = 3; c.stroke() c.closePath() c.beginPath(); c.moveTo(400,100) c.lineTo(400,400) c.strokeStyle ='yellow'; c.lineWidth = 3; c.stroke() c.closePath() c.beginPath(); c.moveTo(400,400) c.lineTo(100,400) c.strokeStyle ='red'; c.lineWidth = 3; c.stroke() c.closePath() c.beginPath(); c.moveTo(100,400) c.lineTo(100,100) c.strokeStyle ='blue'; c.lineWidth = 3; c.stroke() c.closePath() </script> </body> </html>
2.2 直线封装
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') drawLine(100,100,400,100,'green',4) drawLine(400,100,400,400,'yellow',4) drawLine(400,400,100,400,'red',4) drawLine(100,400,100,100,'blue',4) /* 画直线 */ function drawLine(x1,y1,x2,y2,color,lineWidth){ c.beginPath(); c.moveTo(x1,y1) c.lineTo(x2,y2) c.strokeStyle =color; c.lineWidth = lineWidth; c.stroke() c.closePath() } </script> </body> </html>
2.3 直线连用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') /*直线可多次使用*/ c.beginPath(); c.moveTo(100,100) c.lineTo(400,100) c.lineTo(400,400) c.lineTo(100,400) c.lineTo(100,100) c.strokeStyle ='blue'; c.lineWidth = 2; c.stroke() c.closePath() </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') /*直线可多次使用*/ c.beginPath(); c.moveTo(100,100) c.lineTo(400,100) c.lineTo(400,400) // c.lineTo(100,400) c.lineTo(100,100) c.strokeStyle ='blue'; c.lineWidth = 2; c.stroke() c.closePath() </script> </body> </html>
2.4 画虚线
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //画虚线 for(let i=0;i<30;i++) { drawLine(100+10*i,100,105+10*i,100,'red',2) drawLine(100+10*i,100+10*i,105+10*i,100+10*i,'green',2) drawLine(100+10*i,100+5*i,105+10*i,100+5*i,'green',2) } function drawLine(x1,y1,x2,y2,color,lineWidth){ c.beginPath(); c.moveTo(x1,y1) c.lineTo(x2,y2) c.strokeStyle =color; c.lineWidth = lineWidth; c.stroke() c.closePath() } </script> </body> </html>
3. 随机统计图和圆和圆弧
3.1 随机统计图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //画坐标系 c.beginPath(); c.moveTo(100,100); c.lineTo(100,400); c.lineTo(400,400); c.stroke(); c.closePath(); //画矩形 //fillRect(x,y,width,height) 绘制实心矩形 //strokeRect(x,y,width,height) 绘制空心矩形 for(let i=0;i<6;i++) { let height =Math.random()*280+10 c.fillStyle='#'+parseInt(Math.random()*0xffffff).toString(16); c.fillRect(120+40*i,400-height,20,height); } </script> </body> </html>
3.2 清除画布
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') c.rect(100,100,300,200) //填充 c.fillStyle='green'; c.fill() //描边 c.strokeStyle='red'; c.lineWidth=5; c.stroke(); //清除画布 /** * clearRect(x,y,width,height); 清除画布 * x 矩形左上角的 x 坐标 * y 矩形左上角的 y 坐标 * width 矩形的宽度,以像素计 * height 矩形的高度,以像素计 ** / */ c.clearRect(120,120,120,100); </script> </body> </html>
3.3 画圆
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') /** * x,y 描述弧的圆形的圆心的坐标 * radius 描述弧的圆形的半径 * startAngle,endAngle * 沿着圆指定弧的开始点和结束点的一个角度。这个角度用弧度来衡量。 * 沿着x轴正半轴的三点钟方向的角度为,角度沿着逆时针方向而增加。 * counterclockwise 弧沿着圆周的逆时针方向(TRUE)还是顺时针方向(FALSE)遍历。 * **/ c.arc(250, 250, 200, 0, Math.PI *2, false); c.fillStyle='gold'; c.fill(); c.lineWidth=3; c.strokeStyle='blue' c.stroke(); </script> </body> </html>
3.4 画弧
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') /** * x,y 描述弧的圆形的圆心的坐标 * radius 描述弧的圆形的半径 * startAngle,endAngle * 沿着圆指定弧的开始点和结束点的一个角度。这个角度用弧度来衡量。 * 沿着x轴正半轴的三点钟方向的角度为,角度沿着逆时针方向而增加。 * counterclockwise 弧沿着圆周的逆时针方向(TRUE)还是顺时针方向(FALSE)遍历。 * **/ c.beginPath(); c.arc(100, 100, 100, 0, Math.PI, true); c.stroke(); c.beginPath(); c.arc(300, 100, 100, 0, Math.PI, false); c.stroke(); c.beginPath(); c.arc(250, 250, 100, 0, Math.PI*2, false); c.stroke(); c.beginPath(); c.arc(250, 250, 80, 0, Math.PI*2, false); c.stroke(); c.beginPath(); c.arc(250, 250, 120, 0, Math.PI*2, false); c.stroke(); </script> </body> </html>
4.实例画茶杯
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //画杯身 c.lineWidth=4 c.strokeStyle='pink' c.strokeRect(100,200,200,200) //画把手 drawCirle(300,300,50,10,false,'orange') function drawCirle(x,y,r,width,flag,color){ c.beginPath(); c.arc(x,y,r, -Math.PI/2, Math.PI/2, flag); c.lineWidth=width; c.strokeStyle=color || "#000" c.stroke(); } // 画气 // drawCirle(120,120,20,1,true) // drawCirle(120,160,20,1,false) for(let i=0;i<5;i++) { drawCirle(120+i*40,120,20,1,true,'gray') drawCirle(120+i*40,160,20,1,false,'gray') } </script> </body> </html>
5.碰撞检测
5.1 圆形统计图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') var deg=Math.PI*2/360; var count=0; var timer=setInterval(function(){ count++; c.beginPath(); c.arc(250,250,200, -Math.PI/2, count*deg-Math.PI/2, false); c.lineWidth=5; c.strokeStyle="#f00" c.stroke(); if(count==360){ clearInterval(timer); } },10) </script> </body> </html>
5.2 碰撞检测
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //画布的宽高 var w=h=500; var x=100; var y=100; var r=30; //水平速度 xSpeed=5 //垂直速度 ySpeed=3 setInterval(function(){ //清除画布 c.clearRect(0, 0, w, h); /** * 在小球碰到边缘时,速度取相反即可!! * **/ if(x-r<=0 || x+r>=w) { xSpeed=-xSpeed; } //水平运动 x+=xSpeed; if(y-r<=0 || y+r>=h) { ySpeed=-ySpeed; } y+=ySpeed drawCirle(x,y,r); },10) /** * 画小球 * */ function drawCirle(x,y,r){ c.beginPath(); c.arc(x, y, r, 0, Math.PI*2, false); c.fillStyle ='orange' c.fill(); } </script> </body> </html>
5.3面向对象的小球
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //画布的宽高 var w=h=500; /** * 面向对象 * **/ function r(num){ return Math.random()*num; } /** * x:起始x坐标 * y:起始y坐标 * r:小球半径 * color:小球颜色 * xSpeed:x水平速度 * ySpeed:y水平速度 * **/ function Ball() { this.x=r(5)+60; this.y=r(3)+60; this.r=r(50)+10; this.color='#'+parseInt(Math.random()*0xffffff).toString(16); this.xSpeed=r(3)+2; this.ySpeed=r(3)+1; } //定义小球显示方法 Ball.prototype.show =function(){ c.beginPath(); c.arc(this.x, this.y, this.r, 0, Math.PI*2, false); c.fillStyle =this.color; c.fill(); }; //定义小球运动的方法(碰撞检测) Ball.prototype.change=function(){ if(this.x-this.r<=0 || this.x+this.r>=w) { this.xSpeed=-this.xSpeed; } //水平运动 this.x+=this.xSpeed; if(this.y-this.r<=0 || this.y+this.r>=h) { this.ySpeed=-this.ySpeed; } this.y+=this.ySpeed }; //创建一个小球的数组 let barrArr=[]; //创造小球 for(let i=0;i<50;i++) { let ball =new Ball(); //放入数组 barrArr.push(ball) ball.show(); } //小球运动 setInterval(function(){ //清除画布 c.clearRect(0,0,w,h); for(let i=0;i<barrArr.length;i++) { let ball=barrArr[i]; //更新坐标,显示小球 ball.change(); ball.show(); } },10) </script> </body> </html>
6. 画文字
6.1画文字与空心文字
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') /** * fillText()方法在画布上绘制填色的文本,默认颜色是黑色。 * fillText(text,x,y,maxWidth); * text 规定在画布上输出的文本 * x 开始绘制文本的x坐标位置(相对于画布)。 * y 开始绘制文本的y坐标位置(相对于画布)。 * maxWidth 可选,允许的最大文本宽度,以像素计。 * **/ //画文字 //设置字体相关样式 //font c.font='100px 微软雅黑' //设置文字颜色 c.fillStyle='blue' c.fillText('Hello', 200, 250); /** * 绘制空心文字 * * **/ c.strokeStyle='red' c.strokeText('你好', 0, 250); c.fillStyle='gold'; c.fillText('Hello canvas!',100,100,300); </script> </body> </html>
6.2 画线性渐变文字
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') c.font='100px 微软雅黑' //设置线性渐变 var gradient=c.createLinearGradient(0, 0, canvas.width, 0); gradient.addColorStop("0", 'yellow'); gradient.addColorStop("0.5", 'blue'); gradient.addColorStop("1.0", 'red'); //应用渐变 c.strokeStyle=gradient; c.strokeText('胡歌 林依晨', 0,300,500); c.fillStyle=gradient; c.fillText('天外飞仙', 0,100,400); </script> </body> </html>
6.3 文字位置
textAlign属性根据锚点,设置或返回文本内容的当前对齐方式。
textBaseline属性设置或返回在绘制文本时的当前文本基础。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') /** * 画直线 * **/ c.beginPath(); c.moveTo(250,0); c.lineTo(250,500); c.stroke(); c.closePath(); c.beginPath(); c.moveTo(0,250); c.lineTo(500,250); c.stroke(); c.closePath(); c.font='40px 微软雅黑'; c.textAlign='center'; c.textBaseline='middle'; c.fillText('射雕英雄传',250,250); /** * 水平文字的属性值 textAlign:start end left right center * 垂直文字的属性值 textBaseline:top Bottom Middle Alphabetic Hanging * * **/ </script> </body> </html>
6.4 米字格
第一种方法:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //画垂直和水平线 drawLine(0,250,500,250); drawLine(250,0,250,500); //画虚线 for(let i=0;i<500;i+=10) { drawLine(i+0,i+0,i+5,i+5); } for(let i=0;i<500;i+=10) { drawLine(500-i,0+i,495-i,5+i); } //画文字 drawText('米',250,250) /** * 画直线 * **/ function drawLine(x1,y1,x2,y2,color) { c.beginPath(); c.moveTo(x1,y1); c.lineTo(x2,y2); c.strokeStyle=color || '#000'; c.stroke(); c.closePath(); } //画文字 function drawText(text,x,y) { c.font='400px 楷体'; c.textAlign='center'; c.textBaseline='middle'; c.fillText(text,x,y); } </script> </body> </html>
第二种方法:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //画垂直和水平线 drawLine(0,250,500,250); drawLine(250,0,250,500); //画虚线 c.setLineDash([5,5]); drawLine(0,0,500,500); drawLine(0,500,500,0); //画文字 drawText('米',250,250) /** * 画直线 * **/ function drawLine(x1,y1,x2,y2,color) { c.beginPath(); c.moveTo(x1,y1); c.lineTo(x2,y2); c.strokeStyle=color || '#000'; c.stroke(); c.closePath(); } //画文字 function drawText(text,x,y) { c.font='400px 楷体'; c.textAlign='center'; c.textBaseline='middle'; c.fillText(text,x,y); } </script> </body> </html>
7.画图片
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //绘制图片 /** * drawImage() * 在画布上绘制图像、画布或视频。 * 也能够绘制图像的某些部分,以及/或者增加或减少图像的尺寸。 * 语法1: * 在画布上定位图像: * context.drawImage(img,x,y) * 语法2: * 在画布上定位图像,并规定图像的宽度和高度: * context.drawImage(img,x,y,width,height); * 语法3: * 剪切图像,并在画布上定位被剪切的部分: * context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height); * img 规定要使用的图像、画布或视频 * sx 可选。开始剪切的x坐标位置。 * sy 可选。开始剪切的y坐标位置。 * swidth 可选。被剪切图像的宽度 * sheight 可选。被剪切图像的高度。 * x 在画布上放置图像的x坐标位置。 * y 在画布上放置图像的y坐标位置。 * width 可选。要使用的图像的宽度。(伸展或缩少图像) * height 可选。要使用的图像的高度。(伸展或缩小图像) * * * **/ //绘制图片 let img = new Image(); img.src='squre.jpg'; /** * onload:图片加载成功之后触发 * onerror:图片加载失败后触发 * **/ img.οnlοad=function() { /** * 获取当前图片的实际宽高 * **/ console.log(img.width,img.height) /** * 1.语法:在画布上定位图像: context.drawImage(img,x,y) * **/ //c.drawImage(img,100,100); /** *语法2: * 在画布上定位图像,并规定图像的宽度和高度: * context.drawImage(img,x,y,width,height); * **/ //c.drawImage(img,0,0,400,250); /** * 语法3: * 剪切图像,并在画布上定位被剪切的部分: * context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height); * **/ c.drawImage(img,0,0,100,100,250,250,100,100); } </script> </body> </html>
8. 线性小球
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{margin: 0;padding: 0;} </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //画布的宽高 let w=document.documentElement.clientWidth-6; let h=document.documentElement.clientHeight-6; //更新canvas的宽高 canvas.width=w; canvas.height=h; /** * 面向对象 * **/ function r(num){ return Math.random()*num; } /** * x:起始x坐标 * y:起始y坐标 * r:小球半径 * color:小球颜色 * xSpeed:x水平速度 * ySpeed:y水平速度 * **/ function Ball(text) { this.x=r(380)+60; this.y=r(380)+60; this.r=r(20)+10; this.color='#'+parseInt(Math.random()*0xffffff).toString(16); this.xSpeed=r(3)+2; this.ySpeed=r(3)+1; //接受外部参数 this.text=text } //定义小球显示方法 Ball.prototype.show =function(){ drawCirle(this.x,this.y,this.r,this.color)//画小球 drawText(this.text,this.x+this.r+5,this.y)//画文字 // c.beginPath(); // c.arc(this.x, this.y, this.r, 0, Math.PI*2, false); // c.fillStyle =this.color; // c.fill(); }; //定义小球运动的方法(碰撞检测) Ball.prototype.change=function(){ if(this.x-this.r<=0 || this.x+this.r>=w) { this.xSpeed=-this.xSpeed; } //水平运动 this.x+=this.xSpeed; if(this.y-this.r<=0 || this.y+this.r>=h) { this.ySpeed=-this.ySpeed; } this.y+=this.ySpeed }; let titleArr='JavaScript HTML前端 JAVA Python C++ VUE React NodeJs'.split(" ") //创建一个小球的数组 let barrArr=[]; //创造小球 for(let i=0;i<8;i++) { let ball =new Ball(titleArr[i]);//传入title //放入数组 barrArr.push(ball) ball.show(); /** * 小球连线 A球和B球 * **/ for(let j=0;j<i;j++) { let preBall=barrArr[j]; drawLine(ball.x,ball.y,preBall.x,preBall.y); } } //小球运动 setInterval(function(){ //清除画布 c.clearRect(0,0,w,h); for(let i=0;i<barrArr.length;i++) { let ball=barrArr[i]; //更新坐标,显示小球 ball.change(); ball.show(); for(let j=0;j<i;j++) { let preBall=barrArr[j]; drawLine(ball.x,ball.y,preBall.x,preBall.y); } } },10) function drawLine(x1,y1,x2,y2,color) { c.beginPath(); c.moveTo(x1,y1); c.lineTo(x2,y2); c.strokeStyle=color || '#000'; c.stroke(); c.closePath(); } //画文字 function drawText(text,x,y) { c.font='20px 楷体'; c.textAlign='top'; c.textBaseline='middle'; c.fillText(text,x,y); } function drawCirle(x,y,r,color){ c.beginPath(); c.arc(x, y, r, 0, Math.PI*2, false); c.fillStyle =color||'#000' c.fill(); } </script> </body> </html>
9.炫彩小球
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{margin: 0;padding: 0;} </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //画布的宽高 let w=document.documentElement.clientWidth-6; let h=document.documentElement.clientHeight-6; //更新canvas的宽高 canvas.width=w; canvas.height=h; /** * 面向对象 * **/ function r(num){ return Math.random()*num; } /** * x:起始x坐标 * y:起始y坐标 * r:小球半径 * color:小球颜色 * xSpeed:x水平速度 * ySpeed:y水平速度 * **/ function Ball(x,y) { this.x=x; this.y=y; this.r=30; this.color='rgb('+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+','+parseInt(Math.random()*255) } //定义小球显示方法 Ball.prototype.show =function(){ this.r--;//半径越来越小 drawCirle(this.x,this.y,this.r,this.color);//画小球 }; let ballArr=[]; //鼠标滑动事件 window.οnmοusemοve=function(ev){ let ball=new Ball(ev.x,ev.y) ballArr.push(ball); ball.show(); } //小球数组 setInterval(function(){ //清除画布 c.clearRect(0, 0, w, h); for(let i=0;i<ballArr.length;i++) { /** * 判断:如果小球半径r已经小于0,需要从数组中删除 * .splice(位置,1) * **/ let ball=ballArr[i]; if(ball.r<=0) { ballArr.splice(i,1) } else { ball.show(); } } ball.show(); },10); function drawCirle(x,y,r,color){ c.beginPath(); c.arc(x, y, r, 0, Math.PI*2, false); c.fillStyle =color||'#000' c.fill(); } </script> </body> </html>
10. 画时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .clock{ width: 400px; margin: 100px auto; background: #ddd; border-radius: 20px; } </style> </head> <body> <div class="clock"> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas id="canvas" height="400px" width="400px">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> </div> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //定义基础变量 let w=h=400;//时钟宽高 let x=y=200;//时钟中心坐标 let r=180;//时钟半径 let r_hour=60//时针长度 let r_minute=120//分针长度 let r_second=140//秒针长度 let r_text=140;//定义表盘文字的半径 let r_square=165//刻度 let r_circle=10;//表盘小圆点 let deg=2*Math.PI;//定义基本的圆周 /** * 面向对象 * **/ //平移中心点 c.translate(w/2, h/2); //画圆盘 drawCirle(0,0,r,'#fff') //画时钟的表针 //获取当前事件 let time=new Date(); let hour=time.getHours()*(deg/12)-deg/4; let minute=time.getMinutes()*(deg/60)-deg/4; let second=time.getSeconds()*(deg/60)-deg/4; drawLine(0,0,r_hour*Math.cos(hour),r_hour*Math.sin(hour),'#000',10) drawLine(0,0,r_minute*Math.cos(minute),r_minute*Math.sin(minute),'#000',5) drawLine(0,0,r_second*Math.cos(second),r_second*Math.sin(second),'#f00',2) //画表钉 drawCirle(0,0,r_circle,'#000') //画数字 for(let i=1;i<=12;i++) { /** * 计算圆周坐标 * x=x+r*cos(a) * y=y+r*sin(a) * **/ let a=((Math.PI*2)/12)*i-Math.PI/2 let x=r_text*Math.cos(a) let y=r_text*Math.sin(a) drawText(i,x,y) } //画刻度 for(let i=1;i<=60;i++) { let a=((Math.PI*2)/60)*i-Math.PI/2 let x1=r*Math.cos(a) let y1=r*Math.sin(a) /* 判断是否为整点 **/ if(i%5==0) { let x2=r_square*Math.cos(a) let y2=r_square*Math.sin(a) drawLine(x1,y1,x2,y2,'#999',3) } else { let x2=(r_square+4)*Math.cos(a) let y2=(r_square+4)*Math.sin(a) drawLine(x1,y1,x2,y2,'#aaa',2) } } function clock(){ //画圆盘 drawCirle(0,0,r,'#fff') //画时钟的表针 //获取当前事件 let time=new Date(); let hour=time.getHours()*(deg/12)-deg/4; let minute=time.getMinutes()*(deg/60)-deg/4; let second=time.getSeconds()*(deg/60)-deg/4; drawLine(0,0,r_hour*Math.cos(hour),r_hour*Math.sin(hour),'#000',10) drawLine(0,0,r_minute*Math.cos(minute),r_minute*Math.sin(minute),'#000',5) drawLine(0,0,r_second*Math.cos(second),r_second*Math.sin(second),'#f00',2) //画表钉 drawCirle(0,0,r_circle,'#000') //画数字 for(let i=1;i<=12;i++) { /** * 计算圆周坐标 * x=x+r*cos(a) * y=y+r*sin(a) * **/ let a=((Math.PI*2)/12)*i-Math.PI/2 let x=r_text*Math.cos(a) let y=r_text*Math.sin(a) drawText(i,x,y) } //画刻度 for(let i=1;i<=60;i++) { let a=((Math.PI*2)/60)*i-Math.PI/2 let x1=r*Math.cos(a) let y1=r*Math.sin(a) /* 判断是否为整点 **/ if(i%5==0) { let x2=r_square*Math.cos(a) let y2=r_square*Math.sin(a) drawLine(x1,y1,x2,y2,'#999',3) } else { let x2=(r_square+4)*Math.cos(a) let y2=(r_square+4)*Math.sin(a) drawLine(x1,y1,x2,y2,'#aaa',2) } } } setInterval(function() { clock(); },1000) function drawLine(x1,y1,x2,y2,color,width) { c.beginPath(); c.moveTo(x1,y1); c.lineTo(x2,y2); c.lineWidth=width; c.strokeStyle=color || '#000'; c.lineCap='round' c.stroke(); c.closePath(); } //画文字 function drawText(text,x,y) { c.font='bold 26px 微软雅黑'; c.fillStyle='#000' c.textAlign='center'; c.textBaseline='middle'; c.fillText(text,x,y); } function drawCirle(x,y,r,color){ c.beginPath(); c.arc(x, y, r, 0, Math.PI*2, false); c.fillStyle =color||'#000' c.fill(); } </script> </body> </html>
11. 像素操作和像素坐标
putImageData() 返回ImageData对象,该对象拷贝了画布指定矩形的像素数据。 对于ImageData对象中的每个像素,都存在着四方面的信息,即RGBA值: R- 红色(0-255) G- 绿色(0-255) B- 蓝色(0-255) A- alpha 通道(0-255;0是透明的,255是完全可见的) color/alpha 以数组形式存在,并存储于ImageData对象的data属性中。 let imgData=context.getImageData(x,y,width,height);
参数 | 描述 |
---|---|
x | 开始复制的左上角位置的x坐标 |
y | 开始复制的左上角位置的y坐标 |
width | 将要复制的矩形区域的宽度 |
height | 将要复制的矩形区域的高度 |
putImageData() putImageData(imgData,x,y,dirtyX,dirtyY,dirtyWidth,dirtyHeight);
参数 | 描述 |
---|---|
imgData | 规定要放回画布的ImageData对象 |
x | ImageData对象左上角的x坐标,以像素计 |
y | ImageData对象左上角的y坐标,以像素计 |
dirtyX | 可选。水平值(x),以像素计,在画布上放置图像的位置 |
dirtyY | 可选。水平值(y),以像素计,在画布上放置图像的位置 |
dirtyWidth | 可选。在画布上绘制图像所使用的宽度。 |
dirtyHeight | 可选。在画布上绘制图像所使用的高度 |
11.1 复制图片
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //创建图片 let img = new Image(); // img.crossOrigin='' img.src='squre.jpg'; img.οnlοad=function(){ c.drawImage(img,0,0,250,250); //绘制图片 // c.drawImage(img,0,0); //获取像素点 let copy = c.getImageData(100,100,50,50); console.log(copy) c.putImageData(copy,0,350); } </script> </body> </html>
11.2 像素动态文字
11.2.1 静态像素文字
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') const w=h=500; //画文字 drawText("心♥",w/2,h/2); //获取像素点 let copy=c.getImageData(0,0,w,h) //清除画布 c.clearRect(0,0,w,h); //筛选有效像素点 let leap=3; for(let y=0;y<h;y+=leap){ for(let x=0;x<w;x+=leap) { let index=x+y*w; let a=copy.data[index*4+3] if(a>128) { drawCirle(x,y,1,'#f00') } } } function drawCirle(x,y,r,color){ c.beginPath(); c.arc(x, y, r, 0, Math.PI*2, false); c.fillStyle =color||'#000' c.fill(); } function drawText(text,x,y) { c.font='bold 150px 微软雅黑'; c.fillStyle='#000' c.textAlign='center'; c.textBaseline='middle'; c.fillText(text,x,y,w); } </script> </body> </html>
11.2.2 动态像素文字
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') const w=h=500; //画文字 drawText("胡歌",w/2,h/2); //获取像素点 let copy=c.getImageData(0,0,w,h) //清除画布 c.clearRect(0,0,w,h); //筛选有效像素点 //产生随机数 function r(num){ return parseInt(Math.random()*num); } //运动次数 const times=100; function Ball(xEnd,yEnd) { //起始位置 this.xStart=r(w); this.yStart=r(h); //结束位置 this.xEnd=xEnd; this.yEnd=yEnd; //计算运动速度,通过起始点和结束点 this.xSpeed=(this.xEnd-this.xStart)/times; this.ySpeed=(this.yEnd-this.yStart)/times; //小球运动的中心点 this.x=this.xStart; this.y=this.yStart; //设置半径和颜色 this.r=2; this.color='#f00'; } Ball.prototype.show=function(){ //计算每次运动坐标位置 this.x+=this.xSpeed; this.y+=this.ySpeed; drawCirle(this.x,this.y,this.r,this.color); } let leap=10; let ballArr=[]; for(let y=0;y<h;y+=leap){ for(let x=0;x<w;x+=leap) { let index=x+y*w; let a=copy.data[index*4+3] if(a>128) { let ball=new Ball(x,y); ballArr.push(ball); ball.show(); drawCirle(x,y,2,'#f00') } } } let count=0; let timer=setInterval(function(){ count++; //清除画布 c.clearRect(0,0,w,h); for(let i=0;i<ballArr.length;i++) { ballArr[i].show(); } /* 判断何时停止 **/ if(count+1==times) { clearInterval(timer); } },50) function drawText(text,x,y) { c.font='bold 250px 微软雅黑'; c.fillStyle='#000' c.textAlign='center'; c.textBaseline='middle'; c.fillText(text,x,y,w); } function drawCirle(x,y,r,color){ c.beginPath(); c.arc(x, y, r, 0, Math.PI*2, false); c.fillStyle =color||'#000' c.fill(); } </script> </body> </html>
11.3 彩色心
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') const w=h=500; //画文字 drawText("胡歌",w/2,h/2); //获取像素点 let copy=c.getImageData(0,0,w,h) //清除画布 c.clearRect(0,0,w,h); //筛选有效像素点 //产生随机数 function r(num){ return parseInt(Math.random()*num); } //运动次数 const times=100; function Ball(xEnd,yEnd) { //起始位置 this.xStart=r(w); this.yStart=r(h); //结束位置 this.xEnd=xEnd; this.yEnd=yEnd; //计算运动速度,通过起始点和结束点 this.xSpeed=(this.xEnd-this.xStart)/times; this.ySpeed=(this.yEnd-this.yStart)/times; //小球运动的中心点 this.x=this.xStart; this.y=this.yStart; //设置半径和颜色 this.r=2; this.color='#f00'; //设置填充文字和颜色 this.text='♥' this.color='rgb('+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+','+parseInt(Math.random()*255) } Ball.prototype.show=function(){ //计算每次运动坐标位置 this.x+=this.xSpeed; this.y+=this.ySpeed; // drawCirle(this.x,this.y,this.r,this.color); drawFillText(this.text,this.x,this.y,this.color); } let leap=10; let ballArr=[]; for(let y=0;y<h;y+=leap){ for(let x=0;x<w;x+=leap) { let index=x+y*w; let a=copy.data[index*4+3] if(a>128) { let ball=new Ball(x,y); ballArr.push(ball); ball.show(); // drawCirle(x,y,2,'#f00') } } } let count=0; let timer=setInterval(function(){ count++; //清除画布 c.clearRect(0,0,w,h); for(let i=0;i<ballArr.length;i++) { ballArr[i].show(); } /* 判断何时停止 **/ if(count+1==times) { clearInterval(timer); } },50) function drawText(text,x,y) { c.font='bold 250px 微软雅黑'; c.fillStyle='#000' c.textAlign='center'; c.textBaseline='middle'; c.fillText(text,x,y,w); } function drawCirle(x,y,r,color){ c.beginPath(); c.arc(x, y, r, 0, Math.PI*2, false); c.fillStyle =color||'#000' c.fill(); } function drawFillText(text,x,y,color){ c.font='12px 微软雅黑'; c.fillStyle=color; c.textAlign='center'; c.textBaseline='middle'; c.fillText(text,x,y,w); } </script> </body> </html>
12.画矩形和统计图
12.1 画矩形
rect(x,y,width,height);绘制矩形 x 矩形左上角的x坐标 y 矩形左上角的y坐标 width 矩形的宽度,以像素计 height 矩形的高度,以像素计 fillRect(x,y,width,height);绘制实心矩形 strokeRect(x,y,width,height);绘制空心矩形
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') c.rect(10, 10, 200, 200); /** * 如果需要描边和填充时,先填充再描边 */ //填充 c.fillStyle='orange' c.fill(); //描边 c.strokeStyle='gold'; c.lineWidth=5; c.stroke(); //绘制实心矩形 c.beginPath(); c.fillStyle='blue' c.fillRect(220,220,50,50) c.closePath(); //绘制描边矩形 c.strokeStyle='yellow' c.lineWidth=4 c.strokeRect(300,300,100,100) </script> </body> </html>
12.2 画统计图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> canvas{ margin: 0 auto; border: 2px solid #aaa; display: block; } </style> </head> <body> <!--canvas在低版本浏览器中不兼容,需要提示用户---> <!--如果让canvas左右居中,使用display设置为block--> <canvas width="500" height="500" id="canvas">您的浏览器版本过低,请升级浏览器或者使用chrome打开!</canvas> <script> var canvas =document.getElementById("canvas"); var c=canvas.getContext('2d') //画坐标系 c.beginPath(); c.moveTo(100,100); c.lineTo(100,400); c.lineTo(400,400); c.stroke(); c.closePath(); //画矩形 c.fillRect(200,200,20,200); </script> </body> </html>