@bikariya/image-viewer 0.0.1
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/LICENSE +21 -0
- package/dist/module.d.mts +5 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +24 -0
- package/dist/runtime/image-viewer.d.vue.ts +12 -0
- package/dist/runtime/image-viewer.vue +180 -0
- package/dist/runtime/image-viewer.vue.d.ts +12 -0
- package/dist/types.d.mts +7 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present KazariEX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineNuxtModule, createResolver, addComponent } from '@nuxt/kit';
|
|
2
|
+
|
|
3
|
+
const name = "@bikariya/image-viewer";
|
|
4
|
+
const packageJson = {
|
|
5
|
+
name: name};
|
|
6
|
+
|
|
7
|
+
const module$1 = defineNuxtModule({
|
|
8
|
+
meta: {
|
|
9
|
+
name: packageJson.name,
|
|
10
|
+
configKey: "image-viewer"
|
|
11
|
+
},
|
|
12
|
+
moduleDependencies: {
|
|
13
|
+
"@bikariya/modals": {}
|
|
14
|
+
},
|
|
15
|
+
setup() {
|
|
16
|
+
const resolver = createResolver(import.meta.url);
|
|
17
|
+
addComponent({
|
|
18
|
+
name: "BikariyaImageViewer",
|
|
19
|
+
filePath: resolver.resolve("runtime/image-viewer.vue")
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export { module$1 as default };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type __VLS_Props = {
|
|
2
|
+
target: HTMLImageElement;
|
|
3
|
+
duration?: number;
|
|
4
|
+
isOpening?: boolean;
|
|
5
|
+
};
|
|
6
|
+
declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
7
|
+
close: () => any;
|
|
8
|
+
}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
|
|
9
|
+
onClose?: (() => any) | undefined;
|
|
10
|
+
}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
11
|
+
declare const _default: typeof __VLS_export;
|
|
12
|
+
export default _default;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { computed, ref, useEventListener, usePointer, useTemplateRef } from "#imports";
|
|
3
|
+
const props = defineProps({
|
|
4
|
+
target: { type: null, required: true },
|
|
5
|
+
duration: { type: Number, required: false },
|
|
6
|
+
isOpening: { type: Boolean, required: false }
|
|
7
|
+
});
|
|
8
|
+
const emit = defineEmits(["close"]);
|
|
9
|
+
const rootEl = useTemplateRef("root");
|
|
10
|
+
const rate = 0.9;
|
|
11
|
+
const options = computed(() => {
|
|
12
|
+
return {
|
|
13
|
+
duration: props.duration ?? 400,
|
|
14
|
+
easing: "ease",
|
|
15
|
+
fill: "forwards"
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
let startRect;
|
|
19
|
+
let startCenter;
|
|
20
|
+
let startDistance;
|
|
21
|
+
const pointers = ref({});
|
|
22
|
+
const fingers = computed(() => {
|
|
23
|
+
return Object.values(pointers.value).slice(0, 2);
|
|
24
|
+
});
|
|
25
|
+
const center = computed(() => getCenter("current"));
|
|
26
|
+
const distance = computed(() => getDistance("current"));
|
|
27
|
+
function getCenter(mode) {
|
|
28
|
+
return {
|
|
29
|
+
x: fingers.value.reduce((sum, finger) => sum + finger[`${mode}X`], 0) / fingers.value.length,
|
|
30
|
+
y: fingers.value.reduce((sum, finger) => sum + finger[`${mode}Y`], 0) / fingers.value.length
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function getDistance(mode) {
|
|
34
|
+
const [finger1, finger2] = fingers.value;
|
|
35
|
+
return finger1 && finger2 ? Math.hypot(
|
|
36
|
+
finger1[`${mode}X`] - finger2[`${mode}X`],
|
|
37
|
+
finger1[`${mode}Y`] - finger2[`${mode}Y`]
|
|
38
|
+
) : 0;
|
|
39
|
+
}
|
|
40
|
+
function initialize() {
|
|
41
|
+
for (const pointer of Object.values(pointers.value)) {
|
|
42
|
+
pointer.startX = pointer.currentX;
|
|
43
|
+
pointer.startY = pointer.currentY;
|
|
44
|
+
}
|
|
45
|
+
startRect = rootEl.value.getBoundingClientRect();
|
|
46
|
+
startCenter = getCenter("start");
|
|
47
|
+
startDistance = getDistance("start");
|
|
48
|
+
}
|
|
49
|
+
const { isHolding } = usePointer(rootEl, {
|
|
50
|
+
onPointerdown(event) {
|
|
51
|
+
pointers.value[event.pointerId] = {
|
|
52
|
+
startX: event.screenX,
|
|
53
|
+
startY: event.screenY,
|
|
54
|
+
currentX: event.screenX,
|
|
55
|
+
currentY: event.screenY
|
|
56
|
+
};
|
|
57
|
+
initialize();
|
|
58
|
+
},
|
|
59
|
+
onPointermove(event) {
|
|
60
|
+
const pointer = pointers.value[event.pointerId];
|
|
61
|
+
if (!pointer) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
pointer.currentX = event.screenX;
|
|
65
|
+
pointer.currentY = event.screenY;
|
|
66
|
+
const rate2 = distance.value / startDistance || 1;
|
|
67
|
+
const left = startRect.left + center.value.x - startCenter.x;
|
|
68
|
+
const top = startRect.top + center.value.y - startCenter.y;
|
|
69
|
+
const finalLeft = left - (center.value.x - left) * (rate2 - 1);
|
|
70
|
+
const finalTop = top - (center.value.y - top) * (rate2 - 1);
|
|
71
|
+
rootEl.value.animate({
|
|
72
|
+
left: finalLeft + "px",
|
|
73
|
+
top: finalTop + "px",
|
|
74
|
+
width: startRect.width * rate2 + "px",
|
|
75
|
+
height: startRect.height * rate2 + "px"
|
|
76
|
+
}, {
|
|
77
|
+
duration: 0,
|
|
78
|
+
fill: "forwards"
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
onPointerup(event) {
|
|
82
|
+
delete pointers.value[event.pointerId];
|
|
83
|
+
if (Object.keys(pointers.value).length) {
|
|
84
|
+
initialize();
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
function onWheel(event) {
|
|
90
|
+
if (isHolding.value) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
let rate2 = 1 + Math.abs(event.deltaY) / (event.ctrlKey ? 20 : 200);
|
|
94
|
+
if (event.deltaY > 0) {
|
|
95
|
+
rate2 = 1 / rate2;
|
|
96
|
+
}
|
|
97
|
+
const { left, top, width, height } = rootEl.value.getBoundingClientRect();
|
|
98
|
+
const finalLeft = left - (event.clientX - left) * (rate2 - 1);
|
|
99
|
+
const finalTop = top - (event.clientY - top) * (rate2 - 1);
|
|
100
|
+
rootEl.value.animate({
|
|
101
|
+
left: finalLeft + "px",
|
|
102
|
+
top: finalTop + "px",
|
|
103
|
+
width: width * rate2 + "px",
|
|
104
|
+
height: height * rate2 + "px"
|
|
105
|
+
}, options.value);
|
|
106
|
+
}
|
|
107
|
+
useEventListener("keydown", (event) => {
|
|
108
|
+
if (event.key === "Escape") {
|
|
109
|
+
emit("close");
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
const onEnter = (el) => {
|
|
113
|
+
const fixedWidth = window.innerWidth * rate;
|
|
114
|
+
const fixedHeight = window.innerHeight * rate;
|
|
115
|
+
const naturalRatio = props.target.naturalWidth / props.target.naturalHeight;
|
|
116
|
+
const [finalWidth, finalHeight] = fixedWidth / fixedHeight > naturalRatio ? [fixedHeight * naturalRatio, fixedHeight] : [fixedWidth, fixedWidth / naturalRatio];
|
|
117
|
+
el.animate([getOriginalKeyframe(), {
|
|
118
|
+
top: `calc(50% - ${Math.floor(finalHeight / 2)}px)`,
|
|
119
|
+
left: `calc(50% - ${Math.floor(finalWidth / 2)}px)`,
|
|
120
|
+
width: Math.floor(finalWidth) + "px",
|
|
121
|
+
height: Math.floor(finalHeight) + "px",
|
|
122
|
+
clipPath: "inset(0)"
|
|
123
|
+
}], options.value);
|
|
124
|
+
};
|
|
125
|
+
const onLeave = (el, done) => {
|
|
126
|
+
const { left: elLeft, top: elTop } = el.getBoundingClientRect();
|
|
127
|
+
const { scrollX: x, scrollY: y } = window;
|
|
128
|
+
const animation = el.animate([{
|
|
129
|
+
top: 2 * y + elTop + "px",
|
|
130
|
+
left: 2 * x + elLeft + "px",
|
|
131
|
+
clipPath: "inset(0)"
|
|
132
|
+
}, getOriginalKeyframe(x, y)], options.value);
|
|
133
|
+
animation.addEventListener("finish", done);
|
|
134
|
+
};
|
|
135
|
+
function getOriginalKeyframe(x = 0, y = 0) {
|
|
136
|
+
const { left, top, width, height } = props.target.getBoundingClientRect();
|
|
137
|
+
const { naturalWidth, naturalHeight } = props.target;
|
|
138
|
+
const { objectPosition } = getComputedStyle(props.target);
|
|
139
|
+
const [horizontal, vertical] = objectPosition.split(" ").map((pos) => Number(pos.slice(0, -1)) / 100);
|
|
140
|
+
const ratio = width / height;
|
|
141
|
+
const naturalRatio = naturalWidth / naturalHeight;
|
|
142
|
+
let clipTop = 0;
|
|
143
|
+
let clipBottom = 0;
|
|
144
|
+
let clipLeft = 0;
|
|
145
|
+
let clipRight = 0;
|
|
146
|
+
if (ratio > naturalRatio) {
|
|
147
|
+
const fullHeight = naturalHeight * width / naturalWidth;
|
|
148
|
+
clipTop = (fullHeight - height) * vertical;
|
|
149
|
+
clipBottom = fullHeight - height - clipTop;
|
|
150
|
+
} else {
|
|
151
|
+
const fullWidth = naturalWidth * height / naturalHeight;
|
|
152
|
+
clipLeft = (fullWidth - width) * horizontal;
|
|
153
|
+
clipRight = fullWidth - width - clipLeft;
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
top: y + top - clipTop + "px",
|
|
157
|
+
left: x + left - clipLeft + "px",
|
|
158
|
+
width: width + clipLeft + clipRight + "px",
|
|
159
|
+
height: height + clipTop + clipBottom + "px",
|
|
160
|
+
clipPath: `inset(${clipTop}px ${clipRight}px ${clipBottom}px ${clipLeft}px)`
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
</script>
|
|
164
|
+
|
|
165
|
+
<template>
|
|
166
|
+
<transition @enter="onEnter" @leave="onLeave">
|
|
167
|
+
<img
|
|
168
|
+
v-if="isOpening"
|
|
169
|
+
ref="root"
|
|
170
|
+
class="bikariya-image-viewer"
|
|
171
|
+
:src="target.src"
|
|
172
|
+
:draggable="false"
|
|
173
|
+
@wheel.prevent="onWheel"
|
|
174
|
+
/>
|
|
175
|
+
</transition>
|
|
176
|
+
</template>
|
|
177
|
+
|
|
178
|
+
<style>
|
|
179
|
+
.bikariya-image-viewer{position:fixed;touch-action:none}.bikariya-image-viewer.v-leave-active{pointer-events:none;position:absolute}
|
|
180
|
+
</style>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type __VLS_Props = {
|
|
2
|
+
target: HTMLImageElement;
|
|
3
|
+
duration?: number;
|
|
4
|
+
isOpening?: boolean;
|
|
5
|
+
};
|
|
6
|
+
declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
7
|
+
close: () => any;
|
|
8
|
+
}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
|
|
9
|
+
onClose?: (() => any) | undefined;
|
|
10
|
+
}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
11
|
+
declare const _default: typeof __VLS_export;
|
|
12
|
+
export default _default;
|
package/dist/types.d.mts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bikariya/image-viewer",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "",
|
|
6
|
+
"author": "KazariEX",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": "KazariEX/bikariya",
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./dist/module.mjs"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/module.mjs",
|
|
14
|
+
"types": "./dist/module.d.mts",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@bikariya/modals": "",
|
|
20
|
+
"@nuxt/kit": "^4.2.2"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "nuxt-module-build build",
|
|
24
|
+
"dev": "nuxt-module-build build --stub",
|
|
25
|
+
"dev:prepare": "nuxt-module-build prepare"
|
|
26
|
+
}
|
|
27
|
+
}
|