@patrick91/stickers 0.2.1 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.2
4
+
5
+ - A peeled sticker keeps the point you grabbed under the pointer. Before, grabbing anywhere but the very edge, or dragging diagonally, peeled a far edge or a corner first and the flap trailed behind the pointer.
6
+ - Hovering a die-cut sticker lifts the edge under the pointer. Before, it could lift whichever part of the outline reached furthest that way, such as the bowl of a "d" when you hovered its stem.
7
+
3
8
  ## 0.2.1
4
9
 
5
10
  - Die-cut outlines follow the artwork smoothly. Before, the white border bumped out and in along its edge, most visibly on large stickers and long, thin ones.
@@ -654,6 +654,24 @@ export class Sticker {
654
654
  return m;
655
655
  }
656
656
 
657
+ // The way to lift edge point (px, py), closest to its outward normal (nx, ny), in which that point is
658
+ // within `slack` of the outermost: a lift starts at the outermost point, so straight along the normal a
659
+ // part reaching further, such as the bowl of a d, would lift instead. Better still is a way in which no
660
+ // other part reaches into the `depth` that lifts. Null for a point deep in a notch.
661
+ liftFrom(px, py, nx, ny, slack, depth) {
662
+ let fallback = null;
663
+ for (let i = 0; i <= 24; i++) for (const a of i ? [i, -i] : [0]) {
664
+ const t = a * Math.PI / 48, c = Math.cos(t), n = Math.sin(t), mx = c * nx - n * ny, my = n * nx + c * ny;
665
+ const top = this.support(mx, my);
666
+ if (top - (px * mx + py * my) > slack) continue;
667
+ fallback = fallback || [mx, my];
668
+ let other = -INF;
669
+ for (const q of this.shape.hull) if (Math.hypot(q[0] - px, q[1] - py) > 3 * depth) other = Math.max(other, q[0] * mx + q[1] * my);
670
+ if (other <= top - depth) return [mx, my];
671
+ }
672
+ return fallback;
673
+ }
674
+
657
675
  radius(tau) { return this.rMax * (1 - Math.exp(-tau / (2.4 * this.rMax))); }
658
676
  get tau() { return Math.hypot(this.fx, this.fy); }
659
677
  get size() { return this.scale; }
@@ -1179,14 +1197,17 @@ export class StickerEngine {
1179
1197
  // The first few pixels of a drag pick the way it peels. Pulling back the other way lays
1180
1198
  // the flap down again rather than peeling the opposite edge.
1181
1199
  if (!s.pull && delta > 4) s.pull = [-d[0] / delta, -d[1] / delta];
1182
- if (s.pull) delta = Math.max(0, -(d[0] * s.pull[0] + d[1] * s.pull[1]));
1200
+ // Within 60 degrees of that way the whole drag counts, so the flap keeps up with the pointer as it swings.
1201
+ if (s.pull) delta = Math.max(0, Math.min(delta, -2 * (d[0] * s.pull[0] + d[1] * s.pull[1])));
1183
1202
  if (delta > 0.5) {
1184
1203
  const len = Math.hypot(d[0], d[1]), nx = -d[0] / len, ny = -d[1] / len;
1185
1204
  const far = s.support(nx, ny), near = s.support(-nx, -ny);
1186
1205
  const inset = Math.max(0, far - (s.gx * nx + s.gy * ny));
1187
1206
  let tau = s.tau;
1188
1207
  for (let i = 0; i < 5; i++) tau = inset + 0.5 * (delta + Math.PI * s.radius(tau));
1189
- tau = Math.min(s.byTack(1.6, 1.15, 0.85) * delta, tau);
1208
+ // This puts the grabbed point under the pointer. Tack only slows the first pixels of the peel
1209
+ // past it: holding back the part behind it too would leave the flap short of the pointer.
1210
+ tau = Math.min(inset + s.byTack(1.6, 1.15, 0.85) * delta, tau);
1190
1211
  tau = Math.max(tau, s.floor * Math.max(0, nx * s.floorNx + ny * s.floorNy));
1191
1212
  tx = nx * tau; ty = ny * tau;
1192
1213
  if (tau > s.byTack(0.48, this.opts.detachAt, 0.93) * (far + near)) this._detach(s);
@@ -1201,7 +1222,8 @@ export class StickerEngine {
1201
1222
  if (at) {
1202
1223
  const [sd, gx, gy] = s.sample(at[0], at[1]);
1203
1224
  const e = s.hint ? 1 : smooth(Math.min(s.w, s.h) * 0.36, 0, -sd), len = Math.hypot(gx, gy);
1204
- if (e > 0.01 && len > 1e-4) { tx = gx / len * e * amount * s.hoverLift; ty = gy / len * e * amount * s.hoverLift; }
1225
+ const lift = e * amount * s.hoverLift, n = e > 0.01 && len > 1e-4 && s.liftFrom(at[0] - gx / len * sd, at[1] - gy / len * sd, gx / len, gy / len, 0.5 * lift + 1, lift);
1226
+ if (n) { tx = n[0] * lift; ty = n[1] * lift; }
1205
1227
  }
1206
1228
  }
1207
1229
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patrick91/stickers",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Peelable WebGL stickers for plain JavaScript, Web Components, React, and Svelte.",
5
5
  "keywords": ["stickers", "peel", "drag", "webgl", "webgl2", "web-components", "custom-elements", "react", "svelte"],
6
6
  "license": "MIT",