Skip to content

Commit

Permalink
feat(examples): add rdom-reactive-svg example
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 28, 2024
1 parent 46ddf39 commit 8886cac
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 0 deletions.
Binary file added assets/examples/rdom-reactive-svg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions examples/rdom-reactive-svg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# rdom-reactive-svg

![screenshot](https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/rdom-reactive-svg.jpg)

[Live demo](http://demo.thi.ng/umbrella/rdom-reactive-svg/)

## Developing & building

Please refer to the instructions on the wiki:

- [Development](https://github.com/thi-ng/umbrella/wiki/Development-mode-for-examples-using-thi.ng-meta%E2%80%90css)
- [Production build](https://github.com/thi-ng/umbrella/wiki/Example-build-instructions)

## Authors

- Karsten Schmidt

## License

© 2024 Karsten Schmidt // Apache Software License 2.0
7 changes: 7 additions & 0 deletions examples/rdom-reactive-svg/css/custom.mcss.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tables": {},
"vars": {},
"decls": [],
"specs": [],
"templates": []
}
2 changes: 2 additions & 0 deletions examples/rdom-reactive-svg/css/includes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// list of CSS class names to force-include in generated CSS
// (one class per line, basic wildcards supported)
11 changes: 11 additions & 0 deletions examples/rdom-reactive-svg/css/style.mcss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// thi.ng/meta-css stylesheet
// see package readme for more details/usage
// use `yarn css:build` or `yarn css:watch` to transpile to CSS
// also see component-specific *.mcss files in /src folder

// (optional) variable declarations
:root {
// color1=#fff
}

body { system-sans-serif ma3 }
21 changes: 21 additions & 0 deletions examples/rdom-reactive-svg/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link
rel="icon"
href='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">⛱️</text></svg>'
/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>rdom-reactive-svg · @thi.ng/umbrella</title>
<link href="/css/style.css" rel="stylesheet">
<script>window.goatcounter = { path: (p) => location.host + p };</script>
<script data-goatcounter="https://thing.goatcounter.com/count" async src="//gc.zgo.at/count.js"></script>
</head>
<body>
<div id="app"></div>
<div><a class="link" href="https://github.com/thi-ng/umbrella/tree/develop/examples/rdom-reactive-svg">Source code</a></div>
<script type="module" src="/src/index.ts"></script>
</body>
</html>
37 changes: 37 additions & 0 deletions examples/rdom-reactive-svg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@example/rdom-reactive-svg",
"version": "0.0.1",
"private": true,
"description": "Animated SVG elements with reactive attributes",
"repository": "https://github.com/thi-ng/umbrella",
"author": "Karsten Schmidt <k+npm@thi.ng>",
"license": "Apache-2.0",
"scripts": {
"start": "yarn css:build && yarn start:only",
"start:only": "vite --host --open",
"css:watch": "../../node_modules/.bin/metacss develop --bundle --watch --pretty --out-specs css/framework.json --out-css css/style.css --force @css/includes.txt ../../packages/meta-css/specs/*.mcss.json css/*.mcss.json css/*.mcss",
"css:build": "../../node_modules/.bin/metacss develop --bundle --out-specs css/framework.json --out-css css/style.css --force @css/includes.txt ../../packages/meta-css/specs/*.mcss.json css/*.mcss.json css/*.mcss",
"build": "yarn css:build && tsc && vite build --base='./'",
"preview": "vite preview --host --open"
},
"devDependencies": {
"@thi.ng/meta-css": "workspace:^",
"typescript": "^5.4.3",
"vite": "^5.2.6"
},
"dependencies": {
"@thi.ng/color": "workspace:^",
"@thi.ng/hiccup-svg": "workspace:^",
"@thi.ng/random": "workspace:^",
"@thi.ng/rdom": "workspace:^",
"@thi.ng/rstream": "workspace:^",
"@thi.ng/transducers": "workspace:^"
},
"browser": {
"process": false
},
"thi.ng": {
"readme": false,
"screenshot": "examples/rdom-reactive-svg.jpg"
}
}
40 changes: 40 additions & 0 deletions examples/rdom-reactive-svg/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { css, lch } from "@thi.ng/color";
import { group, svg } from "@thi.ng/hiccup-svg";
import { SYSTEM } from "@thi.ng/random";
import { $compile } from "@thi.ng/rdom";
import { fromRAF } from "@thi.ng/rstream";
import { repeatedly } from "@thi.ng/transducers";

// time in seconds
const time = fromRAF({ timestamp: true }).map((t) => t * 0.001);

// create circle element w/ randomized reactive attribs
const circle = () => {
const fX = SYSTEM.minmax(0.5, 3);
const fY = SYSTEM.minmax(0.5, 3);
const fR = SYSTEM.minmax(0.5, 3);
const fC = SYSTEM.minmax(0.1, 1);
// use plain hiccup to define the SVG circle element
return [
"circle",
{
cx: time.map((t) => Math.cos(t * fX) * 200),
cy: time.map((t) => Math.sin(t * fY) * 200),
r: time.map((t) => Math.sin(t * fR) * 45 + 50),
fill: time.map((t) => css(lch(0.8, 1, t * fC))),
},
];
};

$compile(
// for the non-reactive outer elements, we can use hiccup-svg
// for convenience & syntax sugar...
svg(
{ width: 600, height: 600, style: { background: "#ccc" } },
group(
{ translate: [300, 300] },
// create N random circles
...repeatedly(circle, 10)
)
)
).mount(document.body);
1 change: 1 addition & 0 deletions examples/rdom-reactive-svg/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
6 changes: 6 additions & 0 deletions examples/rdom-reactive-svg/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"include": ["src/**/*"],
"compilerOptions": {
}
}
3 changes: 3 additions & 0 deletions examples/rdom-reactive-svg/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
build: { target: "esnext" },
};
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,22 @@ __metadata:
languageName: unknown
linkType: soft

"@example/rdom-reactive-svg@workspace:examples/rdom-reactive-svg":
version: 0.0.0-use.local
resolution: "@example/rdom-reactive-svg@workspace:examples/rdom-reactive-svg"
dependencies:
"@thi.ng/color": "workspace:^"
"@thi.ng/hiccup-svg": "workspace:^"
"@thi.ng/meta-css": "workspace:^"
"@thi.ng/random": "workspace:^"
"@thi.ng/rdom": "workspace:^"
"@thi.ng/rstream": "workspace:^"
"@thi.ng/transducers": "workspace:^"
typescript: "npm:^5.4.3"
vite: "npm:^5.2.6"
languageName: unknown
linkType: soft

"@example/rdom-search-docs@workspace:examples/rdom-search-docs":
version: 0.0.0-use.local
resolution: "@example/rdom-search-docs@workspace:examples/rdom-search-docs"
Expand Down

0 comments on commit 8886cac

Please sign in to comment.