当前位置: 移动技术网 > IT编程>开发语言>JavaScript > canvas绘制的直线动画

canvas绘制的直线动画

2019年03月28日  | 移动技术网IT编程  | 我要评论

咸宁租房网,傲龙传说txt全集下载,中国gps

话不多说,请看代码:

<!doctype html>
<html>
 <head>
 <meta charset="utf-8">
 <title>first line</title>
 <style type="text/css">
       body{
         background: #456e89;
       }
 .canvas {
 height: 300px;
 width: 300px; 
 margin: 0 auto;
 font-family: arial;
 }
 </style>
 </head>
 <body>
 <div class="canvas">
 <canvas id="cvs" width="300" height="300"></canvas>
 </div>
 <script type="text/javascript">
 var cvs = document.getelementbyid("cvs").getcontext("2d");
 function anim(opt) { //初始化值
 this.opt = opt;
 }
 //node 表示画布节点
 //stax 表示开始x坐标
 //stay 表示开始y坐标
 //len表示终点坐标,
 //timing表示运行的间隔时间, 
 //num表示坐标增长的大小 
 //direc表示判断绘制线条的方向,false表示x轴,ture表示y轴
 //lw表示线宽的大小 
 //color 表示绘制线条颜色
 anim.prototype.draw = function() { //绘制直线的线条
 var opt = this.opt; //设置对象的属性
 var adx = opt.stax;
 var ady = opt.stay;
 var that = {
  x: opt.stax,
  y: opt.stay
 };
 var time = setinterval(function() {
  opt.direc //判断绘制方向
  ?
  opt.len > ady ? ady += opt.num : ady -= opt.num :
  opt.len > adx ? adx += opt.num : adx -= opt.num;
  if(adx == opt.len || ady == opt.len) { //停止循环
  clearinterval(time);
  }
  opt.node.beginpath(); // 开始绘制线条
  opt.node.moveto(that.x, that.y);
  opt.node.lineto(adx, ady);
  opt.node.linewidth = opt.lw || 1;
  opt.node.strokestyle = opt.color;
  opt.node.stroke();
 }, opt.timing);
 };
 anim.prototype.txt = function(opc) {//绘制文字 
 cvs.beginpath();
 cvs.fillstyle = "rgba(255,255,255,"+opc+")";
 cvs.font = "200px arial";
 cvs.filltext("l", 100, 200);
 };
 var line1 = new anim({ //实例
 node: cvs,
 color: "#fff",
 stax: 114,
 stay: 58,
 len: 134,
 timing: 50,
 num: 1,
 direc: false,
 lw: 2
 });
 line1.draw(); //执行绘制
 var line2 = new anim({
 node: cvs,
 color: "#fff",
 stax: 115,
 stay: 58,
 len: 200,
 timing: 20,
 num: 1,
 direc: true,
 lw: 2
 });
 line2.draw();
 var line3 = new anim({
 node: cvs,
 color: "#fff",
 stax: 133,
 stay: 184,
 len: 58,
 timing: 20,
 num: 1,
 direc: true,
 lw: 2
 });
 line3.draw();
 var line4 = new anim({
 node: cvs,
 color: "#fff",
 stax: 132,
 stay: 184,
 len: 203,
 timing: 35,
 num: 1,
 direc: false,
 lw: 2
 });
 line4.draw();
 var line5 = new anim({
 node: cvs,
 color: "#fff",
 stax: 203,
 stay: 199,
 len: 115,
 timing: 35,
 num: 1,
 direc: false,
 lw: 2
 });
 line5.draw();
 var line6 = new anim({
 node: cvs,
 color: "#fff",
 stax: 203,
 stay: 199,
 len: 184,
 timing: 50,
 num: 1,
 direc: true,
 lw: 2
 });
 line6.draw();
 var test = new anim();//绘制文字实例
 settimeout(function () {
 var num = 0;
 var times = setinterval(function () {
  num++;
  var t = num/100;
  if(t === 1){
  clearinterval(times);
  }
  test.txt(t);
 },50)
 },3000)
 </script>
 </body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网