Skip to content

Commit

Permalink
Invisible root pendulum
Browse files Browse the repository at this point in the history
  • Loading branch information
GniLudio committed May 18, 2024
1 parent 11348b0 commit 2f6314f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
8 changes: 6 additions & 2 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let pendulum = new Pendulum();
/**
* The pendulum count (of real pendulums).
*/
let pendulumCount = Math.max(3, parseInt(urlParameter.get("count") ?? randomIntInRange(5, 10).toFixed()));
let pendulumCount = Math.max(2, parseInt(urlParameter.get("count") ?? randomIntInRange(5, 10).toFixed())) + 1;
/**
* The animation speed.
*/
Expand Down Expand Up @@ -84,8 +84,12 @@ function generateRandomPendulumTree() {
}
// 3: connect the remaining two nodes
connect(list[0], list[1]);
// make graph directed
makeDirected(nodes[0]);
// the root pendulum is just a dummy (container)
nodes[0].isDummy = true;
// assign the root pendulum
pendulum = nodes[0];
makeDirected(pendulum);
function connect(a, b) {
nodes[a].children.push(nodes[b]);
nodes[b].children.push(nodes[a]);
Expand Down
7 changes: 6 additions & 1 deletion build/pendulum.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"use strict";
console.log("pendulum.ts loaded");
class Pendulum {
/**
* Whether this is just a dummy. (invisible container for children)
*/
isDummy;
/**
* The length.
*/
Expand Down Expand Up @@ -51,6 +55,7 @@ class Pendulum {
* Spinner - Fast rotation speed
* Cleaner - Same color as background
*/
this.isDummy = isDummy;
this.length = {
base: randomIntInRange(100, 250),
amplitude: randomInRange(10, 25),
Expand Down Expand Up @@ -84,7 +89,7 @@ class Pendulum {
const x = parentX + this.getCurrentValue(this.length) * Math.cos(angle * Math.PI / 180);
const y = parentY + this.getCurrentValue(this.length) * Math.sin(angle * Math.PI / 180);
const currentColor = this.updateColor(deltaTime);
if (this.lastPosition) {
if (!this.isDummy && this.lastPosition) {
context.beginPath();
context.strokeStyle = currentColor;
context.lineWidth = Math.max(this.getCurrentValue(this.width), 1);
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let pendulum: Pendulum = new Pendulum();
/**
* The pendulum count (of real pendulums).
*/
let pendulumCount: number = Math.max(3, parseInt(urlParameter.get("count") ?? randomIntInRange(5, 10).toFixed()));
let pendulumCount: number = Math.max(2, parseInt(urlParameter.get("count") ?? randomIntInRange(5, 10).toFixed())) + 1;

/**
* The animation speed.
Expand Down Expand Up @@ -95,9 +95,15 @@ function generateRandomPendulumTree(): void {
}
// 3: connect the remaining two nodes
connect(list[0], list[1]);
pendulum = nodes[0];

makeDirected(pendulum);
// make graph directed
makeDirected(nodes[0]);

// the root pendulum is just a dummy (container)
nodes[0].isDummy = true;

// assign the root pendulum
pendulum = nodes[0];

function connect(a: number, b: number) {
nodes[a].children.push(nodes[b]);
Expand Down
8 changes: 7 additions & 1 deletion src/pendulum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
console.log("pendulum.ts loaded");

class Pendulum {
/**
* Whether this is just a dummy. (invisible container for children)
*/
public isDummy: boolean;

/**
* The length.
*/
Expand Down Expand Up @@ -65,6 +70,7 @@ class Pendulum {
* Spinner - Fast rotation speed
* Cleaner - Same color as background
*/
this.isDummy = isDummy;
this.length = {
base: randomIntInRange(100, 250),
amplitude: randomInRange(10, 25),
Expand Down Expand Up @@ -100,7 +106,7 @@ class Pendulum {
const x = parentX + this.getCurrentValue(this.length) * Math.cos(angle * Math.PI / 180);
const y = parentY + this.getCurrentValue(this.length) * Math.sin(angle * Math.PI / 180);
const currentColor = this.updateColor(deltaTime);
if (this.lastPosition) {
if (!this.isDummy && this.lastPosition) {
context.beginPath();
context.strokeStyle = currentColor;
context.lineWidth = Math.max(this.getCurrentValue(this.width), 1);
Expand Down

0 comments on commit 2f6314f

Please sign in to comment.