原文链接: http://www.shitu91.com/cms/canvasSub/index.html

01.canvas简单的认识


canvas 是html5提供给我们的一个绘图标签 默认大小 300X150 背景透明

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas简单的认识</title>
</head>
<body>
<canvas id="myCanvas" style="border:1px solid #ddd;display:block;margin:20px auto;"></canvas> </body>
</html>

效果图 如下 查看案例

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUMAAACnCAYAAACLkT4nAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAINSURBVHhe7dRBDcAwEMCw8edYLLdAaB97dLKlUMgzAIwZAsQMAWKGADFDgJghQLZmuNaSpKs6tT1DgFuYIUDMECBmCBAzBIgZAsQMAWKGADFDgJghQMwQIGYIEDMEiBkCxAwBYoYAMUOAmCFAzBAgZggQMwSIGQLEDAFihgAxQ4CYIUDMECBmCBAzBIgZAsQMAWKGADFDgJghQMwQIGYIEDMEiBkCxAwBYoYAMUOAmCFAzBAgZggQMwSIGQLEDAFihgAxQ4CYIUDMECBmCBAzBIgZAsQMAWKGADFDgJghQMwQIGYIEDMEiBkCxAwBYoYAMUOAmCFAzBAgZggQMwSIGQLEDAFihgAxQ4CYIUDMECBmCBAzBIgZAsQMAWKGADFDgJghQMwQIGYIEDMEiBkCxAwBYoYAMUOAmCFAzBAgZggQMwSIGQLEDAFihgAxQ4CYIUDMECBmCBAzBIgZAsQMAWKGADFDgJghQMwQIGYIEDMEiBkCxAwBYoYAMUOAmCFAzBAgZggQMwSIGQLEDAFihgAxQ4CYIUDMECBmCBAzBIgZAsQMAWKGADFDgJghQMwQIGYIEDMEiBkCxAwBYoYAMUOAmCFAzBAgZggQMwSIGQLEDAFihgAxQ4CYIUDMECBmCBAzBIgZAuTTGUrSTZ3amiHA35khQMwQIGYIEDMEiBkCxAwBYoYAMUOAmXkBjIFSF73FG84AAAAASUVORK5CYII=" alt="" />

02.绘制 一个带有填充颜色的 矩形相关代码


        var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300"; var cxt=myCanvas.getContext("2d"); cxt.fillStyle="#f0f";
cxt.fillRect(50,50,200,100);

效果图 如下 查看案例

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAf8AAAE1CAYAAADpgulsAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAW4SURBVHhe7dWxTUQBFANB+u/x13KI8Im7gBDvjLQFOPLXCwBIcf4AEOP8ASDmnP/zPJIkaaRPfp0/APD/OX8AiHH+ABDj/AEgxvkDQIzzB4AY5w8AMc4fAGKcPwDEOH8AiHH+ABDj/AEgxvkDQIzzB4AY5w8AMc4fAGKcPwDEOH8AiHH+ABDj/AEgxvkDQIzzB4AY5w8AMc4fAGKcPwDEOH8AiHH+ABDj/AEgxvkDQIzzB4AY5w8AMc4fAGKcPwDEOH8AiHH+ABDj/AEgxvkDQIzzB4AY5w8AMc4fAGKcPwDEOH8AiHH+ABDj/AEgxvkDQIzzB4AY5w8AMc4fAGKcPwDEOH8AiGme/88ySe8D5jl/STdgnvOXdAPmOX9JN2Ce85d0A+Y5f0k3YJ7zl3QD5jl/STdgnvOXdAPmOX9JN2Ce85d0A+Y5f0k3YJ7zl3QD5jl/STdgnvOXdAPmOX9JN2Ce85d0A+Y5f0k3YJ7zl3QD5jl/STdgnvOXdAPmOX9JN2Ce85d0A+Y5f0k3YJ7zl3QD5jl/STdgnvOXdAPmOX9JN2Ce85d0A+Y5f0k3YJ7zl3QD5jl/STdgnvOXdAPmOX9JN2Ce85d0A+Y5f0k3YJ7zl3QD5jl/STdgnvOXdAPmOX9JN2Ce85d0A+Y5f0k3YJ7zl3QD5jl/STdgnvOXdAPmOX9JN2Ce85d0A+Y5f0k3YJ7zl3QD5jl/STdgnvOXdAPmOX9JN2Ce85d0A+Y5f0k3YJ7zl3QD5jl/STdgnvOXdAPmOX9JN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Ihx/gAQ4/wBIMb5A0CM8weAGOcPADHOHwBinD8AxDh/AIhx/gAQ4/wBIMb5A0CM8weAGOcPADHOHwBinD8AxPzp/CVJ0kafnPMHAPY5fwCIcf4AEOP8ASDl9foG2nnDJBjUV5cAAAAASUVORK5CYII=" alt="" />

03.画线


