Skip to content

Commit 6074996

Browse files
committed
Move corners on drag area resize
1 parent 84afadc commit 6074996

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

TODO.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
- [ ] Update readme for API updates
2-
32
- [ ] Favicon
4-
53
- [ ] Touch support
6-
7-
- [ ] Support gradients
8-
9-
Gradients support adds complexity since instead of just passing `--squircle-fill` to the canvas `fillStyle`, we have to either parse the `--squircle-fill` as a color or gradient definition to create a `CanvasGradient` in code. Either that or we can support arbitrary `<image>` definitions, which requires creating an `SVGImageElement` for `drawImage`. That could involve a lot of string manipulation, which might not be as good for something that needs to run really fast.
4+
- [ ] Title gradient
5+
- [ ] Animation
6+
- [ ] Corner click offset
7+
- [ ] Footer
8+
- [ ] Fade in on load

demo/assets/js/drag-area.js

+41-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import { Corner } from "./corner.js";
33

44
const SIDE_OFFSET = 32;
55

6-
// TODO: Resize observer and move handler accordingly
7-
86
export class DragArea extends HTMLElement {
97
_l = SIDE_OFFSET;
108
_r = 0;
119
_t = SIDE_OFFSET;
1210
_b = 0;
11+
_width = 0;
12+
_height = 0;
1313
/** @type {HTMLElement?} */
1414
_squircle = null;
15+
/** @type {Corner?} */
16+
_topLeft = null;
17+
/** @type {Corner?} */
18+
_bottomRight = null;
1519

1620
constructor() {
1721
super();
@@ -22,11 +26,44 @@ export class DragArea extends HTMLElement {
2226

2327
connectedCallback() {
2428
const { clientWidth: w, clientHeight: h } = this;
29+
this._width = w;
30+
this._height = h;
2531
this._r = w - SIDE_OFFSET;
2632
this._b = h - SIDE_OFFSET;
2733

2834
this._squircle = this.querySelector("th-squircle");
2935
this._updateSquircleCorners();
36+
37+
const observer = new ResizeObserver((entries) => {
38+
for (const entry of entries) {
39+
const sizes = entry.borderBoxSize ?? entry.contentBoxSize;
40+
if (sizes) {
41+
const [{ inlineSize, blockSize }] = sizes;
42+
this._width = inlineSize;
43+
this._height = blockSize;
44+
} else {
45+
const { width, height } = entry.contentRect;
46+
this._width = width;
47+
this._height = height;
48+
}
49+
this._l = Math.min(this._l, this._width);
50+
this._r = Math.min(this._r, this._width);
51+
this._t = Math.min(this._t, this._height);
52+
this._b = Math.min(this._b, this._height);
53+
54+
if (this._topLeft !== null) {
55+
this._topLeft.x = `${this._l}px`;
56+
this._topLeft.y = `${this._t}px`;
57+
}
58+
if (this._bottomRight !== null) {
59+
this._bottomRight.x = `${this._r}px`;
60+
this._bottomRight.y = `${this._b}px`;
61+
}
62+
63+
this._updateSquircleCorners();
64+
}
65+
});
66+
observer.observe(this);
3067
}
3168

3269
/**
@@ -40,17 +77,14 @@ export class DragArea extends HTMLElement {
4077
case "top-left": {
4178
corner.x = `${this._l}px`;
4279
corner.y = `${this._t}px`;
80+
this._topLeft = corner;
4381
break;
4482
}
4583

4684
case "bottom-right": {
4785
corner.x = `${this._r}px`;
4886
corner.y = `${this._b}px`;
49-
break;
50-
}
51-
52-
default: {
53-
console.warn(`Unexpected corner side: ${corner.side}`);
87+
this._bottomRight = corner;
5488
break;
5589
}
5690
}

0 commit comments

Comments
 (0)