-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major correction to explor. spaces 4.5
- Loading branch information
Showing
10 changed files
with
984 additions
and
239 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
dist/demo/articles/exploring_spaces_4_and_a_half/dragging1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
dist/demo/articles/exploring_spaces_4_and_a_half/draggingCircleOld.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.