-
Notifications
You must be signed in to change notification settings - Fork 0
/
move2d.js
143 lines (133 loc) · 5.76 KB
/
move2d.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
MOVEMENT_VELOCITY = 1;
duration = 0.01; // every frame in seconds
let canvas = document.getElementById('canvas2d'),
ctx = canvas.getContext('2d'),
keyPresses = {},
positionX = 40, positionY = 40,
img = new Image(),
x = [], y = [];
function moveTo(
from = [ 40, 40], // x, y
to = [150, 150], // x, y
velocity = 50 // velocity in pixels per second
){
document.getElementById('b1').disabled = true;
document.getElementById('b2').disabled = true;
positionX = from[0], positionY = from[1]; // starting positions
var coordinates = [
{
x : positionX,
y : positionY
}
]
lx = to[0] - positionX, // projections
ly = to[1] - positionY,
l = Math.sqrt( lx**2 + ly**2 ); // total way length
velocityX = velocity * lx / l, // projections, according to similar triangles and to vector direction (lx and ly sign)
velocityY = velocity * ly / l;
time = performance.now();
myTimer = setInterval(function() {
if ( Math.abs(to[0] - positionX) < 1 && Math.abs(to[1] - positionY) < 1 ) {
time = performance.now() - time;
console.log('script running time is ', time, 'milliseconds');
document.getElementById('b1').disabled = false;
document.getElementById('b2').disabled = false;
clearInterval(myTimer); // exit from setInterval
}
ctx.clearRect(0, 0, canvas.width, canvas.height); // clearing the area before drawing a new frame
//console.log(positionX + ' ' + positionY + ' ' + velocityX + ' ' + velocityY); // for testing and debugging
ctx.drawImage( img, positionX += velocityX * duration, // calculation for seconds, not milliseconds
positionY += velocityY * duration);
coordinates.push({
x : positionX,
y : positionY
});
if ( positionX < -20 || positionX > canvas.width + 20 || positionY < -20 || positionY > canvas.height + 20 ){
location.reload();
clearInterval(myTimer); // exit from setInterval
}
}, duration * 1000); // every frame, just send milliseconds to the setInterval function
return coordinates;
}
function moveWithAcceleration(
from = [ 30, 30], // x, y
to = [450, 450], // x, y
startingVelocity = -40, // velocity in pixels per second
acceleration = 20 // acceleration in pixels per square second
){
document.getElementById('b1').disabled = true;
document.getElementById('b2').disabled = true;
positionX = from[0], positionY = from[1]; // starting positions
var coordinates = [
{
x : positionX,
y : positionY
}
]
lx = to[0] - positionX, // projections
ly = to[1] - positionY,
l = Math.sqrt( lx ** 2 + ly ** 2 ); // total way length
startingVelocityX = startingVelocity * lx / l, // projections, according to similar triangles and to vector direction (lx and ly sign)
startingVelocityY = startingVelocity * ly / l,
accelrationX = acceleration * lx / l,
accelrationY = acceleration * ly / l,
velocityX = startingVelocityX,
velocityY = startingVelocityY;
time = performance.now();
myTimer = setInterval(function() {
if ( Math.abs(to[0] - positionX) < 1 && Math.abs(to[1] - positionY) < 1 ) {
time = performance.now() - time;
console.log('script running time is ', time, 'milliseconds');
document.getElementById('b1').disabled = false;
document.getElementById('b2').disabled = false;
clearInterval(myTimer); // exit from setInterval
}
ctx.clearRect(0, 0, canvas.width, canvas.height); // clearing the area before drawing a new frame
//console.log(positionX + ' ' + positionY + ' ' + velocityX + ' ' + velocityY); // for testing and debugging
velocityX += accelrationX * duration; // velocity projection change according to acceleration
velocityY += accelrationY * duration;
ctx.drawImage( img, positionX += velocityX * duration, // calculation for seconds, not milliseconds
positionY += velocityY * duration);
coordinates.push({
x : positionX,
y : positionY
});
if ( positionX < -20 || positionX > canvas.width + 20 || positionY < -20 || positionY > canvas.height + 20 ){
location.reload();
clearInterval(myTimer); // exit from setInterval
}
}, duration * 1000); // every frame, just send milliseconds to the setInterval function
return coordinates;
}
window.addEventListener('keydown', keyDownListener);
function keyDownListener(event) {
keyPresses[event.key] = true;
}
window.addEventListener('keyup', keyUpListener);
function keyUpListener(event) {
keyPresses[event.key] = false;
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (keyPresses.w)
moveCharacter(0, -MOVEMENT_VELOCITY);
if (keyPresses.s)
moveCharacter(0, MOVEMENT_VELOCITY);
if (keyPresses.a)
moveCharacter( -MOVEMENT_VELOCITY, 0);
if (keyPresses.d)
moveCharacter( MOVEMENT_VELOCITY, 0);
ctx.drawImage(img, positionX, positionY);
window.requestAnimationFrame(gameLoop);
}
function moveCharacter(deltaX, deltaY) {
positionX += deltaX;
positionY += deltaY;
}
function loadImage() {
img.src = 'img.png';
img.onload = function() {
window.requestAnimationFrame(gameLoop);
};
}
loadImage();