@@ -9,61 +9,101 @@ magic](https://tim-harding.github.io/squircle/).
9
9
10
10
### CSS
11
11
12
- First, initialize the library. Unlike most JavaScript, the worklet cannot be
13
- imported as a module, so the worklet file must be served at the provided URL.
14
- You can either use the minified files from [ unpkg] ( https://unpkg.com/ ) or use
15
- your bundler to get a URL.
12
+ First, register the squircle web worker.
16
13
17
14
``` js
18
- import url from " superellipse-squircle/worklet.min.js?url" ; // Vite
19
- import url from " url:superellipse-squircle/worklet.min.js" ; // Parcel & WMR
20
- import url from " omt:superellipse-squircle/worklet.min.js" ; // Rollup
21
- import url from " url-loader!:superellipse-squircle/worklet.min.js" ; // WebPack 4 & 5
22
- const url = " https://unpkg.com/[email protected] /worklet.min.js" ;
// unpkg
23
-
24
15
import { register } from " superellipse-squircle" ;
25
- register (url);
16
+ register ();
17
+ ```
18
+
19
+ Now you can use ` paint(squircle) ` as a image source in CSS. Usually that will be
20
+ ` background: paint(squircle); ` , in which case you can use the provided
21
+ ` squircle ` class. This includes a fallback to normal rounded rectangles when CSS
22
+ Houdini isn't available.
26
23
27
- import " superellipse-squircle/index.css " ; // Optional
24
+ ``` html
28
25
<div
29
26
class =" squircle"
30
27
style ="
31
- --squircle-radius: 1rem;
32
- --squircle-fill : black;"
28
+ --squircle-border- radius : 1rem ;
29
+ --squircle-background-color : black ; "
33
30
></div >;
34
31
```
35
32
36
- Linking or importing the provided CSS creates a ` squircle ` class with fallbacks
37
- for unsupported browsers. Alternatively, use the image source directly in your
38
- CSS. For example, try ` mask-image: paint(squircle); ` to create a squircle layer
39
- mask. At time of writing, only Chromium-based browsers support the Paint API, so
40
- use ` @supports (paint(id)) {...} ` to check for support. See [ Can I
41
- Use] ( https://caniuse.com/css-paint-api ) for updates.
42
-
43
33
These properties control the squircle drawing:
44
34
45
- | Property | Equivalent |
46
- | ------------------------- | ------------------ |
47
- | ` --squircle-radius ` | ` border-radius ` |
48
- | ` --squircle-fill ` | ` background-color ` |
49
- | ` --squircle-border-width ` | ` border-width ` |
50
- | ` --squircle-border-color ` | ` border-color ` |
35
+ | Property | Equivalent |
36
+ | ----------------------------- | ------------------ |
37
+ | ` --squircle-background-color ` | ` background-color ` |
38
+ | ` --squircle-border-radius ` | ` border-radius ` |
39
+ | ` --squircle-border-width ` | ` border-width ` |
40
+ | ` --squircle-border-color ` | ` border-color ` |
51
41
52
- To reduce verbosity, consider aliasing the CSS properties. For example, to use
53
- ` --radius ` instead of ` --squircle-radius ` :
42
+ To reduce the verbosity, consider using aliases:
54
43
55
44
``` css
56
45
.squircle {
57
- --squircle-radius : var (--radius );
46
+ --squircle-background-color : var (--fill );
47
+ --squircle-border-radius : var (--radius );
48
+ --squircle-border-width : var (--border-width );
49
+ --squircle-border-color : var (--border-color );
58
50
}
59
51
```
60
52
53
+ ### Web component
54
+
55
+ The ` squircle ` class falls back to normal rounded rectangles on browsers that
56
+ don't support the Paint API, which is [ most of
57
+ them] ( https://caniuse.com/css-paint-api ) at time of writing. To guarantee
58
+ squircles on all platforms, you can use the web component instead. It will add
59
+ an HTML5 ` <canvas> ` to draw with when the Paint API isn't available.
60
+
61
+ ``` js
62
+ import { createCustomElement } from ' superellipse-squircle' ;
63
+ createCustomElement ();
64
+ ```
65
+
66
+ ``` html
67
+ <th-squircle
68
+ background-color =" rgba(64, 128, 192, 0.5)"
69
+ border-radius =" 16"
70
+ border-width =" 4"
71
+ border-color =" black"
72
+ >
73
+ Hello, world!
74
+ </th-squircle >
75
+ ```
76
+
61
77
### Canvas
62
78
63
79
You can also use the squircle drawing code directly in an HTML canvas. Import
64
80
the ` paint ` function to draw into a canvas context.
65
81
66
82
``` js
67
- import { paint } from " superellipse-squircle" ;
68
- paint (canvasContext, posX, posY, width, height, borderRadius);
83
+ import { draw , paint } from " superellipse-squircle" ;
84
+
85
+ // Just create a path
86
+ draw (canvasContext, posX, posY, width, height, borderRadius);
87
+
88
+ // Draw the squircle with stroke and fill
89
+ paint (canvasContext, posX, posY, width, height, borderRadius,
90
+ borderWidth, fillColor, borderColor);
91
+ ```
92
+
93
+ ### SVG
94
+
95
+ You can create a string that is compatible with SVG ` path ` elements.
96
+
97
+ ``` svg
98
+ <svg viewBox =" 0 0 512 512" >
99
+ <path id =" my-path" />
100
+ </svg >
101
+ ```
102
+
103
+ ``` js
104
+ import { path } from " superellipse-squircle" ;
105
+
106
+ const d = path (0 , 0 , 512 , 512 , myRadius);
107
+ const path = document .getElementById (' my-path' );
108
+ path .d = d;
69
109
```
0 commit comments