@gradio/imageslider 0.2.0
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/CHANGELOG.md +1115 -0
- package/Example.svelte +69 -0
- package/Index.svelte +206 -0
- package/LICENSE +201 -0
- package/README.md +67 -0
- package/dist/Example.svelte +68 -0
- package/dist/Example.svelte.d.ts +19 -0
- package/dist/Index.svelte +178 -0
- package/dist/Index.svelte.d.ts +153 -0
- package/dist/img_01.png +0 -0
- package/dist/img_02.png +0 -0
- package/dist/img_03.png +0 -0
- package/dist/shared/ClearImage.svelte +28 -0
- package/dist/shared/ClearImage.svelte.d.ts +18 -0
- package/dist/shared/Image.svelte +208 -0
- package/dist/shared/Image.svelte.d.ts +41 -0
- package/dist/shared/ImageEl.svelte +119 -0
- package/dist/shared/ImageEl.svelte.d.ts +258 -0
- package/dist/shared/Slider.svelte +183 -0
- package/dist/shared/Slider.svelte.d.ts +28 -0
- package/dist/shared/SliderPreview.svelte +201 -0
- package/dist/shared/SliderPreview.svelte.d.ts +32 -0
- package/dist/shared/SliderUpload.svelte +41 -0
- package/dist/shared/SliderUpload.svelte.d.ts +71 -0
- package/dist/shared/zoom.d.ts +69 -0
- package/dist/shared/zoom.js +349 -0
- package/img_01.png +0 -0
- package/img_02.png +0 -0
- package/img_03.png +0 -0
- package/package.json +47 -0
- package/shared/ClearImage.svelte +30 -0
- package/shared/Image.svelte +235 -0
- package/shared/ImageEl.svelte +165 -0
- package/shared/Slider.svelte +200 -0
- package/shared/SliderPreview.svelte +229 -0
- package/shared/SliderUpload.svelte +46 -0
- package/shared/zoom.ts +487 -0
package/Example.svelte
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
<script lang="ts">
|
2
|
+
export let value: [string, string];
|
3
|
+
export let samples_dir: string;
|
4
|
+
export let type: "gallery" | "table";
|
5
|
+
export let selected = false;
|
6
|
+
</script>
|
7
|
+
|
8
|
+
<!-- TODO: fix -->
|
9
|
+
<!-- svelte-ignore a11y-missing-attribute -->
|
10
|
+
<div
|
11
|
+
class="wrap"
|
12
|
+
class:table={type === "table"}
|
13
|
+
class:gallery={type === "gallery"}
|
14
|
+
class:selected
|
15
|
+
>
|
16
|
+
<img src={samples_dir + value[0]} />
|
17
|
+
|
18
|
+
<img src={samples_dir + value[1]} />
|
19
|
+
<span></span>
|
20
|
+
</div>
|
21
|
+
|
22
|
+
<style>
|
23
|
+
.wrap {
|
24
|
+
position: relative;
|
25
|
+
height: var(--size-64);
|
26
|
+
width: var(--size-40);
|
27
|
+
overflow: hidden;
|
28
|
+
border-radius: var(--radius-lg);
|
29
|
+
}
|
30
|
+
img {
|
31
|
+
height: var(--size-64);
|
32
|
+
width: var(--size-40);
|
33
|
+
position: absolute;
|
34
|
+
/* border-radius: var(--radius-lg); */
|
35
|
+
/* max-width: none; */
|
36
|
+
object-fit: cover;
|
37
|
+
}
|
38
|
+
|
39
|
+
.wrap.selected {
|
40
|
+
border-color: var(--color-accent);
|
41
|
+
}
|
42
|
+
.wrap img:first-child {
|
43
|
+
clip-path: inset(0 50% 0 0%);
|
44
|
+
}
|
45
|
+
|
46
|
+
.wrap img:nth-of-type(2) {
|
47
|
+
clip-path: inset(0 0 0 50%);
|
48
|
+
}
|
49
|
+
span {
|
50
|
+
position: absolute;
|
51
|
+
top: 0;
|
52
|
+
left: calc(50% - 0.75px);
|
53
|
+
height: var(--size-64);
|
54
|
+
width: 1.5px;
|
55
|
+
background: var(--border-color-primary);
|
56
|
+
}
|
57
|
+
|
58
|
+
.table {
|
59
|
+
margin: 0 auto;
|
60
|
+
border: 2px solid var(--border-color-primary);
|
61
|
+
border-radius: var(--radius-lg);
|
62
|
+
}
|
63
|
+
|
64
|
+
.gallery {
|
65
|
+
border: 2px solid var(--border-color-primary);
|
66
|
+
/* max-height: var(--size-20); */
|
67
|
+
object-fit: cover;
|
68
|
+
}
|
69
|
+
</style>
|
package/Index.svelte
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
<svelte:options accessors={true} />
|
2
|
+
|
3
|
+
<script lang="ts">
|
4
|
+
import type { Gradio, SelectData, ValueData } from "@gradio/utils";
|
5
|
+
import StaticImage from "./shared/SliderPreview.svelte";
|
6
|
+
import ImageUploader from "./shared/SliderUpload.svelte";
|
7
|
+
import { afterUpdate } from "svelte";
|
8
|
+
|
9
|
+
import { Block, Empty, UploadText } from "@gradio/atoms";
|
10
|
+
import { Image } from "@gradio/icons";
|
11
|
+
import { StatusTracker } from "@gradio/statustracker";
|
12
|
+
import { type FileData } from "@gradio/client";
|
13
|
+
import type { LoadingStatus } from "@gradio/statustracker";
|
14
|
+
|
15
|
+
type sources = "upload" | "webcam" | "clipboard" | null;
|
16
|
+
|
17
|
+
export let value_is_output = false;
|
18
|
+
export let elem_id = "";
|
19
|
+
export let elem_classes: string[] = [];
|
20
|
+
export let visible = true;
|
21
|
+
export let value: [FileData | null, FileData | null] = [null, null];
|
22
|
+
let old_value: [FileData | null, FileData | null] = [null, null];
|
23
|
+
export let label: string;
|
24
|
+
export let show_label: boolean;
|
25
|
+
export let show_download_button: boolean;
|
26
|
+
export let root: string;
|
27
|
+
export let height: number | undefined;
|
28
|
+
export let width: number | undefined;
|
29
|
+
export let container = true;
|
30
|
+
export let scale: number | null = null;
|
31
|
+
export let min_width: number | undefined = undefined;
|
32
|
+
export let loading_status: LoadingStatus;
|
33
|
+
export let interactive: boolean;
|
34
|
+
export let placeholder: string | undefined = undefined;
|
35
|
+
export let show_fullscreen_button: boolean;
|
36
|
+
export let input_ready: boolean;
|
37
|
+
export let slider_position: number;
|
38
|
+
export let upload_count = 1;
|
39
|
+
export let slider_color = "var(--border-color-primary)";
|
40
|
+
export let max_height: number;
|
41
|
+
let uploading = false;
|
42
|
+
|
43
|
+
$: normalised_slider_position =
|
44
|
+
Math.max(0, Math.min(100, slider_position)) / 100;
|
45
|
+
|
46
|
+
$: input_ready = !uploading;
|
47
|
+
|
48
|
+
export let gradio: Gradio<{
|
49
|
+
input: never;
|
50
|
+
change: never;
|
51
|
+
error: string;
|
52
|
+
edit: never;
|
53
|
+
stream: ValueData;
|
54
|
+
drag: never;
|
55
|
+
upload: never;
|
56
|
+
clear: never;
|
57
|
+
select: SelectData;
|
58
|
+
share: ShareData;
|
59
|
+
clear_status: LoadingStatus;
|
60
|
+
close_stream: string;
|
61
|
+
}>;
|
62
|
+
|
63
|
+
$: {
|
64
|
+
if (JSON.stringify(value) !== JSON.stringify(old_value)) {
|
65
|
+
old_value = value;
|
66
|
+
gradio.dispatch("change");
|
67
|
+
if (!value_is_output) {
|
68
|
+
gradio.dispatch("input");
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
afterUpdate(() => {
|
74
|
+
value_is_output = false;
|
75
|
+
});
|
76
|
+
|
77
|
+
let dragging: boolean;
|
78
|
+
let active_source: sources = null;
|
79
|
+
let upload_component: ImageUploader;
|
80
|
+
|
81
|
+
const handle_drag_event = (event: Event): void => {
|
82
|
+
const drag_event = event as DragEvent;
|
83
|
+
drag_event.preventDefault();
|
84
|
+
drag_event.stopPropagation();
|
85
|
+
if (drag_event.type === "dragenter" || drag_event.type === "dragover") {
|
86
|
+
dragging = true;
|
87
|
+
} else if (drag_event.type === "dragleave") {
|
88
|
+
dragging = false;
|
89
|
+
}
|
90
|
+
};
|
91
|
+
|
92
|
+
const handle_drop = (event: Event): void => {
|
93
|
+
if (interactive) {
|
94
|
+
const drop_event = event as DragEvent;
|
95
|
+
drop_event.preventDefault();
|
96
|
+
drop_event.stopPropagation();
|
97
|
+
dragging = false;
|
98
|
+
|
99
|
+
if (upload_component) {
|
100
|
+
upload_component.loadFilesFromDrop(drop_event);
|
101
|
+
}
|
102
|
+
}
|
103
|
+
};
|
104
|
+
</script>
|
105
|
+
|
106
|
+
{#if !interactive || (value?.[1] && value?.[0])}
|
107
|
+
<Block
|
108
|
+
{visible}
|
109
|
+
variant={"solid"}
|
110
|
+
border_mode={dragging ? "focus" : "base"}
|
111
|
+
padding={false}
|
112
|
+
{elem_id}
|
113
|
+
{elem_classes}
|
114
|
+
height={height || undefined}
|
115
|
+
{width}
|
116
|
+
allow_overflow={false}
|
117
|
+
{container}
|
118
|
+
{scale}
|
119
|
+
{min_width}
|
120
|
+
>
|
121
|
+
<StatusTracker
|
122
|
+
autoscroll={gradio.autoscroll}
|
123
|
+
i18n={gradio.i18n}
|
124
|
+
{...loading_status}
|
125
|
+
/>
|
126
|
+
<StaticImage
|
127
|
+
on:select={({ detail }) => gradio.dispatch("select", detail)}
|
128
|
+
on:share={({ detail }) => gradio.dispatch("share", detail)}
|
129
|
+
on:error={({ detail }) => gradio.dispatch("error", detail)}
|
130
|
+
on:clear={() => gradio.dispatch("clear")}
|
131
|
+
{interactive}
|
132
|
+
bind:value
|
133
|
+
{label}
|
134
|
+
{show_label}
|
135
|
+
{show_download_button}
|
136
|
+
i18n={gradio.i18n}
|
137
|
+
{show_fullscreen_button}
|
138
|
+
position={normalised_slider_position}
|
139
|
+
{slider_color}
|
140
|
+
{max_height}
|
141
|
+
/>
|
142
|
+
</Block>
|
143
|
+
{:else}
|
144
|
+
<Block
|
145
|
+
{visible}
|
146
|
+
variant={value === null ? "dashed" : "solid"}
|
147
|
+
border_mode={dragging ? "focus" : "base"}
|
148
|
+
padding={false}
|
149
|
+
{elem_id}
|
150
|
+
{elem_classes}
|
151
|
+
height={height || undefined}
|
152
|
+
{width}
|
153
|
+
allow_overflow={false}
|
154
|
+
{container}
|
155
|
+
{scale}
|
156
|
+
{min_width}
|
157
|
+
on:dragenter={handle_drag_event}
|
158
|
+
on:dragleave={handle_drag_event}
|
159
|
+
on:dragover={handle_drag_event}
|
160
|
+
on:drop={handle_drop}
|
161
|
+
>
|
162
|
+
<StatusTracker
|
163
|
+
autoscroll={gradio.autoscroll}
|
164
|
+
i18n={gradio.i18n}
|
165
|
+
{...loading_status}
|
166
|
+
on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
|
167
|
+
/>
|
168
|
+
|
169
|
+
<ImageUploader
|
170
|
+
bind:this={upload_component}
|
171
|
+
bind:value
|
172
|
+
bind:dragging
|
173
|
+
{root}
|
174
|
+
on:edit={() => gradio.dispatch("edit")}
|
175
|
+
on:clear={() => {
|
176
|
+
gradio.dispatch("clear");
|
177
|
+
}}
|
178
|
+
on:drag={({ detail }) => (dragging = detail)}
|
179
|
+
on:upload={() => gradio.dispatch("upload")}
|
180
|
+
on:error={({ detail }) => {
|
181
|
+
loading_status = loading_status || {};
|
182
|
+
loading_status.status = "error";
|
183
|
+
gradio.dispatch("error", detail);
|
184
|
+
}}
|
185
|
+
on:close_stream={() => {
|
186
|
+
gradio.dispatch("close_stream", "stream");
|
187
|
+
}}
|
188
|
+
{label}
|
189
|
+
{show_label}
|
190
|
+
{upload_count}
|
191
|
+
max_file_size={gradio.max_file_size}
|
192
|
+
i18n={gradio.i18n}
|
193
|
+
upload={(...args) => gradio.client.upload(...args)}
|
194
|
+
stream_handler={gradio.client?.stream}
|
195
|
+
{max_height}
|
196
|
+
>
|
197
|
+
{#if active_source === "upload" || !active_source}
|
198
|
+
<UploadText i18n={gradio.i18n} type="image" {placeholder} />
|
199
|
+
{:else if active_source === "clipboard"}
|
200
|
+
<UploadText i18n={gradio.i18n} type="clipboard" mode="short" />
|
201
|
+
{:else}
|
202
|
+
<Empty unpadded_box={true} size="large"><Image /></Empty>
|
203
|
+
{/if}
|
204
|
+
</ImageUploader>
|
205
|
+
</Block>
|
206
|
+
{/if}
|
package/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# technical notes
|
2
|
+
|
3
|
+
## tldr
|
4
|
+
|
5
|
+
- We use `object-fit: contain` to show the full image at all times.
|
6
|
+
- This causes the DOM width of the image to differ from the real image width.
|
7
|
+
- Slider positions are based on the unscaled image's coordinate space.
|
8
|
+
- Zooming introduces translation and scale, which makes slider positions behave in surprising ways (including going negative).
|
9
|
+
|
10
|
+
## details
|
11
|
+
|
12
|
+
The main challenge with this component is managing image sizing. A different approach might surface other challenges, but I wanted to document this one somewhere because it’s not particularly intuitive.
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
### `object-fit: contain`
|
17
|
+
|
18
|
+
The image slider is essentially two images overlaid exactly; the “comparison” image is gradually revealed by modifying its clip-path.
|
19
|
+
|
20
|
+
We have a few key requirements:
|
21
|
+
|
22
|
+
- The whole image should be visible with no clipping.
|
23
|
+
- The DOMRect of the image should ideally match 100% of the parent’s width and height—this is crucial when zooming, as we don’t want any part of the image to get clipped.
|
24
|
+
- Both images must be identically overlaid.
|
25
|
+
|
26
|
+
We use a few standard CSS techniques to achieve this, but the key part for this discussion is `object-fit: contain`.
|
27
|
+
|
28
|
+
This is a great CSS property and ensures that the entire image is always visible which is a great baseline for the image slider. The challenge (and also the benefit) of object-fit: contain is that an image with width: 100% and this property will stretch to the full width of its parent, even if the image itself is narrower.
|
29
|
+
|
30
|
+
This is problematic because slider-progress should be relative to the unscaled image, not the container box. When someone sets `slider_position=10` that should be 10% across the image, not the container.
|
31
|
+
|
32
|
+

|
33
|
+
|
34
|
+
This is fine. We manually calculate the “real” image width and offsets (there’s no built-in API for this), and map slider position values accordingly. It works, but introduces a few quirks:
|
35
|
+
|
36
|
+
|
37
|
+
#### **zooming needs to know about the 'real' image size.**
|
38
|
+
|
39
|
+
The zoom applies constraints to prevent users from dragging the image out of view, so it needs to know the image's true start and end positions (i.e. the actual pixel bounds of the image within the container).
|
40
|
+
|
41
|
+
|
42
|
+
#### **slider positions can become 'negative' (and this is ok).**
|
43
|
+
|
44
|
+
This is confusing but bear with me.
|
45
|
+
|
46
|
+
At the default zoom level, `slider = 0` and `slider = 100` correspond to the actual edges of the image. Makes sense. But when we zoom in, we still use the same 'unscaled, actual image coordinate space'.
|
47
|
+
|
48
|
+
So now, say the slider is at 25: it still refers to 25% across the unscaled image. But visually, that might no longer line up with the expected spot in the zoomed view. It’s viewport-relative, but calculated against a base coordinate system that isn’t.
|
49
|
+
|
50
|
+
I did a picture to help conceptualise. Here we zoom (scale and translate) but the slider stays in the same position on the screen, corresponding to its 25% of image width + left offset origin.
|
51
|
+
|
52
|
+

|
53
|
+
|
54
|
+
Because of this, the 0 point might actually lie partway into the image’s visible region when zoomed. This means we can't actually compare the whole image at higher zoom levels unless we allow negative slider positions.
|
55
|
+
|
56
|
+
So that's exactly what we do.
|
57
|
+
|
58
|
+

|
59
|
+
|
60
|
+
The image slider performs a fair amount of translation and projections, nothing absurd but there is enough. We need to make sure that any time we are interacting with the slider position, our calculations can handle negative values.
|
61
|
+
|
62
|
+
We also need to dynamically clamp the slider position, based on:
|
63
|
+
|
64
|
+
- the current zoom scale
|
65
|
+
- any translation offsets
|
66
|
+
- the real image bounds (not just the DOMRect)
|
67
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
<script>export let value;
|
2
|
+
export let samples_dir;
|
3
|
+
export let type;
|
4
|
+
export let selected = false;
|
5
|
+
</script>
|
6
|
+
|
7
|
+
<!-- TODO: fix -->
|
8
|
+
<!-- svelte-ignore a11y-missing-attribute -->
|
9
|
+
<div
|
10
|
+
class="wrap"
|
11
|
+
class:table={type === "table"}
|
12
|
+
class:gallery={type === "gallery"}
|
13
|
+
class:selected
|
14
|
+
>
|
15
|
+
<img src={samples_dir + value[0]} />
|
16
|
+
|
17
|
+
<img src={samples_dir + value[1]} />
|
18
|
+
<span></span>
|
19
|
+
</div>
|
20
|
+
|
21
|
+
<style>
|
22
|
+
.wrap {
|
23
|
+
position: relative;
|
24
|
+
height: var(--size-64);
|
25
|
+
width: var(--size-40);
|
26
|
+
overflow: hidden;
|
27
|
+
border-radius: var(--radius-lg);
|
28
|
+
}
|
29
|
+
img {
|
30
|
+
height: var(--size-64);
|
31
|
+
width: var(--size-40);
|
32
|
+
position: absolute;
|
33
|
+
/* border-radius: var(--radius-lg); */
|
34
|
+
/* max-width: none; */
|
35
|
+
object-fit: cover;
|
36
|
+
}
|
37
|
+
|
38
|
+
.wrap.selected {
|
39
|
+
border-color: var(--color-accent);
|
40
|
+
}
|
41
|
+
.wrap img:first-child {
|
42
|
+
clip-path: inset(0 50% 0 0%);
|
43
|
+
}
|
44
|
+
|
45
|
+
.wrap img:nth-of-type(2) {
|
46
|
+
clip-path: inset(0 0 0 50%);
|
47
|
+
}
|
48
|
+
span {
|
49
|
+
position: absolute;
|
50
|
+
top: 0;
|
51
|
+
left: calc(50% - 0.75px);
|
52
|
+
height: var(--size-64);
|
53
|
+
width: 1.5px;
|
54
|
+
background: var(--border-color-primary);
|
55
|
+
}
|
56
|
+
|
57
|
+
.table {
|
58
|
+
margin: 0 auto;
|
59
|
+
border: 2px solid var(--border-color-primary);
|
60
|
+
border-radius: var(--radius-lg);
|
61
|
+
}
|
62
|
+
|
63
|
+
.gallery {
|
64
|
+
border: 2px solid var(--border-color-primary);
|
65
|
+
/* max-height: var(--size-20); */
|
66
|
+
object-fit: cover;
|
67
|
+
}
|
68
|
+
</style>
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { SvelteComponent } from "svelte";
|
2
|
+
declare const __propDef: {
|
3
|
+
props: {
|
4
|
+
value: [string, string];
|
5
|
+
samples_dir: string;
|
6
|
+
type: "gallery" | "table";
|
7
|
+
selected?: boolean | undefined;
|
8
|
+
};
|
9
|
+
events: {
|
10
|
+
[evt: string]: CustomEvent<any>;
|
11
|
+
};
|
12
|
+
slots: {};
|
13
|
+
};
|
14
|
+
export type ExampleProps = typeof __propDef.props;
|
15
|
+
export type ExampleEvents = typeof __propDef.events;
|
16
|
+
export type ExampleSlots = typeof __propDef.slots;
|
17
|
+
export default class Example extends SvelteComponent<ExampleProps, ExampleEvents, ExampleSlots> {
|
18
|
+
}
|
19
|
+
export {};
|