ywana-core8 0.2.15 → 0.2.17

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.umd.js CHANGED
@@ -22683,24 +22683,34 @@ const rows = [
22683
22683
  };
22684
22684
  const handleMouseUp = () => setDragging(false);
22685
22685
  const draw = () => {
22686
- if (canvasRef.current) {
22687
- const { width, height } = canvasRef.current;
22688
- const context = canvasRef.current.getContext("2d");
22689
- canvasRef.current.width = width;
22690
- canvasRef.current.height = height;
22686
+ if (!canvasRef.current || !background.complete || !background.naturalWidth) {
22687
+ return;
22688
+ }
22689
+ try {
22690
+ const canvas = canvasRef.current;
22691
+ const { width, height } = canvas;
22692
+ const context = canvas.getContext("2d");
22693
+ canvas.width = width;
22694
+ canvas.height = height;
22695
+ context.save();
22691
22696
  context.translate(-offset2.x, -offset2.y);
22692
22697
  context.scale(zoom, zoom);
22693
22698
  context.clearRect(0, 0, width, height);
22694
- const x = (context.canvas.width / zoom - background.width) / 2;
22695
- const y = (context.canvas.height / zoom - background.height) / 2;
22699
+ const x = (canvas.width / zoom - background.width) / 2;
22700
+ const y = (canvas.height / zoom - background.height) / 2;
22696
22701
  context.drawImage(background, x, y);
22702
+ context.restore();
22703
+ } catch (error) {
22704
+ console.warn("Error drawing image:", error);
22697
22705
  }
22698
22706
  };
22699
22707
  React.useEffect(() => {
22708
+ const container = containerRef.current;
22709
+ if (!container) return;
22700
22710
  observer.current = new ResizeObserver((entries) => {
22701
22711
  entries.forEach(({ target }) => {
22702
22712
  const { width, height } = background;
22703
- if (target.clientWidth < width) {
22713
+ if (target.clientWidth < width && canvasRef.current) {
22704
22714
  const scale = target.clientWidth / width;
22705
22715
  canvasRef.current.width = width * scale;
22706
22716
  canvasRef.current.height = height * scale;
@@ -22708,23 +22718,37 @@ const rows = [
22708
22718
  }
22709
22719
  });
22710
22720
  });
22711
- observer.current.observe(containerRef.current);
22712
- return () => observer.current.unobserve(containerRef.current);
22721
+ observer.current.observe(container);
22722
+ return () => {
22723
+ if (observer.current && container) {
22724
+ observer.current.unobserve(container);
22725
+ }
22726
+ };
22713
22727
  }, []);
22714
22728
  React.useEffect(() => {
22729
+ if (!image) return;
22715
22730
  background.src = image;
22716
- if (canvasRef.current) {
22717
- background.onload = () => {
22731
+ const handleImageLoad = () => {
22732
+ if (canvasRef.current) {
22718
22733
  const { width, height } = background;
22719
22734
  canvasRef.current.width = width;
22720
22735
  canvasRef.current.height = height;
22721
22736
  canvasRef.current.getContext("2d").drawImage(background, 0, 0);
22722
- };
22723
- }
22724
- }, [background]);
22737
+ }
22738
+ };
22739
+ const handleImageError = () => {
22740
+ console.warn("Failed to load image:", image);
22741
+ };
22742
+ background.onload = handleImageLoad;
22743
+ background.onerror = handleImageError;
22744
+ return () => {
22745
+ background.onload = null;
22746
+ background.onerror = null;
22747
+ };
22748
+ }, [image, background]);
22725
22749
  React.useEffect(() => {
22726
22750
  draw();
22727
- }, [zoom, offset2]);
22751
+ }, [zoom, offset2, background.src]);
22728
22752
  return /* @__PURE__ */ React.createElement("div", { className: "image-viewer", ref: containerRef }, /* @__PURE__ */ React.createElement(
22729
22753
  "canvas",
22730
22754
  {