|
5 | 5 |
|
6 | 6 | <script> |
7 | 7 | import { createEventDispatcher } from "svelte"; |
8 | | - import extreme_ironing from '$lib/assets/imageData/extreme_ironing.jpg'; |
9 | | - import waterview from '$lib/assets/imageData/waterview.jpg'; |
| 8 | + import extreme_ironing from "$lib/assets/imageData/extreme_ironing.png"; |
| 9 | + import waterview from "$lib/assets/imageData/waterview.png"; |
10 | 10 | import { base64ImageStore } from "$lib/shared/stores/common/Store"; |
11 | 11 |
|
12 | | - let dispatch = createEventDispatcher(); |
| 12 | + let dispatch = createEventDispatcher(); |
13 | 13 |
|
14 | | - let images = [ |
15 | | - { |
16 | | - id: 1, |
17 | | - alt: 'Waterview', |
18 | | - imgurl: waterview, |
19 | | - prompt: 'What are the things I should be cautious about when I visit here?' |
20 | | - }, |
21 | | - { |
22 | | - id: 0, |
23 | | - alt: 'Extreme Ironing', |
24 | | - imgurl: extreme_ironing, |
25 | | - prompt: 'What is unusual about this image?' |
26 | | - } |
27 | | - ]; |
| 14 | + let images = [ |
| 15 | + { |
| 16 | + id: 1, |
| 17 | + alt: "Waterview", |
| 18 | + imgurl: waterview, |
| 19 | + prompt: |
| 20 | + "What are the things I should be cautious about when I visit here?", |
| 21 | + }, |
| 22 | + { |
| 23 | + id: 0, |
| 24 | + alt: "Extreme Ironing", |
| 25 | + imgurl: extreme_ironing, |
| 26 | + prompt: "What is unusual about this image?", |
| 27 | + }, |
| 28 | + ]; |
28 | 29 |
|
29 | | - let currentIndex = 0; |
| 30 | + let currentIndex = 0; |
30 | 31 |
|
31 | | - function nextImage() { |
32 | | - currentIndex = (currentIndex + 1) % images.length; |
33 | | - } |
| 32 | + function nextImage() { |
| 33 | + currentIndex = (currentIndex + 1) % images.length; |
| 34 | + } |
34 | 35 |
|
35 | | - function prevImage() { |
36 | | - currentIndex = (currentIndex - 1 + images.length) % images.length; |
37 | | - } |
| 36 | + function prevImage() { |
| 37 | + currentIndex = (currentIndex - 1 + images.length) % images.length; |
| 38 | + } |
38 | 39 |
|
| 40 | + async function handleImageClick() { |
| 41 | + const imgUrl = images[currentIndex].imgurl; |
39 | 42 |
|
40 | | - async function handleImageClick() { |
41 | | - const imgUrl = images[currentIndex].imgurl; |
42 | | - const base64Data = await convertImageToBase64(imgUrl); |
43 | | - const currentPrompt = images[currentIndex].prompt; |
44 | | - dispatch("imagePrompt", { content: currentPrompt }); |
45 | | - base64ImageStore.set(base64Data); |
46 | | - } |
| 43 | + const base64Data = await convertImageToBase64(imgUrl); |
47 | 44 |
|
48 | | - async function convertImageToBase64(url) { |
49 | | - const response = await fetch(url); |
50 | | - const blob = await response.blob(); |
51 | | - return new Promise((resolve, reject) => { |
52 | | - const reader = new FileReader(); |
53 | | - reader.onloadend = () => resolve(reader.result); |
54 | | - reader.onerror = reject; |
55 | | - reader.readAsDataURL(blob); |
56 | | - }); |
57 | | - } |
| 45 | + base64ImageStore.set(base64Data); |
| 46 | +
|
| 47 | + const currentPrompt = images[currentIndex].prompt; |
| 48 | + dispatch("imagePrompt", { content: currentPrompt }); |
| 49 | + } |
| 50 | +
|
| 51 | + async function convertImageToBase64(url) { |
| 52 | + const response = await fetch(url); |
| 53 | + const blob = await response.blob(); |
| 54 | + return new Promise((resolve, reject) => { |
| 55 | + const reader = new FileReader(); |
| 56 | + reader.onloadend = () => resolve(reader.result); |
| 57 | + reader.onerror = reject; |
| 58 | + reader.readAsDataURL(blob); |
| 59 | + }); |
| 60 | + } |
58 | 61 | </script> |
59 | 62 |
|
60 | | -<div class="flex w-full flex-col gap-3 rounded-xl bg-white p-5 my-2"> |
61 | | - <p>Example</p> |
62 | | - <div class="relative w-full max-w-4xl mx-auto"> |
63 | | - <button |
64 | | - class="absolute left-0 top-1/2 transform -translate-y-1/2 z-10 w-8 h-8 rounded-full sm:w-10 sm:h-10 bg-white/30 dark:bg-gray-800/30 group-hover:bg-white/50 dark:group-hover:bg-gray-800/60 group-focus:ring-4 group-focus:ring-white dark:group-focus:ring-gray-800/70 group-focus:outline-none" |
65 | | - on:click={prevImage} |
66 | | - aria-label="Previous image" |
67 | | - > |
68 | | - ❮ |
69 | | - </button> |
| 63 | +<div class="my-2 flex w-full flex-col gap-3 rounded-xl bg-white p-5"> |
| 64 | + <p>Example</p> |
| 65 | + <div class="relative mx-auto w-full max-w-4xl"> |
| 66 | + <button |
| 67 | + class="absolute left-0 top-1/2 z-10 h-8 w-8 -translate-y-1/2 transform rounded-full bg-white/30 group-hover:bg-white/50 group-focus:outline-none group-focus:ring-4 group-focus:ring-white dark:bg-gray-800/30 dark:group-hover:bg-gray-800/60 dark:group-focus:ring-gray-800/70 sm:h-10 sm:w-10" |
| 68 | + on:click={prevImage} |
| 69 | + aria-label="Previous image" |
| 70 | + > |
| 71 | + ❮ |
| 72 | + </button> |
70 | 73 |
|
71 | | - <div class="relative"> |
72 | | - <img |
73 | | - src={images[currentIndex].imgurl} |
74 | | - alt={images[currentIndex].alt} |
75 | | - class="carousel-image w-full h-auto cursor-pointer" |
76 | | - on:click={handleImageClick} |
77 | | - /> |
78 | | - <div class="absolute bottom-0 left-0 bg-opacity-55 bg-black text-white p-3 w-full"> |
79 | | - <p>{images[currentIndex].prompt}</p> |
80 | | - </div> |
81 | | - </div> |
| 74 | + <div class="relative"> |
| 75 | + <img |
| 76 | + src={images[currentIndex].imgurl} |
| 77 | + alt={images[currentIndex].alt} |
| 78 | + class="carousel-image h-auto w-full cursor-pointer" |
| 79 | + on:click={handleImageClick} |
| 80 | + /> |
| 81 | + <div |
| 82 | + class="absolute bottom-0 left-0 w-full bg-black bg-opacity-55 p-3 text-white" |
| 83 | + > |
| 84 | + <p>{images[currentIndex].prompt}</p> |
| 85 | + </div> |
| 86 | + </div> |
82 | 87 |
|
83 | | - <button |
84 | | - class="absolute right-0 top-1/2 transform -translate-y-1/2 z-10 w-8 h-8 rounded-full sm:w-10 sm:h-10 bg-white/30 dark:bg-gray-800/30 group-hover:bg-white/50 dark:group-hover:bg-gray-800/60 group-focus:ring-4 group-focus:ring-white dark:group-focus:ring-gray-800/70 group-focus:outline-none" |
85 | | - on:click={nextImage} |
86 | | - aria-label="Next image" |
87 | | - > |
88 | | - ❯ |
89 | | - </button> |
90 | | - </div> |
| 88 | + <button |
| 89 | + class="absolute right-0 top-1/2 z-10 h-8 w-8 -translate-y-1/2 transform rounded-full bg-white/30 group-hover:bg-white/50 group-focus:outline-none group-focus:ring-4 group-focus:ring-white dark:bg-gray-800/30 dark:group-hover:bg-gray-800/60 dark:group-focus:ring-gray-800/70 sm:h-10 sm:w-10" |
| 90 | + on:click={nextImage} |
| 91 | + aria-label="Next image" |
| 92 | + > |
| 93 | + ❯ |
| 94 | + </button> |
| 95 | + </div> |
91 | 96 | </div> |
92 | 97 |
|
93 | 98 | <style> |
94 | | - .relative img { |
95 | | - object-fit: cover; |
96 | | - } |
| 99 | + .relative img { |
| 100 | + object-fit: cover; |
| 101 | + } |
97 | 102 | </style> |
0 commit comments