-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglide2Click.js
More file actions
73 lines (69 loc) · 2.11 KB
/
glide2Click.js
File metadata and controls
73 lines (69 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var canvas = document.querySelector("canvas")
canvas.width = window.innerWidth
canvas.height = window.innerHeight
var ctx = canvas.getContext("2d")
class Circle{
constructor(x,y,radius,color){
this.x = x
this.y = y
this.radius = radius
this.color = color
}
//made by gurpreet
draw(){
ctx.fillStyle = this.color
ctx.strokeStyle = "black"
ctx.lineWidth = 10
ctx.beginPath()
ctx.arc(
this.x,
this.y,
this.radius,
0,
2*Math.PI
)
ctx.fill()
ctx.stroke()
}
//made by gurpreet
glideTo(glideToX, glideToY, speedToGlide) {
if (!this.isMoving) {
function animate(circleInstance) {
let dx = glideToX - circleInstance.x;
let dy = glideToY - circleInstance.y;
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 1) {
let stepX = dx / speedToGlide;
let stepY = dy / speedToGlide;
circleInstance.x += stepX;
circleInstance.y += stepY;
ctx.clearRect(0, 0, canvas.width, canvas.height);
circleInstance.draw();
requestAnimationFrame(() => animate(circleInstance));
} else {
circleInstance.x = glideToX;
circleInstance.y = glideToY;
circleInstance.isMoving = false;
}
}
this.isMoving = true;
animate(this);
} else {
// Update target coordinates
this.glideToX = glideToX;
this.glideToY = glideToY;
}
}
}
//made by gurpreet
addEventListener("click", function(event){
circle1.glideTo(event.x,event.y,speed)
})
addEventListener("keydown", function(event){
if(event.key == " "){
speed = Number(prompt("Enter a Number for Speed (Larger is Slower)"))
}
})
var speed = 14
var circle1 = new Circle(canvas.width/2,canvas.height/2,50,"red")
circle1.draw()