aboutsummaryrefslogtreecommitdiffhomepage
path: root/main.js
blob: 57bf0e8c50c74e2b22491f0b7e0929c45b0e82f4 (plain) (blame)
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
/* Compute the total duration of the run */
function compute()
{
	const fps = parseInt(document.getElementById("framerate").value);
	const st = document.getElementById("startobj").value;
	const et = document.getElementById("endobj").value;

	if (st === undefined || et === undefined || fps === undefined)
		return;

	const frames = (et - st) * fps;
	const s = Math.round(frames / fps * 1000) / 1000;

	/* Show the time and mod message in the DOM */
	const sf = st * fps;
	const ef = et * fps;
	const t = time_format(s);
	const mod_message = `Mod Note: Retimed (Start Frame: ${
		sf}, End Frame: ${ef}, FPS: ${fps}, Total Time: ${t})`;

	document.getElementById("time").value = t;
	document.getElementById("mod_message").disabled = false;
	document.getElementById("mod_message").innerText = mod_message;
	document.getElementById("mod_message_button").disabled = false;
}

/* Convert seconds to human readable time */
function time_format(t)
{
	const h = ~~(t / 3600);
	const m = ~~((t % 3600) / 60);
	const s = ~~t % 60;
	let ret = "";

	if (h > 0)
		ret += h + ":" + (m < 10 ? "0" : "");

	ret += m + ":" + (s < 10 ? "0" : "");
	ret += s;

	/* Milliseconds */
	if (t % 1 != 0)
		ret += "." + t.toFixed(3).split(".")[1];
	else
		ret += ".000";

	return ret;
}

/* Allow user to copy mod message to clipboard */
function copy_mod_message()
{
	const text_area = document.getElementById("mod_message");
	text_area.focus();
	text_area.select();
	document.execCommand("copy");
}

/* If framerate is invalid, show an error message and disable start and end
 * frame fields */
function check_fps(event)
{
	fps = event.target.value;
	if (fps > 0 && fps % 1 == 0) {
		document.getElementById("startobj").disabled = false;
		document.getElementById("endobj").disabled = false;
		document.getElementById("compute_button").disabled = false;
	} else {
		document.getElementById("framerate")
			.setCustomValidity("Please enter a valid framerate.");
		document.getElementById("framerate").reportValidity();
		document.getElementById("startobj").disabled = true;
		document.getElementById("endobj").disabled = true;
		document.getElementById("compute_button").disabled = true;
	}
}

/* Get current frame from input field (either start time or end time) */
function parse_time(event)
{
	let inptext_frame = (JSON.parse(event.target.value)).lct;
	if (inptext_frame !== undefined) {
		const fps = parseInt(
			document.getElementById("framerate").value);
		const frame_from_obj = (t, fps) => Math.floor(t * fps) / fps;
		const fframe = frame_from_obj(inptext_frame, fps);
		document.getElementById(event.target.id).value = `${fframe}`;
	}
}