1.效果图
2.less
* {
margin: 0;
padding: 0;
#wrap {
width: 600px;
height: 800px;
background: tomato;
border: 2px solid white;
border-radius: 5%;
//这里不可使用position,会影响画布的坐标定位
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: 5% auto;
font: 20px "微软雅黑";
text-align: center;
color: white;
transition: 2s;
&:hover {
background: steelblue;
}
h1 {
margin-top: 8%;
}
h2 {
margin-left: 30%;
margin-top: 100%;
}
#wrap-canvas {
position: absolute;
background: white;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
}
}
3.js
$(function(){
var canvasNode=document.querySelector("#wrap-canvas");
canvasNode.background="white";
if(canvasNode.getContext){
var canvasPen=canvasNode.getContext("2d");
var imgFlag=0;
var translatex=0;
setInterval(function(){
canvasPen.clearRect(0,0,canvasNode.width,canvasNode.height);
imgFlag++;
translatex+=10;
if(imgFlag==9){
imgFlag=1;
}
var img=new Image();
img.src="img/q_r"+(imgFlag)+".jpg";
img.onload=function(){
imgDraw(this);
}
},7000/60);
function imgDraw(img){
canvasPen.drawImage(img,translatex,translatex);
};
canvasPen.save();
canvasPen.fillStyle="tomato";
canvasPen.beginPath();
canvasPen.fillRect(250,250,100,100);
canvasPen.restore();
}
})
4.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no" />
<title>canvas动画-fly bird</title>
</head>
<link rel="stylesheet" href="../6-bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="css/14-canvas-flybird.css" />
<body>
<div id="wrap">
<h1>canvas动画-fly bird</h1>
<canvas id="wrap-canvas" width="500px" height="500px"></canvas>
<h2>PS:一刀coder</h2>
</div>
</body>
<script type="text/javascript" src="../6-bootstrap/js/jquery.min.js"></script>
<script type="text/javascript" src="../6-bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/14-canvas-flybird.js"></script>
</html>