<script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300"; var cxt=myCanvas.getContext("2d");
//画三角形
cxt.moveTo(50,50);
cxt.lineTo(250,100);
cxt.lineTo(50,200);
cxt.lineTo(50,50); //画直线
cxt.moveTo(350,50);
cxt.lineTo(350,200); //定义画线样式
cxt.strokeStyle="red";
cxt.lineWidth="5"; cxt.lineCap="round"; cxt.stroke();

效果图 如下 查看案例

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAf8AAAE9CAYAAAAF0WsBAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAkcSURBVHhe7dndcVRHFIVRZ2BnAJk4BJOBQyAEkwHOiBCIgBgcwpixkI31x8zo7nu7e69V1S8SqEoPs79T8NMJAKgi/gBQRvwBoIz4A0AZ8QeAMuIPAGXEHwDK/Bv/L1++eJ7neZ63yHvJ/+IPAMxP/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCU6Yn/p0+n09u3p9O7d6fThw937/w1gNGct+l+p+wVAT3x/+OPr7/N11/n+/fLL3fHwJ9/nk6fP3/7gwAHe2qvzl+DjXTH/+FzDAAjEH/CeuL/22+PP0w/eo4B4A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qIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4Ay4g8AZcQfAMqIPwCUEX8AKCP+AFBG/AGgjPgDQBnxB4AyV8Xf8zzP87w13kv+jT8A0EH8AaCM+ANAGfEHgDLiDwBlxB8Ayog/AJQRfwCocjr9DUcfzuXVaikOAAAAAElFTkSuQmCC" alt="" />

04.画矩形


<script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300"; var cxt=myCanvas.getContext("2d");
//方法一 cxt.moveTo(100,50);
cxt.lineTo(300,50);
cxt.lineTo(300,200);
cxt.lineTo(100,200);
cxt.lineTo(100,50);
cxt.fill();
//cxt.stroke(); //方法二
// /cxt.strokeStyle="red";
cxt.fillStyle="red";
cxt.fillRect(0,0,300,40);
//cxt.strokeRect(0,0,300,40); </script>

效果图 如下 查看案例

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgUAAAE6CAYAAACGWCHoAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAg2SURBVHhe7dYxbh1ADEPBf/87+iyOIbVikiJNuDPA61QL/HwDAPwwCgCAYRQAAMMoAACGUQAADKMAABh/HAVfX1+SJKmk3/mrUfD9+TmTpP8hIDIKJL0VEBkFkt4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HqEkjYgMwoKXY9Q0gZkRkGh6xFK2oDMKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMIwCAGAYBQDAMAoAgGEUAADDKAAAhlEAAAyjAAAYRgEAMP7JKJAkSR39zh9HAQDwBqMAABhGAQAwjAIAYBgFAMAwCgCAH9/fvwDDHwegB0SQwgAAAABJRU5ErkJggg==" alt="" />

05.路径的开始与关闭


    <script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300"; var cxt=myCanvas.getContext("2d");
//方法一 cxt.moveTo(100,50);
cxt.lineTo(300,50);
cxt.lineTo(300,200);
cxt.lineTo(100,200);
cxt.lineTo(100,50);
cxt.fill(); //cxt.stroke(); //方法二
//cxt.strokeStyle="red";
//cxt.strokeRect(0,0,300,40); cxt.fillStyle="red";
cxt.fillRect(0,0,300,40);

效果图 如下 查看案例

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgEAAAE4CAYAAADCeyCZAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQA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CWCDiK8QeeTASQJQLSjD9wNxFAlghIMf7As4kAskTA3ow/sBsRQJYI2IvxB3YnAsgSAc9l/IEYEUCWCLiX8QfiRABZIuCpjD9wGBFAlgi4lfEHDicCyBIBv2L8gdOIALJEwGOMP3A6EUCWCPie8QeGIQLIEgH/Mf7AcEQAWSLA+APDEgFk9UbAX39t24sXxh8YmAggqzcCLow/MLTL4F872Ed3BABAMREAAKVEAACUEgEAUEoEAEApEQAApUQAAJQSAQBQSgQAQCkRAAClRAAAlBIBAFBKBABAKREAAKVEAACUEgEAUEoEAEApEQAApUQAAJQSAQBQSgQAQCkRAAClRAAAlBIBAFBKBABAKREAAKVEAACUEgEAUEoEAEApEQAApUQAAJQSAQBQSgQAQCkRAAClRAAAlBIBAFBKBABAKREAAKVEAACUEgEAUEoEAEApEQAApUQAAJQSAQBQSgQAQCkRAAClRAAAlBIBAFBKBABAKREAAKVEAACUEgEAUEoEAEApEQAApUQAAJQSAQBQSgQAQCkRAAClRAAAlBIBAFBKBABAKREAAKVEAACUEgEAUEoEAEApEQAApUQAAJQSAQBQSgQAQCkRAAClRAAAlBIBAFBKBABAKREAAKVEAACUEgEAUEoEAEApEQAApUQAAJQSAQBQSgQAQCkRAAClRAAAlBIBAFBKBABAKREAAKVEAACUEgEAUEoEAEApEQAApUQAAJQSAQBQSgQAQCkRAAClRAAAlBIBAFBKBABAKREAAKVEAACUuisCnHPOObfGPeZqBAAA6xMBAFBKBABAKREAAKVEAACUEgEAUGnb/gVkcb6k4OplwwAAAABJRU5ErkJggg==" alt="" />

