Skip to content

Commit

Permalink
Various fixes, rename params on vector, make line and vector draggable
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasdeluna committed Oct 15, 2023
1 parent 1fd5add commit 84e50f1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 30 deletions.
23 changes: 16 additions & 7 deletions src/Components/Line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "three";
import { Line2, LineGeometry, LineMaterial } from "three-fatline";
import { toVector2 } from "../utils";
import { Collider, Component } from "./interfaces";
import { Collider, Component, Draggable } from "./interfaces";
import { InputPosition } from "./types";

const ARROWHEAD_LENGTH = 12;
Expand All @@ -20,6 +20,7 @@ export type LineOptions = {
dashed?: boolean;
opacity?: number;
transparent?: boolean;
draggable?: Draggable;
};

export const defaultLineOptions: LineOptions = {
Expand All @@ -29,24 +30,31 @@ export const defaultLineOptions: LineOptions = {
dashed: false,
opacity: 1,
transparent: false,
draggable: undefined,
};

class Line extends Component implements Collider {
start: InputPosition;
end: InputPosition;
draggable = undefined;
arrowhead: boolean;

constructor(start: InputPosition, end: InputPosition, options?: LineOptions) {
super();
const { color, lineWidth, arrowhead, dashed, opacity, transparent } = {
const {
color,
lineWidth,
arrowhead,
dashed,
opacity,
transparent,
draggable,
} = {
...defaultLineOptions,
...options,
};
this.start = start;
this.end = end;
this.arrowhead = arrowhead ?? false;

this.material = new LineMaterial({
color: color,
linewidth: lineWidth,
Expand All @@ -67,6 +75,7 @@ class Line extends Component implements Collider {
this.add(arrowheadLine);
}
this.initialUpdateGeometry(start, end);
this.draggable = draggable;
}

collidesWith(other: Object3D): boolean {
Expand Down Expand Up @@ -158,15 +167,15 @@ class Line extends Component implements Collider {
]);
}

setEnd(end: InputPosition) {
public setEnd(end: InputPosition) {
this.end = end;
}

setStart(start: InputPosition) {
public setStart(start: InputPosition) {
this.start = start;
}

setPosition(start: InputPosition, end: InputPosition) {
public setPosition(start: InputPosition, end: InputPosition) {
this.start = start;
this.end = end;
}
Expand Down
18 changes: 15 additions & 3 deletions src/Components/Vector.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import { Vector2, OrthographicCamera } from "three";
import { toVector2, toVector3 } from "./../utils";
import Line from "./Line";
import { Draggable } from "./interfaces";
import { InputPosition } from "./types";

const defaultVectorOptions = {
color: 0x000000,
normalize: true,
draggable: undefined,
};

export type VectorOptions = {
color?: number;
normalize?: boolean;
draggable?: Draggable;
};

class Vector extends Line {
private vector: InputPosition;

constructor(
position: InputPosition,
origin: InputPosition,
vector: InputPosition,
options?: VectorOptions
) {
const { color, normalize } = {
const { color, normalize, draggable } = {
...defaultVectorOptions,
...options,
};
super(position, position, { lineWidth: 4, arrowhead: true, color: color });
super(origin, origin, {
lineWidth: 4,
arrowhead: true,
color: color,
draggable: draggable,
});
if (normalize) {
this.vector = toVector2(vector).normalize();
} else {
Expand All @@ -52,6 +60,10 @@ class Vector extends Line {
this.start = position;
}

public getOriginPoint(): InputPosition {
return this.start;
}

public normalize(): void {
this.vector = toVector2(this.vector).normalize();
}
Expand Down
68 changes: 48 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
export { default as Graphica } from "./Graphica";
export { default as Arc } from "./Components/Arc";
export { default as Bracket } from "./Components/Bracket";
export { default as Button } from "./Components/Button";
export { default as Circle } from "./Components/Circle";
export { default as Grid } from "./Components/Grid";
export { default as InfiniteLine } from "./Components/InfiniteLine";
export { default as InputField } from "./Components/InputField";
export { default as Label } from "./Components/Label";
export { default as Latex } from "./Components/Latex";
export { default as Line } from "./Components/Line";
export { default as Plot } from "./Components/Plot";
export { default as Point } from "./Components/Point";
export { default as Polygon } from "./Components/Shape";
export { default as Slider } from "./Components/Slider";
export { default as Text } from "./Components/Text";
export { default as Vector } from "./Components/Vector";
export { default as Fraction } from "./Components/Derived/Fraction";
export { default as SVGLoader } from "./Components/SVGLoader";
// export { default as Graphica } from "./Graphica";
// export { default as Arc } from "./Components/Arc";
// export { default as Bracket } from "./Components/Bracket";
// export { default as Button } from "./Components/Button";
// export { default as Circle } from "./Components/Circle";
// export { default as Grid } from "./Components/Grid";
// export { default as InfiniteLine } from "./Components/InfiniteLine";
// export { default as InputField } from "./Components/InputField";
// export { default as Label } from "./Components/Label";
// export { default as Latex } from "./Components/Latex";
// export { default as Line } from "./Components/Line";
// export { default as Plot } from "./Components/Plot";
// export { default as Point } from "./Components/Point";
// export { default as Polygon } from "./Components/Shape";
// export { default as Slider } from "./Components/Slider";
// export { default as Text } from "./Components/Text";
// export { default as Vector } from "./Components/Vector";
// export { default as Fraction } from "./Components/Derived/Fraction";
// export { default as SVGLoader } from "./Components/SVGLoader";

export * as three from "three";
import { Vector3 } from "three";
import Arc from "./Components/Arc";
import Grid from "./Components/Grid";
import Line from "./Components/Line";
import Point from "./Components/Point";
import Polygon from "./Components/Shape";
import Vector from "./Components/Vector";
import Graphica from "./Graphica";

// export * as three from "three";

const g = new Graphica();
const gg = new Grid();

const v = new Line([-2, 2], [-10, 0], {
draggable: "unrestricted",
});

const p = new Polygon([
[1, 1],
[2, 3],
[4, 4],
]);

g.add(p);
g.add(v);
g.add(gg);
console.log(g.draggables);
g.run((t) => {});

0 comments on commit 84e50f1

Please sign in to comment.