Public
Edited
Dec 19
Insert cell
Insert cell
viewof w = Weight()
Insert cell
w
Insert cell
Weight = () => {
const maxForce = 3.0;
const gramsPerForceUnit = 1000 / maxForce;

const output = htl.html`<output style="
font-size: 1.5em;
font-weight: bold;
margin: 10px 0;
display: block;
">`;

const status = htl.html`<div style="
color: #666;
margin-bottom: 10px;
">Click and press to measure</div>`;

const node = htl.html`<div style="font-family: system-ui, sans-serif;">
<div style="
padding: 20px;
border: 2px dashed #ccc;
border-radius: 8px;
text-align: center;
cursor: pointer;
transition: all 0.2s;
"
onmousedown=${handleForce}
onmousemove=${handleForce}
onmouseup=${handleReset}>
${status}
Current weight: ${output}g
</div>
</div>`;

function handleForce(event) {
if (typeof event.webkitForce !== "undefined") {
const force = event.webkitForce;
const grams = Math.round(force * gramsPerForceUnit);
set(node, grams);
status.textContent = "Measuring...";
event.target.style.background = "#e6f3ff";
event.target.style.borderColor = "#3b82f6";
} else {
status.textContent = "Force Touch not supported. Use Safari.";
event.target.style.background = "#fee2e2";
event.target.style.borderColor = "#ef4444";
}
}

function handleReset() {
status.textContent = "Click and press to measure";
event.target.style.background = "";
event.target.style.borderColor = "#ccc";
}

Object.defineProperty(node, "value", {
get() {
return +output.value || 0;
},
set(v) {
output.value = v;
}
});

// Set initial value to 0
node.value = 0;

return node;
}
Insert cell
function set(input, value) {
input.value = value;
input.dispatchEvent(new Event("input", { bubbles: true }));
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more