06.canvas 画圆 画弧


<script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300"; var cxt=myCanvas.getContext("2d");
//空心圆
cxt.beginPath()
cxt.arc(250,150,100,0,Math.PI*2);
cxt.closePath();
cxt.stroke(); //空心圆
cxt.beginPath()
cxt.arc(250,150,50,0,Math.PI*2);
cxt.closePath();
cxt.fill(); //弧度
cxt.beginPath()
cxt.arc(250,150,130,Math.PI*3/2,Math.PI,true);
cxt.stroke();
cxt.closePath();

效果图 如下 查看案例

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhkAAAE9CAYAAACmz3A2AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAACH/SURBVHhe7d17iFbV/sfx0sw/vOSFIMcjKv6R0gXFE2gRiFpekErREcMktNRRETGRUfDShIhHGfECP4dBJcpKkRHGQQklCEkhCy+gpnKOGaPlBVOPhkjx/Z3PbsXx1G4anWfty9rvF3yhtWfmefaz9ne3vj5777UeMgAAAA8oMgAAgBcUGQAAwAuKDAAA4AVFBgAA8IIiAwAAeEGRAQAAvKDIAAAAXlBkAAAAL0peZPzzn/8kCIIgCCKQaAkvRQYAAMg/igwAAOAFRQYAAPCCIgMAAHhBkQEAALygyAAAAF5QZAAAAC8oMgAAgBcUGQAAwAuKDAAA4AVFBgAA8IIiAwAAeEGRAQAAvKDIAAAAXlBkAAAALygyAACAFxQZAADAC4oMAADgBUUGAADwgiIDQMlcvnzZTp06ZV988YXt2bPHPvnkE9u8ebNt2LDBVq9ebStWrLB3333Xli9f7v4CQMgoMgA0y61bt+zIkSO2c+fOqGCYM2eOvfrqqzZw4EArKyuzVq1aWdeuXe3JJ5+0QYMG2ciRI628vNymTp0a/e6CBQts8eLFtnTpUlu2bJl7VQAho8gA8AcqJt5///2oMBg1apT16tXL2rZta88884y99tprNn/+fFu3bp3t2rXLDh8+bBcuXLBffvnF/TUA/IoiAyg4nXMffvihzZ071wYPHmyPPvqoPfXUU/b666/bypUrbffu3ZyXAB4IRQZQMGfPnrXa2lqbPHly9A1F9+7dbfz48dElkM8//9xu377tfhMAWoYiAwjc3bt3rb6+Provom/fvlFRoQJDhcbp06fdbwFA6VFkAAHSUx56qmPs2LHR5Y+hQ4faqlWronstACApFBlAIK5duxZ9O6EbNXWTpp7s+OCDD+zHH390vwEAyaLIAHJOj5ROmDDBHn744aiw2L59e3SJBADSR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PgwcPRjNZajVRPWHRt2/f6H4FLV9+4MABu3HjhvvN9GlftE/aN+2j9lX7rH3XZ9BnAZAeigwATdKS5bW1tVZRUWGDBw+OptPu3bt3NJDr24KNGzfanj17orU8fBQgek29tt5D76X31HtrH7Qv2iftm/aR5dWBbKHIAHDfNFFVfX19tKy5BviRI0dGC4V16NAhuu+hT58+NmjQoKgYmDRpUjRTpooDzTuh2TOXLVtmy5cvj/5b2/Qz/Y5+V3+jv9Vr6LX0mnptvYfeS++p92bCMSD7KDIAlJS+eVABoEsVDQ0Ntm3btuipFs1DoUsYVVVVUZGh0H9rm36m39Hv6m/0t3qNLF2aAXD/KDIAAIAXFBkAAMALigwAAOAFRQYAAPCCIgMAAHhBkQEAALygyAAAAF5QZAAAAC8oMgAAgBcUGQAAwAuKDAAA4AVFBgAA8IIiAwAAeEGRAQAAvKDIAAAAXlBkAAAALygyAACAFxQZAADAC4oMAADgBUUGAADwgiIDAAB4QZEBAAC8oMgAAABeUGQAAAAvKDIAAIAXFBkAAMALigwAAOAFRQYAAPCCIgMAAHhBkQEAALygyAAAAF5QZAAAAC8oMgAAgBcUGQAAwAuKDAAA4AVFBgAA8IIiAwAAeEGRAQAAvKDIAAAAXlBkAAAALygyAACAFxQZAADAC4oMAADgBUUGAADwgiIDAAB4QZEBAAC8oMgAAABeUGQAAAAvKDIAAIAXmSwyCIIgCIIII1qi5EUGAACAUGQAAAAvKDIAAIAXFBkAAMALigwAAOAFRQYAAPDA7P8BHg0PLEKnZKIAAAAASUVORK5CYII=" alt="" />

