Public
Edited
Sep 27, 2023
Importers
1 star
Insert cell
Insert cell
viewof w = vectorInput({
label: "a vector",
max: 100,
step: 5,
value: [10, 10],
size: 80
})
Insert cell
w
Insert cell
function vectorInput(options = {}) {
let {
label = options.label,
value = [0, 0],
max = 10,
step = 0.1,
arrowSize = 8,
arrowAngle = Math.PI / 3,
size = 100
} = options;
const div = html`<div style="margin:2px;display:flex" >`;
const ctx = DOM.context2d(size, size);
const canvas = ctx.canvas;
if (label) {
const labelDiv = htl.html`<div>${label}`;
Object.assign(labelDiv.style, {
font: "12px sans-serif",
display: "inline-block",
"min-width": "120px"
});
div.append(labelDiv);
}
div.append(canvas);
const draw = () => {
ctx.clearRect(0, 0, size, size);
ctx.fillStyle = "rgb(200,200,200)";
ctx.fillRect(0, 0, size, size);
ctx.strokeStyle = ctx.fillStyle = "black";
ctx.beginPath();
if (value[0] == 0 && value[1] == 0) {
ctx.arc(size / 2, size / 2, 2, 0, Math.PI * 2);
ctx.fill();
} else {
let ang = Math.atan2(value[1], value[0]);
ctx.moveTo(size / 2, size / 2);
let [x, y] = [
((value[0] / max) * size + size) / 2,
((value[1] / max) * size + size) / 2
];
ctx.lineTo(x, y);
const a = ang + arrowAngle / 2 + Math.PI,
b = ang - arrowAngle / 2 + Math.PI;
const r = arrowSize;
ctx.lineTo(x + r * Math.cos(a), y + r * Math.sin(a));
ctx.lineTo(x + r * Math.cos(b), y + r * Math.sin(b));
ctx.lineTo(x, y);
ctx.fill();
}
ctx.stroke();
};
draw();
const xy = xyInput([-max, max], { step });
xy.style.marginLeft = "2px";
//Inputs.bind(xy, div);
div.append(xy);
// Update the display whenever the value changes
Object.defineProperty(div, "value", {
get() {
return value;
},
set(v) {
value = v;
draw();
}
});
div.value = value;
Inputs.bind(xy, div);
//Inputs.bind(xyInput, div);
canvas.onmousedown = canvas.onmousemove = (e) => {
if (e.buttons) {
value = [e.offsetX, e.offsetY].map(
(x) => Math.round((((x - size / 2) / size) * 2 * max) / step) * step
);
draw();
set(div, value);
}
};
return div;
}
Insert cell
function set(input, value) {
input.value = value;
input.dispatchEvent(new Event("input", { bubbles: true })); // Native events bubble, so we should too
}
Insert cell
function xyInput([min, max] = [0, 100], options = {}) {
let { label = options.label, value = [0, 0], step = 0.1 } = options;
const div = htl.html`<div style="max-width:120px">`;
const x = Inputs.number([min, max], { step, value: value[0] });
const y = Inputs.number([min, max], { step, value: value[1] });
div.append(x, y);
Object.defineProperty(div, "value", {
get() {
return [x.value, y.value];
},
set(v) {
[x.value, y.value] = v;
}
});
div.value = value;
return div;
}
Insert cell
viewof v = xyInput()
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