通过不断更新物体的坐标位置使其追随鼠标指针的位置,就可以实现在canvas元素上拖拽物体。
1、捕捉 mousedown 事件,判断鼠标坐标是否在物体所在的坐标范围内;
2、如果是,则表示鼠标点击在物体上,此时为 canvas 添加 mousemove 事件的处理程序,并在该处理程序内将物体的 x、y 坐标更新为鼠标指针当前的坐标位置;
3、最后,在 mouseup 事件触发时,将 mousemove 事件的处理程序移除。
代码实现如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,width=device-width">
<title></title>
<style>
*{margin: 0;padding: 0}
body {
background-color: #eee
}
canvas {
background-color: #fff
}
</style>
</head>
<body>
<canvas id="canvas" width="600" height="400">
Your browser does not support HTML5 Canvas.
</canvas>
<script>
(function(){
window.addEventListener("load", eventWindowLoaded, false);
function eventWindowLoaded(){
canvasApp();
}
function canvasSupport(e){
return !!e.getContext;
}
function canvasApp(){
let canvas = document.getElementById("canvas");
if(!canvasSupport(canvas)){
return ;
}
// start
let context = canvas.getContext("2d");
drawScreen(canvas, context);
}
// write your codes
function drawScreen(canvas, context){
let mouse = captureMouse(canvas);
let ball = new Ball();
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
canvas.addEventListener("mousedown", function() {
if (containsPoint(ball.getBounds(), mouse.x, mouse.y)) {
canvas.addEventListener("mouseup", onMouseUp, false);
canvas.addEventListener("mousemove", onMouseMove, false);
}
}, false);
function onMouseUp() {
canvas.removeEventListener("mouseup", onMouseUp, false);
canvas.removeEventListener("mousemove", onMouseMove, false);
}
function onMouseMove() {
ball.x = mouse.x;
ball.y = mouse.y;
}
(function drawFrame() {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
ball.draw(context);
} ())
}
})();
</script>
<script>
function containsPoint(rect, x, y) {
return !(x < rect.x || x > rect.x + rect.width || y < rect.y || y > rect.y + rect.height);
}
function captureMouse(element) {
var mouse = {x: 0, y: 0, event: null},
body_scrollLeft = document.body.scrollLeft,
body_scrollTop = document.body.scrollTop,
element_scrollLeft = document.documentElement.scrollLeft,
element_scrollTop = document.documentElement.scrollTop,
offsetLeft = element.offsetLeft,
offsetTop = element.offsetTop;
element.addEventListener("mousemove", function(){
var x, y;
if(event.pageX || event.pageY){
x = event.pageX;
y = event.pageY;
}else{
x = event.clientX + body_scrollLeft + element_scrollLeft;
y = event.clientY + body_scrollTop + element_scrollTop;
}
x -= offsetLeft;
y -= offsetTop;
mouse.x = x;
mouse.y = y;
mouse.event = event;
}, false);
return mouse;
}
</script>
<script>
function Ball(radius, color) {
if (radius === undefined) {
radius = 40;
}
if (color === undefined) {
color = "#ff0000";
}
this.x = 0;
this.y = 0;
this.color = color;
this.radius = radius;
}
Ball.prototype.draw = function(context) {
context.save();
context.translate(this.x, this.y);
context.fillStyle = this.color;
context.beginPath();
context.arc(0, 0, this.radius, 0, 360 * Math.PI / 180, false);
context.closePath();
context.fill();
context.restore();
}
Ball.prototype.getBounds = function() {
return {
x: this.x - this.radius,
y: this.y - this.radius,
width: this.radius * 2,
height: this.radius * 2
};
}
</script>
</body>
</html>