Skip to content

Commit

Permalink
LegendBox updates if a plot is dynamic (moves)
Browse files Browse the repository at this point in the history
Related to #72
  • Loading branch information
amaliejvik committed Jun 28, 2024
1 parent 4af059a commit f277f9d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
21 changes: 8 additions & 13 deletions src/Components/LegendBox.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import { largerEq } from "mathjs";
import Core from "../Core";
import renderToString from "katex";
import Plot from "./Plot";
import { GuiComponent } from "./interfaces";
import { Component, GuiComponent } from "./interfaces";
import "style.css";

//usikker på om denne trengs
type LegendBoxOptions = {
label: string;
};

const defaultLegendBoxOptions: LegendBoxOptions = {
label: "",
};
import { OrthographicCamera } from "three";

class LegendBox implements GuiComponent {
private core: Core;
htmlElement: HTMLElement;
// observers: (() => void)[];

constructor(core: Core, options?: LegendBoxOptions) {
constructor(core: Core) {
this.core = core;
const legendBoxWrapper = document.createElement("div");
legendBoxWrapper.className = "legendBox-wrapper";
Expand All @@ -34,9 +24,14 @@ class LegendBox implements GuiComponent {

legendBoxWrapper.appendChild(button);
this.updatePlots();

Plot.emitter.on("expressionUpdated", (plot) => {
this.updatePlots();
});
}

updatePlots() {
this.htmlElement.innerHTML = "";
for (const component of this.core.getComponents()) {
if (!(component instanceof Plot)) {
continue;
Expand Down
3 changes: 3 additions & 0 deletions src/Components/Plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Vector3, CatmullRomCurve3, Vector2, OrthographicCamera } from "three";
import { parse } from "mathjs";
import { Line2, LineGeometry, LineMaterial } from "three-fatline";
import { Component } from "./interfaces";
import { EventEmitter } from "events";

type PlotOptions = {
hideFromLegend?: boolean;
Expand Down Expand Up @@ -47,6 +48,7 @@ class Plot extends Component {
private DEFAULT_RENDERTHRESHOLD = 1400;

private plotMaterial: LineMaterial;
static emitter = new EventEmitter();

constructor(func: string, options?: PlotOptions) {
super();
Expand Down Expand Up @@ -135,6 +137,7 @@ class Plot extends Component {
public setExpression(expression: string): void {
this.func = expression;
this.reRenderPlot(this.currentMinX, this.currentMaxX);
Plot.emitter.emit("expressionUpdated", this);
}

private reRenderPlot(minX: number, maxX: number): void {
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const plot3 = new Plot("x^4");
const plot4 = new Plot("x^5");
const plot5 = new Plot("e^x + 2x + log(x)", { hideFromLegend: true });
const plot6 = new Plot("x^2 + 2x + 1");
const plot7 = new Plot("e^(log(x)) + 2x + 1/2");

const core = new Core();
const legend = new LegendBox(core);
Expand All @@ -43,9 +42,10 @@ core.add(plot3);
core.add(plot4);
core.add(plot5);
core.add(plot6);
core.add(plot7);

core.addGui(legend);
console.log("hei");
//core.run();

core.run();
core.run((t) => {
plot1.setExpression(`x^2+${(t / 10).toFixed(2)}`);
});

0 comments on commit f277f9d

Please sign in to comment.