Skip to content

Commit

Permalink
Major correction to explor. spaces 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
vezwork committed May 29, 2024
1 parent 16f0aa6 commit 97fa515
Show file tree
Hide file tree
Showing 10 changed files with 984 additions and 239 deletions.
76 changes: 76 additions & 0 deletions dist/demo/articles/exploring_spaces_4_and_a_half/dragging1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { add, angleOf, length, setAngle, setLength, sub, } from "../../../lib/math/Vec2.js";
import { swapActive } from "./draggingCircle.js";
function makeDraggable(state, el, ...clones) {
let pos = [0, 0];
let dragging = null;
// modified from https://www.redblobgames.com/making-of/draggable/
function start(event) {
if (event.button !== 0)
return; // left button only
let { x, y } = state.eventToCoordinates(event);
dragging = [pos[0] - x, pos[1] - y];
el.classList.add("dragging");
for (const clone of clones)
clone.classList.add("dragging");
el.setPointerCapture(event.pointerId);
}
function end(_event) {
dragging = null;
el.classList.remove("dragging");
for (const clone of clones)
clone.classList.remove("dragging");
}
function move(event) {
if (dragging === null)
return;
let { x, y } = state.eventToCoordinates(event);
const newPos = [x + dragging[0], y + dragging[1]];
const diff = sub(newPos, pos);
pos = newPos;
state.movePos(diff);
}
el.addEventListener("pointerdown", start);
el.addEventListener("pointerup", end);
el.addEventListener("pointercancel", end);
el.addEventListener("pointermove", move);
el.addEventListener("touchstart", (e) => e.preventDefault());
}
function eventToSvgCoordinates(event, el = event.currentTarget) {
const svg = el.ownerSVGElement;
let p = svg.createSVGPoint();
p.x = event.clientX;
p.y = event.clientY;
p = p.matrixTransform(svg.getScreenCTM().inverse());
return p;
}
const v2 = ({ x, y }) => [x, y];
const xy = ([x, y]) => ({
x,
y,
});
const reverse = (v) => setLength(100 - length(v), setAngle(angleOf(v) + Math.PI)(v));
// Make the circle draggable only to the snap points
const el = document.querySelector("#dragging1 circle.draggable");
const clone = document.querySelector("#dragging1 circle.clone");
export const rp2State = {
eventToCoordinates: eventToSvgCoordinates,
dragging: null,
pos: [0, 0],
movePos(v) {
this.setPos(add(this.pos, v));
},
setPos(p) {
this.pos = p;
if (length(p) > 50) {
this.pos = reverse(p);
swapActive();
}
const cpos = reverse(this.pos);
clone?.setAttribute("cx", cpos[0]);
clone?.setAttribute("cy", cpos[1]);
el?.setAttribute("cx", this.pos[0]);
el?.setAttribute("cy", this.pos[1]);
},
};
makeDraggable(rp2State, el, clone);
makeDraggable(rp2State, clone, el);
78 changes: 68 additions & 10 deletions dist/demo/articles/exploring_spaces_4_and_a_half/draggingCircle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { saxis } from "./draggingSphereUsage.js";
// import { saxis } from "./draggingSphereUsage.js";
const mod = (a, n, nL = 0) => ((((a - nL) % (n - nL)) + (n - nL)) % (n - nL)) + nL;
function makeDraggable(state, el) {
// from https://www.redblobgames.com/making-of/draggable/
Expand Down Expand Up @@ -47,30 +47,88 @@ const makeIt = (el, width, f) => {
eventToCoordinates: eventToSvgCoordinates,
dragging: null,
_y: undefined,
_pos: undefined,
setActive(isActive) {
if (isActive) {
el.style.fill = "white";
el.style.stroke = "black";
// el.classList.add("draggable");
el.parentElement.append(el); // move to top of drawing order
}
else {
el.style.fill = "#bbb";
el.style.stroke = "#bbb";
// el.classList.remove("draggable");
}
},
get y() {
return this._y;
},
movey(n) {
this.sety(this._y + n);
},
sety(n) {
sety(n, shouldRecurse = true) {
this._y = n;
el.setAttribute("cy", mod(this._y, 0, width));
try {
f(this._y);
if (shouldRecurse) {
try {
f(this._y);
}
catch (e) { }
}
catch (e) { }
},
};
state.sety(20);
makeDraggable(state, el);
return state;
};
const s1 = makeIt(document.getElementsByClassName("draggable")[0], 200, (y) => {
window.scrub = y;
export const s1 = makeIt(document.getElementsByClassName("draggable-circle")[0], 200, (y) => {
window.scrub = s1 === active ? y : -y;
try {
s2.sety(-y, false);
document.getElementById("dragging1").style.width = Math.abs(mod(window.scrub, -100, 100) * 1.8);
document
.getElementById("dragging1")
.querySelector(".draggable")
?.setAttribute("r", Math.abs(600 / mod(window.scrub, -100, 100)));
document
.getElementById("dragging1")
.querySelector(".clone")
?.setAttribute("r", Math.abs(600 / mod(window.scrub, -100, 100)));
}
catch (e) { }
});
export const s2 = makeIt(document.getElementsByClassName("draggable-circle")[1], 200, (y) => {
window.scrub = s2 === active ? y : -y;
try {
saxis.radius(Math.abs(mod(y + 100, -100, 100)) / 2 + 6);
s1.sety(-y, false);
//saxis.radius(Math.abs(mod(y + 100, -100, 100)) / 2 + 6);
document.getElementById("dragging1").style.width = Math.abs(mod(window.scrub, -100, 100) * 1.8);
document
.getElementById("dragging1")
.querySelector(".draggable")
?.setAttribute("r", Math.abs(600 / mod(window.scrub, -100, 100)));
document
.getElementById("dragging1")
.querySelector(".clone")
?.setAttribute("r", Math.abs(600 / mod(window.scrub, -100, 100)));
}
catch (e) { }
});
window.scrub = 80;
s1.sety(80);
s2.sety(120);
export let active = s1;
s1.setActive(true);
s2.setActive(false);
export const swapActive = () => {
window.scrub = -window.scrub;
if (active === s1) {
s1.setActive(false);
s2.setActive(true);
active = s2;
}
else {
s2.setActive(false);
s1.setActive(true);
active = s1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { saxis } from "./draggingSphereUsage.js";
const mod = (a, n, nL = 0) => ((((a - nL) % (n - nL)) + (n - nL)) % (n - nL)) + nL;
function makeDraggable(state, el) {
// from https://www.redblobgames.com/making-of/draggable/
let pos = 0;
let dragging = null;
// modified from https://www.redblobgames.com/making-of/draggable/
function start(event) {
if (event.button !== 0)
return; // left button only
let { x, y } = state.eventToCoordinates(event);
dragging = pos - y;
el.classList.add("dragging");
// for (const clone of clones) clone.classList.add("dragging");
el.setPointerCapture(event.pointerId);
}
function end(_event) {
dragging = null;
el.classList.remove("dragging");
// for (const clone of clones) clone.classList.remove("dragging");
}
function move(event) {
if (dragging === null)
return;
let { x, y } = state.eventToCoordinates(event);
const newPos = y + dragging;
const diff = newPos - pos;
pos = newPos;
state.movey(diff);
}
el.addEventListener("pointerdown", start);
el.addEventListener("pointerup", end);
el.addEventListener("pointercancel", end);
el.addEventListener("pointermove", move);
el.addEventListener("touchstart", (e) => e.preventDefault());
}
function eventToSvgCoordinates(event, el = event.currentTarget) {
const svg = el.ownerSVGElement;
let p = svg.createSVGPoint();
p.x = event.clientX;
p.y = event.clientY;
p = p.matrixTransform(svg.getScreenCTM().inverse());
return p;
}
const makeIt = (el, width, f) => {
let state = {
eventToCoordinates: eventToSvgCoordinates,
dragging: null,
_y: undefined,
_pos: undefined,
get y() {
return this._y;
},
movey(n) {
this.sety(this._y + n);
},
sety(n) {
this._y = n;
el.setAttribute("cy", mod(this._y, 0, width));
try {
f(this._y);
}
catch (e) { }
},
};
state.sety(20);
makeDraggable(state, el);
return state;
};
const s1 = makeIt(document
.querySelector(".circle-input-old")
.querySelector(".draggable-circle-old"), 200, (y) => {
try {
saxis.radius(Math.abs(mod(y + 100, -100, 100)) / 2 + 6);
}
catch (e) { }
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createDot, sphereToTwoUnitDisks, twoUnitDisksToSphere, uiDiskToUnitDisk, unitDiskToUIDisk, } from "./draggingSphere.js";
import { createDot, sphereToTwoUnitDisks, twoUnitDisksToSphere, uiDiskToUnitDisk, unitDiskToHemisphere, unitDiskToUIDisk, } from "./draggingSphere.js";
import { v3normalize, v3rot } from "./v3.js";
import { qap, qfromAxisAngle } from "./quat.js";
import { saxis } from "./draggingSphereUsage.js";
import { mod } from "../../../lib/math/Number.js";
import { rp2State } from "./dragging1.js";
const SVG1 = document.querySelector("#dragging3a");
const SVG2 = document.querySelector("#dragging3b");
const SVG3 = document.querySelector("#dragging4a");
Expand Down Expand Up @@ -70,12 +70,12 @@ function draw() {
disk: s.disk,
v: uiDiskToUnitDisk(s.pos),
});
const rh = v3rot(twoUnitDisksToSphere({
disk: saxis.disk,
v: uiDiskToUnitDisk(saxis.pos),
}),
//unitDiskToHemisphere(uiDiskToUnitDisk(dotState.pos)) as V3,
(mod(window.scrub ?? 0, 400) / 400) * Math.PI * 2)(h);
const rh = v3rot(
// twoUnitDisksToSphere({
// disk: saxis.disk,
// v: uiDiskToUnitDisk(saxis.pos),
// }) as V3,
unitDiskToHemisphere(uiDiskToUnitDisk(rp2State.pos)), (mod(window.scrub ?? 0, 200) / 200) * Math.PI * 2)(h);
const go = sphereToTwoUnitDisks(rh);
otherS.pos = unitDiskToUIDisk(go.v);
otherS.disk = go.disk;
Expand Down
Loading

0 comments on commit 97fa515

Please sign in to comment.