Skip to content

Commit 9253652

Browse files
adjust random behaviour (#245)
* cleanup * cleanup * cleanup * remove unnecessary condition for friction of the spring * fix dot movement when switching canvas or loading project ...while random notes is activated * fix dot movement after random notes * fix dot movement after randomNotes Co-authored-by: Niklas Kramer <> Co-authored-by: Simon Thormeyer <simonthormeyer@gmail.com>
1 parent 04c6007 commit 9253652

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

Frontend/src/components/threeJS/Dot.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Dot = forwardRef((props, ref) => {
1919
// local
2020
const [dragging, setDragging] = useState(false);
2121
const dragAnimationRunning = useRef(false);
22+
const randomNotesAnimationFinishing = useRef(false);
2223
const [animatedPosition, setAnimatedPosition] = useState([canvases[canvasId][props.name].x, canvases[canvasId][props.name].y, 0]);
2324
const [position, setPosition] = useState(animatedPosition);
2425
const beforeDragPosition = useRef(
@@ -33,7 +34,7 @@ const Dot = forwardRef((props, ref) => {
3334
// store.js
3435
const hasLoaded = useStore(state => state.functions.dotHasLoaded);
3536
const loadingProject = useStore(state => state.loadingProject);
36-
37+
3738

3839
// save current dot position into canvas with the given id
3940
const saveCurrentPositionInGlobalState = useCallback((canvasId) => {
@@ -49,16 +50,22 @@ const Dot = forwardRef((props, ref) => {
4950
}, [animatedPosition, canvases, props.name, setCanvases])
5051

5152

52-
53-
5453
useSpring({
5554
posX: position[0], posY: position[1],
56-
config: { mass: 5, tension: 1000, friction: 50, precision: 0.0000001 },
55+
config: {
56+
mass: 5,
57+
tension: (dragAnimationRunning.current || loadingDotPosition) ? 1000 :
58+
randomNotesRunning[canvasId] || randomNotesAnimationFinishing.current ? 20 : 1000,
59+
friction: 50,
60+
precision: 0.01
61+
},
62+
5763
// position change will be canceled or not be exectued if this is true
5864
pause: !(randomNotesRunning[canvasId] || dragAnimationRunning.current || loadingDotPosition || !loadingProject),
5965
onRest: () => {
6066
saveCurrentPositionInGlobalState(canvasId);
6167
dragAnimationRunning.current = false;
68+
randomNotesAnimationFinishing.current = false;
6269
setLoadingDotPosition(false);
6370
hasLoaded();
6471
if (randomNotesRunning[canvasId] && !dragging) {
@@ -81,14 +88,18 @@ const Dot = forwardRef((props, ref) => {
8188
let x = Math.random() * viewport.width - viewport.width / 2;
8289
let y = Math.random() * viewport.height - viewport.height / 2;
8390
setPosition([x, y, 0]);
91+
} else {
92+
let posAfterRandomNotes = ref.current.position.clone();
93+
randomNotesAnimationFinishing.current = true; // careful, ugly quickfix
94+
setPosition([posAfterRandomNotes.x, posAfterRandomNotes.y, 0])
8495
}
85-
}, [randomNotesRunning, canvasId, viewport.height, viewport.width])
96+
}, [randomNotesRunning, canvasId, viewport.height, viewport.width, ref])
8697

8798
// when canvas changes, change position accordingly
8899
useEffect(() => {
89100
let switchingCanvas = (canvasId !== oldCanvasId.current)
90101
if (switchingCanvas || loadingProject) {
91-
if(switchingCanvas) saveCurrentPositionInGlobalState(oldCanvasId.current);
102+
if (switchingCanvas) saveCurrentPositionInGlobalState(oldCanvasId.current);
92103
setLoadingDotPosition(true);
93104
oldCanvasId.current = canvasId;
94105
setPosition([
@@ -114,7 +125,7 @@ const Dot = forwardRef((props, ref) => {
114125
dragAnimationRunning.current = true;
115126
setPosition([
116127
clamp(beforeDragPosition.current[0] + mx / aspect, left, right),
117-
clamp(- my / aspect + beforeDragPosition.current[1], top, bottom),
128+
clamp(-my / aspect + beforeDragPosition.current[1], top, bottom),
118129
0]);
119130
}),
120131
onDragEnd: () => {
@@ -139,7 +150,6 @@ const Dot = forwardRef((props, ref) => {
139150
}, [animatedPosition, camera, canvasId, musicCtrl, props.name, ref])
140151

141152

142-
143153
return (
144154
<a.mesh
145155
{...props}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export class RandomNotes {
22

3-
constructor(){
3+
constructor() {
44
this.time = undefined;
55
this.xAxis = 0;
66
this.yAxis = 0;
@@ -9,8 +9,8 @@ export class RandomNotes {
99
this.timeout = undefined;
1010
}
1111

12-
toggleRandomNotes () {
13-
if(this.running === false) {
12+
toggleRandomNotes() {
13+
if (this.running === false) {
1414
this.running = true;
1515
this.playRandomNote();
1616
} else {
@@ -19,16 +19,16 @@ export class RandomNotes {
1919
}
2020
}
2121

22-
playRandomNote () {
23-
if(this.running === true) {
22+
playRandomNote() {
23+
if (this.running === true) {
2424
let xAxis = Math.random();
2525
let yAxis = Math.random();
2626
if (this._simulateCanvasClick) {
2727
this._simulateCanvasClick([xAxis, yAxis]);
2828
}
29-
let time = Math.floor((Math.random() * 1000) + 50);
29+
let time = Math.floor((Math.random() * 3000) + 50);
3030
this.timeout = setTimeout(() => this.playRandomNote(), time);
31-
};
32-
31+
}
32+
;
3333
}
3434
}

0 commit comments

Comments
 (0)