-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90b5594
commit 8508ab4
Showing
7 changed files
with
403 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// widget.js | ||
import * as React from "react"; | ||
import { createRender, useModelState } from "@anywidget/react"; | ||
import { Tldraw, createShapeId } from "@tldraw/tldraw"; | ||
import "@tldraw/tldraw/tldraw.css"; | ||
|
||
const render = createRender(() => { | ||
const [flowerData] = useModelState("flower_data"); | ||
const [editor, setEditor] = React.useState(null); | ||
|
||
const handleMount = (editorInstance) => { | ||
setEditor(editorInstance); | ||
}; | ||
|
||
React.useEffect(() => { | ||
if (editor && flowerData && flowerData.length > 0) { | ||
const gridCols = 15; // Set to 15 columns | ||
const gridSize = 70; // Reduce gap by reducing the grid size | ||
|
||
const commonProps = { | ||
dash: "draw", | ||
fill: "solid", | ||
font: "draw", | ||
geo: "ellipse", | ||
size: "xl", | ||
}; | ||
|
||
const shapes = flowerData.flatMap((flower, index) => { | ||
const row = Math.floor(index / gridCols); | ||
const col = index % gridCols; | ||
|
||
const centerX = col * gridSize + 50; | ||
const centerY = row * gridSize + 50; | ||
|
||
const scale = 5; | ||
const petalW = flower.petal_width * scale; | ||
const petalH = flower.petal_length * scale; | ||
const sepalW = flower.sepal_width * scale; | ||
const sepalH = flower.sepal_length * scale; | ||
const r = 1 * scale; | ||
|
||
const anglesPetal = [0, (Math.PI * 2) / 3, (Math.PI * 4) / 3]; | ||
const anglesSepal = anglesPetal.map((a) => a + Math.PI / 3); | ||
|
||
return [ | ||
// Sepals | ||
...anglesSepal.map((a, i) => ({ | ||
id: createShapeId(`sepal${index}-${i + 1}`), | ||
type: "geo", | ||
x: centerX + Math.sin(a) * (sepalW / 2), | ||
y: centerY - Math.cos(a) * (sepalW / 2), | ||
rotation: a, | ||
props: { h: sepalW, w: sepalH, color: "light-violet", ...commonProps }, | ||
})), | ||
// Petals | ||
...anglesPetal.map((a, i) => ({ | ||
id: createShapeId(`petal${index}-${i + 1}`), | ||
type: "geo", | ||
x: centerX + Math.sin(a) * (petalW / 2), | ||
y: centerY - Math.cos(a) * (petalW / 2), | ||
rotation: a, | ||
props: { h: petalW, w: petalH, color: "violet", ...commonProps }, | ||
})), | ||
// Center Ellipse | ||
{ | ||
id: createShapeId(`centerEllipse${index}`), | ||
type: "geo", | ||
x: centerX - r, | ||
y: centerY - r, | ||
props: { h: r * 2, w: r * 2, color: "violet", ...commonProps }, | ||
}, | ||
]; | ||
}); | ||
|
||
editor.createShapes(shapes); | ||
} | ||
}, [editor, flowerData]); | ||
|
||
return ( | ||
<div style={{ position: "relative", width: "1100px", height: "800px" }}> | ||
<Tldraw | ||
autoFocus={false} | ||
onMount={handleMount} | ||
showMenu={false} | ||
showPages={false} | ||
/> | ||
</div> | ||
); | ||
}); | ||
|
||
export default { render }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.