File size: 9,469 Bytes
be997d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Main Image Magnifier Class
class ImageMagnifier {
    constructor(containerId, previewId, options = {}) {
        this.container = document.getElementById(containerId);
        this.preview = document.getElementById(previewId);
        this.img = this.container.querySelector('img');
        this.lens = document.getElementById('lens');
        
        this.zoom = options.zoom || 2;
        this.lensSize = options.lensSize || 150;
        
        this.init();
    }

    init() {
        if (this.img.complete) {
            this.setupMagnifier();
        } else {
            this.img.addEventListener('load', () => this.setupMagnifier());
        }
    }

    setupMagnifier() {
        // Update preview background
        this.updatePreviewBackground();

        // Mouse events
        this.container.addEventListener('mouseenter', () => this.showMagnifier());
        this.container.addEventListener('mouseleave', () => this.hideMagnifier());
        this.container.addEventListener('mousemove', (e) => this.moveMagnifier(e));

        // Touch events for mobile
        this.container.addEventListener('touchstart', (e) => {
            e.preventDefault();
            this.showMagnifier();
            this.moveMagnifier(e.touches[0]);
        });
        this.container.addEventListener('touchmove', (e) => {
            e.preventDefault();
            this.moveMagnifier(e.touches[0]);
        });
        this.container.addEventListener('touchend', () => this.hideMagnifier());
    }

    updatePreviewBackground() {
        this.preview.style.backgroundImage = `url('${this.img.src}')`;
        this.preview.style.backgroundSize = `${this.img.width * this.zoom}px ${this.img.height * this.zoom}px`;
    }

    showMagnifier() {
        this.lens.style.opacity = '1';
        this.lens.classList.add('active');
        this.preview.classList.add('active');
        
        // Clear the placeholder content
        const placeholder = this.preview.querySelector('.absolute');
        if (placeholder) placeholder.style.display = 'none';
    }

    hideMagnifier() {
        this.lens.style.opacity = '0';
        this.lens.classList.remove('active');
        this.preview.classList.remove('active');
    }

    moveMagnifier(e) {
        const rect = this.img.getBoundingClientRect();
        const x = e.clientX - rect.left;
        const y = e.clientY - rect.top;

        let lensX = x - (this.lensSize / 2);
        let lensY = y - (this.lensSize / 2);

        // Boundary checks
        lensX = Math.max(0, Math.min(lensX, this.img.width - this.lensSize));
        lensY = Math.max(0, Math.min(lensY, this.img.height - this.lensSize));

        // Position lens
        this.lens.style.left = `${lensX}px`;
        this.lens.style.top = `${lensY}px`;
        this.lens.style.width = `${this.lensSize}px`;
        this.lens.style.height = `${this.lensSize}px`;

        // Calculate background position for preview
        const bgX = (lensX / this.img.width) * 100;
        const bgY = (lensY / this.img.height) * 100;
        
        this.preview.style.backgroundPosition = `${bgX}% ${bgY}%`;
    }

    updateZoom(newZoom) {
        this.zoom = newZoom;
        this.updatePreviewBackground();
    }

    updateLensSize(newSize) {
        this.lensSize = newSize;
    }

    changeImage(newSrc) {
        this.img.src = newSrc;
        this.img.onload = () => {
            this.updatePreviewBackground();
        };
    }
}

// Inline Magnifier
class InlineMagnifier {
    constructor() {
        this.container = document.getElementById('inline-magnifier');
        this.img = this.container.querySelector('img');
        this.lens = document.getElementById('inline-lens');
        this.zoom = 2;
        
        this.init();
    }

    init() {
        this.lens.style.backgroundImage = `url('${this.img.src}')`;
        this.lens.style.backgroundSize = `${this.img.width * this.zoom}px ${this.img.height * this.zoom}px`;

        this.container.addEventListener('mouseenter', () => this.lens.style.opacity = '1');
        this.container.addEventListener('mouseleave', () => this.lens.style.opacity = '0');
        this.container.addEventListener('mousemove', (e) => this.moveLens(e));
    }

    moveLens(e) {
        const rect = this.img.getBoundingClientRect();
        const x = e.clientX - rect.left;
        const y = e.clientY - rect.top;

        const lensWidth = 200;
        const lensHeight = 200;
        
        let lensX = x - (lensWidth / 2);
        let lensY = y - (lensHeight / 2);

        lensX = Math.max(0, Math.min(lensX, this.img.width - lensWidth));
        lensY = Math.max(0, Math.min(lensY, this.img.height - lensHeight));

        this.lens.style.left = `${lensX}px`;
        this.lens.style.top = `${lensY}px`;

        const bgX = (lensX / this.img.width) * 100;
        const bgY = (lensY / this.img.height) * 100;
        
        this.lens.style.backgroundPosition = `${bgX}% ${bgY}%`;
    }
}

