<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>接金币</title>
<style>
body{margin:0;}
canvas{background:#0FC;border:4px solid #333;-webkit-border-radius:10px;-moz-border-radius:10px;margin:20px auto;display:block;}
</style>
<script>
/*绘制画布*/
function animateCanvas(event){
if(s){
ctx.clearRect( x, y, 50, 50);
y = y+8;
// x = x;
if( y <= canvas.height - 50 -40){
ctx.drawImage(img, x, y);
}
else {
if (x >= boxX && x + 50 <= boxX + 150){
alert("Yes");
}
else {
alert("No");
}
s=false;
}
}
}
</script>
</head>
<body>
<canvas id="canvas" width="350" height="430"></canvas>
<script>
/*主函数*/
var x = 0; var y = 0; var s = true;
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var img = new Image();
img.src = 'http://tp1.sinaimg.cn/1409181652/50/1290411958/0';
canvas.onmousemove = function(event){
msX = event.clientX;
msY = event.clientY;
ctx.clearRect( 0, canvas.height-40, canvas.width, 40);
boxX = msX - canvas.offsetLeft;
// document.title = msX +","+msY;
ctx.fillRect(boxX, 400, 150, 40);
};
canvas.onmouseout = function(event){
if (event.clientX <= canvas.offsetLeft ){
ctx.fillRect(0, 400, 150, 40);
}
if (event.clientX >= canvas.offsetLeft + canvas.width ){
ctx.fillRect(canvas.width - 150, 400, 150, 40);
}
}
setInterval(function(){animateCanvas()}, 60);
</script>
</body>
</html>
|