@patrick91/stickers 0.2.0 → 0.2.1

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.1
4
+
5
+ - 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.
6
+ - Long, thin stickers get as fine an outline as other stickers, rather than a few rows of large steps.
7
+
3
8
  ## 0.2.0
4
9
 
5
10
  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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patrick91/stickers",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
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",