Skip to content

Commit 77ade6c

Browse files
committed
(worklet): Connections
1 parent 77296d1 commit 77ade6c

17 files changed

+313
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
.DS_Store
22
node_modules
33
package-lock.json
4+
**/test/*.js
5+
6+
# Local Netlify folder
7+
.netlify

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Extends CSS with the Houdini powers
22

3-
A set of modules to extends your CSS with the Houdini powers.
3+
A set of worklets to extends your CSS with the Houdini powers.

src/connections/.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# editorconfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
indent_style = space
8+
end_of_line = lf
9+
indent_size = 2
10+
charset = utf-8

src/connections/.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
src
2+
assets
3+
test
4+
.editorconfig
5+
server.js
6+
package-lock.json

src/connections/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# CSS Houdini Connections
2+
3+
A CSS Houdini Worklet to show connected nodes.
4+
5+
![CSS Houdini Connections](./assets/connections.png)
148 KB
Loading

src/connections/dist/connections.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/connections/dist/connections.modern.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/connections/dist/connections.module.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/connections/dist/connections.umd.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/connections/package.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "@css-houdini/connections",
3+
"version": "1.0.0",
4+
"description": "CSS Houdini Background Connections",
5+
"source": "src/index.js",
6+
"main": "dist/connections.js",
7+
"module": "dist/connections.module.js",
8+
"unpkg": "dist/connections.umd.js",
9+
"scripts": {
10+
"build": "rimraf dist && microbundle --no-sourcemap",
11+
"prepublish": "npm run build",
12+
"test": "npm run test:copy && npm run test:server",
13+
"test:copy": "cp -fv ./src/index.js ./test/connections.js",
14+
"test:server": "node ./server.js"
15+
},
16+
"keywords": [
17+
"houdini",
18+
"css",
19+
"css-houdini",
20+
"PaintWorklet",
21+
"paitn",
22+
"worklet"
23+
],
24+
"author": {
25+
"name": "Joan León",
26+
"email": "joan.leon@gmail.com",
27+
"twitter": "@nucliweb",
28+
"web": "https://joanleon.dev"
29+
},
30+
"license": "MIT",
31+
"eslintConfig": {
32+
"extends": [
33+
"./node_modules/@s-ui/lint/eslintrc.js"
34+
]
35+
},
36+
"prettier": "./node_modules/@s-ui/lint/.prettierrc.js",
37+
"stylelint": {
38+
"extends": "./node_modules/@s-ui/lint/stylelint.config.js"
39+
},
40+
"devDependencies": {
41+
"@s-ui/lint": "^3.25.0",
42+
"express": "^4.17.1",
43+
"microbundle": "^0.12.4",
44+
"rimraf": "^3.0.2"
45+
}
46+
}

src/connections/server.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-env node */
2+
/* eslint no-console: "off" */
3+
4+
const path = require('path')
5+
const express = require('express')
6+
const app = express()
7+
const htdocs = path.join(__dirname, 'test')
8+
9+
// Run static server
10+
app.use(express.static(htdocs))
11+
app.listen(8080)
12+
13+
console.log('🚀 Paint worklet test up and running at http://localhost:8080/')
14+
console.log('ℹ️ Press Ctrl+C to return to the real world.')

src/connections/src/index.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const CONNECTIONS_PROPS = [
2+
'--connections-particleColor',
3+
'--connections-lineColor',
4+
'--connections-particleAmount',
5+
'--connections-defaultRadius',
6+
'--connections-variantRadius',
7+
'--connections-linkRadius'
8+
]
9+
10+
registerPaint(
11+
'connections',
12+
class {
13+
static get inputProperties() {
14+
return CONNECTIONS_PROPS
15+
}
16+
17+
parseProps(props) {
18+
return CONNECTIONS_PROPS.map(
19+
prop => props.get(prop).toString().trim() || undefined
20+
)
21+
}
22+
23+
checkDistance(x1, y1, x2, y2) {
24+
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
25+
}
26+
27+
paint(ctx, geometry, props) {
28+
const {width: w, height: h} = geometry
29+
const [
30+
particleColor = 'rgb(74,74,74)',
31+
lineColor = 'rgb(76,76,76)',
32+
particleAmount = (w * h) / 1000,
33+
defaultRadius = 1.5,
34+
variantRadius = 3,
35+
linkRadius = 80
36+
] = this.parseProps(props)
37+
38+
let particles = []
39+
const [r, g, b] = lineColor.match(/\d+/g)
40+
41+
const particle = (x, y) => {
42+
let radius = +defaultRadius + Math.random() * +variantRadius
43+
ctx.beginPath()
44+
ctx.arc(x, y, radius, 0, Math.PI * 2)
45+
ctx.fillStyle = particleColor
46+
ctx.fill()
47+
return {x, y}
48+
}
49+
50+
const linkPoints = point => {
51+
for (let i = 0; i < particleAmount; i++) {
52+
let distance = this.checkDistance(
53+
point.x,
54+
point.y,
55+
particles[i].x,
56+
particles[i].y
57+
)
58+
let opacity = 1 - distance / linkRadius
59+
if (opacity > 0) {
60+
ctx.lineWidth = 0.5
61+
ctx.strokeStyle = `rgba(${r}, ${g}, ${b}, ${opacity})`
62+
ctx.beginPath()
63+
ctx.moveTo(point.x, point.y)
64+
ctx.lineTo(particles[i].x, particles[i].y)
65+
ctx.closePath()
66+
ctx.stroke()
67+
}
68+
}
69+
}
70+
71+
for (let i = 0; i < particleAmount; i++) {
72+
let x = Math.round(Math.random() * w)
73+
let y = Math.round(Math.random() * h)
74+
particles.push(particle(x, y))
75+
}
76+
77+
for (let i = 0; i < particleAmount; i++) {
78+
linkPoints(particles[i])
79+
}
80+
}
81+
}
82+
)

