var canvas=document.getElementById("canvas");
var cxt=canvas.getContext("2d");
var centerX=canvas.width/2;
var centerY=canvas.height/2;
function ball(x,y,radius,speed){
this.x=x;
this.y=y;
this.radius=radius;
this.speed=speed;
}
var speed={x:0,y:0}
var b=[];
b.push(new ball(centerX,centerY,150,speed))
function draw(){
var d=b[0];
cxt.clearRect(0,0,canvas.width,canvas.height)
cxt.beginPath();
cxt.arc(d.x,d.y,d.radius,0,Math.PI*2,true);
cxt.fillStyle="red";
cxt.fill();
cxt.closePath()
}
draw();
function isMoveUpBall(d,x,y){
var dx=x-d.x;
var dy=y-d.y;
var dist=Math.sqrt(dx*dx+dy*dy);
return dist<d.radius;
}
function isMoveUp(body,x,y){
return x>body.x && x<body.x+body.width && y>body.y && y<body.height+body.y;
}
var isUp=1;
var d=b[0];
var oldX = 0;
var oldY = 0;
function move(event){
var x=event.point.x,y=event.point.y;
var body={x:d.x-d.radius,y:d.y-d.radius,width:d.radius*2,height:d.radius*2};
if(isUp==0 && isMoveUpBall(d,x,y))
{
console.log(oldX,oldY)
d.x = x+(-oldX);
d.y = y+(-oldY);
d.speed.x = d.x - oldX;
d.speed.y = d.y - oldY;
}
draw();
}
var CsX=centerX,CsY=centerY;
function up(event){
isUp=1;
CsX=d.x;
CsY=d.y;
console.log(CsX,CsY)
}
function down(event){
isUp=0;
oldX = event.point.x-CsX;
oldY = event.point.y-CsY;
draw();
}
tool.MT(canvas,move,down,up);