Files
Graphite/website/other/bezier-rs-demos/src/components/BezierDemoPane.ts
T
5759600f95 Replace TS relative @ import path (#1087)
Migrated the import shortcut used in Svelte from @ to @graphite for better future package compatibility

Co-authored-by: Andre Roelofs <andreroelofsai@gmail.com>
Co-authored-by: Keavon Chambers <keavon@keavon.com>
2023-03-25 17:39:38 -07:00

81 lines
2.2 KiB
TypeScript

import bezierFeatures, { BezierFeatureKey } from "@graphite/features/bezier-features";
import { renderDemoPane } from "@graphite/utils/render";
import { BezierCurveType, BEZIER_CURVE_TYPE, BezierDemoOptions, InputOption, Demo, DemoPane, BezierDemoArgs } from "@graphite/utils/types";
const demoDefaults = {
Linear: {
points: [
[55, 60],
[165, 120],
],
},
Quadratic: {
points: [
[55, 50],
[165, 30],
[185, 170],
],
},
Cubic: {
points: [
[55, 30],
[85, 140],
[175, 30],
[185, 160],
],
},
};
class BezierDemoPane extends HTMLElement implements DemoPane {
// Props
key!: BezierFeatureKey;
name!: string;
demoOptions!: BezierDemoOptions;
triggerOnMouseMove!: boolean;
// Data
demos!: BezierDemoArgs[];
id!: string;
connectedCallback(): void {
this.key = (this.getAttribute("name") || "") as BezierFeatureKey;
this.id = `bezier/${this.key}`;
this.name = bezierFeatures[this.key].name;
this.demoOptions = JSON.parse(this.getAttribute("demoOptions") || "[]");
this.triggerOnMouseMove = this.getAttribute("triggerOnMouseMove") === "true";
// Use quadratic slider options as a default if sliders are not provided for the other curve types.
const defaultSliderOptions: InputOption[] = this.demoOptions.Quadratic?.inputOptions || [];
this.demos = BEZIER_CURVE_TYPE.map((curveType: BezierCurveType) => {
const givenData = this.demoOptions[curveType];
const defaultData = demoDefaults[curveType];
return {
title: curveType,
disabled: givenData?.disabled || false,
points: givenData?.customPoints || defaultData.points,
inputOptions: givenData?.inputOptions || defaultSliderOptions,
};
});
this.render();
}
render(): void {
renderDemoPane(this);
}
buildDemo(demo: BezierDemoArgs): Demo {
const bezierDemo = document.createElement("bezier-demo");
bezierDemo.setAttribute("title", demo.title);
bezierDemo.setAttribute("points", JSON.stringify(demo.points));
bezierDemo.setAttribute("key", this.key);
bezierDemo.setAttribute("inputOptions", JSON.stringify(demo.inputOptions));
bezierDemo.setAttribute("triggerOnMouseMove", String(this.triggerOnMouseMove));
return bezierDemo;
}
}
export default BezierDemoPane;