// Corner Magnifier
class CornerMagnifier {
    constructor() {
        this.container = document.getElementById('corner-magnifier');
        this.img = this.container.querySelector('img');
        this.zoom = document.getElementById('corner-zoom');
        this.content = document.getElementById('corner-zoom-content');
        this.zoomLevel = 3;
        
        this.init();
    }

    init() {
        this.content.style.backgroundImage = `url('${this.img.src}')`;
        this.content.style.backgroundSize = `${this.img.width * this.zoomLevel}px ${this.img.height * this.zoomLevel}px`;

        this.container.addEventListener('mouseenter', () => this.zoom.style.opacity = '1');
        this.container.addEventListener('mouseleave', () => this.zoom.style.opacity = '0');
        this.container.addEventListener('mousemove', (e) => this.updateZoom(e));
    }

    updateZoom(e) {
        const rect = this.img.getBoundingClientRect();
        const x = e.clientX - rect.left;
        const y = e.clientY - rect.top;

        const bgX = (x / this.img.width) * 100;
        const bgY = (y / this.img.height) * 100;
        
        this.content.style.backgroundPosition = `${bgX}% ${bgY}%`;
    }
}

// Glass Magnifier
class GlassMagnifier {
    constructor() {
        this.container = document.getElementById('glass-magnifier');
        this.img = this.container.querySelector('img');
        this.lens = document.getElementById('glass-lens');
        this.zoom = 2.5;
        
        this.init();
    }

    init() {
        this.lens.style.backgroundImage = `url('${this.img.src}')`;
        this.lens.style.backgroundSize = `${this.img.width * this.zoom}px ${this.img.height * this.zoom}px`;

        this.container.addEventListener('mouseenter', () => this.lens.style.opacity = '1');
        this.container.addEventListener('mouseleave', () => this.lens.style.opacity = '0');
        this.container.addEventListener('mousemove', (e) => this.moveLens(e));
    }

    moveLens(e) {
        const rect = this.img.getBoundingClientRect();
        const x = e.clientX - rect.left;
        const y = e.clientY - rect.top;

        const lensSize = 120;
        let lensX = x - (lensSize / 2);
        let lensY = y - (lensSize / 2);

        lensX = Math.max(0, Math.min(lensX, this.img.width - lensSize));
        lensY = Math.max(0, Math.min(lensY, this.img.height - lensSize));

        this.lens.style.left = `${lensX}px`;
        this.lens.style.top = `${lensY}px`;

        const bgX = (lensX / this.img.width) * 100;
        const bgY = (lensY / this.img.height) * 100;
        
        this.lens.style.backgroundPosition = `${bgX}% ${bgY}%`;
    }
}

// Initialize all magnifiers when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
    // Main magnifier
    const mainMagnifier = new ImageMagnifier('magnifier-container', 'zoom-preview');

    // Zoom controls
    const zoomSlider = document.getElementById('zoom-slider');
    const zoomValue = document.getElementById('zoom-value');
    const lensSizeSlider = document.getElementById('lens-size');
    const lensSizeValue = document.getElementById('lens-size-value');

    zoomSlider.addEventListener('input', (e) => {
        const value = e.target.value;
        zoomValue.textContent = `${value}x`;
        mainMagnifier.updateZoom(parseFloat(value));
    });

    lensSizeSlider.addEventListener('input', (e) => {
        const value = e.target.value;
        lensSizeValue.textContent = `${value}px`;
        mainMagnifier.updateLensSize(parseInt(value));
    });

    // Thumbnail switching
    const thumbnails = document.querySelectorAll('.thumbnail-card');
    thumbnails.forEach(thumb => {
        thumb.addEventListener('click', () => {
            // Remove active class from all thumbnails
            thumbnails.forEach(t => {
                t.classList.remove('active');
                t.classList.remove('border-purple-500');
                t.classList.add('border-gray-200');
            });
            
            // Add active class to clicked thumbnail
            thumb.classList.add('active');
            thumb.classList.remove('border-gray-200');
            thumb.classList.add('border-purple-500');
            
            // Change main image
            const imageSrc = thumb.dataset.image;
            mainMagnifier.changeImage(imageSrc);
        });
    });

    // Initialize alternative magnifiers
    new InlineMagnifier();
    new CornerMagnifier();
    new GlassMagnifier();
});