-
Notifications
You must be signed in to change notification settings - Fork 0
/
future.js
80 lines (65 loc) · 2.62 KB
/
future.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
// Future time array (data to plot after the submit button is pressed)
const future = [
{ timestamp: 7, value: 18 },
{ timestamp: 8, value: 22 },
{ timestamp: 9, value: 19 },
{ timestamp: 10, value: 25 }
];
// Get the submit button
const submitBtn = document.getElementById('submitBtn');
// Function to clear the canvas
function clearDrawingCanvas() {
drawingCtx.clearRect(0, 0, drawingCanvas.width, drawingCanvas.height);
console.log('Canvas cleared');
}
// Function to animate the future data being drawn from left to right
function animateFutureData() {
const padding = 0;
const height = drawingCanvas.height;
const width = drawingCanvas.width;
// Find max and min values for scaling
const maxY = Math.max(...future.map(d => d.value));
const minY = Math.min(...future.map(d => d.value));
const maxX = Math.max(...future.map(d => d.timestamp));
const minX = Math.min(...future.map(d => d.timestamp));
// Scale the data to fit the canvas
function scaleX(value) {
return padding + ((value - minX) / (maxX - minX)) * (width - 2 * padding);
}
function scaleY(value) {
return height - padding - ((value - minY) / (maxY - minY)) * (height - 2 * padding);
}
// Animation variables
let currentPoint = 0; // Track the current point to be drawn
// Animation function to draw the line progressively
function draw() {
// Clear the canvas before each frame
clearDrawingCanvas();
// Draw the future line up to the current point
drawingCtx.beginPath();
drawingCtx.moveTo(scaleX(future[0].timestamp), scaleY(future[0].value));
for (let i = 1; i <= currentPoint; i++) {
drawingCtx.lineTo(scaleX(future[i].timestamp), scaleY(future[i].value));
}
drawingCtx.strokeStyle = '#f00'; // Blue for the future data
drawingCtx.lineWidth = 2;
drawingCtx.stroke();
// Move to the next point if possible, with a delay between frames
if (currentPoint < future.length - 1) {
currentPoint++;
setTimeout(() => {
requestAnimationFrame(draw); // Call the next frame with a delay
}, 1200 / future.length); // Delay of 2000ms divided by the number of points
}
}
// Start the animation
draw();
}
// Submit button functionality: clears drawing and animates future data
submitBtn.addEventListener('click', function () {
console.log('Submit button pressed!'); // Check if button works
// Clear the drawing canvas
clearDrawingCanvas();
// Animate the future data being drawn
animateFutureData();
});