1.得到一个canvas元素 2.用canvas元素去得到一个html5内置内置的工具getContext("2d")
3.常用的方法,fillStyle属性指明填充的颜色样式,fillRect(x,y,width,height)画矩形
4.画直线moveTo(x,y) lineTo(x,y) closePath() stroke() 属性:lineJoin 默认是平角round是圆角 lineCap有3个值:butt(平,默认),round(圆),square(方) lineWidth线条宽度 stokeStyle线条的颜色样式 学习http://www.jb51.net/html5/70307.html
5画圆形cxt2.arc(100,100,100,0,Math.PI*2,true);cxt2.closePath();cxt2.fill();
arc(x,y,r,0,Math.PI*2,true)x,y圆心 r半径
6.渐变填充
var grd=cxt3.createLinearGradient(0,0,180,100);grd.addColorStop(0,"#FF0000");
grd.addColorStop(1,"#00FF00");cxt3.fillStyle=grd;cxt3.fillRect(0,0,180,100);
addColorStop(x,color) 0=《x《=1 开始和技术时的颜色
7.图像填充var can4 = document.getElementById('can4');
var cxt4 = can4.getContext("2d");
var img = new Image();
img.src ="22.gif";
console.log(img);
cxt4.drawImage(img,0,0);这样的结果并不会显示出来图像
因为img没有加载进来,解决办法img.onload = function () {
cxt4.drawImage(img, 50, 50,100,100);
}
代码:1.html
<style>
#can1{border:solid 1px blue;}
</style>
</head>
<body>
<canvas id="can1" width="200" height="200"></canvas>
<script>
var can1 = document.getElementById('can1');
var cxt = can1.getContext("2d");
cxt.fillStyle="blue";
cxt.fillRect(0,0,50,50);
cxt.moveTo(60,60);
cxt.lineTo(150,60);
//cxt.moveTo(150,60);
cxt.lineTo(150,150);
cxt.lineTo(60,150);
cxt.lineTo(60,60);
//cxt.lineJoin="round";
//cxt.closePath();
cxt.lineCap="square";
cxt.lineWidth=20;
cxt.strokeStyle = 'rgba(255,0,0,0.5)';
cxt.stroke();
</script>
代码2.html
<canvas id="can2" width="200" height="200"></canvas>
<canvas id="can3" width="200" height="200"></canvas>
<canvas id="can4" width="200" height="200"></canvas>
<script>
//
var can2 = document.getElementById('can2');
var cxt2 = can2.getContext("2d");
cxt2.fillStyle="#FF0000";
cxt2.arc(100,100,100,0,Math.PI*2,true);
cxt2.closePath();
cxt2.fill();
var can3 = document.getElementById('can3');
var cxt3 = can3.getContext("2d");
var grd=cxt3.createLinearGradient(0,0,180,100);
grd.addColorStop(0,"#FF0000");
grd.addColorStop(1,"#00FF00");
cxt3.fillStyle=grd;
cxt3.fillRect(0,0,180,100);
var can4 = document.getElementById('can4');
var cxt4 = can4.getContext("2d");
var img = new Image();
img.src ="22.gif";
console.log(img);
//cxt4.drawImage(img,0,0);
img.onload = function () {
cxt4.drawImage(img, 50, 50,100,100);
}
</script>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。