Public
Edited
Dec 16, 2022
Importers
Insert cell
Insert cell
dallE20221210204519MouseCursorsFollowingCurvesOnAW = FileAttachment("DALL·E 2022-12-10 20.45.19 - mouse cursors following curves on a white background.png").image()
Insert cell
Insert cell
Drag = (callback) => {
const reset = {
event: null,
xo: 0,
yo: 0,
x: 0,
y: 0,
dx: 0,
dy: 0,
};
const context = { node: undefined };
const behaviour = {
context,
on: node => {
context.node = node;
on(node, behaviour.start);
return behaviour;
},
off: node => {
context.node = undefined;
off(node, behaviour.start);
off(node, behaviour.drag);
return behaviour;
},
start: {
mousedown: event => {
Object.assign(context, reset);
off(context.node, behaviour["start"]);
context.xo = event.pageX;
context.yo = event.pageY;
callback && callback(event, context, "dragstart");
on(context.overlay||window.document.body, behaviour["drag"]);
}
},
drag: {
mousemove: event => {
context.x = event.pageX;
context.y = event.pageY;
context.dx = context.x - context.xo;
context.dy = context.y - context.yo;
callback && callback(event, context, "drag");
},
mouseup: event => {
callback && callback(event, context, "dragend");
off(context.overlay||window.document.body, behaviour["drag"]);
on(context.node, behaviour["start"]);
}
}
};
return behaviour;
}
Insert cell
draggable = (node, callback) => {
Drag(callback).on(node);
return node
}
Insert cell
draggable(html.div({style:"position:relative;width:100px;height:100px;background:red;"}), (event,_,state)=>{
switch (state) {
case "dragend":
_.node.style.cursor = "default";
_.node.style.left = null;
_.node.style.top = null;
_.node.style.zIndex = null;
break
default:
_.node.style.cursor = "move";
_.node.style.left = _.dx + "px";
_.node.style.top = _.dy + "px";
_.node.style.zIndex = 100;
}})
Insert cell
editable = (node, callback) => {
const absorb = _ => _.preventDefault()
const behaviour = {
}
node.setAttribute("contenteditable", true);
return on(node, behaviour)
}
Insert cell
editable(html.div({style:"position:relative;width:100px;border:1px solid blue;overflow-wrap: break-word;"}))
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
import {on,off,html,style,idem} from "@sebastien/boilerplate"
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