Skip to content

Commit 116f03f

Browse files
committed
Updated readme documentation
1 parent 58c2712 commit 116f03f

File tree

5 files changed

+117
-57
lines changed

5 files changed

+117
-57
lines changed

CONTRIBUTING.md

+19-8
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,39 @@ cd squircle
88
npm install
99
```
1010

11-
## Test
11+
## Library
1212

13-
To serve a test page and watch for changes:
13+
To serve a test page:
1414

1515
```sh
16-
npm run serve
16+
npm run dev
1717
```
1818

19-
The test page is available at `http://localhost:8080`.
19+
To build the library for production:
2020

21-
## Build
21+
```sh
22+
npm run build
23+
```
2224

23-
To build the package into `./package`:
25+
To publish the library to NPM:
2426

2527
```sh
26-
npm run build
28+
./publish.sh
2729
```
2830

29-
## Publish
31+
### Publish
3032

3133
```sh
3234
cd package
3335
npm publish
3436
cd ..
3537
```
38+
39+
## GitHub Pages demo
40+
41+
The GitHub Actions workflow automatically builds and deploys the site after
42+
pushing to `main`. To serve the website for local development, use
43+
44+
```sh
45+
npm run pages-dev
46+
```

README.md

+72-32
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,101 @@ magic](https://tim-harding.github.io/squircle/).
99

1010
### CSS
1111

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.
1613

1714
```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-
2415
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.
2623

27-
import "superellipse-squircle/index.css"; // Optional
24+
```html
2825
<div
2926
class="squircle"
3027
style="
31-
--squircle-radius: 1rem;
32-
--squircle-fill: black;"
28+
--squircle-border-radius: 1rem;
29+
--squircle-background-color: black;"
3330
></div>;
3431
```
3532

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-
4333
These properties control the squircle drawing:
4434

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` |
5141

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:
5443

5544
```css
5645
.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);
5850
}
5951
```
6052

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+
6177
### Canvas
6278

6379
You can also use the squircle drawing code directly in an HTML canvas. Import
6480
the `paint` function to draw into a canvas context.
6581

6682
```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;
69109
```

TODO.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- Use slots for Canvas web component
12
- Update readme for API updates
23
- Touch support for corners
34
- Title gradient

src/index.js

+24-14
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,50 @@ export * from "./drawing.js";
1111
* @property {number} y y-coordinate
1212
*/
1313

14+
function registerInner() {
15+
let isRegistered = false;
16+
return function() {
17+
if (isRegistered) return;
18+
isRegistered = true;
19+
20+
const style = document.createElement("style");
21+
style.textContent = styles;
22+
document.head.append(style);
23+
24+
/* @ts-ignore */
25+
if (!CSS.paintWorklet) return;
26+
/* @ts-ignore */
27+
CSS.paintWorklet.addModule(workletUrl);
28+
}
29+
}
30+
1431
/**
1532
* Register the squircle CSS Paint worklet.
1633
*/
17-
export function register() {
18-
const style = document.createElement("style");
19-
style.textContent = styles;
20-
document.head.append(style);
21-
22-
/* @ts-ignore */
23-
if (!CSS.paintWorklet) return;
24-
/* @ts-ignore */
25-
CSS.paintWorklet.addModule(workletUrl);
26-
}
34+
export const register = registerInner()
2735

2836
/**
2937
* Creates a custom element that uses the Paint API if available or an HTML
3038
* canvas if not.
3139
*
3240
* Usage:
3341
* ```js
42+
* // No argument defaults to th-squircle
3443
* createCustomElement('my-squircle')
3544
* ```
3645
* ```html
3746
* <my-squircle
38-
* radius="16"
39-
* fill="#deadbeef"
47+
* background-color="#deadbeef"
48+
* border-radius="16"
4049
* border-width="4"
4150
* border-color="#cafebabe"
4251
* ></my-squircle>
4352
* ```
4453
*
45-
* @param {string} name
54+
* @param {string} [name='th-squircle'] HTML element tag
4655
*/
47-
export function createCustomElement(name) {
56+
export function createCustomElement(name = 'th-squircle') {
57+
register()
4858
/* @ts-ignore */
4959
const isPaintSupported = !!CSS.paintWorklet;
5060
const component = isPaintSupported ? SquircleHoudini : SquircleCanvas;

tsconfig.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
"isolatedModules": true,
1818
"strict": true,
1919
"allowJs": true,
20-
"checkJs": true,
21-
"declaration": true,
22-
"emitDeclarationOnly": true
20+
"checkJs": true
2321
}
2422
}

0 commit comments

Comments
 (0)