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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ywana-core8",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "description": "ywana-core8",
5
5
  "homepage": "https://ywana.github.io/workspace",
6
6
  "author": "Ernesto Roldan Garcia",
@@ -71,34 +71,50 @@ export const ImageViewer = forwardRef(({ image }, ref) => {
71
71
  const handleMouseUp = () => setDragging(false);
72
72
 
73
73
  const draw = () => {
74
- if (canvasRef.current) {
75
- const { width, height } = canvasRef.current;
76
- const context = canvasRef.current.getContext("2d");
74
+ if (!canvasRef.current || !background.complete || !background.naturalWidth) {
75
+ return;
76
+ }
77
+
78
+ try {
79
+ const canvas = canvasRef.current;
80
+ const { width, height } = canvas;
81
+ const context = canvas.getContext("2d");
77
82
 
78
83
  // Set canvas dimensions
79
- canvasRef.current.width = width;
80
- canvasRef.current.height = height;
84
+ canvas.width = width;
85
+ canvas.height = height;
86
+
87
+ // Save context state
88
+ context.save();
81
89
 
82
- // Clear canvas and scale it
90
+ // Clear canvas and apply transformations
83
91
  context.translate(-offset.x, -offset.y);
84
92
  context.scale(zoom, zoom);
85
93
  context.clearRect(0, 0, width, height);
86
94
 
87
95
  // Make sure we're zooming to the center
88
- const x = (context.canvas.width / zoom - background.width) / 2;
89
- const y = (context.canvas.height / zoom - background.height) / 2;
96
+ const x = (canvas.width / zoom - background.width) / 2;
97
+ const y = (canvas.height / zoom - background.height) / 2;
90
98
 
91
99
  // Draw image
92
100
  context.drawImage(background, x, y);
101
+
102
+ // Restore context state
103
+ context.restore();
104
+ } catch (error) {
105
+ console.warn('Error drawing image:', error);
93
106
  }
94
107
  };
95
108
 
96
109
  useEffect(() => {
110
+ const container = containerRef.current;
111
+ if (!container) return;
112
+
97
113
  observer.current = new ResizeObserver((entries) => {
98
114
  entries.forEach(({ target }) => {
99
115
  const { width, height } = background;
100
116
  // If width of the container is smaller than image, scale image down
101
- if (target.clientWidth < width) {
117
+ if (target.clientWidth < width && canvasRef.current) {
102
118
  // Calculate scale
103
119
  const scale = target.clientWidth / width;
104
120
 
@@ -111,16 +127,23 @@ export const ImageViewer = forwardRef(({ image }, ref) => {
111
127
  }
112
128
  });
113
129
  });
114
- observer.current.observe(containerRef.current);
115
130
 
116
- return () => observer.current.unobserve(containerRef.current);
131
+ observer.current.observe(container);
132
+
133
+ return () => {
134
+ if (observer.current && container) {
135
+ observer.current.unobserve(container);
136
+ }
137
+ };
117
138
  }, []);
118
139
 
119
140
  useEffect(() => {
141
+ if (!image) return;
142
+
120
143
  background.src = image;
121
144
 
122
- if (canvasRef.current) {
123
- background.onload = () => {
145
+ const handleImageLoad = () => {
146
+ if (canvasRef.current) {
124
147
  // Get the image dimensions
125
148
  const { width, height } = background;
126
149
  canvasRef.current.width = width;
@@ -128,13 +151,25 @@ export const ImageViewer = forwardRef(({ image }, ref) => {
128
151
 
129
152
  // Set image as background
130
153
  canvasRef.current.getContext("2d").drawImage(background, 0, 0);
131
- };
132
- }
133
- }, [background]);
154
+ }
155
+ };
156
+
157
+ const handleImageError = () => {
158
+ console.warn('Failed to load image:', image);
159
+ };
160
+
161
+ background.onload = handleImageLoad;
162
+ background.onerror = handleImageError;
163
+
164
+ return () => {
165
+ background.onload = null;
166
+ background.onerror = null;
167
+ };
168
+ }, [image, background]);
134
169
 
135
170
  useEffect(() => {
136
171
  draw();
137
- }, [zoom, offset]);
172
+ }, [zoom, offset, background.src]);
138
173
 
139
174
  return (
140
175
  <div className="image-viewer" ref={containerRef}>