animation frame controller
This commit is contained in:
parent
6f80ca89f9
commit
3270b18597
|
|
@ -1,9 +1,9 @@
|
||||||
import { DOMRectTransform } from './common/DOMRectTransform.ts';
|
|
||||||
import { FolkBaseSet } from './folk-base-set.ts';
|
import { FolkBaseSet } from './folk-base-set.ts';
|
||||||
import { PropertyValues } from '@lit/reactive-element';
|
import { PropertyValues } from '@lit/reactive-element';
|
||||||
import { Layout } from 'webcola';
|
import { Layout } from 'webcola';
|
||||||
import { FolkShape } from './folk-shape.ts';
|
import { FolkShape } from './folk-shape.ts';
|
||||||
import { FolkArrow } from './folk-arrow.ts';
|
import { FolkArrow } from './folk-arrow.ts';
|
||||||
|
import { AnimationFrameController, AnimationFrameControllerHost } from './common/animation-frame-controller.ts';
|
||||||
|
|
||||||
type ColaNode = {
|
type ColaNode = {
|
||||||
id: FolkShape;
|
id: FolkShape;
|
||||||
|
|
@ -19,29 +19,29 @@ type ColaLink = {
|
||||||
target: FolkShape;
|
target: FolkShape;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class FolkGraph extends FolkBaseSet {
|
export class FolkGraph extends FolkBaseSet implements AnimationFrameControllerHost {
|
||||||
static override tagName = 'folk-graph';
|
static override tagName = 'folk-graph';
|
||||||
|
|
||||||
private graphSim: Layout;
|
private graphSim: Layout;
|
||||||
private animationFrameId?: number;
|
|
||||||
private colaNodes: Map<FolkShape, ColaNode> = new Map();
|
private colaNodes: Map<FolkShape, ColaNode> = new Map();
|
||||||
private colaLinks: Map<FolkArrow, ColaLink> = new Map();
|
private colaLinks: Map<FolkArrow, ColaLink> = new Map();
|
||||||
|
#rAF = new AnimationFrameController(this);
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.graphSim = new Layout();
|
this.graphSim = new Layout();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
render() {}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
super.connectedCallback();
|
super.connectedCallback();
|
||||||
this.startSimulation();
|
this.#rAF.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
super.disconnectedCallback();
|
super.disconnectedCallback();
|
||||||
if (this.animationFrameId) {
|
this.#rAF.stop();
|
||||||
cancelAnimationFrame(this.animationFrameId);
|
|
||||||
}
|
|
||||||
this.colaNodes.clear();
|
this.colaNodes.clear();
|
||||||
this.colaLinks.clear();
|
this.colaLinks.clear();
|
||||||
}
|
}
|
||||||
|
|
@ -51,21 +51,35 @@ export class FolkGraph extends FolkBaseSet {
|
||||||
this.updateGraph();
|
this.updateGraph();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tick() {
|
||||||
|
this.graphSim.start(1, 0, 0, 0, true, false);
|
||||||
|
|
||||||
|
for (const node of this.graphSim.nodes() as ColaNode[]) {
|
||||||
|
const shape = node.id;
|
||||||
|
if (shape !== document.activeElement) {
|
||||||
|
shape.x = node.x - shape.width / 2;
|
||||||
|
shape.y = node.y - shape.height / 2;
|
||||||
|
} else {
|
||||||
|
const rect = shape.getTransformDOMRect();
|
||||||
|
node.x = rect.center.x;
|
||||||
|
node.y = rect.center.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private updateGraph() {
|
private updateGraph() {
|
||||||
// Clear existing nodes and links
|
|
||||||
this.colaNodes.clear();
|
this.colaNodes.clear();
|
||||||
this.colaLinks.clear();
|
this.colaLinks.clear();
|
||||||
|
|
||||||
// Create nodes for shapes
|
// Create nodes for shapes
|
||||||
for (const element of this.sourceElements) {
|
for (const element of this.sourceElements) {
|
||||||
if (!(element instanceof FolkShape)) continue;
|
if (!(element instanceof FolkShape)) continue;
|
||||||
const rect = this.sourcesMap.get(element);
|
const rect = element.getTransformDOMRect();
|
||||||
if (!(rect instanceof DOMRectTransform)) continue;
|
|
||||||
|
|
||||||
const node: ColaNode = {
|
const node: ColaNode = {
|
||||||
id: element,
|
id: element,
|
||||||
x: rect.x + rect.width / 2,
|
x: rect.center.x,
|
||||||
y: rect.y + rect.height / 2,
|
y: rect.center.y,
|
||||||
width: rect.width,
|
width: rect.width,
|
||||||
height: rect.height,
|
height: rect.height,
|
||||||
rotation: rect.rotation,
|
rotation: rect.rotation,
|
||||||
|
|
@ -104,28 +118,4 @@ export class FolkGraph extends FolkBaseSet {
|
||||||
|
|
||||||
this.graphSim.nodes(nodes).links(links).linkDistance(250).avoidOverlaps(true).handleDisconnected(true);
|
this.graphSim.nodes(nodes).links(links).linkDistance(250).avoidOverlaps(true).handleDisconnected(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private startSimulation() {
|
|
||||||
const step = () => {
|
|
||||||
this.graphSim.start(1, 0, 0, 0, true, false);
|
|
||||||
|
|
||||||
for (const node of this.graphSim.nodes() as ColaNode[]) {
|
|
||||||
const shape = node.id;
|
|
||||||
const rect = this.sourcesMap.get(shape);
|
|
||||||
if (!(rect instanceof DOMRectTransform)) continue;
|
|
||||||
|
|
||||||
if (shape !== document.activeElement) {
|
|
||||||
shape.x = node.x - rect.width / 2;
|
|
||||||
shape.y = node.y - rect.height / 2;
|
|
||||||
} else {
|
|
||||||
node.x = rect.x + rect.width / 2;
|
|
||||||
node.y = rect.y + rect.height / 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.animationFrameId = requestAnimationFrame(step);
|
|
||||||
};
|
|
||||||
|
|
||||||
this.animationFrameId = requestAnimationFrame(step);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue