Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
/** @jsxImportSource npm:react **/
import { useEffect, useState } from "npm:react";
import { renderToString } from "npm:react-dom@18/server";
const TrackpadScale = () => {
const [forceText, setForceText] = useState("Place object here");
useEffect(() => {
const maxForce = 3.0;
const gramsPerForceUnit = 1000 / maxForce;
const handleForce = (event: MouseEvent) => {
if ("webkitForce" in event) {
const force = (event as any).webkitForce;
const grams = Math.round(force * gramsPerForceUnit);
setForceText(`Weight: ${grams}g`);
} else {
setForceText("Force Touch not supported");
}
};
const resetText = () => setForceText("Place object here");
document.addEventListener("mousedown", handleForce);
document.addEventListener("mousemove", handleForce);
document.addEventListener("mouseup", resetText);
return () => {
document.removeEventListener("mousedown", handleForce);
document.removeEventListener("mousemove", handleForce);
document.removeEventListener("mouseup", resetText);
};
}, []);
return (
<div className="container">
<h1>Trackpad Scale</h1>
<h2>OPEN IN SAFARI</h2>
<h3>Weigh things on your trackpad</h3>
<p>
(This mostly works to see how hard you are pressing your trackpad. Once you click, you will see a weight in
grams. Then you can try placing something on the trackpad)
</p>
<div id="touchArea">{forceText}</div>
</div>
);
};
export default (req: Request) => {
return new Response(
renderToString(
<html>
<head>
<meta charSet="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Trackpad Scale</title>
<style>
{`
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
h1 {
color: #333;
margin-bottom: 10px;
}
h2 {
color: #666;
margin-bottom: 5px;
}
p {
color: #999;
margin-bottom: 20px;
}
#touchArea {
width: 300px;
height: 200px;
background-color: #4CAF50;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 24px;
cursor: pointer;
transition: background-color 0.3s;
}
#touchArea:active {
background-color: #45a049;
}
perbhat-mactrackpadscale.web.val.run
September 6, 2024