Home > Web Front-end > HTML Tutorial > Implementation of html5 Canvas image animation

Implementation of html5 Canvas image animation

伊谢尔伦
Release: 2016-12-10 09:32:03
Original
1761 people have browsed it

<span style="font-size:18px;"><!DOCTYPE html>
<head>
    <meta charset=utf-8>
    <title>PHP100 HTML5视频教程-canvas-吹气球效果</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
    <canvas id="php100" width="500" height="200" ></canvas>
    <br>  <!-- 画布 -->
    <script type="text/javascript">
       var canvas = document.getElementById(&#39;php100&#39;); //获取画布
       var p100=canvas.getContext("2d");  //设置模式
       var dir=0;       //设置线条起点
       var width=500;   //要清除的矩形的宽度
       var height=200;  //要清除的矩形的高度
       var exp=1; //像素移动的位置,正数向下,负数向上
       p100.strokeStyle = "rgba(255,0,0,1)"; //定义颜色
 function fff(){
    p100.clearRect(0,0,width,height); //清除原始图形 0 0  表示矩形的左上角的X Y轴坐标
    p100.fillStyle="red";//定义颜色
     p100.beginPath();//从新开始画,防止冲突重叠
     p100.arc(180,dir,dir,0,Math.PI*2,1); //x坐标,y坐标,半径,Math.PI是圆周率  半径也设置成圆点的位置就实现吹气球的效果
     p100.closePath();//结束画布,防止冲突重叠
     p100.fill();//结束渲染
     dir=dir+exp;   //向下移动
 if(dir==0 || dir==height){
   exp=exp*-1; //掉头位置//乘以负一用来调整线路方向
  }
}
//setInterval(code,millisec) 按照指定的周期来调用函数,返回值为定时器的ID值 赋值给一个变量
//clearInterval(idofsetInterval)取消由setInterval()方法设置的定时器。
</script>
<button onclick="tt=setInterval(fff,20);">开始</button>
<button onclick="clearInterval(tt);">停止</button>
</body>
</html>
</span>
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template