再次绘制的时候会使用以前的样式,后面的样式覆盖前面的web
... <style> canvas{ border: 1px solid #ccc; /*不建议在这里设置宽高*/ /*width: 600px; height: 300px;*/ } </style> ... <body> <canvas height="300" width="600"></canvas> <script> //获取canvas元素 var canvas=document.querySelector('canvas'); //获取绘制环境(工具) var ctx=canvas.getContext('2d'); //设置起始绘图的位置 ctx.moveTo(100,100); //绘制路径 ctx.lineTo(200,100); //描边 ctx.stroke(); </script> </body>
/*1.获取canvas元素*/ var canvas = document.querySelector('canvas'); /*2.获取绘制环境 (获取绘制工具)*/ /*content内容 context 上下文 */ var ctx = canvas.getContext('2d'); /*3.设置起始绘图的位置*/ ctx.moveTo(100,100); /*6.设置宽度*/ ctx.lineWidth = 10; /*4.绘制路径 */ ctx.lineTo(200,100); /*7. 设置描边的颜色*/ ctx.strokeStyle = 'red'; /*5.描边*/ ctx.stroke();
/*1.获取canvas元素*/ var canvas = document.querySelector('canvas'); /*2.获取绘制环境 (获取绘制工具)*/ /*content内容 context 上下文 */ var ctx = canvas.getContext('2d'); /*3.设置起始绘图的位置*/ ctx.moveTo(100,100); /*6.设置宽度*/ ctx.lineWidth = 10; /*4.绘制路径 */ ctx.lineTo(200,100); /*7. 设置描边的颜色*/ ctx.strokeStyle = 'red'; /*5.描边*/ ctx.stroke(); /*绿色*/ /*再次绘制的时候 会使用以前设置的样式 设置的样式最后的会生效*/ /*开启新路径*/ ctx.beginPath(); ctx.moveTo(100,200); ctx.lineTo(200,200); ctx.strokeStyle = 'green'; ctx.stroke();
var canvas = document.querySelector('canvas'); /*获取绘图工具*/ var ctx = canvas.getContext('2d'); /*3d目前还不支持*//*webgl*/ /*移动画笔*/ ctx.moveTo(100,100); ctx.lineTo(200,100); ctx.lineTo(200,200); //ctx.lineWidth = 10; /*自动闭合*/ ctx.closePath(); //ctx.stroke(); //ctx.strokeStyle = 'red'; //ctx.stroke(); /*填充*/ //ctx.fillStyle = 'red'; ctx.fill();
var canvas = document.querySelector('canvas'); var ctx = canvas.getContext('2d'); /*1.获取画布的宽度和高度*/ //var width = canvas.width; var width = ctx.canvas.width; var height = ctx.canvas.height; /*2.假设大容器 200*200*/ /*3.假设小容器 100*100*/ /*4. 计算起始位置*/ var x = width/2 - 100; var y = height/2 -100; ctx.moveTo(x,y); ctx.lineTo(x+200,y); ctx.lineTo(x+200,y+200); ctx.lineTo(x,y+200); ctx.lineTo(x,y); /*第二个方形的方向须要是逆方向*/ var x1 = width/2 - 50; var y1 = height/2 -50; ctx.moveTo(x1,y1); ctx.lineTo(x1,y1+100); ctx.lineTo(x1+100,y1+100); ctx.lineTo(x1+100,y1); ctx.lineTo(x1,y1); ctx.stroke(); ctx.fill();
var canvas = document.querySelector('canvas'); /*获取绘图工具*/ var ctx = canvas.getContext('2d'); /*3d目前还不支持*//*webgl*/ ctx.lineWidth = 10; ctx.moveTo(100,100); ctx.lineTo(200,100); ctx.strokeStyle = 'red'; ctx.lineCap = 'butt'; ctx.stroke(); ctx.beginPath(); ctx.moveTo(100,200); ctx.lineTo(200,200); ctx.strokeStyle = 'green'; ctx.lineCap = 'round'; ctx.stroke(); ctx.beginPath(); ctx.moveTo(100,300); ctx.lineTo(200,300); ctx.strokeStyle = 'pink'; ctx.lineCap = 'square'; ctx.stroke();
var canvas = document.querySelector('canvas'); /*获取绘图工具*/ var ctx = canvas.getContext('2d'); /*3d目前还不支持*//*webgl*/ ctx.lineWidth = 10; ctx.moveTo(100,100); ctx.lineTo(150,150); ctx.lineTo(200,100); ctx.strokeStyle = 'red'; ctx.lineJoin = 'miter'; ctx.stroke(); ctx.beginPath(); ctx.moveTo(100,200); ctx.lineTo(150,250); ctx.lineTo(200,200); ctx.strokeStyle = 'green'; ctx.lineJoin = 'round'; ctx.stroke(); ctx.beginPath(); ctx.moveTo(100,300); ctx.lineTo(150,350); ctx.lineTo(200,300); ctx.strokeStyle = 'pink'; ctx.lineJoin = 'bevel'; ctx.stroke();
var canvas = document.querySelector('canvas'); /*获取绘图工具*/ var ctx = canvas.getContext('2d'); /*3d目前还不支持*//*webgl*/ /*1.从左到右*/ /*2.起始颜色 白色*/ /*3.结束颜色 红色*/ ctx.lineWidth = 10; for (var i = 0; i < 255; i++) { ctx.beginPath(); ctx.moveTo(99+i,100); ctx.lineTo(100+i,100); var g = 255 - i; var b = 255 - i; ctx.strokeStyle = 'rgb(255,'+g+','+b+')'; ctx.stroke(); }
另外虚线还有偏移量,负值表示向右偏移canvas
var canvas = document.querySelector('canvas'); /*获取绘图工具*/ var ctx = canvas.getContext('2d'); /*3d目前还不支持*//*webgl*/ ctx.moveTo(100,100); ctx.lineTo(500,100); /*绘制虚线的方法*/ /*传数组 设置虚线长度的*/ ctx.setLineDash([5,10]); /*offset表示位移*/ ctx.lineDashOffset=5; /*若是是偶数个数设置 */ /*若是是奇数数个数设置 */ /*获取的不重复的一段*/ console.log(ctx.getLineDash()); ctx.stroke();
clearRect清除绘制内容数组
var canvas=document.querySelector('canvas'); var ctx=canvas.getContext('2d'); ctx.rect(100,100,20,100); ctx.strokeRect(200,100,20,100); ctx.fillRect(300,100,20,100); ctx.clearRect(210,100,20,100); ctx.stroke();
addColorStop表示颜色的设置,第一个参数表示占据位置,第二个参数是颜色浏览器
var canvas = document.querySelector('canvas'); var ctx = canvas.getContext('2d'); /*渐变的方案*/ var linearGradient = ctx.createLinearGradient(100,150,300,150); /* 0-1 0-100% */ linearGradient.addColorStop(0,'red'); linearGradient.addColorStop(0.5,'blue'); linearGradient.addColorStop(1,'yellow'); ctx.fillStyle = linearGradient; ctx.fillRect(100,100,200,100);
var canvas = document.querySelector('canvas'); var ctx = canvas.getContext('2d'); /*1.线都是由点构成*/ var yy = 0; for (var i = 0; i < 600; i++) { var x = i; /*y会随着公式去计算*/ //var y = 2*x; var y = Math.pow(x+2,2); //var y = Math.sin(x/20)*100 + 300; ctx.beginPath(); ctx.moveTo(x,yy); ctx.lineTo(x+1,y); yy = y; ctx.stroke(); }
顺时针转是正方向,是正值。逆时针转是负方向是负值缓存
var canvas=document.querySelector('canvas'); var ctx=canvas.getContext('2d'); ctx.arc(100,100,60,0,2*Math.PI,false); ctx.stroke();
若是设定起始点的坐标是(100,100)那么向右对齐的时候,文本会出如今(100,100)的左边,上下方式的对齐方式同理app
var canvas=document.querySelector('canvas'); var ctx=canvas.getContext('2d'); ctx.font = '微软雅黑' ctx.stroke(); ctx.fillText('内容内容',100,100,150);
canvas中绘制图片有三种方式工具
第一种,三个参数字体
第二种,五个参数动画
第三种,九个参数webgl
/*img元素也有onload事件*/ /*document.querySelector('img').onload = function () { console.log('加载完成'); }*/ /*怎么动态建立一个图片元素*/ /*这个元素在内存里面*/ var img = document.createElement('img'); img.src = 'images/01.jpg'; img.onload = function () { console.log(img); } /*使用Image对象*/ var img1 = new Image(); img1.onload = function () { console.log(img1); } img1.src = 'images/01.jpg'; /*补充:兼容问题*/ /*IE onload必须先绑定*/
九个参数中,获取img对象,在图片的(40,195)的位置截取图片,截取图片的大小是40X65,放在画布的(startX,startY)的位置,在画布中显示大小是40X65
var canvas = document.querySelector('canvas'); var ctx = canvas.getContext('2d'); var img = new Image(); img.onload = function () { var width = ctx.canvas.width; var height = ctx.canvas.height; var startX = width/2-20; var startY = height/2-32.5; /*绘制*/ ctx.drawImage(img,40,195,40,65,startX,startY,40,65); } img.src = 'images/03.png';
绘制新图片,截取位置的x方向长度是索引和人物宽度的乘积,y方向就是最后一行的坐标195,图片截取大小就是人物大小,开始位置就是绘制起始位置,画布中图片显示大小就是人物大小
var canvas = document.querySelector('canvas'); var ctx = canvas.getContext('2d'); var img = new Image(); img.onload = function () { /*画布大小*/ var width = ctx.canvas.width; var height = ctx.canvas.height; /*人物大小*/ var perWidth = img.width/4; var perHeight = img.height/4; /*绘制起点*/ var startX = width/2-perWidth/2; var startY = height/2-perHeight/2; /*绘制*/ /*图片的索引*/ var index = 0; ctx.drawImage(img,0,0,perWidth,perHeight,startX,startY,perWidth,perHeight); setInterval(function () { index ++; if(index > 3){ index = 0; } /*绘制以前清除以前的图片*/ ctx.clearRect(startX,startY,perWidth,perHeight); ctx.drawImage(img,index*perWidth,195,perWidth,perHeight, startX,startY,perWidth,perHeight); },100); } img.src = 'images/03.png';
若是先保存一个红色宽30,再保存一个绿色宽20,以后恢复的时候,会先恢复
var canvas = document.querySelector('canvas'); var ctx = canvas.getContext('2d'); ctx.lineWidth = 30; ctx.strokeStyle = 'red'; ctx.moveTo(100,100); ctx.lineTo(300,100); ctx.stroke(); /*保存一系列样式*/ /*保存多个系列样式*/ /*存储结构是栈 后进先出*/ ctx.save(); ctx.beginPath(); ctx.lineWidth = 20; ctx.strokeStyle = 'green'; ctx.moveTo(100,200); ctx.lineTo(300,200); ctx.stroke(); ctx.save(); ctx.beginPath(); ctx.restore(); ctx.moveTo(100,300); ctx.lineTo(300,300); ctx.stroke(); ctx.beginPath(); ctx.restore(); ctx.moveTo(100,400); ctx.lineTo(300,400); ctx.stroke();
var canvas = document.querySelector('canvas'); var ctx = canvas.getContext('2d'); var img = new Image(); img.onload = function () { /*填充方案*/ var pat = ctx.createPattern(img,'no-repeat'); /*图片描边*/ ctx.strokeStyle = pat; /*设置线的样式*/ ctx.lineWidth = 25; ctx.lineCap = 'round'; ctx.lineJoin = 'round'; var isDown = false; ctx.canvas.addEventListener('mousedown',function (e) { /*设置起始坐标*/ ctx.moveTo(e.clientX,e.clientY); isDown = true; }) ctx.canvas.addEventListener('mousemove',function (e) { if(isDown){ ctx.lineTo(e.clientX,e.clientY); ctx.stroke(); } }) ctx.canvas.addEventListener('mouseup',function () { isDown = false; }) } img.src = 'images/05.jpg';
var Per = function () { this.ctx = document.querySelector('canvas').getContext('2d'); this.width = this.ctx.canvas.width; this.height = this.ctx.canvas.height; this.stepSize = 10; this.index = 0; } Per.prototype.init = function () { var that = this; that.loadImage(function (img) { that.perWidth = img.width/4; that.perHeight = img.height/4; that.startX = that.width/2-that.perWidth/2; that.startY = that.height/2-that.perHeight/2; that.drawPer(img,0,0,0); that.bindEvent(img); }) } /*图片加载*/ Per.prototype.loadImage = function (callback) { var img = new Image(); img.onload = function () { /*完成其余业务*/ callback && callback(img); } img.src = 'images/04.png'; } /*事件绑定*/ Per.prototype.bindEvent = function (img) { var that = this; /*按键编码 左上右下 37 38 39 40*/ /* 0 1 2 3*/ var direction = 0; var stepX = 0; var stepY = 0; document.addEventListener('keydown',function (e) { console.log(e.keyCode); switch(e.keyCode){ case 37: /*左*/ stepX --; direction = 1; break; case 38: /*上*/ stepY --; direction = 3; break; case 39: /*右*/ stepX ++; direction = 2; break; case 40: /*下*/ stepY ++; direction = 0; break; } that.index ++; /*绘制*/ that.drawPer(img,stepX,stepY,direction); }) } /*绘制人物*/ Per.prototype.drawPer = function (img,stepX,stepY,direction) { /*清空*/ this.ctx.clearRect(0,0,this.width,this.height); if(this.index > 3){ this.index = 0; } /*绘制*/ this.ctx.drawImage(img,this.perWidth*this.index,direction*this.perHeight, this.perWidth,this.perHeight, this.startX+(stepX*this.stepSize),this.startY+(stepY*this.stepSize), this.perWidth,this.perHeight); } new Per().init();