07.canvas 添加图片 添加文字


<script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300"; var cxt=myCanvas.getContext("2d"); //添加图片
var img=new Image();
img.src="data:images/meizi.jpg";
img.onload=function(){
cxt.drawImage(img,85,40);
} //添加文字
cxt.font="30px Arial"; cxt.fillStyle="red";
cxt.fillText("我女朋友",380,290); cxt.strokeStyle="red";
cxt.strokeText("我女朋友",380,290);

效果图 如下 查看案例

08.canvas 橡皮擦 物体运动(本质是图形不断的擦除与绘制)


<script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300"; var cxt=myCanvas.getContext("2d"); //圆
var x=10,y=10;
var a=490,b=290;
var duration=2000;
var cishu=2000/30;
var xstep=(a-x)/cishu;
var ystep=(b-y)/cishu; function huayuan(x,y){
cxt.beginPath();
cxt.arc(x,y,10,0,Math.PI*2);
cxt.fillStyle="red";
cxt.fill();
} huayuan();
var timer=setInterval(function(){
//橡皮擦
cxt.clearRect(x-11,y-11,22,22); x+=xstep;
y+=ystep; if(x>=a){
x=a;
y=b;
}
huayuan(x,y); },30);

效果图 如下 查看案例

09. canvas 鼠标画笔


<script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300"; var cxt=myCanvas.getContext("2d"); var canvaL=myCanvas.offsetLeft;
var canvaT=myCanvas.offsetTop; myCanvas.onmousedown=function(e){
var ev=e||window.event;
var left=ev.clientX;
var top=ev.clientY;
var x=left-canvaL;
var y=top-canvaT; cxt.moveTo(x,y);
myCanvas.onmousemove=function(e){
var ev=e||window.event;
var left=ev.clientX;
var top=ev.clientY;
var x=left-canvaL;
var y=top-canvaT;
cxt.lineTo(x,y); cxt.stroke(); } myCanvas.onmouseup=function(){
myCanvas.onmouseup=null;
myCanvas.onmousemove=null;
}
myCanvas.onmouseout=function(){
myCanvas.onmouseup=null;
myCanvas.onmousemove=null;
myCanvas.onmouseout=null;
} }

效果图 如下 查看案例

10.canvas 渐变线性渐变


<script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300";
var ctx=myCanvas.getContext("2d"); //创建线性渐变对象
var lg=ctx.createLinearGradient(10,10,210,110);
lg.addColorStop(0,"red");
lg.addColorStop(1,"blue"); //带线性渐变矩形
ctx.fillStyle=lg;
ctx.fillRect(10,10,200,100); //带线性渐变圆
var lg1=ctx.createLinearGradient(80,130,140,200);
lg1.addColorStop(0,"green");
lg1.addColorStop(1,"yellow"); ctx.beginPath();
ctx.arc(105,160,50,0,Math.PI*2);
ctx.fillStyle=lg1;
ctx.fill();
ctx.closePath(); </script>

效果图 如下 查看案例

11.canvas 渐变之径向渐变


<script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300";
var ctx=myCanvas.getContext("2d"); //创建径向渐变对象
var rg=ctx.createRadialGradient(110,110,1,110,110,110); rg.addColorStop(0,"#f00");
rg.addColorStop(0.5,"rgb(238,182,231)");
rg.addColorStop(1,"blue"); //带径向渐变矩形
ctx.fillStyle=rg;
ctx.fillRect(10,10,200,200); //创建径向渐变的圆 var rg1=ctx.createRadialGradient(310,61,1,310,111,100);
rg1.addColorStop(0,"#fff");
rg1.addColorStop(1,"black"); ctx.beginPath();
ctx.arc(310,111,100,0,Math.PI*2);
ctx.fillStyle=rg1;
ctx.fill();
ctx.closePath(); </script>

效果图 如下 查看案例

12.canvas 填充--图案


<script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300";
var ctx=myCanvas.getContext("2d"); var img=new Image();
img.src="data:images/flower.jpg"; img.onload=function(){
var tuan=ctx.createPattern(img,"repeat"); ctx.fillStyle=tuan;
ctx.fillRect(0,0,500,300);
}
</script>

效果图如下:查看案例

13.canvas 图片 之深入谈


