canvas Realization of Drawing the Effect of Eating Beanfish

  • 2021-07-10 18:38:01
  • OfStack

Without saying much, please look at the code:


<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>canvas Eat bean fish </title>
 </head>
 <style>
 body{
 text-align:center;
 }
 canvas{
 background: #efefef;
 } 
 </style>
 <body>
 <h1>
  Angle to Radian: <br />
  Radian =2*PI* Angle /360= Angle *PI/180
 </h1>
 <!-- Width and height of canvas can only use attributes, not styles -->
 <canvas id='a1' width="500" height="400"></canvas>
 </body>
</html>
<script>
 var ctx=a1.getContext('2d');// Get the brush on the canvas and set the drawing method 
 function openMouse(){
 // Draw a circle ( 3/4 ) 
 ctx.beginPath();// Begin 1 Strip path 
 ctx.arc(250,200,100,45*Math.PI/180,315*Math.PI/180);// The center of the circle is ( 250,200 ) with a radius of 100
 ctx.lineTo(250,200);
 ctx.closePath();
 ctx.stroke();// Outline / Stroke 
 ctx.fillStyle='#00ffff';
 ctx.fill();
 eye();
 }
 //openMouse();
 function closeMouse(){
 ctx.beginPath();// Begin 1 Strip path 
 ctx.arc(250,200,100,0*Math.PI/180,360*Math.PI/180);// The center of the circle is ( 250,200 ) with a radius of 100
 ctx.lineTo(250,200);
 ctx.closePath();
 ctx.stroke();// Outline / Stroke 
 ctx.fillStyle='#00ffff';
 ctx.fill();
 eye();
 }
 //closeMouse();
 // Draw common part eyes 
 function eye(){
 // Draw eyes 
 ctx.beginPath();
 ctx.arc(250,200-100/2,25,0,2*Math.PI);// Eye radius is 25
 ctx.stroke();
 ctx.fillStyle='#001900';
 ctx.fill();
 // Draw eye light 
 ctx.beginPath();
 ctx.arc(265,140,5,0,2*Math.PI);// The radius of eye light is 5
 ctx.stroke();
 ctx.fillStyle='#ffffff';
 ctx.fill();
 }
 var isOpen=true;// Defining variables isOpen Is it open or not 
 var timer=setInterval(function(){
 var ctx=a1.getContext('2d');
 ctx.clearRect(0,0,500,400);// Empty the canvas size 
 if(isOpen){
 closeMouse();
 isOpen=false;
 }else{
 openMouse();
 isOpen=true;
 }
 },500);
</script>

Related articles: