vue-peel-card 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -52
- package/dist/vue-peel-card.js +83 -83
- package/dist/vue-peel-card.umd.cjs +3 -3
- package/package.json +1 -1
- package/src/PeelCard.vue +59 -59
- package/src/index.js +1 -1
- package/types/index.d.ts +4 -4
package/README.md
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
# vue-peel-card
|
|
2
2
|
|
|
3
|
-
Vue 3
|
|
4
|
-
|
|
3
|
+
Vue 3 component: a card you can peel off by its edge. Rendered with WebGL; the card content is plain HTML.
|
|
4
|
+
The curl follows your finger and rotates if you drag up or down. Includes shadows, a highlight on the fold and the back side of the paper.
|
|
5
5
|
|
|
6
|
-
##
|
|
6
|
+
## Installation
|
|
7
7
|
|
|
8
8
|
```bash
|
|
9
9
|
npm i vue-peel-card
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
Requires Vue `^3.3`. No separate CSS import is needed.
|
|
13
13
|
|
|
14
|
-
##
|
|
14
|
+
## Quick start
|
|
15
15
|
|
|
16
16
|
```vue
|
|
17
17
|
<script setup>
|
|
@@ -19,8 +19,8 @@ import { PeelCard } from 'vue-peel-card'
|
|
|
19
19
|
|
|
20
20
|
const cards = [
|
|
21
21
|
{
|
|
22
|
-
bg: '#f6efe0', //
|
|
23
|
-
back: '#f1eadb', //
|
|
22
|
+
bg: '#f6efe0', // front side color
|
|
23
|
+
back: '#f1eadb', // back side paper color
|
|
24
24
|
html: `<div style="padding:30px;font:700 44px Georgia,serif">Serendipity</div>`,
|
|
25
25
|
},
|
|
26
26
|
{
|
|
@@ -33,85 +33,85 @@ const cards = [
|
|
|
33
33
|
|
|
34
34
|
<template>
|
|
35
35
|
<div style="height: 480px">
|
|
36
|
-
<PeelCard :cards="cards" @change="i => console.log('
|
|
36
|
+
<PeelCard :cards="cards" @change="i => console.log('on top', i)" />
|
|
37
37
|
</div>
|
|
38
38
|
</template>
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
The component fills its parent (`width: 100%; height: 100%`), so the parent must have a height.
|
|
42
|
+
Global registration: `app.use(PeelCardPlugin)`.
|
|
43
43
|
|
|
44
|
-
##
|
|
44
|
+
## Toggle mode
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
Exactly two cards. The first swipe peels the top card, the second (back) sticks it into place.
|
|
47
47
|
|
|
48
48
|
```vue
|
|
49
49
|
<PeelCard toggle :cards="[front, back]" @toggle="peeled => (isPeeled = peeled)" ref="card" />
|
|
50
|
-
<button @click="card.peel()"
|
|
50
|
+
<button @click="card.peel()">Toggle</button>
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
While the card is peeled, it stands at an angle by the edge and the scene shifts so both cards fit.
|
|
54
|
+
You can stick it back with a swipe anywhere on the canvas against the peel direction, or with a button via `peel()`.
|
|
55
55
|
|
|
56
|
-
##
|
|
56
|
+
## Card format
|
|
57
57
|
|
|
58
58
|
```ts
|
|
59
59
|
{ bg: string, back: string, html: string }
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
-
- `bg` — CSS
|
|
63
|
-
- `back` —
|
|
64
|
-
- `html` —
|
|
62
|
+
- `bg` — CSS color of the front side, placed under `html`.
|
|
63
|
+
- `back` — color of the back side, `#rrggbb`.
|
|
64
|
+
- `html` — content. The default card size is 420×280; fit your layout to `width`/`height`.
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
`html` restrictions:
|
|
67
67
|
|
|
68
|
-
-
|
|
69
|
-
-
|
|
70
|
-
-
|
|
68
|
+
- It must be well-formed XHTML: closed tags, `<br/>`, ` ` instead of ` `.
|
|
69
|
+
- Styles only inside: `<style>` or the `style` attribute.
|
|
70
|
+
- External files, images by URL and web fonts are unavailable. Embed images as `data:` URIs.
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
Under the hood the HTML is drawn into a texture via SVG `<foreignObject>`. If the browser does not allow such a canvas in WebGL,
|
|
73
|
+
a fallback is drawn: the `bg` color and plain text.
|
|
74
74
|
|
|
75
75
|
## Props
|
|
76
76
|
|
|
77
|
-
| Prop |
|
|
77
|
+
| Prop | Type | Default | Description |
|
|
78
78
|
|---|---|---|---|
|
|
79
|
-
| `cards` | `{bg, back, html}[]` |
|
|
80
|
-
| `toggle` | `boolean` | `false` |
|
|
81
|
-
| `width` | `number` | `420` |
|
|
82
|
-
| `height` | `number` | `280` |
|
|
83
|
-
| `radius` | `number` | `18` |
|
|
84
|
-
| `background` | `string` |
|
|
85
|
-
| `padding` | `number` | `16` |
|
|
86
|
-
| `maxScale` | `number` | `1.35` |
|
|
87
|
-
| `hint` | `boolean` | `true` |
|
|
88
|
-
| `label` | `string` | — | `aria-label`
|
|
89
|
-
|
|
90
|
-
##
|
|
91
|
-
|
|
92
|
-
|
|
|
79
|
+
| `cards` | `{bg, back, html}[]` | required | cards; cycled in normal mode |
|
|
80
|
+
| `toggle` | `boolean` | `false` | two-card mode; read on mount, use `:key` to change the mode |
|
|
81
|
+
| `width` | `number` | `420` | card width in logical units |
|
|
82
|
+
| `height` | `number` | `280` | card height |
|
|
83
|
+
| `radius` | `number` | `18` | corner radius |
|
|
84
|
+
| `background` | `string` | from parent CSS | "table" color |
|
|
85
|
+
| `padding` | `number` | `16` | gap between the card and the component edges |
|
|
86
|
+
| `maxScale` | `number` | `1.35` | maximum zoom |
|
|
87
|
+
| `hint` | `boolean` | `true` | edge wiggle on start |
|
|
88
|
+
| `label` | `string` | — | `aria-label` of the canvas |
|
|
89
|
+
|
|
90
|
+
## Events
|
|
91
|
+
|
|
92
|
+
| Event | When |
|
|
93
93
|
|---|---|
|
|
94
|
-
| `grab` |
|
|
95
|
-
| `peel(i)` |
|
|
96
|
-
| `change(i)` |
|
|
97
|
-
| `toggle(peeled)` |
|
|
94
|
+
| `grab` | first touch of the card |
|
|
95
|
+
| `peel(i)` | normal mode: card `i` is fully peeled off |
|
|
96
|
+
| `change(i)` | normal mode: card `i` became the top card |
|
|
97
|
+
| `toggle(peeled)` | toggle mode: `true` — peeled, `false` — stuck back |
|
|
98
98
|
|
|
99
|
-
##
|
|
99
|
+
## Methods
|
|
100
100
|
|
|
101
|
-
`peel()`
|
|
101
|
+
`peel()` via `ref`: peels the card; in toggle mode switches the state.
|
|
102
102
|
|
|
103
|
-
##
|
|
103
|
+
## Accessibility
|
|
104
104
|
|
|
105
|
-
|
|
106
|
-
`prefers-reduced-motion`.
|
|
105
|
+
The canvas is keyboard-focusable: Right Arrow, Space or Enter peels the card. The hint animation is disabled with
|
|
106
|
+
`prefers-reduced-motion`. If WebGL is unavailable, the `fallback` slot is shown.
|
|
107
107
|
|
|
108
|
-
##
|
|
108
|
+
## Development
|
|
109
109
|
|
|
110
110
|
```bash
|
|
111
111
|
npm install
|
|
112
|
-
npm run build # dist/vue-peel-card.js (ESM)
|
|
112
|
+
npm run build # dist/vue-peel-card.js (ESM) and dist/vue-peel-card.umd.cjs
|
|
113
113
|
```
|
|
114
114
|
|
|
115
|
-
##
|
|
115
|
+
## License
|
|
116
116
|
|
|
117
117
|
MIT
|
package/dist/vue-peel-card.js
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { openBlock as ye, createElementBlock as Ie, normalizeStyle as
|
|
2
|
-
const
|
|
3
|
-
const c =
|
|
4
|
-
for (const [l, v] of
|
|
1
|
+
import { openBlock as ye, createElementBlock as Ie, normalizeStyle as ie, createElementVNode as Je, renderSlot as et, createTextVNode as tt, createCommentVNode as nt, ref as O, computed as at, onMounted as ot, onBeforeUnmount as rt, watch as it } from "vue";
|
|
2
|
+
const st = (i, s) => {
|
|
3
|
+
const c = i.__vccOpts || i;
|
|
4
|
+
for (const [l, v] of s)
|
|
5
5
|
c[l] = v;
|
|
6
6
|
return c;
|
|
7
7
|
}, B = 18, ct = 75, Le = 1, $ = 1.15;
|
|
8
|
-
function
|
|
9
|
-
return
|
|
8
|
+
function se(i, s, c, l) {
|
|
9
|
+
return i <= c * s ? i - s * Math.sin(i / s) : i - s * Math.sin(l) + (i - c * s) * Math.cos(l);
|
|
10
10
|
}
|
|
11
|
-
function lt(
|
|
11
|
+
function lt(i, s, c, l) {
|
|
12
12
|
let v = 0, T = 4e3;
|
|
13
13
|
for (let R = 0; R < 48; R++) {
|
|
14
14
|
const L = (v + T) / 2;
|
|
15
|
-
|
|
15
|
+
se(L, s, c, l) < i ? v = L : T = L;
|
|
16
16
|
}
|
|
17
17
|
return (v + T) / 2;
|
|
18
18
|
}
|
|
19
|
-
function ft(
|
|
20
|
-
const c = B + (ct - B) * Math.exp(-
|
|
21
|
-
let l = 0.12 + 0.55 * Math.exp(-
|
|
22
|
-
if (
|
|
23
|
-
const T = ce(0.45, 1,
|
|
19
|
+
function ft(i, s) {
|
|
20
|
+
const c = B + (ct - B) * Math.exp(-i / 70);
|
|
21
|
+
let l = 0.12 + 0.55 * Math.exp(-i / 140);
|
|
22
|
+
if (s > 0) {
|
|
23
|
+
const T = ce(0.45, 1, i / s);
|
|
24
24
|
l = l * (1 - T) + $ * T;
|
|
25
25
|
}
|
|
26
26
|
const v = Math.PI - l;
|
|
27
|
-
return { R: c, phi: l, Th: v, tf: lt(
|
|
27
|
+
return { R: c, phi: l, Th: v, tf: lt(i, c, v, l), sp: Math.sin(l), cp: Math.cos(l) };
|
|
28
28
|
}
|
|
29
|
-
const
|
|
30
|
-
const c = Math.atan2(
|
|
29
|
+
const A = (i, s) => {
|
|
30
|
+
const c = Math.atan2(i[1], i[0]) + s;
|
|
31
31
|
return [Math.cos(c), Math.sin(c)];
|
|
32
32
|
};
|
|
33
|
-
function ce(
|
|
34
|
-
return c = Math.min(1, Math.max(0, (c -
|
|
33
|
+
function ce(i, s, c) {
|
|
34
|
+
return c = Math.min(1, Math.max(0, (c - i) / (s - i))), c * c * (3 - 2 * c);
|
|
35
35
|
}
|
|
36
|
-
const ut = (
|
|
37
|
-
const
|
|
38
|
-
return [(
|
|
39
|
-
}, dt = (
|
|
40
|
-
<foreignObject width="${
|
|
36
|
+
const ut = (i) => {
|
|
37
|
+
const s = parseInt(String(i).replace("#", "").padEnd(6, "0").slice(0, 6), 16);
|
|
38
|
+
return [(s >> 16) / 255, (s >> 8 & 255) / 255, (s & 255) / 255];
|
|
39
|
+
}, dt = (i, s, c) => `<svg xmlns="http://www.w3.org/2000/svg" width="${s}" height="${c}">
|
|
40
|
+
<foreignObject width="${s}" height="${c}"><div xmlns="http://www.w3.org/1999/xhtml" style="width:${s}px;height:${c}px;background:${i.bg || "#ffffff"}">${i.html || ""}</div></foreignObject></svg>`, Be = `
|
|
41
41
|
uniform vec2 uG, uU, uHalf;
|
|
42
42
|
uniform float uTf, uR, uTheta, uSinPhi, uCosPhi;
|
|
43
43
|
`, ht = `
|
|
@@ -65,7 +65,7 @@ ${Be}
|
|
|
65
65
|
float sdRR(vec2 p, vec2 b, float r){ vec2 q = abs(p)-b+r; return length(max(q,0.0)) + min(max(q.x,q.y),0.0) - r; }
|
|
66
66
|
float hash(vec2 p){ return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453); }
|
|
67
67
|
float rectMask(vec2 p, float soft){ vec2 d = uHalf-abs(p); return smoothstep(-soft*0.5, soft, min(d.x,d.y)); }
|
|
68
|
-
//
|
|
68
|
+
// Shadow of the folded part: which point of the original card hangs above point q.
|
|
69
69
|
float flapShadow(vec2 q){
|
|
70
70
|
float tq = dot(q-uG, uU);
|
|
71
71
|
float tL = uTf - uR, tA = uTf - uR*uSinPhi;
|
|
@@ -75,11 +75,11 @@ float flapShadow(vec2 q){
|
|
|
75
75
|
else{ float e = (tq-tA)/uCosPhi; s = uTheta*uR + e; z = uR*(1.0+uCosPhi) + e*uSinPhi; }
|
|
76
76
|
vec2 po = q + uU*((uTf - s) - tq);
|
|
77
77
|
return rectMask(po, 3.0 + 0.3*z) * decay * exp(-z/110.0);
|
|
78
|
-
}`, mt = (
|
|
78
|
+
}`, mt = (i) => `${Ue()}
|
|
79
79
|
uniform sampler2D uFront; uniform vec3 uBack; uniform float uScale, uAlpha;
|
|
80
80
|
varying vec2 vOrig, vXY; varying float vZ, vTh, vS;
|
|
81
81
|
void main(){
|
|
82
|
-
float sd = sdRR(vOrig, uHalf, ${
|
|
82
|
+
float sd = sdRR(vOrig, uHalf, ${i.toFixed(1)});
|
|
83
83
|
float a = clamp(0.5 - sd*uScale, 0.0, 1.0);
|
|
84
84
|
if(a <= 0.0) discard;
|
|
85
85
|
float c = cos(vTh);
|
|
@@ -99,17 +99,17 @@ void main(){
|
|
|
99
99
|
col += max(pow(max(dot(N,Hh),0.0),48.0) - pow(Hh.z,48.0), 0.0)*0.2;
|
|
100
100
|
if(front && vTh < 1.5707963) col *= 1.0 - 0.55*flapShadow(vXY)*exp(-vZ/16.0);
|
|
101
101
|
gl_FragColor = vec4(col*a*uAlpha, a*uAlpha);
|
|
102
|
-
}`, gt = "attribute vec2 aPos; void main(){ gl_Position = vec4(aPos,0.0,1.0); }", pt = (
|
|
102
|
+
}`, gt = "attribute vec2 aPos; void main(){ gl_Position = vec4(aPos,0.0,1.0); }", pt = (i) => `${Ue()}
|
|
103
103
|
uniform sampler2D uBase; uniform vec2 uView, uShift; uniform float uScale; uniform vec3 uBg;
|
|
104
104
|
void main(){
|
|
105
105
|
vec2 q = (gl_FragCoord.xy - 0.5*uView)/uScale + uShift;
|
|
106
106
|
vec2 sc = gl_FragCoord.xy/uView - 0.5;
|
|
107
107
|
vec3 col = uBg*(1.0 - 0.22*dot(sc,sc)*2.0) + (hash(gl_FragCoord.xy)-0.5)*0.012;
|
|
108
|
-
float dsh = sdRR(q - vec2(0.0,-14.0), uHalf, ${
|
|
108
|
+
float dsh = sdRR(q - vec2(0.0,-14.0), uHalf, ${i.toFixed(1)});
|
|
109
109
|
col *= 1.0 - 0.30*(1.0 - smoothstep(-20.0, 34.0, dsh));
|
|
110
|
-
float dc = sdRR(q - vec2(0.0,-2.0), uHalf, ${
|
|
110
|
+
float dc = sdRR(q - vec2(0.0,-2.0), uHalf, ${i.toFixed(1)});
|
|
111
111
|
col *= 1.0 - 0.18*(1.0 - smoothstep(-2.0, 8.0, dc));
|
|
112
|
-
float sd = sdRR(q, uHalf, ${
|
|
112
|
+
float sd = sdRR(q, uHalf, ${i.toFixed(1)});
|
|
113
113
|
float a = clamp(0.5 - sd*uScale, 0.0, 1.0);
|
|
114
114
|
vec3 tex = texture2D(uBase, q/(2.0*uHalf)+0.5).rgb*0.97;
|
|
115
115
|
col = mix(col, tex, a);
|
|
@@ -127,10 +127,10 @@ void main(){
|
|
|
127
127
|
padding: { type: Number, default: 16 },
|
|
128
128
|
maxScale: { type: Number, default: 1.35 },
|
|
129
129
|
hint: { type: Boolean, default: !0 },
|
|
130
|
-
label: { type: String, default: "
|
|
130
|
+
label: { type: String, default: "Card: drag the edge to peel it off" }
|
|
131
131
|
},
|
|
132
132
|
emits: ["grab", "peel", "change", "toggle"],
|
|
133
|
-
setup(
|
|
133
|
+
setup(i, { emit: s, expose: c }) {
|
|
134
134
|
const l = O(null), v = O(null), T = O(!1), R = O(!1), L = O(!1), ke = { position: "relative", width: "100%", height: "100%", minHeight: "200px" }, Ce = { position: "absolute", inset: "0", display: "grid", placeItems: "center", padding: "24px", textAlign: "center", opacity: ".7" }, De = at(() => ({
|
|
135
135
|
position: "absolute",
|
|
136
136
|
inset: "0",
|
|
@@ -155,12 +155,12 @@ void main(){
|
|
|
155
155
|
const fe = { peel() {
|
|
156
156
|
} };
|
|
157
157
|
function Oe() {
|
|
158
|
-
const h = v.value, f =
|
|
158
|
+
const h = v.value, f = i.width, g = i.height, W = i.radius, x = i.toggle, t = h.getContext("webgl2", { antialias: !0, alpha: !1 }) || h.getContext("webgl", { antialias: !0, alpha: !1 });
|
|
159
159
|
if (!t) {
|
|
160
160
|
L.value = !0;
|
|
161
161
|
return;
|
|
162
162
|
}
|
|
163
|
-
const Ge = typeof WebGL2RenderingContext < "u" && t instanceof WebGL2RenderingContext, _ = () => x ? 2 :
|
|
163
|
+
const Ge = typeof WebGL2RenderingContext < "u" && t instanceof WebGL2RenderingContext, _ = () => x ? 2 : i.cards.length;
|
|
164
164
|
let G = !0;
|
|
165
165
|
const ue = [];
|
|
166
166
|
let j = 0;
|
|
@@ -179,19 +179,19 @@ void main(){
|
|
|
179
179
|
}, S = de(ht, mt(W)), F = de(gt, pt(W)), he = Math.max(2.5, Math.sqrt((f + 4) * (g + 4) / 6e4)), y = Math.round((f + 4) / he), U = Math.round((g + 4) / he), me = f / 2 + 2, ge = g / 2 + 2, Z = new Float32Array((y + 1) * (U + 1) * 2);
|
|
180
180
|
for (let n = 0, a = 0; n <= U; n++) for (let o = 0; o <= y; o++)
|
|
181
181
|
Z[a++] = -me + 2 * me * o / y, Z[a++] = -ge + 2 * ge * n / U;
|
|
182
|
-
const
|
|
182
|
+
const w = new Uint16Array(y * U * 6);
|
|
183
183
|
for (let n = 0, a = 0; n < U; n++) for (let o = 0; o < y; o++) {
|
|
184
184
|
const r = n * (y + 1) + o, u = r + 1, p = r + y + 1, d = p + 1;
|
|
185
|
-
|
|
185
|
+
w[a++] = r, w[a++] = u, w[a++] = p, w[a++] = u, w[a++] = d, w[a++] = p;
|
|
186
186
|
}
|
|
187
187
|
const K = t.createBuffer();
|
|
188
188
|
t.bindBuffer(t.ARRAY_BUFFER, K), t.bufferData(t.ARRAY_BUFFER, Z, t.STATIC_DRAW);
|
|
189
189
|
const Q = t.createBuffer();
|
|
190
|
-
t.bindBuffer(t.ELEMENT_ARRAY_BUFFER, Q), t.bufferData(t.ELEMENT_ARRAY_BUFFER,
|
|
190
|
+
t.bindBuffer(t.ELEMENT_ARRAY_BUFFER, Q), t.bufferData(t.ELEMENT_ARRAY_BUFFER, w, t.STATIC_DRAW);
|
|
191
191
|
const J = t.createBuffer();
|
|
192
192
|
t.bindBuffer(t.ARRAY_BUFFER, J), t.bufferData(t.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), t.STATIC_DRAW);
|
|
193
193
|
let k = 2, E = 0;
|
|
194
|
-
const
|
|
194
|
+
const P = /* @__PURE__ */ new Map();
|
|
195
195
|
async function qe(n) {
|
|
196
196
|
const a = Math.round(f * k), o = Math.round(g * k), r = document.createElement("canvas");
|
|
197
197
|
r.width = a, r.height = o;
|
|
@@ -203,7 +203,7 @@ void main(){
|
|
|
203
203
|
});
|
|
204
204
|
d.src = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(dt(n, f, g)), await m, u.drawImage(d, 0, 0, a, o), u.getImageData(0, 0, 1, 1), p = !0;
|
|
205
205
|
} catch (d) {
|
|
206
|
-
console.warn("[PeelCard] foreignObject
|
|
206
|
+
console.warn("[PeelCard] foreignObject unavailable, drawing fallback", d);
|
|
207
207
|
}
|
|
208
208
|
if (!p) {
|
|
209
209
|
u.setTransform(1, 0, 0, 1, 0, 0), u.fillStyle = n.bg || "#ffffff", u.fillRect(0, 0, a, o);
|
|
@@ -213,18 +213,18 @@ void main(){
|
|
|
213
213
|
return r;
|
|
214
214
|
}
|
|
215
215
|
function q(n) {
|
|
216
|
-
if (!_() || !
|
|
217
|
-
if (n = (n % _() + _()) % _(),
|
|
218
|
-
const a =
|
|
216
|
+
if (!_() || !i.cards.length) return Promise.resolve();
|
|
217
|
+
if (n = (n % _() + _()) % _(), P.has(n)) return P.get(n).promise;
|
|
218
|
+
const a = i.cards[n % i.cards.length], o = { tex: null, back: ut(a.back || "#f2efe6") };
|
|
219
219
|
return o.promise = qe(a).then((r) => {
|
|
220
|
-
if (!G ||
|
|
220
|
+
if (!G || P.get(n) !== o) return;
|
|
221
221
|
const u = t.createTexture();
|
|
222
222
|
t.bindTexture(t.TEXTURE_2D, u), t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL, !0), t.texImage2D(t.TEXTURE_2D, 0, t.RGBA, t.RGBA, t.UNSIGNED_BYTE, r), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_WRAP_S, t.CLAMP_TO_EDGE), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_WRAP_T, t.CLAMP_TO_EDGE), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_MAG_FILTER, t.LINEAR), Ge ? (t.generateMipmap(t.TEXTURE_2D), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_MIN_FILTER, t.LINEAR_MIPMAP_LINEAR)) : t.texParameteri(t.TEXTURE_2D, t.TEXTURE_MIN_FILTER, t.LINEAR), o.tex = u;
|
|
223
|
-
}),
|
|
223
|
+
}), P.set(n, o), o.promise;
|
|
224
224
|
}
|
|
225
225
|
function ee(n) {
|
|
226
226
|
const a = n ? /* @__PURE__ */ new Set() : /* @__PURE__ */ new Set([E, (E + 1) % _(), (E + 2) % _()]);
|
|
227
|
-
for (const [o, r] of
|
|
227
|
+
for (const [o, r] of P) a.has(o) || (r.tex && t.deleteTexture(r.tex), P.delete(o));
|
|
228
228
|
}
|
|
229
229
|
const pe = () => Promise.all([q(0), q(1), q(2)]);
|
|
230
230
|
let C = 1, D = 1, te = 1, ne = 1, X = 1, H = 1;
|
|
@@ -232,11 +232,11 @@ void main(){
|
|
|
232
232
|
const n = l.value.getBoundingClientRect();
|
|
233
233
|
te = Math.max(1, n.width), ne = Math.max(1, n.height), C = Math.min(window.devicePixelRatio || 1, 2), X = Math.round(te * C), H = Math.round(ne * C), h.width = X, h.height = H;
|
|
234
234
|
const a = x ? 1.5 : 1;
|
|
235
|
-
D = Math.max(0.3, Math.min(
|
|
235
|
+
D = Math.max(0.3, Math.min(i.maxScale, (te - 2 * i.padding) / (f * a), (ne - 2 * i.padding) / (g * a)));
|
|
236
236
|
}
|
|
237
237
|
let I = [0.9, 0.89, 0.85];
|
|
238
238
|
function V() {
|
|
239
|
-
let n =
|
|
239
|
+
let n = i.background;
|
|
240
240
|
if (!n)
|
|
241
241
|
for (let o = l.value; o; o = o.parentElement) {
|
|
242
242
|
const r = getComputedStyle(o).backgroundColor;
|
|
@@ -270,7 +270,7 @@ void main(){
|
|
|
270
270
|
restick: !1,
|
|
271
271
|
shiftF: 0,
|
|
272
272
|
shiftVec: [0, 0]
|
|
273
|
-
}, Xe = matchMedia("(prefers-reduced-motion: reduce)").matches, He = [[-f / 2, -g / 2], [f / 2, -g / 2], [-f / 2, g / 2], [f / 2, g / 2]], Te = (n, a) => He.map((o) => (o[0] - n[0]) * a[0] + (o[1] - n[1]) * a[1]), ae = (n, a) => Math.max(...Te(n, a)), N = (n, a) =>
|
|
273
|
+
}, Xe = matchMedia("(prefers-reduced-motion: reduce)").matches, He = [[-f / 2, -g / 2], [f / 2, -g / 2], [-f / 2, g / 2], [f / 2, g / 2]], Te = (n, a) => He.map((o) => (o[0] - n[0]) * a[0] + (o[1] - n[1]) * a[1]), ae = (n, a) => Math.max(...Te(n, a)), N = (n, a) => se(ae(n, a) + 4, B, Math.PI - $, $), Ve = (n, a) => {
|
|
274
274
|
const o = Te(n, a);
|
|
275
275
|
return Math.cos($) * (Math.max(...o) - Math.min(...o)) + B + 8;
|
|
276
276
|
}, Ee = (n) => {
|
|
@@ -288,31 +288,31 @@ void main(){
|
|
|
288
288
|
function Ye(n) {
|
|
289
289
|
const a = [n[0] - e.p0[0], n[1] - e.p0[1]], o = Math.hypot(a[0], a[1]);
|
|
290
290
|
if (e.restick) {
|
|
291
|
-
const m =
|
|
291
|
+
const m = A(e.nIn, e.angBase).map((Qe) => -Qe), b = Math.max(-0.6, Math.min(0.6, Math.atan2(m[0] * a[1] - m[1] * a[0], a[0] * m[0] + a[1] * m[1])));
|
|
292
292
|
e.angT = e.angBase + b * ce(6, 70, o);
|
|
293
|
-
const M =
|
|
293
|
+
const M = A(e.nIn, e.angT);
|
|
294
294
|
e.dT = Math.max(0, -(a[0] * M[0] + a[1] * M[1])), Re(e.dT);
|
|
295
295
|
return;
|
|
296
296
|
}
|
|
297
297
|
const r = a[0] * e.nIn[0] + a[1] * e.nIn[1], u = e.nIn[0] * a[1] - e.nIn[1] * a[0], p = Math.max(-Le, Math.min(Le, Math.atan2(u, r)));
|
|
298
298
|
e.angT = p * ce(6, 70, o);
|
|
299
|
-
const d =
|
|
299
|
+
const d = A(e.nIn, e.angT);
|
|
300
300
|
e.dT = Math.max(0, a[0] * d[0] + a[1] * d[1]), Re(e.dT);
|
|
301
301
|
}
|
|
302
302
|
function oe() {
|
|
303
|
-
const n =
|
|
303
|
+
const n = A(e.nIn, e.ang);
|
|
304
304
|
if (x) {
|
|
305
305
|
e.dEnd = N(e.g, n);
|
|
306
306
|
const a = Ve(e.g, n) / 2;
|
|
307
307
|
e.shiftVec = [n[0] * a, n[1] * a];
|
|
308
308
|
} else
|
|
309
|
-
e.dEnd =
|
|
309
|
+
e.dEnd = se(ae(e.g, n) + 2.2 * B + 20, B, Math.PI - 0.12, 0.12);
|
|
310
310
|
e.mode = "complete";
|
|
311
311
|
}
|
|
312
312
|
function $e() {
|
|
313
313
|
if (e.mode !== "drag") return;
|
|
314
314
|
T.value = !1;
|
|
315
|
-
const n =
|
|
315
|
+
const n = A(e.nIn, e.ang);
|
|
316
316
|
if (e.restick) {
|
|
317
317
|
const o = e.d / N(e.g, n);
|
|
318
318
|
o < 0.6 || e.velU > 700 && o < 0.9 ? e.mode = "settle" : oe();
|
|
@@ -330,7 +330,7 @@ void main(){
|
|
|
330
330
|
}
|
|
331
331
|
fe.peel = xe;
|
|
332
332
|
function be() {
|
|
333
|
-
!
|
|
333
|
+
!i.hint || e.mode !== "idle" || e.interacted || Xe || (e.nIn = [1, 0], e.g = [-f / 2, 0], e.ang = 0, e.mode = "hint", e.t = 0);
|
|
334
334
|
}
|
|
335
335
|
const Me = (n) => {
|
|
336
336
|
if (e.mode === "complete" || e.mode === "fade") return;
|
|
@@ -339,7 +339,7 @@ void main(){
|
|
|
339
339
|
h.setPointerCapture(n.pointerId), T.value = !0, e.restick = !0, e.angBase = e.ang, e.p0 = a, e.dT = 0, e.velU = 0, e.lastDT = 0, e.lastTime = performance.now(), e.mode = "drag";
|
|
340
340
|
return;
|
|
341
341
|
}
|
|
342
|
-
Math.abs(a[0]) > f / 2 + 30 || Math.abs(a[1]) > g / 2 + 30 || (h.setPointerCapture(n.pointerId), T.value = !0, e.interacted ||
|
|
342
|
+
Math.abs(a[0]) > f / 2 + 30 || Math.abs(a[1]) > g / 2 + 30 || (h.setPointerCapture(n.pointerId), T.value = !0, e.interacted || s("grab"), e.interacted = !0, e.restick = !1, ze(a), e.mode = "drag");
|
|
343
343
|
}, _e = (n) => {
|
|
344
344
|
e.mode === "drag" && Ye(Ee(n));
|
|
345
345
|
}, z = () => $e(), Se = (n) => {
|
|
@@ -348,19 +348,19 @@ void main(){
|
|
|
348
348
|
h.addEventListener("pointerdown", Me), h.addEventListener("pointermove", _e), h.addEventListener("pointerup", z), h.addEventListener("pointercancel", z), h.addEventListener("keydown", Se);
|
|
349
349
|
function We() {
|
|
350
350
|
const n = E;
|
|
351
|
-
E = (E + 1) % _(), e.d = 0, e.ang = 0, e.alpha = 1, e.mode = "idle", q(E + 2), ee(!1),
|
|
351
|
+
E = (E + 1) % _(), e.d = 0, e.ang = 0, e.alpha = 1, e.mode = "idle", q(E + 2), ee(!1), s("peel", n), s("change", E);
|
|
352
352
|
}
|
|
353
353
|
function je(n) {
|
|
354
354
|
switch (e.mode) {
|
|
355
355
|
case "drag":
|
|
356
356
|
if (e.restick) {
|
|
357
|
-
const o = N(e.g,
|
|
357
|
+
const o = N(e.g, A(e.nIn, e.ang));
|
|
358
358
|
e.d += (Math.max(0, o - e.dT) - e.d) * (1 - Math.exp(-n * 30));
|
|
359
359
|
} else e.d += (Math.min(e.dT, 1500) - e.d) * (1 - Math.exp(-n * 30));
|
|
360
360
|
e.ang += (e.angT - e.ang) * (1 - Math.exp(-n * 16));
|
|
361
361
|
break;
|
|
362
362
|
case "settle":
|
|
363
|
-
e.d += (0 - e.d) * (1 - Math.exp(-n * 9)), e.d < 0.05 && (e.d = 0, e.mode = "idle", x && e.peeled && (e.peeled = !1,
|
|
363
|
+
e.d += (0 - e.d) * (1 - Math.exp(-n * 9)), e.d < 0.05 && (e.d = 0, e.mode = "idle", x && e.peeled && (e.peeled = !1, s("toggle", !1)));
|
|
364
364
|
break;
|
|
365
365
|
case "hint": {
|
|
366
366
|
e.t += n;
|
|
@@ -369,10 +369,10 @@ void main(){
|
|
|
369
369
|
break;
|
|
370
370
|
}
|
|
371
371
|
case "complete":
|
|
372
|
-
e.d += Math.max((e.dEnd - e.d) * (1 - Math.exp(-n * 6)), 380 * n), e.d >= e.dEnd && (e.d = e.dEnd, x ? (e.mode = "rest", e.peeled || (e.peeled = !0,
|
|
372
|
+
e.d += Math.max((e.dEnd - e.d) * (1 - Math.exp(-n * 6)), 380 * n), e.d >= e.dEnd && (e.d = e.dEnd, x ? (e.mode = "rest", e.peeled || (e.peeled = !0, s("toggle", !0))) : (e.mode = "fade", e.t = 0));
|
|
373
373
|
break;
|
|
374
374
|
case "rest":
|
|
375
|
-
e.d = N(e.g,
|
|
375
|
+
e.d = N(e.g, A(e.nIn, e.ang));
|
|
376
376
|
break;
|
|
377
377
|
case "fade":
|
|
378
378
|
e.t += n, e.alpha = Math.max(0, 1 - e.t / 0.28), e.t >= 0.28 && We();
|
|
@@ -381,37 +381,37 @@ void main(){
|
|
|
381
381
|
const a = x && (e.peeled && (e.mode === "rest" || e.mode === "drag" && e.restick) || e.mode === "complete") ? 1 : 0;
|
|
382
382
|
e.shiftF += (a - e.shiftF) * (1 - Math.exp(-n * 6));
|
|
383
383
|
}
|
|
384
|
-
function
|
|
384
|
+
function we(n, a, o) {
|
|
385
385
|
const r = n.loc;
|
|
386
386
|
t.uniform2f(r.uG, e.g[0], e.g[1]), t.uniform2f(r.uU, o[0], o[1]), t.uniform2f(r.uHalf, f / 2, g / 2), t.uniform1f(r.uTf, a.tf), t.uniform1f(r.uR, a.R), t.uniform1f(r.uTheta, a.Th), t.uniform1f(r.uSinPhi, a.sp), t.uniform1f(r.uCosPhi, a.cp), t.uniform2f(r.uView, X, H), t.uniform1f(r.uScale, D * C), t.uniform2f(r.uShift, e.shiftVec[0] * e.shiftF, e.shiftVec[1] * e.shiftF);
|
|
387
387
|
}
|
|
388
388
|
function Ze() {
|
|
389
|
-
const n =
|
|
389
|
+
const n = P.get(E), a = P.get((E + 1) % _());
|
|
390
390
|
if (t.viewport(0, 0, X, H), t.clearColor(I[0], I[1], I[2], 1), t.clear(t.COLOR_BUFFER_BIT | t.DEPTH_BUFFER_BIT), !n || !n.tex || !a || !a.tex) return;
|
|
391
|
-
const o =
|
|
392
|
-
t.disable(t.DEPTH_TEST), t.disable(t.BLEND), t.useProgram(F),
|
|
391
|
+
const o = A(e.nIn, e.ang), r = ft(e.d, x ? N(e.g, o) : 0);
|
|
392
|
+
t.disable(t.DEPTH_TEST), t.disable(t.BLEND), t.useProgram(F), we(F, r, o), t.uniform3f(F.loc.uBg, I[0], I[1], I[2]), t.activeTexture(t.TEXTURE0), t.bindTexture(t.TEXTURE_2D, a.tex), t.uniform1i(F.loc.uBase, 0), t.bindBuffer(t.ARRAY_BUFFER, J), t.enableVertexAttribArray(F.aPos), t.vertexAttribPointer(F.aPos, 2, t.FLOAT, !1, 0, 0), t.drawArrays(t.TRIANGLE_STRIP, 0, 4), t.enable(t.DEPTH_TEST), t.depthFunc(t.LEQUAL), t.enable(t.BLEND), t.blendFunc(t.ONE, t.ONE_MINUS_SRC_ALPHA), t.useProgram(S), we(S, r, o), t.activeTexture(t.TEXTURE0), t.bindTexture(t.TEXTURE_2D, n.tex), t.uniform1i(S.loc.uFront, 0), t.uniform3f(S.loc.uBack, n.back[0], n.back[1], n.back[2]), t.uniform1f(S.loc.uAlpha, e.alpha), t.bindBuffer(t.ARRAY_BUFFER, K), t.enableVertexAttribArray(S.aPos), t.vertexAttribPointer(S.aPos, 2, t.FLOAT, !1, 0, 0), t.bindBuffer(t.ELEMENT_ARRAY_BUFFER, Q), t.drawElements(t.TRIANGLES, w.length, t.UNSIGNED_SHORT, 0);
|
|
393
393
|
}
|
|
394
394
|
let re = performance.now();
|
|
395
|
-
function
|
|
395
|
+
function Pe(n) {
|
|
396
396
|
if (!G) return;
|
|
397
397
|
const a = Math.min(0.05, (n - re) / 1e3);
|
|
398
|
-
re = n, je(a), Ze(), j = requestAnimationFrame(
|
|
398
|
+
re = n, je(a), Ze(), j = requestAnimationFrame(Pe);
|
|
399
399
|
}
|
|
400
|
-
const
|
|
401
|
-
|
|
400
|
+
const Ae = new ResizeObserver(ve);
|
|
401
|
+
Ae.observe(l.value);
|
|
402
402
|
const Y = matchMedia("(prefers-color-scheme: dark)");
|
|
403
403
|
Y.addEventListener && Y.addEventListener("change", V);
|
|
404
404
|
const Fe = new MutationObserver(V);
|
|
405
405
|
Fe.observe(document.documentElement, { attributes: !0 }), ve(), V(), k = Math.min(3, Math.max(2, D * C));
|
|
406
|
-
const Ke =
|
|
406
|
+
const Ke = it(() => i.cards, () => {
|
|
407
407
|
ee(!0), E = 0, e.d = 0, e.mode = "idle", e.alpha = 1, e.peeled = !1, e.shiftF = 0, pe();
|
|
408
408
|
});
|
|
409
409
|
pe().then(() => {
|
|
410
410
|
G && (j = requestAnimationFrame((n) => {
|
|
411
|
-
re = n,
|
|
411
|
+
re = n, Pe(n);
|
|
412
412
|
}), ue.push(setTimeout(be, 900), setTimeout(be, 8e3)));
|
|
413
413
|
}), le = () => {
|
|
414
|
-
G = !1, cancelAnimationFrame(j), ue.forEach(clearTimeout), Ke(),
|
|
414
|
+
G = !1, cancelAnimationFrame(j), ue.forEach(clearTimeout), Ke(), Ae.disconnect(), Fe.disconnect(), Y.removeEventListener && Y.removeEventListener("change", V), h.removeEventListener("pointerdown", Me), h.removeEventListener("pointermove", _e), h.removeEventListener("pointerup", z), h.removeEventListener("pointercancel", z), h.removeEventListener("keydown", Se), ee(!0), t.deleteBuffer(K), t.deleteBuffer(Q), t.deleteBuffer(J), t.deleteProgram(S), t.deleteProgram(F);
|
|
415
415
|
const n = t.getExtension("WEBGL_lose_context");
|
|
416
416
|
n && n.loseContext();
|
|
417
417
|
}, typeof window < "u" && window.__PEEL_DEBUG__ && (window.__peelDebug = e);
|
|
@@ -419,33 +419,33 @@ void main(){
|
|
|
419
419
|
return ot(Oe), rt(() => le()), c({ peel: () => fe.peel() }), { root: l, canvas: v, noGL: L, focused: R, rootStyle: ke, noGLStyle: Ce, canvasStyle: De, onFocus: Ne };
|
|
420
420
|
}
|
|
421
421
|
}, Tt = ["aria-label"];
|
|
422
|
-
function Et(
|
|
422
|
+
function Et(i, s, c, l, v, T) {
|
|
423
423
|
return ye(), Ie("div", {
|
|
424
424
|
ref: "root",
|
|
425
|
-
style:
|
|
425
|
+
style: ie(l.rootStyle)
|
|
426
426
|
}, [
|
|
427
427
|
Je("canvas", {
|
|
428
428
|
ref: "canvas",
|
|
429
429
|
tabindex: "0",
|
|
430
430
|
role: "img",
|
|
431
431
|
"aria-label": c.label,
|
|
432
|
-
style:
|
|
433
|
-
onFocus:
|
|
434
|
-
onBlur:
|
|
432
|
+
style: ie(l.canvasStyle),
|
|
433
|
+
onFocus: s[0] || (s[0] = (...R) => l.onFocus && l.onFocus(...R)),
|
|
434
|
+
onBlur: s[1] || (s[1] = (R) => l.focused = !1)
|
|
435
435
|
}, null, 44, Tt),
|
|
436
436
|
l.noGL ? (ye(), Ie("div", {
|
|
437
437
|
key: 0,
|
|
438
|
-
style:
|
|
438
|
+
style: ie(l.noGLStyle)
|
|
439
439
|
}, [
|
|
440
|
-
et(
|
|
441
|
-
|
|
440
|
+
et(i.$slots, "fallback", {}, () => [
|
|
441
|
+
s[2] || (s[2] = tt("WebGL is not available in this browser.", -1))
|
|
442
442
|
])
|
|
443
443
|
], 4)) : nt("", !0)
|
|
444
444
|
], 4);
|
|
445
445
|
}
|
|
446
|
-
const Rt = /* @__PURE__ */
|
|
447
|
-
install(
|
|
448
|
-
|
|
446
|
+
const Rt = /* @__PURE__ */ st(vt, [["render", Et]]), bt = {
|
|
447
|
+
install(i) {
|
|
448
|
+
i.component("PeelCard", Rt);
|
|
449
449
|
}
|
|
450
450
|
};
|
|
451
451
|
export {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(M,f){typeof exports=="object"&&typeof module<"u"?f(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],f):(M=typeof globalThis<"u"?globalThis:M||self,f(M.VuePeelCard={},M.Vue))})(this,function(M,f){"use strict";const Fe=(i,s)=>{const c=i.__vccOpts||i;for(const[l,v]of s)c[l]=v;return c};function $(i,s,c,l){return i<=c*s?i-s*Math.sin(i/s):i-s*Math.sin(l)+(i-c*s)*Math.cos(l)}function Be(i,s,c,l){let v=0,E=4e3;for(let x=0;x<48;x++){const U=(v+E)/2;$(U,s,c,l)<i?v=U:E=U}return(v+E)/2}function Le(i,s){const c=18+57*Math.exp(-i/70);let l=.12+.55*Math.exp(-i/140);if(s>0){const E=W(.45,1,i/s);l=l*(1-E)+1.15*E}const v=Math.PI-l;return{R:c,phi:l,Th:v,tf:Be(i,c,v,l),sp:Math.sin(l),cp:Math.cos(l)}}const P=(i,s)=>{const c=Math.atan2(i[1],i[0])+s;return[Math.cos(c),Math.sin(c)]};function W(i,s,c){return c=Math.min(1,Math.max(0,(c-i)/(s-i))),c*c*(3-2*c)}const Ue=i=>{const s=parseInt(String(i).replace("#","").padEnd(6,"0").slice(0,6),16);return[(s>>16)/255,(s>>8&255)/255,(s&255)/255]},ke=(i,s,c)=>`<svg xmlns="http://www.w3.org/2000/svg" width="${s}" height="${c}">
|
|
2
2
|
<foreignObject width="${s}" height="${c}"><div xmlns="http://www.w3.org/1999/xhtml" style="width:${s}px;height:${c}px;background:${i.bg||"#ffffff"}">${i.html||""}</div></foreignObject></svg>`,ce=`
|
|
3
3
|
uniform vec2 uG, uU, uHalf;
|
|
4
4
|
uniform float uTf, uR, uTheta, uSinPhi, uCosPhi;
|
|
@@ -27,7 +27,7 @@ ${ce}
|
|
|
27
27
|
float sdRR(vec2 p, vec2 b, float r){ vec2 q = abs(p)-b+r; return length(max(q,0.0)) + min(max(q.x,q.y),0.0) - r; }
|
|
28
28
|
float hash(vec2 p){ return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453); }
|
|
29
29
|
float rectMask(vec2 p, float soft){ vec2 d = uHalf-abs(p); return smoothstep(-soft*0.5, soft, min(d.x,d.y)); }
|
|
30
|
-
//
|
|
30
|
+
// Shadow of the folded part: which point of the original card hangs above point q.
|
|
31
31
|
float flapShadow(vec2 q){
|
|
32
32
|
float tq = dot(q-uG, uU);
|
|
33
33
|
float tL = uTf - uR, tA = uTf - uR*uSinPhi;
|
|
@@ -77,4 +77,4 @@ void main(){
|
|
|
77
77
|
col = mix(col, tex, a);
|
|
78
78
|
col *= 1.0 - 0.55*flapShadow(q);
|
|
79
79
|
gl_FragColor = vec4(col,1.0);
|
|
80
|
-
}`,Ge={name:"PeelCard",props:{cards:{type:Array,required:!0},toggle:{type:Boolean,default:!1},width:{type:Number,default:420},height:{type:Number,default:280},radius:{type:Number,default:18},background:{type:String,default:""},padding:{type:Number,default:16},maxScale:{type:Number,default:1.35},hint:{type:Boolean,default:!0},label:{type:String,default:"Карточка: потяните за край, чтобы отклеить"}},emits:["grab","peel","change","toggle"],setup(i,{emit:s,expose:c}){const l=f.ref(null),v=f.ref(null),E=f.ref(!1),x=f.ref(!1),U=f.ref(!1),Ve={position:"relative",width:"100%",height:"100%",minHeight:"200px"},ze={position:"absolute",inset:"0",display:"grid",placeItems:"center",padding:"24px",textAlign:"center",opacity:".7"},Ye=f.computed(()=>({position:"absolute",inset:"0",width:"100%",height:"100%",display:"block",touchAction:"none",userSelect:"none",WebkitUserSelect:"none",cursor:E.value?"grabbing":"grab",outline:x.value?"2px solid currentColor":"none",outlineOffset:"-4px"})),$e=h=>{try{x.value=h.target.matches(":focus-visible")}catch{x.value=!1}};let fe=()=>{};const de={peel(){}};function We(){const h=v.value,d=i.width,p=i.height,Z=i.radius,M=i.toggle,t=h.getContext("webgl2",{antialias:!0,alpha:!1})||h.getContext("webgl",{antialias:!0,alpha:!1});if(!t){U.value=!0;return}const je=typeof WebGL2RenderingContext<"u"&&t instanceof WebGL2RenderingContext,A=()=>M?2:i.cards.length;let G=!0;const ue=[];let K=0;const me=(n,o)=>{const a=(T,m)=>{const g=t.createShader(T);return t.shaderSource(g,m),t.compileShader(g),t.getShaderParameter(g,t.COMPILE_STATUS)||console.error(t.getShaderInfoLog(g)),g},r=t.createProgram();t.attachShader(r,a(t.VERTEX_SHADER,n)),t.attachShader(r,a(t.FRAGMENT_SHADER,o)),t.linkProgram(r),t.getProgramParameter(r,t.LINK_STATUS)||console.error(t.getProgramInfoLog(r)),r.loc={};const u=t.getProgramParameter(r,t.ACTIVE_UNIFORMS);for(let T=0;T<u;T++){const m=t.getActiveUniform(r,T).name;r.loc[m]=t.getUniformLocation(r,m)}return r.aPos=t.getAttribLocation(r,"aPos"),r},w=me(Ce,Ne(Z)),F=me(De,Oe(Z)),he=Math.max(2.5,Math.sqrt((d+4)*(p+4)/6e4)),B=Math.round((d+4)/he),k=Math.round((p+4)/he),ge=d/2+2,pe=p/2+2,Q=new Float32Array((B+1)*(k+1)*2);for(let n=0,o=0;n<=k;n++)for(let a=0;a<=B;a++)Q[o++]=-ge+2*ge*a/B,Q[o++]=-pe+2*pe*n/k;const I=new Uint16Array(B*k*6);for(let n=0,o=0;n<k;n++)for(let a=0;a<B;a++){const r=n*(B+1)+a,u=r+1,T=r+B+1,m=T+1;I[o++]=r,I[o++]=u,I[o++]=T,I[o++]=u,I[o++]=m,I[o++]=T}const J=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,J),t.bufferData(t.ARRAY_BUFFER,Q,t.STATIC_DRAW);const ee=t.createBuffer();t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,ee),t.bufferData(t.ELEMENT_ARRAY_BUFFER,I,t.STATIC_DRAW);const te=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,te),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW);let C=2,R=0;const y=new Map;async function Ze(n){const o=Math.round(d*C),a=Math.round(p*C),r=document.createElement("canvas");r.width=o,r.height=a;const u=r.getContext("2d");let T=!1;try{const m=new Image,g=new Promise((_,S)=>{m.onload=_,m.onerror=S});m.src="data:image/svg+xml;charset=utf-8,"+encodeURIComponent(ke(n,d,p)),await g,u.drawImage(m,0,0,o,a),u.getImageData(0,0,1,1),T=!0}catch(m){console.warn("[PeelCard] foreignObject недоступен, рисую запасной вариант",m)}if(!T){u.setTransform(1,0,0,1,0,0),u.fillStyle=n.bg||"#ffffff",u.fillRect(0,0,o,a);const g=(new DOMParser().parseFromString(n.html||"","text/html").body.textContent||"").split(/\n+/).map(_=>_.trim()).filter(Boolean).slice(0,6);u.scale(C,C),u.fillStyle="#222",u.font="600 22px system-ui,sans-serif",g.forEach((_,S)=>u.fillText(_,28,50+S*34))}return r}function q(n){if(!A()||!i.cards.length)return Promise.resolve();if(n=(n%A()+A())%A(),y.has(n))return y.get(n).promise;const o=i.cards[n%i.cards.length],a={tex:null,back:Ue(o.back||"#f2efe6")};return a.promise=Ze(o).then(r=>{if(!G||y.get(n)!==a)return;const u=t.createTexture();t.bindTexture(t.TEXTURE_2D,u),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,!0),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,r),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.LINEAR),je?(t.generateMipmap(t.TEXTURE_2D),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.LINEAR_MIPMAP_LINEAR)):t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.LINEAR),a.tex=u}),y.set(n,a),a.promise}function ne(n){const o=n?new Set:new Set([R,(R+1)%A(),(R+2)%A()]);for(const[a,r]of y)o.has(a)||(r.tex&&t.deleteTexture(r.tex),y.delete(a))}const Te=()=>Promise.all([q(0),q(1),q(2)]);let N=1,D=1,oe=1,ae=1,X=1,H=1;function ve(){const n=l.value.getBoundingClientRect();oe=Math.max(1,n.width),ae=Math.max(1,n.height),N=Math.min(window.devicePixelRatio||1,2),X=Math.round(oe*N),H=Math.round(ae*N),h.width=X,h.height=H;const o=M?1.5:1;D=Math.max(.3,Math.min(i.maxScale,(oe-2*i.padding)/(d*o),(ae-2*i.padding)/(p*o)))}let L=[.9,.89,.85];function V(){let n=i.background;if(!n)for(let a=l.value;a;a=a.parentElement){const r=getComputedStyle(a).backgroundColor;if(r&&!/rgba?\(.*,\s*0\)$/.test(r)&&r!=="transparent"){n=r;break}}const o=(n||"").match(/[\d.]+/g);o&&o.length>=3&&(L=[o[0]/255,o[1]/255,o[2]/255])}const e={mode:"idle",d:0,ang:0,g:[-d/2,0],nIn:[1,0],p0:[0,0],dT:0,angT:0,angBase:0,alpha:1,t:0,dEnd:0,velU:0,lastDT:0,lastTime:0,interacted:!1,peeled:!1,restick:!1,shiftF:0,shiftVec:[0,0]},Ke=matchMedia("(prefers-reduced-motion: reduce)").matches,Qe=[[-d/2,-p/2],[d/2,-p/2],[-d/2,p/2],[d/2,p/2]],Ee=(n,o)=>Qe.map(a=>(a[0]-n[0])*o[0]+(a[1]-n[1])*o[1]),re=(n,o)=>Math.max(...Ee(n,o)),O=(n,o)=>$(re(n,o)+4,18,Math.PI-1.15,1.15),Je=(n,o)=>{const a=Ee(n,o);return Math.cos(1.15)*(Math.max(...a)-Math.min(...a))+18+8},Re=n=>{const o=h.getBoundingClientRect();return[(n.clientX-o.left-o.width/2)/D,-(n.clientY-o.top-o.height/2)/D]};function et(n){const o=n[0]+d/2,a=d/2-n[0],r=n[1]+p/2,u=p/2-n[1],T=Math.min(o,a,r,u),m=Z*.8,g=(_,S)=>Math.max(-S+m,Math.min(S-m,_));T===o?(e.nIn=[1,0],e.g=[-d/2,g(n[1],p/2)]):T===a?(e.nIn=[-1,0],e.g=[d/2,g(n[1],p/2)]):T===r?(e.nIn=[0,1],e.g=[g(n[0],d/2),-p/2]):(e.nIn=[0,-1],e.g=[g(n[0],d/2),p/2]),e.p0=n,e.dT=0,e.angT=0,e.velU=0,e.lastDT=0,e.lastTime=performance.now()}function xe(n){const o=performance.now(),a=Math.max(1,o-e.lastTime);e.velU+=((n-e.lastDT)/a*1e3-e.velU)*.35,e.lastDT=n,e.lastTime=o}function tt(n){const o=[n[0]-e.p0[0],n[1]-e.p0[1]],a=Math.hypot(o[0],o[1]);if(e.restick){const g=P(e.nIn,e.angBase).map(st=>-st),_=Math.max(-.6,Math.min(.6,Math.atan2(g[0]*o[1]-g[1]*o[0],o[0]*g[0]+o[1]*g[1])));e.angT=e.angBase+_*W(6,70,a);const S=P(e.nIn,e.angT);e.dT=Math.max(0,-(o[0]*S[0]+o[1]*S[1])),xe(e.dT);return}const r=o[0]*e.nIn[0]+o[1]*e.nIn[1],u=e.nIn[0]*o[1]-e.nIn[1]*o[0],T=Math.max(-1,Math.min(1,Math.atan2(u,r)));e.angT=T*W(6,70,a);const m=P(e.nIn,e.angT);e.dT=Math.max(0,o[0]*m[0]+o[1]*m[1]),xe(e.dT)}function ie(){const n=P(e.nIn,e.ang);if(M){e.dEnd=O(e.g,n);const o=Je(e.g,n)/2;e.shiftVec=[n[0]*o,n[1]*o]}else e.dEnd=$(re(e.g,n)+2.2*18+20,18,Math.PI-.12,.12);e.mode="complete"}function nt(){if(e.mode!=="drag")return;E.value=!1;const n=P(e.nIn,e.ang);if(e.restick){const a=e.d/O(e.g,n);a<.6||e.velU>700&&a<.9?e.mode="settle":ie();return}const o=e.d/re(e.g,n);o>.32||e.velU>700&&o>.1?ie():e.mode="settle"}function Me(){if(M&&e.peeled&&(e.mode==="rest"||e.mode==="drag")){e.restick=!1,e.mode="settle";return}e.mode!=="idle"&&e.mode!=="hint"&&e.mode!=="settle"||M&&e.peeled||(e.interacted=!0,e.nIn=[1,0],e.g=[-d/2,0],e.ang=0,ie())}de.peel=Me;function be(){!i.hint||e.mode!=="idle"||e.interacted||Ke||(e.nIn=[1,0],e.g=[-d/2,0],e.ang=0,e.mode="hint",e.t=0)}const _e=n=>{if(e.mode==="complete"||e.mode==="fade")return;const o=Re(n);if(M&&e.peeled){h.setPointerCapture(n.pointerId),E.value=!0,e.restick=!0,e.angBase=e.ang,e.p0=o,e.dT=0,e.velU=0,e.lastDT=0,e.lastTime=performance.now(),e.mode="drag";return}Math.abs(o[0])>d/2+30||Math.abs(o[1])>p/2+30||(h.setPointerCapture(n.pointerId),E.value=!0,e.interacted||s("grab"),e.interacted=!0,e.restick=!1,et(o),e.mode="drag")},Se=n=>{e.mode==="drag"&&tt(Re(n))},z=()=>nt(),Pe=n=>{(n.key==="ArrowRight"||n.key==="Enter"||n.key===" ")&&(n.preventDefault(),Me())};h.addEventListener("pointerdown",_e),h.addEventListener("pointermove",Se),h.addEventListener("pointerup",z),h.addEventListener("pointercancel",z),h.addEventListener("keydown",Pe);function ot(){const n=R;R=(R+1)%A(),e.d=0,e.ang=0,e.alpha=1,e.mode="idle",q(R+2),ne(!1),s("peel",n),s("change",R)}function at(n){switch(e.mode){case"drag":if(e.restick){const a=O(e.g,P(e.nIn,e.ang));e.d+=(Math.max(0,a-e.dT)-e.d)*(1-Math.exp(-n*30))}else e.d+=(Math.min(e.dT,1500)-e.d)*(1-Math.exp(-n*30));e.ang+=(e.angT-e.ang)*(1-Math.exp(-n*16));break;case"settle":e.d+=(0-e.d)*(1-Math.exp(-n*9)),e.d<.05&&(e.d=0,e.mode="idle",M&&e.peeled&&(e.peeled=!1,s("toggle",!1)));break;case"hint":{e.t+=n;const a=e.t/1.5;a>=1?(e.d=0,e.mode="idle"):(e.d=70*Math.pow(Math.sin(Math.PI*a),2),e.ang=.22*Math.sin(2*Math.PI*a));break}case"complete":e.d+=Math.max((e.dEnd-e.d)*(1-Math.exp(-n*6)),380*n),e.d>=e.dEnd&&(e.d=e.dEnd,M?(e.mode="rest",e.peeled||(e.peeled=!0,s("toggle",!0))):(e.mode="fade",e.t=0));break;case"rest":e.d=O(e.g,P(e.nIn,e.ang));break;case"fade":e.t+=n,e.alpha=Math.max(0,1-e.t/.28),e.t>=.28&&ot();break}const o=M&&(e.peeled&&(e.mode==="rest"||e.mode==="drag"&&e.restick)||e.mode==="complete")?1:0;e.shiftF+=(o-e.shiftF)*(1-Math.exp(-n*6))}function Ae(n,o,a){const r=n.loc;t.uniform2f(r.uG,e.g[0],e.g[1]),t.uniform2f(r.uU,a[0],a[1]),t.uniform2f(r.uHalf,d/2,p/2),t.uniform1f(r.uTf,o.tf),t.uniform1f(r.uR,o.R),t.uniform1f(r.uTheta,o.Th),t.uniform1f(r.uSinPhi,o.sp),t.uniform1f(r.uCosPhi,o.cp),t.uniform2f(r.uView,X,H),t.uniform1f(r.uScale,D*N),t.uniform2f(r.uShift,e.shiftVec[0]*e.shiftF,e.shiftVec[1]*e.shiftF)}function rt(){const n=y.get(R),o=y.get((R+1)%A());if(t.viewport(0,0,X,H),t.clearColor(L[0],L[1],L[2],1),t.clear(t.COLOR_BUFFER_BIT|t.DEPTH_BUFFER_BIT),!n||!n.tex||!o||!o.tex)return;const a=P(e.nIn,e.ang),r=Le(e.d,M?O(e.g,a):0);t.disable(t.DEPTH_TEST),t.disable(t.BLEND),t.useProgram(F),Ae(F,r,a),t.uniform3f(F.loc.uBg,L[0],L[1],L[2]),t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,o.tex),t.uniform1i(F.loc.uBase,0),t.bindBuffer(t.ARRAY_BUFFER,te),t.enableVertexAttribArray(F.aPos),t.vertexAttribPointer(F.aPos,2,t.FLOAT,!1,0,0),t.drawArrays(t.TRIANGLE_STRIP,0,4),t.enable(t.DEPTH_TEST),t.depthFunc(t.LEQUAL),t.enable(t.BLEND),t.blendFunc(t.ONE,t.ONE_MINUS_SRC_ALPHA),t.useProgram(w),Ae(w,r,a),t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,n.tex),t.uniform1i(w.loc.uFront,0),t.uniform3f(w.loc.uBack,n.back[0],n.back[1],n.back[2]),t.uniform1f(w.loc.uAlpha,e.alpha),t.bindBuffer(t.ARRAY_BUFFER,J),t.enableVertexAttribArray(w.aPos),t.vertexAttribPointer(w.aPos,2,t.FLOAT,!1,0,0),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,ee),t.drawElements(t.TRIANGLES,I.length,t.UNSIGNED_SHORT,0)}let se=performance.now();function we(n){if(!G)return;const o=Math.min(.05,(n-se)/1e3);se=n,at(o),rt(),K=requestAnimationFrame(we)}const Ie=new ResizeObserver(ve);Ie.observe(l.value);const Y=matchMedia("(prefers-color-scheme: dark)");Y.addEventListener&&Y.addEventListener("change",V);const ye=new MutationObserver(V);ye.observe(document.documentElement,{attributes:!0}),ve(),V(),C=Math.min(3,Math.max(2,D*N));const it=f.watch(()=>i.cards,()=>{ne(!0),R=0,e.d=0,e.mode="idle",e.alpha=1,e.peeled=!1,e.shiftF=0,Te()});Te().then(()=>{G&&(K=requestAnimationFrame(n=>{se=n,we(n)}),ue.push(setTimeout(be,900),setTimeout(be,8e3)))}),fe=()=>{G=!1,cancelAnimationFrame(K),ue.forEach(clearTimeout),it(),Ie.disconnect(),ye.disconnect(),Y.removeEventListener&&Y.removeEventListener("change",V),h.removeEventListener("pointerdown",_e),h.removeEventListener("pointermove",Se),h.removeEventListener("pointerup",z),h.removeEventListener("pointercancel",z),h.removeEventListener("keydown",Pe),ne(!0),t.deleteBuffer(J),t.deleteBuffer(ee),t.deleteBuffer(te),t.deleteProgram(w),t.deleteProgram(F);const n=t.getExtension("WEBGL_lose_context");n&&n.loseContext()},typeof window<"u"&&window.__PEEL_DEBUG__&&(window.__peelDebug=e)}return f.onMounted(We),f.onBeforeUnmount(()=>fe()),c({peel:()=>de.peel()}),{root:l,canvas:v,noGL:U,focused:x,rootStyle:Ve,noGLStyle:ze,canvasStyle:Ye,onFocus:$e}}},qe=["aria-label"];function Xe(i,s,c,l,v,E){return f.openBlock(),f.createElementBlock("div",{ref:"root",style:f.normalizeStyle(l.rootStyle)},[f.createElementVNode("canvas",{ref:"canvas",tabindex:"0",role:"img","aria-label":c.label,style:f.normalizeStyle(l.canvasStyle),onFocus:s[0]||(s[0]=(...x)=>l.onFocus&&l.onFocus(...x)),onBlur:s[1]||(s[1]=x=>l.focused=!1)},null,44,qe),l.noGL?(f.openBlock(),f.createElementBlock("div",{key:0,style:f.normalizeStyle(l.noGLStyle)},[f.renderSlot(i.$slots,"fallback",{},()=>[s[2]||(s[2]=f.createTextVNode("WebGL недоступен в этом браузере.",-1))])],4)):f.createCommentVNode("",!0)],4)}const j=Fe(Ge,[["render",Xe]]),He={install(i){i.component("PeelCard",j)}};b.PeelCard=j,b.PeelCardPlugin=He,b.default=j,Object.defineProperties(b,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
80
|
+
}`,Ge={name:"PeelCard",props:{cards:{type:Array,required:!0},toggle:{type:Boolean,default:!1},width:{type:Number,default:420},height:{type:Number,default:280},radius:{type:Number,default:18},background:{type:String,default:""},padding:{type:Number,default:16},maxScale:{type:Number,default:1.35},hint:{type:Boolean,default:!0},label:{type:String,default:"Card: drag the edge to peel it off"}},emits:["grab","peel","change","toggle"],setup(i,{emit:s,expose:c}){const l=f.ref(null),v=f.ref(null),E=f.ref(!1),x=f.ref(!1),U=f.ref(!1),Ve={position:"relative",width:"100%",height:"100%",minHeight:"200px"},ze={position:"absolute",inset:"0",display:"grid",placeItems:"center",padding:"24px",textAlign:"center",opacity:".7"},Ye=f.computed(()=>({position:"absolute",inset:"0",width:"100%",height:"100%",display:"block",touchAction:"none",userSelect:"none",WebkitUserSelect:"none",cursor:E.value?"grabbing":"grab",outline:x.value?"2px solid currentColor":"none",outlineOffset:"-4px"})),$e=m=>{try{x.value=m.target.matches(":focus-visible")}catch{x.value=!1}};let fe=()=>{};const de={peel(){}};function We(){const m=v.value,d=i.width,p=i.height,Z=i.radius,b=i.toggle,t=m.getContext("webgl2",{antialias:!0,alpha:!1})||m.getContext("webgl",{antialias:!0,alpha:!1});if(!t){U.value=!0;return}const je=typeof WebGL2RenderingContext<"u"&&t instanceof WebGL2RenderingContext,A=()=>b?2:i.cards.length;let G=!0;const ue=[];let K=0;const he=(n,a)=>{const o=(T,h)=>{const g=t.createShader(T);return t.shaderSource(g,h),t.compileShader(g),t.getShaderParameter(g,t.COMPILE_STATUS)||console.error(t.getShaderInfoLog(g)),g},r=t.createProgram();t.attachShader(r,o(t.VERTEX_SHADER,n)),t.attachShader(r,o(t.FRAGMENT_SHADER,a)),t.linkProgram(r),t.getProgramParameter(r,t.LINK_STATUS)||console.error(t.getProgramInfoLog(r)),r.loc={};const u=t.getProgramParameter(r,t.ACTIVE_UNIFORMS);for(let T=0;T<u;T++){const h=t.getActiveUniform(r,T).name;r.loc[h]=t.getUniformLocation(r,h)}return r.aPos=t.getAttribLocation(r,"aPos"),r},w=he(Ce,Ne(Z)),F=he(De,Oe(Z)),me=Math.max(2.5,Math.sqrt((d+4)*(p+4)/6e4)),B=Math.round((d+4)/me),k=Math.round((p+4)/me),ge=d/2+2,pe=p/2+2,Q=new Float32Array((B+1)*(k+1)*2);for(let n=0,a=0;n<=k;n++)for(let o=0;o<=B;o++)Q[a++]=-ge+2*ge*o/B,Q[a++]=-pe+2*pe*n/k;const I=new Uint16Array(B*k*6);for(let n=0,a=0;n<k;n++)for(let o=0;o<B;o++){const r=n*(B+1)+o,u=r+1,T=r+B+1,h=T+1;I[a++]=r,I[a++]=u,I[a++]=T,I[a++]=u,I[a++]=h,I[a++]=T}const J=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,J),t.bufferData(t.ARRAY_BUFFER,Q,t.STATIC_DRAW);const ee=t.createBuffer();t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,ee),t.bufferData(t.ELEMENT_ARRAY_BUFFER,I,t.STATIC_DRAW);const te=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,te),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW);let C=2,R=0;const y=new Map;async function Ze(n){const a=Math.round(d*C),o=Math.round(p*C),r=document.createElement("canvas");r.width=a,r.height=o;const u=r.getContext("2d");let T=!1;try{const h=new Image,g=new Promise((S,_)=>{h.onload=S,h.onerror=_});h.src="data:image/svg+xml;charset=utf-8,"+encodeURIComponent(ke(n,d,p)),await g,u.drawImage(h,0,0,a,o),u.getImageData(0,0,1,1),T=!0}catch(h){console.warn("[PeelCard] foreignObject unavailable, drawing fallback",h)}if(!T){u.setTransform(1,0,0,1,0,0),u.fillStyle=n.bg||"#ffffff",u.fillRect(0,0,a,o);const g=(new DOMParser().parseFromString(n.html||"","text/html").body.textContent||"").split(/\n+/).map(S=>S.trim()).filter(Boolean).slice(0,6);u.scale(C,C),u.fillStyle="#222",u.font="600 22px system-ui,sans-serif",g.forEach((S,_)=>u.fillText(S,28,50+_*34))}return r}function q(n){if(!A()||!i.cards.length)return Promise.resolve();if(n=(n%A()+A())%A(),y.has(n))return y.get(n).promise;const a=i.cards[n%i.cards.length],o={tex:null,back:Ue(a.back||"#f2efe6")};return o.promise=Ze(a).then(r=>{if(!G||y.get(n)!==o)return;const u=t.createTexture();t.bindTexture(t.TEXTURE_2D,u),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,!0),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,r),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.LINEAR),je?(t.generateMipmap(t.TEXTURE_2D),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.LINEAR_MIPMAP_LINEAR)):t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.LINEAR),o.tex=u}),y.set(n,o),o.promise}function ne(n){const a=n?new Set:new Set([R,(R+1)%A(),(R+2)%A()]);for(const[o,r]of y)a.has(o)||(r.tex&&t.deleteTexture(r.tex),y.delete(o))}const Te=()=>Promise.all([q(0),q(1),q(2)]);let N=1,D=1,ae=1,oe=1,X=1,H=1;function ve(){const n=l.value.getBoundingClientRect();ae=Math.max(1,n.width),oe=Math.max(1,n.height),N=Math.min(window.devicePixelRatio||1,2),X=Math.round(ae*N),H=Math.round(oe*N),m.width=X,m.height=H;const a=b?1.5:1;D=Math.max(.3,Math.min(i.maxScale,(ae-2*i.padding)/(d*a),(oe-2*i.padding)/(p*a)))}let L=[.9,.89,.85];function V(){let n=i.background;if(!n)for(let o=l.value;o;o=o.parentElement){const r=getComputedStyle(o).backgroundColor;if(r&&!/rgba?\(.*,\s*0\)$/.test(r)&&r!=="transparent"){n=r;break}}const a=(n||"").match(/[\d.]+/g);a&&a.length>=3&&(L=[a[0]/255,a[1]/255,a[2]/255])}const e={mode:"idle",d:0,ang:0,g:[-d/2,0],nIn:[1,0],p0:[0,0],dT:0,angT:0,angBase:0,alpha:1,t:0,dEnd:0,velU:0,lastDT:0,lastTime:0,interacted:!1,peeled:!1,restick:!1,shiftF:0,shiftVec:[0,0]},Ke=matchMedia("(prefers-reduced-motion: reduce)").matches,Qe=[[-d/2,-p/2],[d/2,-p/2],[-d/2,p/2],[d/2,p/2]],Ee=(n,a)=>Qe.map(o=>(o[0]-n[0])*a[0]+(o[1]-n[1])*a[1]),re=(n,a)=>Math.max(...Ee(n,a)),O=(n,a)=>$(re(n,a)+4,18,Math.PI-1.15,1.15),Je=(n,a)=>{const o=Ee(n,a);return Math.cos(1.15)*(Math.max(...o)-Math.min(...o))+18+8},Re=n=>{const a=m.getBoundingClientRect();return[(n.clientX-a.left-a.width/2)/D,-(n.clientY-a.top-a.height/2)/D]};function et(n){const a=n[0]+d/2,o=d/2-n[0],r=n[1]+p/2,u=p/2-n[1],T=Math.min(a,o,r,u),h=Z*.8,g=(S,_)=>Math.max(-_+h,Math.min(_-h,S));T===a?(e.nIn=[1,0],e.g=[-d/2,g(n[1],p/2)]):T===o?(e.nIn=[-1,0],e.g=[d/2,g(n[1],p/2)]):T===r?(e.nIn=[0,1],e.g=[g(n[0],d/2),-p/2]):(e.nIn=[0,-1],e.g=[g(n[0],d/2),p/2]),e.p0=n,e.dT=0,e.angT=0,e.velU=0,e.lastDT=0,e.lastTime=performance.now()}function xe(n){const a=performance.now(),o=Math.max(1,a-e.lastTime);e.velU+=((n-e.lastDT)/o*1e3-e.velU)*.35,e.lastDT=n,e.lastTime=a}function tt(n){const a=[n[0]-e.p0[0],n[1]-e.p0[1]],o=Math.hypot(a[0],a[1]);if(e.restick){const g=P(e.nIn,e.angBase).map(st=>-st),S=Math.max(-.6,Math.min(.6,Math.atan2(g[0]*a[1]-g[1]*a[0],a[0]*g[0]+a[1]*g[1])));e.angT=e.angBase+S*W(6,70,o);const _=P(e.nIn,e.angT);e.dT=Math.max(0,-(a[0]*_[0]+a[1]*_[1])),xe(e.dT);return}const r=a[0]*e.nIn[0]+a[1]*e.nIn[1],u=e.nIn[0]*a[1]-e.nIn[1]*a[0],T=Math.max(-1,Math.min(1,Math.atan2(u,r)));e.angT=T*W(6,70,o);const h=P(e.nIn,e.angT);e.dT=Math.max(0,a[0]*h[0]+a[1]*h[1]),xe(e.dT)}function ie(){const n=P(e.nIn,e.ang);if(b){e.dEnd=O(e.g,n);const a=Je(e.g,n)/2;e.shiftVec=[n[0]*a,n[1]*a]}else e.dEnd=$(re(e.g,n)+2.2*18+20,18,Math.PI-.12,.12);e.mode="complete"}function nt(){if(e.mode!=="drag")return;E.value=!1;const n=P(e.nIn,e.ang);if(e.restick){const o=e.d/O(e.g,n);o<.6||e.velU>700&&o<.9?e.mode="settle":ie();return}const a=e.d/re(e.g,n);a>.32||e.velU>700&&a>.1?ie():e.mode="settle"}function be(){if(b&&e.peeled&&(e.mode==="rest"||e.mode==="drag")){e.restick=!1,e.mode="settle";return}e.mode!=="idle"&&e.mode!=="hint"&&e.mode!=="settle"||b&&e.peeled||(e.interacted=!0,e.nIn=[1,0],e.g=[-d/2,0],e.ang=0,ie())}de.peel=be;function Me(){!i.hint||e.mode!=="idle"||e.interacted||Ke||(e.nIn=[1,0],e.g=[-d/2,0],e.ang=0,e.mode="hint",e.t=0)}const Se=n=>{if(e.mode==="complete"||e.mode==="fade")return;const a=Re(n);if(b&&e.peeled){m.setPointerCapture(n.pointerId),E.value=!0,e.restick=!0,e.angBase=e.ang,e.p0=a,e.dT=0,e.velU=0,e.lastDT=0,e.lastTime=performance.now(),e.mode="drag";return}Math.abs(a[0])>d/2+30||Math.abs(a[1])>p/2+30||(m.setPointerCapture(n.pointerId),E.value=!0,e.interacted||s("grab"),e.interacted=!0,e.restick=!1,et(a),e.mode="drag")},_e=n=>{e.mode==="drag"&&tt(Re(n))},z=()=>nt(),Pe=n=>{(n.key==="ArrowRight"||n.key==="Enter"||n.key===" ")&&(n.preventDefault(),be())};m.addEventListener("pointerdown",Se),m.addEventListener("pointermove",_e),m.addEventListener("pointerup",z),m.addEventListener("pointercancel",z),m.addEventListener("keydown",Pe);function at(){const n=R;R=(R+1)%A(),e.d=0,e.ang=0,e.alpha=1,e.mode="idle",q(R+2),ne(!1),s("peel",n),s("change",R)}function ot(n){switch(e.mode){case"drag":if(e.restick){const o=O(e.g,P(e.nIn,e.ang));e.d+=(Math.max(0,o-e.dT)-e.d)*(1-Math.exp(-n*30))}else e.d+=(Math.min(e.dT,1500)-e.d)*(1-Math.exp(-n*30));e.ang+=(e.angT-e.ang)*(1-Math.exp(-n*16));break;case"settle":e.d+=(0-e.d)*(1-Math.exp(-n*9)),e.d<.05&&(e.d=0,e.mode="idle",b&&e.peeled&&(e.peeled=!1,s("toggle",!1)));break;case"hint":{e.t+=n;const o=e.t/1.5;o>=1?(e.d=0,e.mode="idle"):(e.d=70*Math.pow(Math.sin(Math.PI*o),2),e.ang=.22*Math.sin(2*Math.PI*o));break}case"complete":e.d+=Math.max((e.dEnd-e.d)*(1-Math.exp(-n*6)),380*n),e.d>=e.dEnd&&(e.d=e.dEnd,b?(e.mode="rest",e.peeled||(e.peeled=!0,s("toggle",!0))):(e.mode="fade",e.t=0));break;case"rest":e.d=O(e.g,P(e.nIn,e.ang));break;case"fade":e.t+=n,e.alpha=Math.max(0,1-e.t/.28),e.t>=.28&&at();break}const a=b&&(e.peeled&&(e.mode==="rest"||e.mode==="drag"&&e.restick)||e.mode==="complete")?1:0;e.shiftF+=(a-e.shiftF)*(1-Math.exp(-n*6))}function Ae(n,a,o){const r=n.loc;t.uniform2f(r.uG,e.g[0],e.g[1]),t.uniform2f(r.uU,o[0],o[1]),t.uniform2f(r.uHalf,d/2,p/2),t.uniform1f(r.uTf,a.tf),t.uniform1f(r.uR,a.R),t.uniform1f(r.uTheta,a.Th),t.uniform1f(r.uSinPhi,a.sp),t.uniform1f(r.uCosPhi,a.cp),t.uniform2f(r.uView,X,H),t.uniform1f(r.uScale,D*N),t.uniform2f(r.uShift,e.shiftVec[0]*e.shiftF,e.shiftVec[1]*e.shiftF)}function rt(){const n=y.get(R),a=y.get((R+1)%A());if(t.viewport(0,0,X,H),t.clearColor(L[0],L[1],L[2],1),t.clear(t.COLOR_BUFFER_BIT|t.DEPTH_BUFFER_BIT),!n||!n.tex||!a||!a.tex)return;const o=P(e.nIn,e.ang),r=Le(e.d,b?O(e.g,o):0);t.disable(t.DEPTH_TEST),t.disable(t.BLEND),t.useProgram(F),Ae(F,r,o),t.uniform3f(F.loc.uBg,L[0],L[1],L[2]),t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,a.tex),t.uniform1i(F.loc.uBase,0),t.bindBuffer(t.ARRAY_BUFFER,te),t.enableVertexAttribArray(F.aPos),t.vertexAttribPointer(F.aPos,2,t.FLOAT,!1,0,0),t.drawArrays(t.TRIANGLE_STRIP,0,4),t.enable(t.DEPTH_TEST),t.depthFunc(t.LEQUAL),t.enable(t.BLEND),t.blendFunc(t.ONE,t.ONE_MINUS_SRC_ALPHA),t.useProgram(w),Ae(w,r,o),t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,n.tex),t.uniform1i(w.loc.uFront,0),t.uniform3f(w.loc.uBack,n.back[0],n.back[1],n.back[2]),t.uniform1f(w.loc.uAlpha,e.alpha),t.bindBuffer(t.ARRAY_BUFFER,J),t.enableVertexAttribArray(w.aPos),t.vertexAttribPointer(w.aPos,2,t.FLOAT,!1,0,0),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,ee),t.drawElements(t.TRIANGLES,I.length,t.UNSIGNED_SHORT,0)}let se=performance.now();function we(n){if(!G)return;const a=Math.min(.05,(n-se)/1e3);se=n,ot(a),rt(),K=requestAnimationFrame(we)}const Ie=new ResizeObserver(ve);Ie.observe(l.value);const Y=matchMedia("(prefers-color-scheme: dark)");Y.addEventListener&&Y.addEventListener("change",V);const ye=new MutationObserver(V);ye.observe(document.documentElement,{attributes:!0}),ve(),V(),C=Math.min(3,Math.max(2,D*N));const it=f.watch(()=>i.cards,()=>{ne(!0),R=0,e.d=0,e.mode="idle",e.alpha=1,e.peeled=!1,e.shiftF=0,Te()});Te().then(()=>{G&&(K=requestAnimationFrame(n=>{se=n,we(n)}),ue.push(setTimeout(Me,900),setTimeout(Me,8e3)))}),fe=()=>{G=!1,cancelAnimationFrame(K),ue.forEach(clearTimeout),it(),Ie.disconnect(),ye.disconnect(),Y.removeEventListener&&Y.removeEventListener("change",V),m.removeEventListener("pointerdown",Se),m.removeEventListener("pointermove",_e),m.removeEventListener("pointerup",z),m.removeEventListener("pointercancel",z),m.removeEventListener("keydown",Pe),ne(!0),t.deleteBuffer(J),t.deleteBuffer(ee),t.deleteBuffer(te),t.deleteProgram(w),t.deleteProgram(F);const n=t.getExtension("WEBGL_lose_context");n&&n.loseContext()},typeof window<"u"&&window.__PEEL_DEBUG__&&(window.__peelDebug=e)}return f.onMounted(We),f.onBeforeUnmount(()=>fe()),c({peel:()=>de.peel()}),{root:l,canvas:v,noGL:U,focused:x,rootStyle:Ve,noGLStyle:ze,canvasStyle:Ye,onFocus:$e}}},qe=["aria-label"];function Xe(i,s,c,l,v,E){return f.openBlock(),f.createElementBlock("div",{ref:"root",style:f.normalizeStyle(l.rootStyle)},[f.createElementVNode("canvas",{ref:"canvas",tabindex:"0",role:"img","aria-label":c.label,style:f.normalizeStyle(l.canvasStyle),onFocus:s[0]||(s[0]=(...x)=>l.onFocus&&l.onFocus(...x)),onBlur:s[1]||(s[1]=x=>l.focused=!1)},null,44,qe),l.noGL?(f.openBlock(),f.createElementBlock("div",{key:0,style:f.normalizeStyle(l.noGLStyle)},[f.renderSlot(i.$slots,"fallback",{},()=>[s[2]||(s[2]=f.createTextVNode("WebGL is not available in this browser.",-1))])],4)):f.createCommentVNode("",!0)],4)}const j=Fe(Ge,[["render",Xe]]),He={install(i){i.component("PeelCard",j)}};M.PeelCard=j,M.PeelCardPlugin=He,M.default=j,Object.defineProperties(M,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-peel-card",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Vue 3 component: a WebGL card you can peel off by its edge. HTML card content, realistic curl that follows your finger, toggle mode.",
|
|
5
5
|
"keywords": ["vue", "vue3", "webgl", "card", "peel", "page-curl", "flashcards", "animation"],
|
|
6
6
|
"license": "MIT",
|
package/src/PeelCard.vue
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
<!--
|
|
2
|
-
PeelCard —
|
|
2
|
+
PeelCard — a card you peel off by its edge (WebGL, Vue 3).
|
|
3
3
|
|
|
4
4
|
<PeelCard :cards="cards" @change="i => current = i" />
|
|
5
5
|
<PeelCard toggle :cards="[front, back]" @toggle="peeled => ..." ref="card" />
|
|
6
6
|
|
|
7
7
|
Props
|
|
8
8
|
cards Array [{ bg, back, html }]
|
|
9
|
-
bg —
|
|
10
|
-
back —
|
|
11
|
-
html —
|
|
12
|
-
|
|
13
|
-
toggle Boolean
|
|
14
|
-
(
|
|
15
|
-
width Number
|
|
16
|
-
height Number
|
|
17
|
-
radius Number
|
|
18
|
-
background String
|
|
19
|
-
padding Number
|
|
20
|
-
maxScale Number
|
|
21
|
-
hint Boolean
|
|
22
|
-
label String aria-label
|
|
9
|
+
bg — front side color (CSS color, backdrop under the html)
|
|
10
|
+
back — back side paper color (#rrggbb)
|
|
11
|
+
html — card content, well-formed XHTML (closed tags, <br/>,   instead of ).
|
|
12
|
+
Styles only inside html (<style> or style=""); external files and fonts are unavailable.
|
|
13
|
+
toggle Boolean "two cards" mode: the first action peels the top card, the second sticks it back
|
|
14
|
+
(read once on mount; to change the mode, recreate the component via :key)
|
|
15
|
+
width Number card width in logical units (420)
|
|
16
|
+
height Number card height (280)
|
|
17
|
+
radius Number corner radius (18)
|
|
18
|
+
background String "table" color; by default taken from the CSS of the nearest ancestor with a background
|
|
19
|
+
padding Number gap between the card and the component edges (16)
|
|
20
|
+
maxScale Number maximum zoom (1.35)
|
|
21
|
+
hint Boolean show a wiggle hint on start (true)
|
|
22
|
+
label String aria-label of the canvas
|
|
23
23
|
Events
|
|
24
|
-
grab
|
|
25
|
-
peel(i) (
|
|
26
|
-
change(i) (
|
|
27
|
-
toggle(b) (
|
|
28
|
-
|
|
29
|
-
peel()
|
|
24
|
+
grab first touch of the card
|
|
25
|
+
peel(i) (normal mode) card i is fully peeled off
|
|
26
|
+
change(i) (normal mode) card i became the top card
|
|
27
|
+
toggle(b) (toggle mode) b = true — peeled, false — stuck back
|
|
28
|
+
Methods (via ref)
|
|
29
|
+
peel() peel off; in toggle mode switches the state
|
|
30
30
|
-->
|
|
31
31
|
<template>
|
|
32
32
|
<div ref="root" :style="rootStyle">
|
|
@@ -39,33 +39,33 @@
|
|
|
39
39
|
@focus="onFocus"
|
|
40
40
|
@blur="focused = false"
|
|
41
41
|
></canvas>
|
|
42
|
-
<div v-if="noGL" :style="noGLStyle"><slot name="fallback">WebGL
|
|
42
|
+
<div v-if="noGL" :style="noGLStyle"><slot name="fallback">WebGL is not available in this browser.</slot></div>
|
|
43
43
|
</div>
|
|
44
44
|
</template>
|
|
45
45
|
|
|
46
46
|
<script>
|
|
47
47
|
import { ref, computed, onMounted, onBeforeUnmount, watch } from 'vue'
|
|
48
48
|
|
|
49
|
-
/* ─────────
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
/* ───────── Curl physics ─────────
|
|
50
|
+
Everything behind the fold line (t < Tf, where t is the coordinate along the pull direction U from the grab point G)
|
|
51
|
+
wraps around a cylinder of radius R through angle Theta, then continues as a straight segment at angle Phi to the plane. */
|
|
52
52
|
const RMIN = 18, RBIG = 75, MAXANG = 1.0
|
|
53
|
-
const PHI_REST = 1.15 //
|
|
53
|
+
const PHI_REST = 1.15 // toggle mode: the angle at which the peeled card stands
|
|
54
54
|
|
|
55
|
-
//
|
|
55
|
+
// How far the grab point moves when the fold is at distance s from it.
|
|
56
56
|
function Gfun(s, R, Th, phi) {
|
|
57
57
|
return s <= Th * R ? s - R * Math.sin(s / R) : s - R * Math.sin(phi) + (s - Th * R) * Math.cos(phi)
|
|
58
58
|
}
|
|
59
|
-
//
|
|
59
|
+
// Inverse problem: find the fold position from the finger displacement d (bisection).
|
|
60
60
|
function solveS(d, R, Th, phi) {
|
|
61
61
|
let lo = 0, hi = 4000
|
|
62
62
|
for (let i = 0; i < 48; i++) { const m = (lo + hi) / 2; if (Gfun(m, R, Th, phi) < d) lo = m; else hi = m }
|
|
63
63
|
return (lo + hi) / 2
|
|
64
64
|
}
|
|
65
|
-
// dRest > 0 —
|
|
65
|
+
// dRest > 0 — toggle mode: near the edge the lift angle grows smoothly up to PHI_REST.
|
|
66
66
|
function geom(d, dRest) {
|
|
67
|
-
const R = RMIN + (RBIG - RMIN) * Math.exp(-d / 70) //
|
|
68
|
-
let phi = 0.12 + 0.55 * Math.exp(-d / 140) //
|
|
67
|
+
const R = RMIN + (RBIG - RMIN) * Math.exp(-d / 70) // the curl starts gentle, then tightens
|
|
68
|
+
let phi = 0.12 + 0.55 * Math.exp(-d / 140) // edge lift angle
|
|
69
69
|
if (dRest > 0) { const b = smooth(0.45, 1, d / dRest); phi = phi * (1 - b) + PHI_REST * b }
|
|
70
70
|
const Th = Math.PI - phi
|
|
71
71
|
return { R, phi, Th, tf: solveS(d, R, Th, phi), sp: Math.sin(phi), cp: Math.cos(phi) }
|
|
@@ -74,11 +74,11 @@ const dirOf = (nIn, ang) => { const a = Math.atan2(nIn[1], nIn[0]) + ang; return
|
|
|
74
74
|
function smooth(a, b, x) { x = Math.min(1, Math.max(0, (x - a) / (b - a))); return x * x * (3 - 2 * x) }
|
|
75
75
|
const hexRGB = h => { const n = parseInt(String(h).replace('#', '').padEnd(6, '0').slice(0, 6), 16); return [(n >> 16) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255] }
|
|
76
76
|
|
|
77
|
-
/* ───────── HTML
|
|
77
|
+
/* ───────── HTML card → SVG foreignObject ───────── */
|
|
78
78
|
const svgFor = (c, W, H) => `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}">
|
|
79
79
|
<foreignObject width="${W}" height="${H}"><div xmlns="http://www.w3.org/1999/xhtml" style="width:${W}px;height:${H}px;background:${c.bg || '#ffffff'}">${c.html || ''}</div></foreignObject></svg>`
|
|
80
80
|
|
|
81
|
-
/* ─────────
|
|
81
|
+
/* ───────── Shaders ───────── */
|
|
82
82
|
const FLAP_UNIFORMS = `
|
|
83
83
|
uniform vec2 uG, uU, uHalf;
|
|
84
84
|
uniform float uTf, uR, uTheta, uSinPhi, uCosPhi;
|
|
@@ -110,7 +110,7 @@ ${FLAP_UNIFORMS}
|
|
|
110
110
|
float sdRR(vec2 p, vec2 b, float r){ vec2 q = abs(p)-b+r; return length(max(q,0.0)) + min(max(q.x,q.y),0.0) - r; }
|
|
111
111
|
float hash(vec2 p){ return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453); }
|
|
112
112
|
float rectMask(vec2 p, float soft){ vec2 d = uHalf-abs(p); return smoothstep(-soft*0.5, soft, min(d.x,d.y)); }
|
|
113
|
-
//
|
|
113
|
+
// Shadow of the folded part: which point of the original card hangs above point q.
|
|
114
114
|
float flapShadow(vec2 q){
|
|
115
115
|
float tq = dot(q-uG, uU);
|
|
116
116
|
float tL = uTf - uR, tA = uTf - uR*uSinPhi;
|
|
@@ -167,7 +167,7 @@ void main(){
|
|
|
167
167
|
gl_FragColor = vec4(col,1.0);
|
|
168
168
|
}`
|
|
169
169
|
|
|
170
|
-
/* ─────────
|
|
170
|
+
/* ───────── Component ───────── */
|
|
171
171
|
export default {
|
|
172
172
|
name: 'PeelCard',
|
|
173
173
|
props: {
|
|
@@ -180,7 +180,7 @@ export default {
|
|
|
180
180
|
padding: { type: Number, default: 16 },
|
|
181
181
|
maxScale: { type: Number, default: 1.35 },
|
|
182
182
|
hint: { type: Boolean, default: true },
|
|
183
|
-
label: { type: String, default: '
|
|
183
|
+
label: { type: String, default: 'Card: drag the edge to peel it off' },
|
|
184
184
|
},
|
|
185
185
|
emits: ['grab', 'peel', 'change', 'toggle'],
|
|
186
186
|
setup(props, { emit, expose }) {
|
|
@@ -213,7 +213,7 @@ export default {
|
|
|
213
213
|
const timers = []
|
|
214
214
|
let raf = 0
|
|
215
215
|
|
|
216
|
-
/* —
|
|
216
|
+
/* — programs — */
|
|
217
217
|
const program = (vs, fs) => {
|
|
218
218
|
const mk = (type, src) => { const s = gl.createShader(type); gl.shaderSource(s, src); gl.compileShader(s)
|
|
219
219
|
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) console.error(gl.getShaderInfoLog(s)); return s }
|
|
@@ -230,7 +230,7 @@ export default {
|
|
|
230
230
|
const pCard = program(VS, fsCard(RAD))
|
|
231
231
|
const pFloor = program(VS_FLOOR, fsFloor(RAD))
|
|
232
232
|
|
|
233
|
-
/* —
|
|
233
|
+
/* — card mesh (step ≈ 2.5, capped at ~60,000 vertices) — */
|
|
234
234
|
const step = Math.max(2.5, Math.sqrt(((W + 4) * (H + 4)) / 60000))
|
|
235
235
|
const NX = Math.round((W + 4) / step), NY = Math.round((H + 4) / step)
|
|
236
236
|
const MX = W / 2 + 2, MY = H / 2 + 2
|
|
@@ -246,7 +246,7 @@ export default {
|
|
|
246
246
|
const quadVB = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, quadVB)
|
|
247
247
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW)
|
|
248
248
|
|
|
249
|
-
/* —
|
|
249
|
+
/* — textures: HTML → SVG → canvas → GL — */
|
|
250
250
|
let texScale = 2, topIdx = 0
|
|
251
251
|
const texCache = new Map()
|
|
252
252
|
async function renderCard(c) {
|
|
@@ -260,10 +260,10 @@ export default {
|
|
|
260
260
|
img.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svgFor(c, W, H))
|
|
261
261
|
await loaded
|
|
262
262
|
ctx.drawImage(img, 0, 0, cw, ch)
|
|
263
|
-
ctx.getImageData(0, 0, 1, 1) //
|
|
263
|
+
ctx.getImageData(0, 0, 1, 1) // throws if the canvas is tainted
|
|
264
264
|
ok = true
|
|
265
|
-
} catch (e) { console.warn('[PeelCard] foreignObject
|
|
266
|
-
if (!ok) { //
|
|
265
|
+
} catch (e) { console.warn('[PeelCard] foreignObject unavailable, drawing fallback', e) }
|
|
266
|
+
if (!ok) { // fallback: background and plain text without layout
|
|
267
267
|
ctx.setTransform(1, 0, 0, 1, 0, 0)
|
|
268
268
|
ctx.fillStyle = c.bg || '#ffffff'; ctx.fillRect(0, 0, cw, ch)
|
|
269
269
|
const doc = new DOMParser().parseFromString(c.html || '', 'text/html')
|
|
@@ -300,7 +300,7 @@ export default {
|
|
|
300
300
|
}
|
|
301
301
|
const warm = () => Promise.all([ensure(0), ensure(1), ensure(2)])
|
|
302
302
|
|
|
303
|
-
/* —
|
|
303
|
+
/* — size and background color — */
|
|
304
304
|
let dpr = 1, S = 1, cssW = 1, cssH = 1, cw = 1, ch = 1
|
|
305
305
|
function resize() {
|
|
306
306
|
const r = root.value.getBoundingClientRect()
|
|
@@ -308,7 +308,7 @@ export default {
|
|
|
308
308
|
dpr = Math.min(window.devicePixelRatio || 1, 2)
|
|
309
309
|
cw = Math.round(cssW * dpr); ch = Math.round(cssH * dpr)
|
|
310
310
|
cv.width = cw; cv.height = ch
|
|
311
|
-
const fit = TOG ? 1.5 : 1 //
|
|
311
|
+
const fit = TOG ? 1.5 : 1 // in toggle mode leave room for the peeled card
|
|
312
312
|
S = Math.max(0.3, Math.min(props.maxScale, (cssW - 2 * props.padding) / (W * fit), (cssH - 2 * props.padding) / (H * fit)))
|
|
313
313
|
}
|
|
314
314
|
let bg = [0.9, 0.89, 0.85]
|
|
@@ -324,7 +324,7 @@ export default {
|
|
|
324
324
|
if (m && m.length >= 3) bg = [m[0] / 255, m[1] / 255, m[2] / 255]
|
|
325
325
|
}
|
|
326
326
|
|
|
327
|
-
/* —
|
|
327
|
+
/* — gesture and animation state — */
|
|
328
328
|
const st = {
|
|
329
329
|
mode: 'idle', // idle | drag | settle | hint | complete | fade | rest
|
|
330
330
|
d: 0, ang: 0, g: [-W / 2, 0], nIn: [1, 0], p0: [0, 0], dT: 0, angT: 0, angBase: 0,
|
|
@@ -335,12 +335,12 @@ export default {
|
|
|
335
335
|
const CORNERS = [[-W / 2, -H / 2], [W / 2, -H / 2], [-W / 2, H / 2], [W / 2, H / 2]]
|
|
336
336
|
const proj = (g, u) => CORNERS.map(c => (c[0] - g[0]) * u[0] + (c[1] - g[1]) * u[1])
|
|
337
337
|
const tmaxOf = (g, u) => Math.max(...proj(g, u))
|
|
338
|
-
//
|
|
338
|
+
// toggle mode: pull distance at which the card is fully flipped back, and how far to shift the scene
|
|
339
339
|
const dRestFor = (g, u) => Gfun(tmaxOf(g, u) + 4, RMIN, Math.PI - PHI_REST, PHI_REST)
|
|
340
340
|
const overhangFor = (g, u) => { const p = proj(g, u); return Math.cos(PHI_REST) * (Math.max(...p) - Math.min(...p)) + RMIN + 8 }
|
|
341
341
|
const toUnits = e => { const r = cv.getBoundingClientRect(); return [(e.clientX - r.left - r.width / 2) / S, -(e.clientY - r.top - r.height / 2) / S] }
|
|
342
342
|
|
|
343
|
-
function beginDrag(p) { //
|
|
343
|
+
function beginDrag(p) { // nearest edge and the grab point on it
|
|
344
344
|
const dl = p[0] + W / 2, dr = W / 2 - p[0], db = p[1] + H / 2, dt = H / 2 - p[1]
|
|
345
345
|
const m = Math.min(dl, dr, db, dt), pad = RAD * 0.8
|
|
346
346
|
const cl = (v, lim) => Math.max(-lim + pad, Math.min(lim - pad, v))
|
|
@@ -355,14 +355,14 @@ export default {
|
|
|
355
355
|
st.velU += ((x - st.lastDT) / dtm * 1000 - st.velU) * 0.35
|
|
356
356
|
st.lastDT = x; st.lastTime = now
|
|
357
357
|
}
|
|
358
|
-
function updateTargets(p) { //
|
|
358
|
+
function updateTargets(p) { // where the finger pulls: fold offset and rotation
|
|
359
359
|
const v = [p[0] - st.p0[0], p[1] - st.p0[1]], len = Math.hypot(v[0], v[1])
|
|
360
|
-
if (st.restick) { //
|
|
360
|
+
if (st.restick) { // sticking back: pull against the peel direction
|
|
361
361
|
const w = dirOf(st.nIn, st.angBase).map(x => -x)
|
|
362
362
|
const rel = Math.max(-0.6, Math.min(0.6, Math.atan2(w[0] * v[1] - w[1] * v[0], v[0] * w[0] + v[1] * w[1])))
|
|
363
363
|
st.angT = st.angBase + rel * smooth(6, 70, len)
|
|
364
364
|
const u2 = dirOf(st.nIn, st.angT)
|
|
365
|
-
st.dT = Math.max(0, -(v[0] * u2[0] + v[1] * u2[1])) //
|
|
365
|
+
st.dT = Math.max(0, -(v[0] * u2[0] + v[1] * u2[1])) // how far it was pulled back
|
|
366
366
|
trackVel(st.dT)
|
|
367
367
|
return
|
|
368
368
|
}
|
|
@@ -390,8 +390,8 @@ export default {
|
|
|
390
390
|
const u = dirOf(st.nIn, st.ang)
|
|
391
391
|
if (st.restick) {
|
|
392
392
|
const f = st.d / dRestFor(st.g, u)
|
|
393
|
-
if (f < 0.6 || (st.velU > 700 && f < 0.9)) st.mode = 'settle' //
|
|
394
|
-
else startComplete() //
|
|
393
|
+
if (f < 0.6 || (st.velU > 700 && f < 0.9)) st.mode = 'settle' // stick back
|
|
394
|
+
else startComplete() // return to the peeled position
|
|
395
395
|
return
|
|
396
396
|
}
|
|
397
397
|
const ratio = st.d / tmaxOf(st.g, u)
|
|
@@ -413,7 +413,7 @@ export default {
|
|
|
413
413
|
const onDown = e => {
|
|
414
414
|
if (st.mode === 'complete' || st.mode === 'fade') return
|
|
415
415
|
const p = toUnits(e)
|
|
416
|
-
if (TOG && st.peeled) { //
|
|
416
|
+
if (TOG && st.peeled) { // peeled: any touch starts sticking back
|
|
417
417
|
cv.setPointerCapture(e.pointerId); grabbing.value = true
|
|
418
418
|
st.restick = true; st.angBase = st.ang; st.p0 = p; st.dT = 0; st.velU = 0; st.lastDT = 0; st.lastTime = performance.now()
|
|
419
419
|
st.mode = 'drag'
|
|
@@ -481,12 +481,12 @@ export default {
|
|
|
481
481
|
if (st.t >= 0.28) swapCards()
|
|
482
482
|
break
|
|
483
483
|
}
|
|
484
|
-
//
|
|
484
|
+
// scene shift: while the card is flipped back, the "base + flipped" pair is centered
|
|
485
485
|
const want = TOG && ((st.peeled && (st.mode === 'rest' || (st.mode === 'drag' && st.restick))) || st.mode === 'complete') ? 1 : 0
|
|
486
486
|
st.shiftF += (want - st.shiftF) * (1 - Math.exp(-dt * 6))
|
|
487
487
|
}
|
|
488
488
|
|
|
489
|
-
/* —
|
|
489
|
+
/* — rendering — */
|
|
490
490
|
function setCommon(p, gm, u) {
|
|
491
491
|
const L = p.loc
|
|
492
492
|
gl.uniform2f(L.uG, st.g[0], st.g[1]); gl.uniform2f(L.uU, u[0], u[1]); gl.uniform2f(L.uHalf, W / 2, H / 2)
|
|
@@ -503,7 +503,7 @@ export default {
|
|
|
503
503
|
const u = dirOf(st.nIn, st.ang)
|
|
504
504
|
const gm = geom(st.d, TOG ? dRestFor(st.g, u) : 0)
|
|
505
505
|
|
|
506
|
-
// 1)
|
|
506
|
+
// 1) table + bottom card + shadows
|
|
507
507
|
gl.disable(gl.DEPTH_TEST); gl.disable(gl.BLEND)
|
|
508
508
|
gl.useProgram(pFloor); setCommon(pFloor, gm, u)
|
|
509
509
|
gl.uniform3f(pFloor.loc.uBg, bg[0], bg[1], bg[2])
|
|
@@ -512,7 +512,7 @@ export default {
|
|
|
512
512
|
gl.enableVertexAttribArray(pFloor.aPos); gl.vertexAttribPointer(pFloor.aPos, 2, gl.FLOAT, false, 0, 0)
|
|
513
513
|
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)
|
|
514
514
|
|
|
515
|
-
// 2)
|
|
515
|
+
// 2) top (peeled) card
|
|
516
516
|
gl.enable(gl.DEPTH_TEST); gl.depthFunc(gl.LEQUAL)
|
|
517
517
|
gl.enable(gl.BLEND); gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
|
518
518
|
gl.useProgram(pCard); setCommon(pCard, gm, u)
|
|
@@ -531,7 +531,7 @@ export default {
|
|
|
531
531
|
raf = requestAnimationFrame(frame)
|
|
532
532
|
}
|
|
533
533
|
|
|
534
|
-
/* —
|
|
534
|
+
/* — observers and startup — */
|
|
535
535
|
const ro = new ResizeObserver(resize); ro.observe(root.value)
|
|
536
536
|
const mq = matchMedia('(prefers-color-scheme: dark)')
|
|
537
537
|
mq.addEventListener && mq.addEventListener('change', readBg)
|
|
@@ -539,7 +539,7 @@ export default {
|
|
|
539
539
|
resize(); readBg()
|
|
540
540
|
texScale = Math.min(3, Math.max(2, S * dpr))
|
|
541
541
|
|
|
542
|
-
const stopWatch = watch(() => props.cards, () => { //
|
|
542
|
+
const stopWatch = watch(() => props.cards, () => { // cards replaced: re-render textures
|
|
543
543
|
freeTextures(true); topIdx = 0; st.d = 0; st.mode = 'idle'; st.alpha = 1; st.peeled = false; st.shiftF = 0; warm()
|
|
544
544
|
})
|
|
545
545
|
warm().then(() => {
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import PeelCard from './PeelCard.vue'
|
|
2
2
|
|
|
3
|
-
//
|
|
3
|
+
// Plugin: app.use(PeelCardPlugin) registers the component globally as <PeelCard>.
|
|
4
4
|
export const PeelCardPlugin = {
|
|
5
5
|
install(app) { app.component('PeelCard', PeelCard) },
|
|
6
6
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { DefineComponent, Plugin } from 'vue'
|
|
2
2
|
|
|
3
3
|
export interface PeelCardItem {
|
|
4
|
-
/**
|
|
4
|
+
/** Front side color (CSS color), backdrop under the html. */
|
|
5
5
|
bg: string
|
|
6
|
-
/**
|
|
6
|
+
/** Back side paper color, #rrggbb. */
|
|
7
7
|
back: string
|
|
8
|
-
/**
|
|
8
|
+
/** Card content: well-formed XHTML with styles inside. */
|
|
9
9
|
html: string
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -23,7 +23,7 @@ export interface PeelCardProps {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export interface PeelCardExposed {
|
|
26
|
-
/**
|
|
26
|
+
/** Peel off; in toggle mode switches the state. */
|
|
27
27
|
peel(): void
|
|
28
28
|
}
|
|
29
29
|
|