@shopify/klint 0.0.4 → 0.0.7

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/dist/index.d.ts CHANGED
@@ -807,4 +807,4 @@ declare const useStorage: <T extends object = Record<string, unknown>>(initialPr
807
807
  store: T;
808
808
  };
809
809
 
810
- export { CONFIG_PROPS, EPSILON, Klint, KlintCanvasOptions, KlintConfig, KlintContext, KlintContextWrapper, KlintContexts, KlintCoreFunctions, KlintFunctions, KlintMouse, KlintOffscreenContext, KlintProps, KlintScroll, useKlint, useProps, useStorage };
810
+ export { CONFIG_PROPS, EPSILON, Klint, type KlintCanvasOptions, type KlintConfig, type KlintContext, type KlintContextWrapper, type KlintContexts, KlintCoreFunctions, KlintFunctions, type KlintMouse, type KlintOffscreenContext, type KlintProps, type KlintScroll, useKlint, useProps, useStorage };
package/dist/index.js CHANGED
@@ -43,10 +43,8 @@ function useAnimate(contextRef, draw, isVisible) {
43
43
  const animationFrameId = useRef(0);
44
44
  const animate = useCallback(
45
45
  (timestamp = 0) => {
46
- if (!contextRef.current || !isVisible)
47
- return;
48
- if (!contextRef.current.__isReadyToDraw)
49
- return;
46
+ if (!contextRef.current || !isVisible) return;
47
+ if (!contextRef.current.__isReadyToDraw) return;
50
48
  if (!contextRef.current.__isPlaying) {
51
49
  return;
52
50
  }
@@ -62,10 +60,8 @@ function useAnimate(contextRef, draw, isVisible) {
62
60
  if (sinceLast >= target - epsilon) {
63
61
  context.deltaTime = now - context.__lastRealTime;
64
62
  draw(context);
65
- if (context.time > 1e7)
66
- context.time = 0;
67
- if (context.frame > 1e7)
68
- context.frame = 0;
63
+ if (context.time > 1e7) context.time = 0;
64
+ if (context.frame > 1e7) context.frame = 0;
69
65
  context.time += context.deltaTime / DEFAULT_FPS;
70
66
  context.frame++;
71
67
  context.__lastTargetTime = now;
@@ -118,19 +114,16 @@ function Klint({
118
114
  if (__options.origin === "center") {
119
115
  context2.translate(canvas.width * 0.5, canvas.height * 0.5);
120
116
  }
121
- if (shouldRedraw)
122
- draw(context2);
117
+ if (shouldRedraw) draw(context2);
123
118
  };
124
119
  useEffect(() => {
125
- if (!canvasRef.current || !containerRef.current)
126
- return;
120
+ if (!canvasRef.current || !containerRef.current) return;
127
121
  const canvas = canvasRef.current;
128
122
  const container = containerRef.current;
129
123
  const dpr = window.devicePixelRatio || 3;
130
124
  contextRef.current = initContext ? initContext(canvas, __options) : null;
131
125
  const context2 = contextRef.current;
132
- if (!context2)
133
- return;
126
+ if (!context2) return;
134
127
  context2.__dpr = dpr;
135
128
  if (__options.fps && __options.fps !== context2.fps) {
136
129
  context2.fps = __options.fps;
@@ -153,8 +146,7 @@ function Klint({
153
146
  );
154
147
  intersectionObserverRef.current.observe(container);
155
148
  const initializeKlint = async () => {
156
- if (!context2)
157
- return;
149
+ if (!context2) return;
158
150
  const handleStaticMode = () => {
159
151
  try {
160
152
  const imageUrl = canvas.toDataURL("image/png");
@@ -168,8 +160,7 @@ function Klint({
168
160
  if (preload && (unsafeReset || !context2.__isPreloaded)) {
169
161
  try {
170
162
  await preload(context2);
171
- if (!unsafeReset)
172
- context2.__isPreloaded = true;
163
+ if (!unsafeReset) context2.__isPreloaded = true;
173
164
  } catch (error) {
174
165
  const message = error instanceof Error ? error.message : String(error);
175
166
  throw new Error(`Klint error in the preload: ${message}`);
@@ -178,8 +169,7 @@ function Klint({
178
169
  if (setup && (unsafeReset || !context2.__isSetup)) {
179
170
  try {
180
171
  setup(context2);
181
- if (!unsafeReset)
182
- context2.__isSetup = true;
172
+ if (!unsafeReset) context2.__isSetup = true;
183
173
  } catch (error) {
184
174
  const message = error instanceof Error ? error.message : String(error);
185
175
  throw new Error(`Klint error in the setup: ${message}`);
@@ -196,14 +186,12 @@ function Klint({
196
186
  }
197
187
  };
198
188
  const unsafeMode = __options.unsafemode === "true";
199
- if (!unsafeMode && context2.__isReadyToDraw)
200
- return;
189
+ if (!unsafeMode && context2.__isReadyToDraw) return;
201
190
  await initializeContext(unsafeMode);
202
191
  if (__options.static === "true") {
203
192
  handleStaticMode();
204
193
  } else {
205
- if (__options.noloop !== "true")
206
- animate();
194
+ if (__options.noloop !== "true") animate();
207
195
  }
208
196
  };
209
197
  initializeKlint();
@@ -256,19 +244,16 @@ var KlintCoreFunctions = {
256
244
  ctx.canvas.requestFullscreen?.();
257
245
  },
258
246
  play: (ctx) => () => {
259
- if (!ctx.__isPlaying)
260
- ctx.__isPlaying = true;
247
+ if (!ctx.__isPlaying) ctx.__isPlaying = true;
261
248
  },
262
249
  pause: (ctx) => () => {
263
- if (ctx.__isPlaying)
264
- ctx.__isPlaying = false;
250
+ if (ctx.__isPlaying) ctx.__isPlaying = false;
265
251
  },
266
252
  // to do
267
253
  redraw: () => () => {
268
254
  },
269
255
  extend: (ctx) => (name, data, enforceReplace = false) => {
270
- if (name in ctx && !enforceReplace)
271
- return;
256
+ if (name in ctx && !enforceReplace) return;
272
257
  ctx[name] = data;
273
258
  },
274
259
  passImage: () => (element) => {
@@ -309,8 +294,7 @@ var KlintCoreFunctions = {
309
294
  alpha: options?.alpha ?? true,
310
295
  willReadFrequently: options?.willreadfrequently ?? false
311
296
  });
312
- if (!context)
313
- throw new Error("Failed to create offscreen context");
297
+ if (!context) throw new Error("Failed to create offscreen context");
314
298
  context.__dpr = ctx.__dpr;
315
299
  context.width = width * ctx.__dpr;
316
300
  context.height = height * ctx.__dpr;
@@ -359,8 +343,7 @@ var KlintCoreFunctions = {
359
343
  };
360
344
  var KlintFunctions = {
361
345
  extend: (ctx) => (name, data, enforceReplace = false) => {
362
- if (name in ctx && !enforceReplace)
363
- return;
346
+ if (name in ctx && !enforceReplace) return;
364
347
  ctx[name] = data;
365
348
  },
366
349
  background: (ctx) => (color) => {
@@ -414,27 +397,21 @@ var KlintFunctions = {
414
397
  ctx.restore();
415
398
  },
416
399
  point: (ctx) => (x, y) => {
417
- if (!ctx.checkTransparency("stroke"))
418
- return;
400
+ if (!ctx.checkTransparency("stroke")) return;
419
401
  ctx.beginPath();
420
402
  ctx.strokeRect(x, y, 1, 1);
421
403
  },
422
404
  checkTransparency: (ctx) => (toCheck) => {
423
- if (toCheck === "stroke" && ctx.strokeStyle === "transparent")
424
- return false;
425
- if (toCheck === "fill" && ctx.fillStyle === "transparent")
426
- return false;
405
+ if (toCheck === "stroke" && ctx.strokeStyle === "transparent") return false;
406
+ if (toCheck === "fill" && ctx.fillStyle === "transparent") return false;
427
407
  return true;
428
408
  },
429
409
  drawIfVisible: (ctx) => () => {
430
- if (ctx.checkTransparency("fill"))
431
- ctx.fill();
432
- if (ctx.checkTransparency("stroke"))
433
- ctx.stroke();
410
+ if (ctx.checkTransparency("fill")) ctx.fill();
411
+ if (ctx.checkTransparency("stroke")) ctx.stroke();
434
412
  },
435
413
  line: (ctx) => (x1, y1, x2, y2) => {
436
- if (!ctx.checkTransparency("stroke"))
437
- return;
414
+ if (!ctx.checkTransparency("stroke")) return;
438
415
  ctx.beginPath();
439
416
  ctx.moveTo(x1, y1);
440
417
  ctx.lineTo(x2, y2);
@@ -480,25 +457,21 @@ var KlintFunctions = {
480
457
  const angle = i * 2 * Math.PI / sides + rotation;
481
458
  const pointX = x + radius * Math.cos(angle);
482
459
  const pointY = y + (radius2 ? radius2 : radius) * Math.sin(angle);
483
- if (i === 0)
484
- ctx.moveTo(pointX, pointY);
485
- else
486
- ctx.lineTo(pointX, pointY);
460
+ if (i === 0) ctx.moveTo(pointX, pointY);
461
+ else ctx.lineTo(pointX, pointY);
487
462
  }
488
463
  ctx.closePath();
489
464
  ctx.drawIfVisible();
490
465
  },
491
466
  beginShape: (ctx) => () => {
492
- if (ctx.__startedShape)
493
- return;
467
+ if (ctx.__startedShape) return;
494
468
  ctx.beginPath();
495
469
  ctx.__startedShape = true;
496
470
  ctx.__currentShape = [];
497
471
  ctx.__currentContours = [];
498
472
  },
499
473
  beginContour: (ctx) => () => {
500
- if (!ctx.__startedShape)
501
- return;
474
+ if (!ctx.__startedShape) return;
502
475
  if (ctx.__startedContour && ctx.__currentContour?.length) {
503
476
  ctx.__currentContours?.push([...ctx.__currentContour]);
504
477
  }
@@ -506,14 +479,12 @@ var KlintFunctions = {
506
479
  ctx.__currentContour = [];
507
480
  },
508
481
  vertex: (ctx) => (x, y) => {
509
- if (!ctx.__startedShape)
510
- return;
482
+ if (!ctx.__startedShape) return;
511
483
  const points = ctx.__startedContour ? ctx.__currentContour : ctx.__currentShape;
512
484
  points?.push([x, y]);
513
485
  },
514
486
  endContour: (ctx) => (forceRevert = true) => {
515
- if (!ctx.__startedContour || !ctx.__currentContour?.length)
516
- return;
487
+ if (!ctx.__startedContour || !ctx.__currentContour?.length) return;
517
488
  const contourPoints = [...ctx.__currentContour];
518
489
  if (forceRevert) {
519
490
  contourPoints.reverse();
@@ -523,13 +494,10 @@ var KlintFunctions = {
523
494
  ctx.__startedContour = false;
524
495
  },
525
496
  endShape: (ctx) => (close = false) => {
526
- if (!ctx.__startedShape)
527
- return;
528
- if (ctx.__startedContour)
529
- ctx.endContour();
497
+ if (!ctx.__startedShape) return;
498
+ if (ctx.__startedContour) ctx.endContour();
530
499
  const points = ctx.__currentShape;
531
- if (!points?.length)
532
- return;
500
+ if (!points?.length) return;
533
501
  const drawPath = (points2, close2 = false) => {
534
502
  ctx.moveTo(points2[0][0], points2[0][1]);
535
503
  for (let i = 1; i < points2.length; i++) {
@@ -579,8 +547,7 @@ var KlintFunctions = {
579
547
  if (mode === "fast") {
580
548
  return n - ~~(n / mod) * mod;
581
549
  }
582
- if (n >= 0)
583
- return n % mod;
550
+ if (n >= 0) return n % mod;
584
551
  return mod - -n % mod;
585
552
  },
586
553
  distance: (ctx) => (x1, y1, x2, y2, mode = "precise") => {
@@ -642,16 +609,14 @@ var KlintFunctions = {
642
609
  },
643
610
  computeFont: (ctx) => () => {
644
611
  ctx.computeTextStyle();
645
- if (ctx.font !== ctx.__computedTextFont)
646
- ctx.font = ctx.__computedTextFont;
612
+ if (ctx.font !== ctx.__computedTextFont) ctx.font = ctx.__computedTextFont;
647
613
  },
648
614
  textWidth: (ctx) => (text) => {
649
615
  ctx.computeFont();
650
616
  return ctx.measureText(text).width;
651
617
  },
652
618
  text: (ctx) => (text, x, y, maxWidth = void 0) => {
653
- if (text === void 0)
654
- return;
619
+ if (text === void 0) return;
655
620
  ctx.computeFont();
656
621
  if (ctx.textAlign !== ctx.__textAlignment.horizontal) {
657
622
  ctx.textAlign = ctx.__textAlignment.horizontal;
@@ -762,8 +727,7 @@ var KlintFunctions = {
762
727
  Object.assign(ctx, config);
763
728
  },
764
729
  resizeCanvas: (ctx) => (width, height) => {
765
- if (ctx.__isMainContext)
766
- return;
730
+ if (ctx.__isMainContext) return;
767
731
  const config = ctx.saveConfig();
768
732
  ctx.canvas.width = ctx.width = width;
769
733
  ctx.canvas.height = ctx.height = height;
@@ -863,8 +827,7 @@ function useKlint() {
863
827
  const mouseDownCallbackRef = useRef2(null);
864
828
  const mouseUpCallbackRef = useRef2(null);
865
829
  useEffect2(() => {
866
- if (!contextRef.current?.canvas)
867
- return;
830
+ if (!contextRef.current?.canvas) return;
868
831
  const canvas = contextRef.current.canvas;
869
832
  const ctx = contextRef.current;
870
833
  const updateMousePosition = (e) => {
@@ -887,32 +850,23 @@ function useKlint() {
887
850
  }
888
851
  };
889
852
  const handleMouseDown = (e) => {
890
- if (mouseRef.current)
891
- mouseRef.current.isPressed = true;
892
- if (mouseDownCallbackRef.current)
893
- mouseDownCallbackRef.current(ctx, e);
853
+ if (mouseRef.current) mouseRef.current.isPressed = true;
854
+ if (mouseDownCallbackRef.current) mouseDownCallbackRef.current(ctx, e);
894
855
  };
895
856
  const handleMouseUp = (e) => {
896
- if (mouseRef.current)
897
- mouseRef.current.isPressed = false;
898
- if (mouseUpCallbackRef.current)
899
- mouseUpCallbackRef.current(ctx, e);
857
+ if (mouseRef.current) mouseRef.current.isPressed = false;
858
+ if (mouseUpCallbackRef.current) mouseUpCallbackRef.current(ctx, e);
900
859
  };
901
860
  const handleMouseEnter = (e) => {
902
- if (mouseRef.current)
903
- mouseRef.current.isHover = true;
904
- if (mouseInCallbackRef.current)
905
- mouseInCallbackRef.current(ctx, e);
861
+ if (mouseRef.current) mouseRef.current.isHover = true;
862
+ if (mouseInCallbackRef.current) mouseInCallbackRef.current(ctx, e);
906
863
  };
907
864
  const handleMouseLeave = (e) => {
908
- if (mouseRef.current)
909
- mouseRef.current.isHover = false;
910
- if (mouseOutCallbackRef.current)
911
- mouseOutCallbackRef.current(ctx, e);
865
+ if (mouseRef.current) mouseRef.current.isHover = false;
866
+ if (mouseOutCallbackRef.current) mouseOutCallbackRef.current(ctx, e);
912
867
  };
913
868
  const handleClick = (e) => {
914
- if (clickCallbackRef.current)
915
- clickCallbackRef.current(ctx, e);
869
+ if (clickCallbackRef.current) clickCallbackRef.current(ctx, e);
916
870
  };
917
871
  canvas.addEventListener("mousemove", updateMousePosition);
918
872
  canvas.addEventListener("mousedown", handleMouseDown);
@@ -944,14 +898,12 @@ function useKlint() {
944
898
  }
945
899
  const scrollCallbackRef = useRef2(null);
946
900
  useEffect2(() => {
947
- if (!contextRef.current?.canvas)
948
- return;
901
+ if (!contextRef.current?.canvas) return;
949
902
  const canvas = contextRef.current.canvas;
950
903
  const ctx = contextRef.current;
951
904
  const handleScroll = (e) => {
952
905
  e.preventDefault();
953
- if (!scrollRef.current)
954
- return;
906
+ if (!scrollRef.current) return;
955
907
  const currentTime = performance.now();
956
908
  const deltaTime = currentTime - scrollRef.current.lastTime;
957
909
  scrollRef.current.distance += e.deltaY;
@@ -977,20 +929,16 @@ function useKlint() {
977
929
  const focusCallbackRef = useRef2(null);
978
930
  const visibilityChangeCallbackRef = useRef2(null);
979
931
  useEffect2(() => {
980
- if (!contextRef.current)
981
- return;
932
+ if (!contextRef.current) return;
982
933
  const ctx = contextRef.current;
983
934
  const handleResize = () => {
984
- if (resizeCallbackRef.current)
985
- resizeCallbackRef.current(ctx);
935
+ if (resizeCallbackRef.current) resizeCallbackRef.current(ctx);
986
936
  };
987
937
  const handleBlur = () => {
988
- if (blurCallbackRef.current)
989
- blurCallbackRef.current(ctx);
938
+ if (blurCallbackRef.current) blurCallbackRef.current(ctx);
990
939
  };
991
940
  const handleFocus = () => {
992
- if (focusCallbackRef.current)
993
- focusCallbackRef.current(ctx);
941
+ if (focusCallbackRef.current) focusCallbackRef.current(ctx);
994
942
  };
995
943
  const handleVisibilityChange = () => {
996
944
  const isVisible = document.visibilityState === "visible";
@@ -1055,8 +1003,7 @@ function useKlint() {
1055
1003
  alpha: options.alpha ?? true,
1056
1004
  willReadFrequently: options.willreadfrequently ?? true
1057
1005
  });
1058
- if (!ctx)
1059
- throw new Error("Failed to get canvas context");
1006
+ if (!ctx) throw new Error("Failed to get canvas context");
1060
1007
  contextRef.current = buildKlintContext(ctx, options);
1061
1008
  }
1062
1009
  return contextRef.current;
@@ -1064,8 +1011,7 @@ function useKlint() {
1064
1011
  []
1065
1012
  );
1066
1013
  const togglePlay = useCallback2((playing) => {
1067
- if (!contextRef.current)
1068
- return;
1014
+ if (!contextRef.current) return;
1069
1015
  if (playing !== void 0) {
1070
1016
  contextRef.current.__isPlaying = playing;
1071
1017
  } else {
@@ -18,8 +18,8 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/plugins/index.tsx
21
- var plugins_exports = {};
22
- __export(plugins_exports, {
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
23
  Color: () => Color_default,
24
24
  Easing: () => Easing_default,
25
25
  SVGfont: () => SVGfont_default,
@@ -29,7 +29,7 @@ __export(plugins_exports, {
29
29
  Time: () => Time_default,
30
30
  Vector: () => Vector_default
31
31
  });
32
- module.exports = __toCommonJS(plugins_exports);
32
+ module.exports = __toCommonJS(index_exports);
33
33
 
34
34
  // src/plugins/Color.tsx
35
35
  var Color = class {
@@ -419,8 +419,7 @@ var Easing = class {
419
419
  const m = val - 1;
420
420
  const t = val * 2;
421
421
  const k = 1.70158 * 1.525;
422
- if (val < 0.5)
423
- return val * t * (t * (k + 1) - k);
422
+ if (val < 0.5) return val * t * (t * (k + 1) - k);
424
423
  return 1 + 2 * m * m * (2 * m * (k + 1) + k);
425
424
  };
426
425
  this.bounceOut = (val) => {
@@ -450,8 +449,7 @@ var Easing = class {
450
449
  };
451
450
  this.bounceInOut = (val) => {
452
451
  const t = val * 2;
453
- if (t < 1)
454
- return 0.5 - 0.5 * this.bounceOut(1 - t);
452
+ if (t < 1) return 0.5 - 0.5 * this.bounceOut(1 - t);
455
453
  return 0.5 + 0.5 * this.bounceOut(t - 1);
456
454
  };
457
455
  this.elasticIn = (val) => {
@@ -464,8 +462,7 @@ var Easing = class {
464
462
  this.elasticInOut = (val) => {
465
463
  const s = 2 * val - 1;
466
464
  const k = (80 * s - 9) * Math.PI / 18;
467
- if (s < 0)
468
- return -0.5 * Math.pow(2, 10 * s) * Math.sin(k);
465
+ if (s < 0) return -0.5 * Math.pow(2, 10 * s) * Math.sin(k);
469
466
  return 1 + 0.5 * Math.pow(2, -10 * s) * Math.sin(k);
470
467
  };
471
468
  this.smoothstep = (val, x0 = 0, x1 = 1) => {
@@ -596,8 +593,7 @@ var SVGfont = class {
596
593
  const glyph = this.glyphs.get(char);
597
594
  if (glyph) {
598
595
  totalWidth += glyph.horizAdvX;
599
- if (i < chars.length - 1)
600
- totalWidth += letterSpacing;
596
+ if (i < chars.length - 1) totalWidth += letterSpacing;
601
597
  }
602
598
  });
603
599
  let currentX = 0;
@@ -622,8 +618,7 @@ var SVGfont = class {
622
618
  }
623
619
  for (const char of chars) {
624
620
  const glyph = this.glyphs.get(char);
625
- if (!glyph)
626
- continue;
621
+ if (!glyph) continue;
627
622
  if (!glyph.d) {
628
623
  currentX += glyph.horizAdvX + letterSpacing;
629
624
  continue;
@@ -632,8 +627,7 @@ var SVGfont = class {
632
627
  const subpaths = glyph.d.split(/(?=[Mm])/);
633
628
  const glyphContours = [];
634
629
  for (const subpath of subpaths) {
635
- if (!subpath.trim())
636
- continue;
630
+ if (!subpath.trim()) continue;
637
631
  path.setAttribute("d", subpath);
638
632
  const pathLength = path.getTotalLength();
639
633
  const numPoints = Math.max(10, Math.floor(pathLength * factor));
@@ -738,7 +732,7 @@ var State = class {
738
732
  var State_default = State;
739
733
 
740
734
  // src/plugins/Vector.tsx
741
- var Vector = class {
735
+ var Vector = class _Vector {
742
736
  /**
743
737
  * Creates a new Vector
744
738
  * @param x - X-coordinate (default: 0)
@@ -845,7 +839,7 @@ var Vector = class {
845
839
  * @returns A new Vector with the same coordinates
846
840
  */
847
841
  copy() {
848
- return new Vector(this.x, this.y);
842
+ return new _Vector(this.x, this.y);
849
843
  }
850
844
  /**
851
845
  * Normalizes this vector (sets its magnitude to 1)
@@ -876,7 +870,7 @@ var Vector = class {
876
870
  static fromAngle(center, a, r) {
877
871
  const x = Math.cos(a) * r + center.x;
878
872
  const y = Math.sin(a) * r + center.y;
879
- return new Vector(x, y);
873
+ return new _Vector(x, y);
880
874
  }
881
875
  };
882
876
  var Vector_default = Vector;