-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
84 lines (65 loc) · 2.15 KB
/
script.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
var inputLagSlider = document.getElementById("input-lag-slider");
var inputLagDisplay = document.getElementById("input-lag-display");
var fpsElem = document.getElementById("fps");
inputLagSlider.oninput = function (e) {
inputLagDisplay.textContent = inputLagSlider.valueAsNumber;
}
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext('2d', {
alpha: true,
desynchronized: !!window.useDesynchronizedCanvas,
});
var oldPos = {x : 0, y : 0};
var currPos = {x : 0, y : 0};
canvas.onmousemove = function(event) {
var r = canvas.getBoundingClientRect();
currPos.x = (event.clientX - r.left) * devicePixelRatio;
currPos.y = (event.clientY - r.top) * devicePixelRatio;
}
function updateCanvasSize() {
var r = canvas.getBoundingClientRect();
var newWidth = Math.round(r.width * devicePixelRatio);;
var newHeight = Math.round(r.height * devicePixelRatio);
if ( canvas.width != newWidth || canvas.height != newHeight ) {
canvas.width = Math.round(r.width * devicePixelRatio);
canvas.height = Math.round(r.height * devicePixelRatio);
}
}
function distance( p0, p1 ) {
var dx = p0.x - p1.x;
var dy = p0.y - p1.y;
return Math.sqrt(dx * dx + dy * dy);
}
function drawCircle( pos, radius, color ) {
if ( color == null ) strokeStyle = "black";
ctx.strokeStyle = color;
ctx.beginPath();
ctx.arc(pos.x, pos.y, radius, 0, 2 * Math.PI);
ctx.stroke();
}
var dist = 0;
var frames = 0;
var lastFpsTime = performance.now();
var lastRendertime = performance.now();
function update(now) {
var dt = now - lastRendertime;
lastRendertime = now;
updateCanvasSize();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.lineWidth = 2;
//dist += (distance(oldPos, currPos) - dist) * 0.05;
dist += (distance(oldPos, currPos)/dt-dist) * (1-Math.exp(-dt*0.008));
drawCircle( currPos, 2 );
drawCircle( currPos, inputLagSlider.valueAsNumber * dist, "red" );
oldPos.x = currPos.x;
oldPos.y = currPos.y;
frames++;
var now = performance.now();
if ( now - lastFpsTime > 500 ) {
fpsElem.textContent = frames * 1000 / (now - lastFpsTime);
lastFpsTime = now;
frames = 0;
}
requestAnimationFrame(update);
}
requestAnimationFrame(update);