<script>
var myCanvas=document.getElementById("myCanvas");
myCanvas.width="500";
myCanvas.height="300";
var ctx=myCanvas.getContext("2d"); //画图 女汉子
var nvhanziImg=new Image();
nvhanziImg.src="data:images/nvhanzi.jpg";
nvhanziImg.onload=function(){ //ctx.drawImage(nvhanziImg,dx,dy,dw,dh); ctx.drawImage(nvhanziImg,225,125,50,50);
} //2 画精灵图片
// source源 destination 目标地
var nvjingli=new Image();
nvjingli.src="data:images/jinglingnvhai.jpg";
var nv2={
x:82,
y:0,
w:84,
h:110
}; nvjingli.onload=function(){ //语法 ctx.drawImage(nvjingli,s.x,s.y,s.w,s.h,d.x,d.y,d.w,d.h); ctx.drawImage(nvjingli,nv2.x,nv2.y,nv2.w,nv2.h,0,0,nv2.w/2,nv2.h/2); } //3.圣诞老人驾车
var shengdanP=[
{
x:0,
y:0,
w:220,
h:80
},
{
x:220,
y:0,
w:220,
h:80
},
{
x:440,
y:0,
w:220,
h:80
},
{
x:660,
y:0,
w:220,
h:80
},
];
var shengdanImg=new Image();
shengdanImg.src="data:images/christmas.jpg"; shengdanImg.onload=function(){ setInterval(pao,100);
} var index=0;
function pao(){
if(index==shengdanP.length){
index=0;
}
var sx=shengdanP[index].x;
var sy=shengdanP[index].y;
var sw=shengdanP[index].w;
var sh=shengdanP[index].h; ctx.drawImage(shengdanImg,sx,sy,sw,sh,10,125,sw*2/3,sh*2/3);
index++; } </script>

效果图 如下 查看案例

14.canvas save && restore


保存或者恢复 canvas 里的状态 (填充,描边,渐变,坐标位移,裁切)

<script>

        var myCanvas=document.getElementById("myCanvas");

        myCanvas.width="500";
myCanvas.height="300";
var ctx=myCanvas.getContext("2d"); var lg=ctx.createLinearGradient(200,100,250,200);
lg.addColorStop(0,"red");
lg.addColorStop(1,"blue"); ctx.strokeStyle="red";
ctx.strokeRect(0,0,50,100);
ctx.save(); ctx.fillStyle=lg;
ctx.strokeStyle="blue";
ctx.strokeRect(100,10,50,100);
ctx.save(); ctx.restore();
ctx.restore();
ctx.strokeRect(200,10,50,100);
ctx.fillRect(200,100,50,100); </script>

效果图如下 你是否能理解下面的绘制呢? 查看案例

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAE8CAYAAAC2KAmxAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAqkSURBVHhe7dzRahxWDARQ/3n8o/kWNwt1moKycVKRdEbnwOBd8IsQXA1+8MsbAHCOAgAABykAAHCQAgAAB30tAJ8/fxYREZGS/Mi/CsDbp089AYCjfq0ApFMAADhOAQCAgxQAADhIAQCAgxQAADhIAQCAgxQAADhIAQCAgxSAYO9jtCTeNFRygGoKQL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AFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAOUgAA4CAFAAAO+ukCICIiIh35ka8FAAC4QwEAgIMUAAA4SAEAgIMUAAA4SAEAgIMUAAA4SAEAgIMUAAA4SAEAgHPe3v4CJY7aXJt16Y4AAAAASUVORK5CYII=" alt="" />;

15.canvas 坐标操作 移动(translate) 旋转(rotate) 缩放(scale)


为了不影响canvas里其它的对象,我们在进行坐标变换前,先保存一下canvas状态 做完变换操作以后恢复原状态

   <script>

        var drawRect=document.getElementById("drawRect");

        myCanvas.width="500";
myCanvas.height="300";
var ctx=myCanvas.getContext("2d"); //绘制球
var x=0,y=0;
setInterval(function(){
ctx.save(); ctx.clearRect(x-11,y-11,22,22); x+=5;
y+=5;
ctx.translate(x,y); ctx.beginPath();
ctx.arc(0,0,10,0,2*Math.PI,false);
ctx.stroke();
ctx.closePath(); ctx.restore(); },100); </script>

效果图如下 查看案例

aaarticlea/gif;base64,R0lGODlhDAJAAbMAAAAAAIAAAACAAICAAAAAgMDAwKCgpICAgP8AAAD/AP//AAAA//8A/wD///////7+/iH/C05FVFNDQVBFMi4wAwEAAAAh+QQEBgAAACwAAAAADAJAAQAE/tDJSau9OOvNu/9gKI5kaZ5oqq5s675wLM90bd94ru987//AoHBILBqPyKRyyWw6n9CodEqtWq/YrHbL7XqrhbB4TC6bz+i0es1uu9/wuHxOr9vv+Lx+z+/7yyYFX4OEhYaHG4IlioiNjo+QT4wjk5GWl5iZNJUhnJqfoKGiGZ4fpaOoqaqNpx2tq7CxslSvibO3uLlOtRq8ur/AwTG+GMTCx8jJpoHKzc7PHsYW0tD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ihthIz2XtOcaXuCbzNZN/MZ7Kai29x0wduOX1Yf2WBgoQ4hocmcyFkEmwmk0swYUAvZR4kGYoVXh8gYiBWbVJKJU4oUogoRDVHO6l0Pz48B4xtYT5fFBEAIfkEBQ0ADwAsvwC/ABsAGwAABJrwySmdvTTrebvfW6eJIGeVD1aqKJu5WXHMR3GOTngAAL0DKZzG0CtoZAAD5UbZGUGOQs+UIT6h0WQlxwFcsQ9pjvmYobYSM9l7TnGl7gm8zWTfzGeymotvcdMHbjl9WH9lgYKEOIaHJnMhZBJsJpNLMGFAL2UeJBmKFV4fIGIgVm1SSiVOKFKIKEQ1RzupdD8+PAeMbWE+XxQRACH5BAUGAA8ALMQAxAAbABsAAASa8Mkpnb0063m731uniSBnlQ9WqiibuVlxzEdxjk54AAC9AymcxtArbHYGyo2yM4IchZ4pQ3Q+oYBkKscBWK+PaG75mKEqXDPZe95Kou43sL1k38xnsjp9oHMlezl4K3p9Ww6DIWRlhltwV38TbCZzLzBhlRxlHiQZiV02nRpiIFVtUVogTShRjSVENaNIbRU/PjwHkbRhPl8UEQAh/g2x6sziOgkJzt6x6sziADs=" alt="" />

16.像素操作


    <canvas id="myCanvas" style="border:1px solid #ddd;display:block;margin:auto">
</canvas>
<canvas id="myCanvas2" style="border:1px solid red;display:block;margin:auto">
</canvas>
 <script>

        var myCanvas=document.getElementById("myCanvas");
var myCanvas2=document.getElementById("myCanvas2"); myCanvas2.width=myCanvas.width="500";
myCanvas2.height=myCanvas.height="300"; var ctx=myCanvas.getContext("2d");
var ctx2=myCanvas2.getContext("2d"); ctx.fillRect(10,10,200,100);
ctx.beginPath();
ctx.arc(200,100,50,0,Math.PI*2);
ctx.closePath();
ctx.fillStyle="red";
ctx.fill(); var imgData=ctx.getImageData(0,0,500,300);
ctx2.putImageData(imgData,0,0); </script>

效果图如下 查看案例

16.canvas像素操作_补充


    <div style="width:300px;margin:auto">
原图
<img src="data:images/twodog.jpg" alt="">
canvas处理后
</div>
<canvas id="myCanvas" style="border:1px solid #ddd;display:block;margin:auto">
</canvas>
<script>
var CANVAS_WIDTH=300;
var CANVAS_HEIGHT=200; var myCanvas=document.getElementById("myCanvas");
myCanvas.width=CANVAS_WIDTH;
myCanvas.height=CANVAS_HEIGHT; var ctx=myCanvas.getContext("2d"); var img=new Image();
img.src="data:images/twodog.jpg";
img.onload=function(){
ctx.drawImage(img,0,0,CANVAS_WIDTH,CANVAS_HEIGHT); var imgData=ctx.getImageData(0,0,CANVAS_WIDTH,CANVAS_HEIGHT);
var pixcelNums=CANVAS_WIDTH*CANVAS_HEIGHT;
for(var i=0;i<pixcelNums;i++){
imgData.data[i*4+0]=0;
/*imgData.data[i*4+1]=0;
imgData.data[i*4+2]=0;
imgData.data[i*4+3]=0;*/
} ctx.putImageData(imgData,0,0);
} </script>

效果图如下: 查看案例

17.canvas 阴影


说明 属性
阴影偏移 shadowOffsetX,shadowOffsetY
阴影颜色 shadowColor
阴影模糊 shadowBlur
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas 阴影 </title>
<style>
canvas{box-shadow: 0px 0px 10px #f00; }
</style>
</head>
<body>
<canvas id="myCanvas" style="border:1px solid #ddd;display:block;margin:auto">
</canvas>
</body>
</html>
<script>
var CANVAS_WIDTH=300;
var CANVAS_HEIGHT=200; var myCanvas=document.getElementById("myCanvas");
myCanvas.width=CANVAS_WIDTH;
myCanvas.height=CANVAS_HEIGHT; var ctx=myCanvas.getContext("2d"); var img=new Image();
img.src="data:images/twodog.jpg";
img.onload=function(){
ctx.shadowColor="#222";
ctx.shadowBlur="10";
ctx.shadowOffsetX="5";
ctx.shadowOffsetY="5"; ctx.drawImage(img,(CANVAS_WIDTH-200)/2,(CANVAS_HEIGHT-100)/2,200,100);
} </script>

效果图如下 查看案例

;

18.canvas 裁剪


设置好裁剪路径 之后绘制的图形 只能显示裁剪路径里面的

<script>
var CANVAS_WIDTH=300;
var CANVAS_HEIGHT=200; var myCanvas=document.getElementById("myCanvas");
myCanvas.width=CANVAS_WIDTH;
myCanvas.height=CANVAS_HEIGHT; var ctx=myCanvas.getContext("2d"); ctx.beginPath();
ctx.arc(150,100,80,0,2*Math.PI);
ctx.closePath(); var guniang=new Image();
guniang.src="data:images/guniang.jpg";
guniang.onload=function(){
ctx.save();
ctx.clip();
ctx.drawImage(guniang,50,0,200,200);
ctx.restore(); } ctx.fillText("女朋友",250,150,50,50); </script>

效果图如下 查看案例

19.canvas 图片保存


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas 图片保存 </title> </head>
<body>
<div style="width:900px;text-align: center;margin:0 auto;">
图片
<img id="tupian" src="" alt="图片暂时没有" style="vertical-align: middle">
<br/>
canvas
<canvas style="vertical-align: middle" id="myCanvas" style="border:1px solid #ddd;margin:auto">
</canvas>
</div> </body>
</html>
 <script>
var CANVAS_WIDTH=300;
var CANVAS_HEIGHT=200; var myCanvas=document.getElementById("myCanvas");
var tupian=document.getElementById("tupian");
myCanvas.width=CANVAS_WIDTH;
myCanvas.height=CANVAS_HEIGHT; var ctx=myCanvas.getContext("2d"); var guniang=new Image();
guniang.src="data:images/huaituzi.jpg";
guniang.onload=function(){ ctx.drawImage(guniang,50,40,200,119);
ctx.moveTo(0,0);
ctx.lineTo(CANVAS_WIDTH,CANVAS_HEIGHT);
ctx.stroke(); var dataStr=myCanvas.toDataURL();
tupian.src=dataStr;
//location.href=dataStr;
}
</script>

效果图如下 查看案例

20.图形组合


调节透明度 globalAlpha

单词 destionation source

globalCompositeOperation 图形组合操作

说明
source-over 目标图像上显示源图像。
destination-over 源图像上方显示目标图像。
source-atop 目标图像顶部显示源图像。源图像位于目标图像之外的部分是不可见的
destination-atop 源图像顶部显示目标图像。源图像之外的目标图像部分不会被显示。
source-in 在目标图像中显示源图像。只有目标图像内的源图像部分会显示,目标图像是透明的。
destination-in 在源图像中显示目标图像。只有源图像内的目标图像部分会被显示,源图像是透明的。
source-out 在目标图像之外显示源图像。只会显示目标图像之外源图像部分,目标图像是透明的。
destination-out 在源图像外显示目标图像。只有源图像外的目标图像部分会被显示,源图像是透明的。
lighter 显示源图像 + 目标图像。
copy 显示源图像。忽略目标图像。
xor 使用异或操作对源图像与目标图像进行组合。
<script>
var CANVAS_WIDTH=300;
var CANVAS_HEIGHT=300; var myCanvas=document.getElementById("myCanvas");
myCanvas.width=CANVAS_WIDTH;
myCanvas.height=CANVAS_HEIGHT; //ctx.globalAlpha=0.5; var compositeArr=[
"source-over","destination-over",
"source-atop","destination-atop",
"destination-in","source-in",
"source-out","destination-out",
"lighter","copy","xor"
]; var i=0,l=compositeArr.length;
draw(compositeArr[i]); setInterval(function(){
i++;
if(i==l){
i=0;
}
draw(compositeArr[i]); },1000); function draw(type){
var ctx=myCanvas.getContext("2d"); ctx.clearRect(0,0,300,300); ctx.fillStyle="blue";
ctx.fillRect(0,0,100,100); ctx.globalCompositeOperation=type; ctx.beginPath();
ctx.arc(100,100,100,0,Math.PI*2);
ctx.fillStyle="red";
ctx.fill();
ctx.closePath(); ctx.globalCompositeOperation="source-over"; ctx.font="30px Arial";
ctx.strokeText(compositeArr[i],0,250); } </script>

效果图如下 查看案例

21. canvas 事件操作


isPointInPath(x,y) 判断坐标为x,y的点是否再当前路径

            <script>
var CANVAS_WIDTH=300;
var CANVAS_HEIGHT=300; var myCanvas=document.getElementById("myCanvas"); myCanvas.width=CANVAS_WIDTH;
myCanvas.height=CANVAS_HEIGHT; var huabi=myCanvas.getContext("2d"); huabi.fillRect(10,10,100,100);
huabi.strokeStyle="red";
huabi.strokeRect(110,110,100,100); function drawCircle(){
huabi.beginPath();
huabi.arc(160,60,50,0,Math.PI*2);
huabi.stroke();
huabi.closePath();
} drawCircle(); function drawSanjiao(){
huabi.beginPath();
huabi.moveTo(60,110);
huabi.lineTo(110,210);
huabi.lineTo(10,210);
huabi.lineTo(60,110);
huabi.stroke();
huabi.closePath();
}
drawSanjiao(); myCanvas.onclick=function(event){
var e=event||window.event; var x=e.clientX-myCanvas.offsetLeft;
var y=e.clientY-myCanvas.offsetTop; if(x>=10&&x<=110&&y>=10&&y<=110){
alert("你点中了黑色矩形");
}else if(x>=110&&x<=210&&y>=110&&y<=210){
alert("你点中了红色矩形");
}else{
drawCircle();
if(huabi.isPointInPath(x,y)){
alert("你点击了圆圈");
}
drawSanjiao();
if(huabi.isPointInPath(x,y)){
alert("你点击了三角");
} } } </script>

效果图如下 查看案例

大家都能看懂的 canvas基础教程的更多相关文章

  1. 尽量写出大家都能看懂的ReactJS入门教程

    个人感觉ReactJS相比于传统的JS框架还是挺有意思的,主要是它将JS代码和HTML代码完美的结合在了一起,有点jsp把java代码和html混在一起写的意思?但是它通过组件的形式实现了代码可复用, ...

  2. 只要听说过电脑的人都能看懂的网上pdf全书获取项目

    作者:周奇 最近我要获取<概统>的教材自学防挂科(线代已死),于是我看到 htt链ps:/链/max链.book接118接.com接/html/2018/0407/160495927.sh ...

  3. 55张图吃透Nacos,妹子都能看懂!

    大家好,我是不才陈某~ 这是<Spring Cloud 进阶>第1篇文章,往期文章如下: 五十五张图告诉你微服务的灵魂摆渡者Nacos究竟有多强? openFeign夺命连环9问,这谁受得 ...

  4. 小学生都能看懂的FFT!!!

    小学生都能看懂的FFT!!! 前言 在创新实践重心偷偷看了一天FFT资料后,我终于看懂了一点.为了给大家提供一份简单易懂的学习资料,同时也方便自己以后复习,我决定动手写这份学习笔记. 食用指南: 本篇 ...

  5. 机器学习敲门砖:任何人都能看懂的TensorFlow介绍

    机器学习敲门砖:任何人都能看懂的TensorFlow介绍 http://www.jiqizhixin.com/article/1440

  6. 搭建分布式事务组件 seata 的Server 端和Client 端详解(小白都能看懂)

    一,server 端的存储模式为:Server 端 存 储 模 式 (store-mode) 支 持 三 种 : file: ( 默 认 ) 单 机 模 式 , 全 局 事 务 会 话 信 息 内 存 ...

  7. 小学生都能看懂的数位dp

    前言 数位dp其实很久前就知道了,也做过几道和其他算法混在一起的题目,其实通过手玩是能做的 但毕竟是种算法,还是系统学下比较好(节省手玩时间) 模板题 P2602 [ZJOI2010]数字计数 化简题 ...

  8. 外行人都能看懂的SpringCloud,错过了血亏!

    一.前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringCloud的一些基础的 ...

  9. SpringCloud(0) 外行人都能看懂的SpringCloud,错过了血亏!

    一.前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringCloud的一些基础的 ...

随机推荐

  1. 【POJ 2828】Buy Tickets

    [题目链接] http://poj.org/problem?id=2828 [算法] 离线用线段树维护序列即可 [代码] #include <algorithm> #include < ...

  2. ROS-SLAM-自主导航

    前言:无. 前提:已下载并编译了相关功能包集,如还未下载,可通过git下载:https://github.com/huchunxu/ros_exploring.git 一.启动仿真环境 cd ~/ca ...

  3. HTML5音频可视化频谱跳动代码

    今天学习到用canvas来写  HTML5音频可视化频谱跳动代码  将代码在此做一总结: <!DOCTYPE html> <html lang="en"> ...

  4. TortoiseSVN—Repo-browser

    TortoiseSVN—Repo-browser,打开你要比较的两个版本所在的地址,选择一个版本做为比较的基础(单击右键—选择mark for comparison),再选择另外一个版本(单击右键—选 ...

  5. Visual Studio icon 含义

    图片摘自:https://msdn.microsoft.com/en-us/library/y47ychfe.aspx

  6. ZBrush中如何实现智能对称

    ZBrush软件智能化和人性化的工作流程让用户在创作中提高工作效率,体验创作乐趣,说起智能化不得不提的就是ZBrush 4R8®给我们提供的智能对称功能,所谓的智能对称就是当您在编辑其中一半的物体模型 ...

  7. 算法21----重塑矩阵 LeetCode566

    1.题目 在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重 ...

  8. d3代码如何改造成update结构(恰当处理enter和exit)

    d3的enter和exit 网上有很多blog讲解.说的还凑合的见:https://blog.csdn.net/nicolecc/article/details/50786661 如何把自己的rude ...

  9. 进入docker 容器内命令

    docker exec :在运行的容器中执行命令 语法 docker exec [OPTIONS] CONTAINER COMMAND [ARG...] OPTIONS说明: -d :分离模式: 在后 ...

  10. 二、frps 完整配置文件

    # [common] is integral section [common] # A literal address or host name for IPv6 must be enclosed # ...