瞎整本来是要点击编辑多张图片的,没想到弄成这样。这不是电视剧里的降龙十八掌吗

特此记录留着以后用,看来canvas做游戏特效都不错啊。

canvas意料之外获得降龙十八掌的效果-LMLPHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body,div{
margin:;
padding:;
}
#d1{
width:500px;
height: 400px;
border: 2px solid #00FFD1;
}
</style>
</head>
<body>
<script src="js/jquery-3.3.1.min.js"></script>
<div id="d1">
<canvas id="myCanvas" width="" height="">
<p>您的系统不支持此程序!</p>
</canvas>
</div>
<img src="data:images/boss.png" alt="test01" id="img01" />
<img src="data:images/weixin.png" alt="test01" id="img02" />
<script>
var canva=document.getElementById("myCanvas");
var cansText=canva.getContext("2d");
var imgs=new Array("images/boss.png","images/weixin.png");
var img;
var draw=new Array();
var i=; $('#img01').on('click', function(){
cansText.save();
cansText.beginPath();
img = new Image();
img.src=imgs[];
cansText.drawImage(img,,,,);
cansText.closePath();
cansText.fill();
cansText.restore();
});
$('#img02').on('click', function(){
cansText.save();
cansText.beginPath();
img = new Image();
img.src=imgs[];
cansText.drawImage(img,,,,);
cansText.closePath();
cansText.fill();
cansText.restore();
}); ini();
function ini(){
img = new Image();
img.src=imgs[];
draw[i]=img;
cansText.drawImage(draw[i],,,,);
} //在事件外声明需要用到的变量
let ax,ay,x,y; canva.onmousedown=function (e) { canva.onmousemove = function(e){
x= e.clientX;y=e.clientY; //限制移动不能超出画布
(x<)? ax= : ax=;
(y<)? ay= : ay=; (x < && x >)? x =e.clientX : x =ax; (y > && y <) ? y=e.clientY : y=ay; //先清除之前的然后重新绘制,关键
// cansText.clearRect(0,0,canva.width,canva.height); cansText.drawImage(img,x-,y-,,);
}; canva.onmouseup = function(){
canva.onmousemove = null;
canva.onmouseup = null;
};
} </script>
</body>
</html>
04-26 12:38