Skip to content

Commit 550a8ae

Browse files
committed
(lines): Change line by rect
1 parent b06be6d commit 550a8ae

File tree

8 files changed

+95
-125
lines changed

8 files changed

+95
-125
lines changed

src/connections/test/js/controls.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const element = document.querySelector('.connections')
2-
console.log(element)
32
const PARAMS = {
43
particleColor: {r: 150, g: 180, b: 200},
54
lineColor: {r: 150, g: 180, b: 200},

src/lines/README.md

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# CSS Houdini Connections
1+
# CSS Houdini Lines
22

33
A CSS Houdini Worklet to show connected nodes.
44

5-
![CSS Houdini Connections](https://github.com/CSSHoudini/css-houdini/blob/main/src/connections/assets/connections.png)
5+
![CSS Houdini Lines](https://github.com/CSSHoudini/css-houdini/blob/main/src/lines/assets/lines.png)
66

77
## Getting started
88

@@ -12,15 +12,15 @@ Using CDN is the easiest way to add the library:
1212

1313
```js
1414
if ('paintWorklet' in CSS) {
15-
CSS.paintWorklet.addModule('https://rawcdn.githack.com/CSSHoudini/css-houdini/6979b873e80f9120f52bd481fbdf2d4c60db6b19/src/connections/dist/connections.js');
15+
CSS.paintWorklet.addModule('...');
1616
}
1717
```
1818

19-
Or, download the latest [Connections Worklet](https://github.com/CSSHoudini/css-houdini/tree/main/src/connections/dist) and import it to your web page:
19+
Or, download the latest [lines Worklet](https://github.com/CSSHoudini/css-houdini/tree/main/src/lines/dist) and import it to your web page:
2020

2121
```js
2222
if ('paintWorklet' in CSS) {
23-
CSS.paintWorklet.addModule('path/to/connections.js');
23+
CSS.paintWorklet.addModule('path/to/lines.js');
2424
}
2525
```
2626

@@ -34,43 +34,34 @@ To add support for all moder browsers, you can load the worklet with [css-paint-
3434
if (CSS['paintWorklet'] === undefined)
3535
await import('https://unpkg.com/css-paint-polyfill')
3636
37-
CSS.paintWorklet.addModule('./connections.js')
37+
CSS.paintWorklet.addModule('./lines.js')
3838
})()
3939
</script>
4040
```
4141

4242
### 3. Ready to use it in your CSS!
4343

44-
To use **connections** worlet you need define some custom properties with values and add the value `paint(connections)` on `background-image` property.
44+
To use **Lines** worlet you need define some custom properties with values and add the value `paint(lines)` on `background-image` property.
4545

4646
> The worklet has default values if you don't define these
4747
4848
```css
4949
.element {
50-
--connections-particleColor: rgb(150,180,200);
51-
--connections-lineColor: rgb(150,180,200);
52-
--connections-particleAmount: 40;
53-
--connections-defaultRadius: 2;
54-
--connections-variantRadius: 1;
55-
--connections-linkRadius: 60;
56-
57-
background-image: paint(connections);
50+
--lines-colors: #f94144, #f3722c, #f8961e, #f9844a;
51+
--lines-widths: 10, 2, 3, 8;
52+
--lines-gaps: 20, 4, 3, 7;
53+
--lines-rotate: 0; /* In degrees */
54+
55+
background-image: paint(lines);
5856
}
5957
```
6058

6159
| property | description | default value |
6260
| -------- | ----------- | ------------- |
63-
| --connections-particleColor | Dot color | `rgb(74,74,74)` |
64-
| --connections-lineColor | Line conections color | `rgb(76,76,76)` |
65-
| --connections-particleAmount | Dots number to show on the element | `(w * h) / 1000` Calc of width * height divided to 1000 |
66-
| --connections-defaultRadius | Dot radius | 1.5 |
67-
| --connections-variantRadius | Dot radius variant | 3 |
68-
| --connections-linkRadius | Minimum distance between dots to draw the line connection | 80 |
69-
70-
#### Important informaction
71-
72-
- The current worklet version needs that the values for `--connections-particleColor` and `--connections-lineColor` are **RGB Color** with format `rgb(150,180,200)`.
73-
- If you define a huge values, you can see hundreds of lines in your element.
61+
| --lines-colors | **Color lines**, you can define one or more hexadecimal colors comma separated | `#71a7ee, #7940c1` |
62+
| --lines-widths | **Width lines**, you can define one or more line widths comma separated | `6, 2` |
63+
| --lines-gaps | **Gap lines**, you can define one or more gaps comma separated | `8` |
64+
| --lines-rotate | **Rotation lines**, with an interger | `0` |
7465

7566
## License
7667

src/lines/assets/connections.png

-148 KB
Binary file not shown.

src/lines/assets/lines.png

27.9 KB
Loading

src/lines/src/index.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1+
/* global registerPaint */
12
const LINES_PROPS = [
23
'--lines-colors',
34
'--lines-widths',
4-
'--lines-gap',
5+
'--lines-gaps',
56
'--lines-rotate'
67
]
78

8-
// Convert from degrees to radians.
9-
const degreesToRadians = degrees => {
10-
return (degrees * Math.PI) / 180
11-
}
12-
13-
// eslint-disable-next-line
149
registerPaint(
1510
'lines',
1611
class {
@@ -31,45 +26,50 @@ registerPaint(
3126
paint(ctx, geometry, props) {
3227
const {width: w, height: h} = geometry
3328
const [
34-
colors = '#123456',
35-
widths = 1,
36-
gap = 8,
29+
colors = '#71a7ee, #7940c1',
30+
widths = '6, 2',
31+
gaps = '8',
3732
rotate = 0
3833
] = this.parseProps(props)
3934

40-
const saveArea = Math.floor(Math.sqrt(Math.pow(w, 2) + Math.pow(h, 2)))
41-
const colorLines = colors.split(',').map(color => color.trim())
35+
const sqrtArea = Math.floor(Math.sqrt(Math.pow(w, 2) + Math.pow(h, 2)))
36+
const saveArea = sqrtArea * 2
37+
const colorLines = colors.split(',').map(color => color)
4238
const maxColors = colorLines.length
4339
let indexColor = 0
4440

45-
const widthLines = widths.split(',').map(width => width.trim())
41+
const widthLines = widths.split(',').map(width => +width)
42+
const maxWidth = Math.min(...widthLines)
4643
const maxLines = widthLines.length
4744
let indexLine = 0
4845

49-
// const gapLines = gaps.split(',').map(gap => gap.trim())
50-
// const sumGaps = gapLines.reduce((a, b) => a + b, 0)
51-
// const maxGaps = gapLines.length
52-
// let indexGap = 0
46+
const gapLines = gaps.split(',').map(gap => +gap)
47+
const maxGaps = gapLines.length
48+
let indexGap = 0
49+
50+
if (rotate !== 0) {
51+
ctx.rotate((rotate * Math.PI) / 180)
52+
ctx.translate(-saveArea, -saveArea)
53+
}
5354

54-
ctx.rotate((rotate * Math.PI) / 180)
55-
// ctx.rotate(degreesToRadians(rotate))
56-
for (let i = 0; i < saveArea; i++) {
57-
ctx.beginPath()
55+
let posY = 0
5856

59-
// const gap = gapLines[indexGap]
60-
// indexGap >= maxGaps - 1 ? (indexGap = 0) : indexGap++
61-
// console.log({gap})
57+
for (let i = 0; i < saveArea / maxWidth; i++) {
58+
const lineGap = +widthLines[indexLine] + gapLines[indexGap]
59+
ctx.fillStyle = colorLines[indexColor]
6260

63-
ctx.moveTo(0 - saveArea, -saveArea + i * gap)
64-
ctx.lineTo(w + saveArea, -saveArea + i * gap)
61+
ctx.fillRect(
62+
-saveArea * 0.5,
63+
posY,
64+
saveArea * 2,
65+
+widthLines[indexLine]
66+
)
6567

66-
ctx.lineWidth = widthLines[indexLine]
6768
indexLine >= maxLines - 1 ? (indexLine = 0) : indexLine++
68-
69-
ctx.strokeStyle = colorLines[indexColor]
69+
indexGap >= maxGaps - 1 ? (indexGap = 0) : indexGap++
7070
indexColor >= maxColors - 1 ? (indexColor = 0) : indexColor++
7171

72-
ctx.stroke()
72+
posY += lineGap
7373
}
7474
}
7575
}

src/lines/test/index.html

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,14 @@
99
<body>
1010
<div class="css-houdini lines"></div>
1111
<script>
12-
CSS.registerProperty({
13-
name: '--lines-gap',
14-
syntax: '<number>',
15-
inherits: false,
16-
initialValue: 0
17-
})
1812
;(async function() {
1913
if (CSS['paintWorklet'] === undefined)
2014
await import('https://unpkg.com/css-paint-polyfill')
2115

2216
CSS.paintWorklet.addModule('./lines.js')
2317
})()
2418
</script>
25-
<!-- <script src="./js/tweakpane-1.5.6.min.js"></script>
26-
<script src="./js/controls.js"></script> -->
19+
<script src="./js/tweakpane-1.5.6.min.js"></script>
20+
<script src="./js/controls.js"></script>
2721
</body>
2822
</html>

src/lines/test/js/controls.js

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,66 @@
1+
/* global Tweakpane */
12
const element = document.querySelector('.lines')
23
const PARAMS = {
3-
particleColor: '',
4-
lineColor: {r: 150, g: 180, b: 200}
4+
colors:
5+
'#f94144, #f3722c, #f8961e, #f9844a, #f9c74f, #90be6d, #43aa8b, #4d908e, #577590, #277da1',
6+
widths: '10, 2, 3, 8',
7+
gaps: '20, 4, 3, 7',
8+
rotate: 0
59
}
610

711
const pane = new Tweakpane()
812
const f1 = pane.addFolder({title: 'Lines Parameters'})
913
const f2 = pane.addFolder({title: 'Information'})
1014
f2.expanded = false
1115

12-
f1.addInput(PARAMS, 'particleColor', {label: 'Particle Color'}).on(
13-
'change',
14-
value => {
15-
const {r, g, b} = value
16-
const color = `rgb(${r},${g},${b})`
17-
element.style.setProperty('--lines-particleColor', color)
16+
f1.addInput(PARAMS, 'colors', {
17+
label: 'Line colors',
18+
options: {
19+
Palette1:
20+
'#f94144, #f3722c, #f8961e, #f9844a, #f9c74f, #90be6d, #43aa8b, #4d908e, #577590, #277da1',
21+
Palette2: '#2a9d8f, #e76f51',
22+
Palette3: '#583d72, #9f5f80, #ffba93, #ff8e71',
23+
Palette4: '#fc00ff, #ff67e2, #ff95f9, #bd42ff, #6100ff'
1824
}
19-
)
20-
f1.addInput(PARAMS, 'lineColor', {label: 'Line Color'}).on('change', value => {
21-
const {r, g, b} = value
22-
const color = `rgb(${r},${g},${b})`
23-
element.style.setProperty('--lines-lineColor', color)
24-
})
25-
f1.addInput(PARAMS, 'particleAmount', {
26-
label: 'Amount',
27-
step: 5,
28-
min: 10,
29-
max: 400
3025
}).on('change', value => {
31-
element.style.setProperty('--lines-particleAmount', value)
26+
element.style.setProperty('--lines-colors', value)
3227
})
33-
f1.addInput(PARAMS, 'defaultRadius', {
34-
label: 'Radius',
35-
step: 0.5,
36-
min: 1,
37-
max: 5
28+
29+
f1.addInput(PARAMS, 'widths', {
30+
label: 'Line widths',
31+
options: {
32+
'10, 2, 3, 8': '10, 2, 3, 8',
33+
'10': '10',
34+
'13, 2, 8, 4, 1': '13, 2, 8, 4, 1',
35+
'35, 12, 5': '35, 12, 5'
36+
}
3837
}).on('change', value => {
39-
element.style.setProperty('--lines-defaultRadius', value)
38+
element.style.setProperty('--lines-widths', value)
4039
})
41-
f1.addInput(PARAMS, 'variantRadius', {
42-
label: 'Variant Radius',
43-
step: 0.5,
44-
min: 1,
45-
max: 4
40+
41+
f1.addInput(PARAMS, 'gaps', {
42+
label: 'Line gaps',
43+
options: {
44+
'20, 4, 3, 7': '20, 4, 3, 7',
45+
'8': '8',
46+
'5, 3': '5, 3',
47+
'6, 7, 1, 3': '6, 7, 1, 3'
48+
}
4649
}).on('change', value => {
47-
element.style.setProperty('--lines-variantRadius', value)
50+
element.style.setProperty('--lines-gaps', value)
4851
})
49-
f1.addInput(PARAMS, 'linkRadius', {
50-
label: 'Link Radius',
52+
53+
f1.addInput(PARAMS, 'rotate', {
54+
label: 'Rotation (Degrees)',
5155
step: 1,
52-
min: 10,
53-
max: 200
56+
min: 0,
57+
max: 360
5458
}).on('change', value => {
55-
element.style.setProperty('--lines-linkRadius', value)
59+
element.style.setProperty('--lines-rotate', value)
5660
})
5761

58-
/* ------------------------------------------------- */
59-
6062
f2.addButton({title: 'Source Code'}).on('click', () =>
61-
window.open('https://github.com/CSSHoudini/css-houdini', 'CSS Houdini lines')
63+
window.open('https://github.com/CSSHoudini/css-houdini', 'CSS Houdini Lines')
6264
)
6365
f2.addButton({title: '@csshoudini'}).on('click', () =>
6466
window.open('https://twitter.com/csshoudini', 'CSS Houdini')

src/lines/test/styles.css

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
:root {
22
--size: 300px;
3-
--color-dark: #353535;
4-
--color-medium: #858585;
5-
--color-light: #d3d3d3;
63
}
74

85
@media (min-width: 600px) {
@@ -36,23 +33,10 @@ body {
3633
}
3734

3835
.lines {
39-
/* --lines-colors: #264653, #2a9d8f, #e9c46a, #f4a261, #e76f51; */
40-
/* --lines-colors: #583d72, #9f5f80, #ffba93, #ff8e71; */
41-
--lines-colors: #f94144ff, #f3722cff, #f8961eff, #f9844aff, #f9c74fff, #90be6dff, #43aa8bff, #4d908eff, #577590ff, #277da1ff;
42-
--lines-widths: 3, 8, 2, 5, 1;
43-
--lines-gap: 20;
44-
--lines-rotate: 130; /* In degrees */
36+
--lines-colors: #f94144, #f3722c, #f8961e, #f9844a, #f9c74f, #90be6d, #43aa8b, #4d908e, #577590, #277da1;
37+
--lines-widths: 10, 2, 3, 8;
38+
--lines-gaps: 20, 4, 3, 7;
39+
--lines-rotate: 0; /* In degrees */
4540

4641
background-image: paint(lines);
47-
48-
/* animation-name: gap;
49-
animation-duration: 4s;
50-
animation-timing-function: linear;
51-
animation-iteration-count: infinite; */
52-
}
53-
54-
@keyframes gap {
55-
0% { --lines-gap: 10 }
56-
50% { --lines-gap: 40 }
57-
100% { --lines-gap: 10 }
5842
}

0 commit comments

Comments
 (0)