@patrick91/stickers 0.2.0 → 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,15 @@
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
+
8
+ ## 0.2.1
9
+
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.
11
+ - Long, thin stickers get as fine an outline as other stickers, rather than a few rows of large steps.
12
+
3
13
  ## 0.2.0
4
14
 
5
15
  The first release on npm.
@@ -404,10 +404,13 @@ function edt(grid, w, h) {
404
404
  for (let x = 0; x < w; x++) edt1d(grid, x, w, h, f, v, z);
405
405
  for (let y = 0; y < h; y++) edt1d(grid, y * w, 1, w, f, v, z);
406
406
  }
407
- // Signed distance (in cells, negative inside) from an anti-aliased coverage map. With outsideOnly,
408
- // cells inside the shape read 0 instead, and it takes one distance transform rather than two.
409
- function signedDistance(cov, w, h, outsideOnly) {
407
+ // Signed distance (in cells, negative inside) from an anti-aliased coverage map. Growing the shape by
408
+ // `grow` cells or shrinking it by `shrink` needs the distance right only that far from the edge, and
409
+ // given a cell or more, each makes it exact there rather than next to the edge. Growing needs no
410
+ // distance inside, so cells inside read 0, and it takes one distance transform rather than two.
411
+ function signedDistance(cov, w, h, grow = 0, shrink = 0) {
410
412
  const n = w * h, outer = new Float64Array(n), inner = new Float64Array(n), out = new Float32Array(n);
413
+ const far = grow >= 1, deep = shrink >= 1;
411
414
  for (let i = 0; i < n; i++) {
412
415
  let a = cov[i];
413
416
  // Only a pixel with a neighbour across the 0.5 line holds the edge. Anything else, such as the
@@ -417,12 +420,21 @@ function signedDistance(cov, w, h, outsideOnly) {
417
420
  (i < w || (cov[i - w] >= 0.5) === side) && (i >= n - w || (cov[i + w] >= 0.5) === side)) a = side ? 1 : 0;
418
421
  if (a >= 1) { outer[i] = 0; inner[i] = INF; }
419
422
  else if (a <= 0) { outer[i] = INF; inner[i] = 0; }
420
- else { const d = 0.5 - a; outer[i] = d > 0 ? d * d : 0; inner[i] = d < 0 ? d * d : 0; }
423
+ else {
424
+ // The edge is d past this cell's middle, so a point r further out is r + d from the edge, and
425
+ // one r further in is r - d. The transform reads sqrt(r² + seed): a seed of d² is right next
426
+ // to the cell, but far off it drops d, and an outline grown or shrunk that far follows the
427
+ // cells, with a bump at each one. Seeds of 2·grow·d − d² and −2·shrink·d − d² are right where
428
+ // r + d = grow and r − d = shrink.
429
+ const d = 0.5 - a;
430
+ outer[i] = far ? 2 * grow * d - d * d : d > 0 ? d * d : 0;
431
+ inner[i] = deep ? -2 * shrink * d - d * d : d < 0 ? d * d : 0;
432
+ }
421
433
  }
422
434
  edt(outer, w, h);
423
- if (outsideOnly) { for (let i = 0; i < n; i++) out[i] = Math.sqrt(outer[i]); return out; }
435
+ if (far) { for (let i = 0; i < n; i++) out[i] = Math.sqrt(Math.max(0, outer[i])); return out; }
424
436
  edt(inner, w, h);
425
- for (let i = 0; i < n; i++) out[i] = Math.sqrt(outer[i]) - Math.sqrt(inner[i]);
437
+ for (let i = 0; i < n; i++) out[i] = Math.sqrt(outer[i]) - Math.sqrt(Math.max(0, inner[i]));
426
438
  return out;
427
439
  }
428
440
 
@@ -572,8 +584,10 @@ export class Sticker {
572
584
  // Die-cut outline: grow the artwork's alpha by `border`, rounding inside corners too.
573
585
  _buildShape(shape) {
574
586
  const { canvas, w, h, W, H } = shape;
575
- // a borderless cut runs right along the artwork's edge, so it needs a finer outline than a bordered one
576
- const k = Math.min(1, (this.border <= 0 ? 420 : 240) / Math.max(canvas.width, canvas.height));
587
+ // A borderless cut runs right along the artwork's edge, so it needs a finer outline than a bordered
588
+ // one. A long, thin sticker gets as many cells as a 4:3 one, rather than a few rows of large ones.
589
+ const side = this.border <= 0 ? 420 : 240, cw = canvas.width, ch = canvas.height;
590
+ const k = Math.min(1, Math.max(side / Math.max(cw, ch), side * Math.sqrt(0.75 / (cw * ch))));
577
591
  const gw = Math.max(8, Math.round(canvas.width * k)), gh = Math.max(8, Math.round(canvas.height * k));
578
592
  const small = document.createElement('canvas');
579
593
  small.width = gw; small.height = gh;
@@ -596,11 +610,11 @@ export class Sticker {
596
610
  }
597
611
  }
598
612
  const round = this.border <= 0 ? 0 : Math.max(2, this.border * 0.7); // with no border there is nothing to round into
599
- // Growing by half a cell or more covers every cell inside the artwork fully, however deep it is.
600
- const d1 = signedDistance(cov, gw, gh, (this.border + round) / cell >= 0.5);
601
- for (let i = 0; i < n; i++) cov[i] = clamp(0.5 - (d1[i] * cell - (this.border + round)) / cell, 0, 1);
613
+ const grow = (this.border + round) / cell;
614
+ const d1 = signedDistance(cov, gw, gh, grow);
615
+ for (let i = 0; i < n; i++) cov[i] = clamp(0.5 - d1[i] + grow, 0, 1);
602
616
  if (this.fillHoles) fillEnclosedCoverage(cov, gw, gh);
603
- const d2 = signedDistance(cov, gw, gh);
617
+ const d2 = signedDistance(cov, gw, gh, 0, round / cell);
604
618
  const sd = new Float32Array(n);
605
619
  // A borderless contour normally clips to artwork alpha. Filled holes need the stock mask instead.
606
620
  const trim = shape.trim = this.border <= 0 && this.cut === 'contour' && !this.fillHoles;
@@ -640,6 +654,24 @@ export class Sticker {
640
654
  return m;
641
655
  }
642
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
+
643
675
  radius(tau) { return this.rMax * (1 - Math.exp(-tau / (2.4 * this.rMax))); }
644
676
  get tau() { return Math.hypot(this.fx, this.fy); }
645
677
  get size() { return this.scale; }
@@ -1165,14 +1197,17 @@ export class StickerEngine {
1165
1197
  // The first few pixels of a drag pick the way it peels. Pulling back the other way lays
1166
1198
  // the flap down again rather than peeling the opposite edge.
1167
1199
  if (!s.pull && delta > 4) s.pull = [-d[0] / delta, -d[1] / delta];
1168
- 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])));
1169
1202
  if (delta > 0.5) {
1170
1203
  const len = Math.hypot(d[0], d[1]), nx = -d[0] / len, ny = -d[1] / len;
1171
1204
  const far = s.support(nx, ny), near = s.support(-nx, -ny);
1172
1205
  const inset = Math.max(0, far - (s.gx * nx + s.gy * ny));
1173
1206
  let tau = s.tau;
1174
1207
  for (let i = 0; i < 5; i++) tau = inset + 0.5 * (delta + Math.PI * s.radius(tau));
1175
- 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);
1176
1211
  tau = Math.max(tau, s.floor * Math.max(0, nx * s.floorNx + ny * s.floorNy));
1177
1212
  tx = nx * tau; ty = ny * tau;
1178
1213
  if (tau > s.byTack(0.48, this.opts.detachAt, 0.93) * (far + near)) this._detach(s);
@@ -1187,7 +1222,8 @@ export class StickerEngine {
1187
1222
  if (at) {
1188
1223
  const [sd, gx, gy] = s.sample(at[0], at[1]);
1189
1224
  const e = s.hint ? 1 : smooth(Math.min(s.w, s.h) * 0.36, 0, -sd), len = Math.hypot(gx, gy);
1190
- 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; }
1191
1227
  }
1192
1228
  }
1193
1229
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patrick91/stickers",
3
- "version": "0.2.0",
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",