src/connections/test/index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>CSS Houdini Connections</title>
7+
<link rel="stylesheet" href="./styles.css" />
8+
</head>
9+
<body>
10+
<div class="css-houdini connections"></div>
11+
<script>
12+
;(async function() {
13+
if (CSS['paintWorklet'] === undefined)
14+
await import('https://unpkg.com/css-paint-polyfill')
15+
16+
CSS.paintWorklet.addModule('./connections.js')
17+
})()
18+
</script>
19+
<script src="./js/tweakpane-1.5.6.min.js"></script>
20+
<script src="./js/controls.js"></script>
21+
</body>
22+
</html>

src/connections/test/js/controls.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const element = document.querySelector('.connections')
2+
console.log(element)
3+
const PARAMS = {
4+
particleColor: {r: 150, g: 180, b: 200},
5+
lineColor: {r: 150, g: 180, b: 200},
6+
particleAmount: 80,
7+
defaultRadius: 2,
8+
variantRadius: 3,
9+
linkRadius: 80
10+
}
11+
12+
const pane = new Tweakpane()
13+
const f1 = pane.addFolder({title: 'Connections Parameters'})
14+
const f2 = pane.addFolder({title: 'Information'})
15+
f2.expanded = false
16+
17+
f1.addInput(PARAMS, 'particleColor', {label: 'Particle Color'}).on(
18+
'change',
19+
value => {
20+
const {r, g, b} = value
21+
const color = `rgb(${r},${g},${b})`
22+
element.style.setProperty('--connections-particleColor', color)
23+
}
24+
)
25+
f1.addInput(PARAMS, 'lineColor', {label: 'Line Color'}).on('change', value => {
26+
const {r, g, b} = value
27+
const color = `rgb(${r},${g},${b})`
28+
element.style.setProperty('--connections-lineColor', color)
29+
})
30+
f1.addInput(PARAMS, 'particleAmount', {
31+
label: 'Amount',
32+
step: 5,
33+
min: 10,
34+
max: 400
35+
}).on('change', value => {
36+
element.style.setProperty('--connections-particleAmount', value)
37+
})
38+
f1.addInput(PARAMS, 'defaultRadius', {
39+
label: 'Radius',
40+
step: 0.5,
41+
min: 1,
42+
max: 5
43+
}).on('change', value => {
44+
element.style.setProperty('--connections-defaultRadius', value)
45+
})
46+
f1.addInput(PARAMS, 'variantRadius', {
47+
label: 'Variant Radius',
48+
step: 0.5,
49+
min: 1,
50+
max: 4
51+
}).on('change', value => {
52+
element.style.setProperty('--connections-variantRadius', value)
53+
})
54+
f1.addInput(PARAMS, 'linkRadius', {
55+
label: 'Link Radius',
56+
step: 1,
57+
min: 10,
58+
max: 200
59+
}).on('change', value => {
60+
element.style.setProperty('--connections-linkRadius', value)
61+
})
62+
63+
f2.addButton({title: 'Source Code'}).on('click', () =>
64+
window.open(
65+
'https://github.com/CSSHoudini/css-houdini',
66+
'CSS Houdini Connections'
67+
)
68+
)
69+
f2.addButton({title: '@csshoudini'}).on('click', () =>
70+
window.open('https://twitter.com/csshoudini', 'CSS Houdini')
71+
)

src/connections/test/js/tweakpane-1.5.6.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/connections/test/styles.css

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
:root {
2+
--size: 300px;
3+
--color-dark: #353535;
4+
--color-medium: #858585;
5+
--color-light: #d3d3d3;
6+
}
7+
8+
@media (min-width: 600px) {
9+
:root {
10+
--size: 600px;
11+
}
12+
}
13+
14+
html,
15+
body {
16+
margin: 0;
17+
padding: 0;
18+
height: 100%;
19+
}
20+
21+
body {
22+
align-items: center;
23+
background-color: white;
24+
display: flex;
25+
flex-direction: column;
26+
font-family: sans-serif;
27+
justify-content: center;
28+
padding-top: 1em;
29+
}
30+
31+
.css-houdini {
32+
height: var(--size);
33+
padding: 1em;
34+
box-sizing: content-box;
35+
width: var(--size);
36+
}
37+
38+
.connections {
39+
--connections-particleColor: rgb(150,180,200);
40+
--connections-lineColor: rgb(150,180,200);
41+
--connections-particleAmount: 200;
42+
--connections-defaultRadius: 2;
43+
--connections-variantRadius: 1;
44+
--connections-linkRadius: 60;
45+
46+
background-image: paint(connections);
47+
}

0 commit comments

Comments
 (0)