@supermousejs/image 2.0.1 → 2.0.2
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 +9 -0
- package/README.md +4 -0
- package/package.json +3 -3
- package/src/index.ts +85 -85
- package/tsconfig.json +17 -17
- package/vite.config.ts +21 -21
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @supermousejs/image
|
|
2
2
|
|
|
3
|
+
## 2.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- ae219a0: Update READMEs with correct link to documentation
|
|
8
|
+
- Updated dependencies [ae219a0]
|
|
9
|
+
- @supermousejs/utils@2.0.2
|
|
10
|
+
- @supermousejs/core@2.0.2
|
|
11
|
+
|
|
3
12
|
## 2.0.1
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -27,3 +27,7 @@ app.use(Image({
|
|
|
27
27
|
```html
|
|
28
28
|
<a href="#" data-supermouse-img="/path/to/image.jpg">Hover to see preview</a>
|
|
29
29
|
```
|
|
30
|
+
|
|
31
|
+
## Documentation
|
|
32
|
+
|
|
33
|
+
Full documentation and interactive playground available at [supermouse](https://supermouse.vercel.app) or [check out the repo](https://github.com/Whitestar14/supermouse-js).
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supermousejs/image",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"main": "dist/index.umd.js",
|
|
5
5
|
"module": "dist/index.mjs",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@supermousejs/
|
|
9
|
-
"@supermousejs/
|
|
8
|
+
"@supermousejs/utils": "2.0.2",
|
|
9
|
+
"@supermousejs/core": "2.0.2"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {},
|
|
12
12
|
"peerDependencies": {},
|
package/src/index.ts
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
import { definePlugin, dom, math, Layers, Easings } from '@supermousejs/utils';
|
|
2
|
-
|
|
3
|
-
export interface ImageOptions {
|
|
4
|
-
name?: string;
|
|
5
|
-
isEnabled?: boolean;
|
|
6
|
-
className?: string;
|
|
7
|
-
offset?: [number, number];
|
|
8
|
-
duration?: number;
|
|
9
|
-
smoothness?: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const Image = (options: ImageOptions = {}) => {
|
|
13
|
-
const className = options.className || 'supermouse-image';
|
|
14
|
-
const [offX, offY] = options.offset || [0, 30];
|
|
15
|
-
const duration = options.duration || 200;
|
|
16
|
-
const smoothness = options.smoothness || 1;
|
|
17
|
-
|
|
18
|
-
let img: HTMLImageElement;
|
|
19
|
-
let lastSrc: string | null = null;
|
|
20
|
-
let isVisible = false;
|
|
21
|
-
let lx = 0;
|
|
22
|
-
let ly = 0;
|
|
23
|
-
|
|
24
|
-
return definePlugin<HTMLDivElement, ImageOptions>({
|
|
25
|
-
name: 'image',
|
|
26
|
-
selector: '[data-supermouse-img]',
|
|
27
|
-
|
|
28
|
-
create: () => {
|
|
29
|
-
const container = dom.createActor('div') as HTMLDivElement;
|
|
30
|
-
if (className) {
|
|
31
|
-
container.classList.add(...className.split(' ').filter(Boolean));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
img = document.createElement('img');
|
|
35
|
-
dom.applyStyles(img, {
|
|
36
|
-
width: '100%', height: '100%', objectFit: 'cover', display: 'block'
|
|
37
|
-
});
|
|
38
|
-
container.appendChild(img);
|
|
39
|
-
|
|
40
|
-
dom.applyStyles(container, {
|
|
41
|
-
zIndex: Layers.OVERLAY,
|
|
42
|
-
opacity: '0',
|
|
43
|
-
overflow: 'hidden',
|
|
44
|
-
transition: `opacity ${duration}ms ${Easings.SMOOTH}`,
|
|
45
|
-
width: '150px', height: 'auto'
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
dom.setTransform(container, -100, -100);
|
|
49
|
-
return container;
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
update: (app, container) => {
|
|
53
|
-
// Use parsed interaction data
|
|
54
|
-
const src = app.state.interaction.img;
|
|
55
|
-
|
|
56
|
-
if (app.state.isHover && src) {
|
|
57
|
-
if (src !== lastSrc) {
|
|
58
|
-
img.src = src;
|
|
59
|
-
lastSrc = src;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
dom.setStyle(container, 'opacity', '1');
|
|
63
|
-
|
|
64
|
-
const targetX = app.state.pointer.x + offX;
|
|
65
|
-
const targetY = app.state.pointer.y + offY;
|
|
66
|
-
|
|
67
|
-
// Reset position on first appearance to prevent flying in
|
|
68
|
-
if (!isVisible) {
|
|
69
|
-
lx = targetX;
|
|
70
|
-
ly = targetY;
|
|
71
|
-
isVisible = true;
|
|
72
|
-
} else {
|
|
73
|
-
lx = math.lerp(lx, targetX, smoothness);
|
|
74
|
-
ly = math.lerp(ly, targetY, smoothness);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
dom.setTransform(container, lx, ly);
|
|
78
|
-
|
|
79
|
-
} else {
|
|
80
|
-
dom.setStyle(container, 'opacity', '0');
|
|
81
|
-
isVisible = false;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}, options);
|
|
85
|
-
};
|
|
1
|
+
import { definePlugin, dom, math, Layers, Easings } from '@supermousejs/utils';
|
|
2
|
+
|
|
3
|
+
export interface ImageOptions {
|
|
4
|
+
name?: string;
|
|
5
|
+
isEnabled?: boolean;
|
|
6
|
+
className?: string;
|
|
7
|
+
offset?: [number, number];
|
|
8
|
+
duration?: number;
|
|
9
|
+
smoothness?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const Image = (options: ImageOptions = {}) => {
|
|
13
|
+
const className = options.className || 'supermouse-image';
|
|
14
|
+
const [offX, offY] = options.offset || [0, 30];
|
|
15
|
+
const duration = options.duration || 200;
|
|
16
|
+
const smoothness = options.smoothness || 1;
|
|
17
|
+
|
|
18
|
+
let img: HTMLImageElement;
|
|
19
|
+
let lastSrc: string | null = null;
|
|
20
|
+
let isVisible = false;
|
|
21
|
+
let lx = 0;
|
|
22
|
+
let ly = 0;
|
|
23
|
+
|
|
24
|
+
return definePlugin<HTMLDivElement, ImageOptions>({
|
|
25
|
+
name: 'image',
|
|
26
|
+
selector: '[data-supermouse-img]',
|
|
27
|
+
|
|
28
|
+
create: () => {
|
|
29
|
+
const container = dom.createActor('div') as HTMLDivElement;
|
|
30
|
+
if (className) {
|
|
31
|
+
container.classList.add(...className.split(' ').filter(Boolean));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
img = document.createElement('img');
|
|
35
|
+
dom.applyStyles(img, {
|
|
36
|
+
width: '100%', height: '100%', objectFit: 'cover', display: 'block'
|
|
37
|
+
});
|
|
38
|
+
container.appendChild(img);
|
|
39
|
+
|
|
40
|
+
dom.applyStyles(container, {
|
|
41
|
+
zIndex: Layers.OVERLAY,
|
|
42
|
+
opacity: '0',
|
|
43
|
+
overflow: 'hidden',
|
|
44
|
+
transition: `opacity ${duration}ms ${Easings.SMOOTH}`,
|
|
45
|
+
width: '150px', height: 'auto'
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
dom.setTransform(container, -100, -100);
|
|
49
|
+
return container;
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
update: (app, container) => {
|
|
53
|
+
// Use parsed interaction data
|
|
54
|
+
const src = app.state.interaction.img;
|
|
55
|
+
|
|
56
|
+
if (app.state.isHover && src) {
|
|
57
|
+
if (src !== lastSrc) {
|
|
58
|
+
img.src = src;
|
|
59
|
+
lastSrc = src;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
dom.setStyle(container, 'opacity', '1');
|
|
63
|
+
|
|
64
|
+
const targetX = app.state.pointer.x + offX;
|
|
65
|
+
const targetY = app.state.pointer.y + offY;
|
|
66
|
+
|
|
67
|
+
// Reset position on first appearance to prevent flying in
|
|
68
|
+
if (!isVisible) {
|
|
69
|
+
lx = targetX;
|
|
70
|
+
ly = targetY;
|
|
71
|
+
isVisible = true;
|
|
72
|
+
} else {
|
|
73
|
+
lx = math.lerp(lx, targetX, smoothness);
|
|
74
|
+
ly = math.lerp(ly, targetY, smoothness);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
dom.setTransform(container, lx, ly);
|
|
78
|
+
|
|
79
|
+
} else {
|
|
80
|
+
dom.setStyle(container, 'opacity', '0');
|
|
81
|
+
isVisible = false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}, options);
|
|
85
|
+
};
|
package/tsconfig.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.base.json",
|
|
3
|
-
"include": [
|
|
4
|
-
"src"
|
|
5
|
-
],
|
|
6
|
-
"compilerOptions": {
|
|
7
|
-
"outDir": "dist",
|
|
8
|
-
"baseUrl": ".",
|
|
9
|
-
"paths": {
|
|
10
|
-
"@supermousejs/core": [
|
|
11
|
-
"../core/src/index.ts"
|
|
12
|
-
],
|
|
13
|
-
"@supermousejs/utils": [
|
|
14
|
-
"../utils/src/index.ts"
|
|
15
|
-
]
|
|
16
|
-
}
|
|
17
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"include": [
|
|
4
|
+
"src"
|
|
5
|
+
],
|
|
6
|
+
"compilerOptions": {
|
|
7
|
+
"outDir": "dist",
|
|
8
|
+
"baseUrl": ".",
|
|
9
|
+
"paths": {
|
|
10
|
+
"@supermousejs/core": [
|
|
11
|
+
"../core/src/index.ts"
|
|
12
|
+
],
|
|
13
|
+
"@supermousejs/utils": [
|
|
14
|
+
"../utils/src/index.ts"
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
18
|
}
|
package/vite.config.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { defineConfig } from 'vite';
|
|
2
|
-
import dts from 'vite-plugin-dts';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
|
|
5
|
-
export default defineConfig({
|
|
6
|
-
build: {
|
|
7
|
-
lib: {
|
|
8
|
-
entry: path.resolve(__dirname, 'src/index.ts'),
|
|
9
|
-
name: 'SupermouseImage',
|
|
10
|
-
fileName: (format) => format === 'es' ? 'index.mjs' : 'index.umd.js',
|
|
11
|
-
},
|
|
12
|
-
rollupOptions: {
|
|
13
|
-
external: ['@supermousejs/core', '@supermousejs/utils'],
|
|
14
|
-
output: {
|
|
15
|
-
globals: {
|
|
16
|
-
'@supermousejs/core': 'SupermouseCore'
|
|
17
|
-
, '@supermousejs/utils': 'SupermouseUtils'}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
plugins: [dts({ rollupTypes: true })]
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
import dts from 'vite-plugin-dts';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
build: {
|
|
7
|
+
lib: {
|
|
8
|
+
entry: path.resolve(__dirname, 'src/index.ts'),
|
|
9
|
+
name: 'SupermouseImage',
|
|
10
|
+
fileName: (format) => format === 'es' ? 'index.mjs' : 'index.umd.js',
|
|
11
|
+
},
|
|
12
|
+
rollupOptions: {
|
|
13
|
+
external: ['@supermousejs/core', '@supermousejs/utils'],
|
|
14
|
+
output: {
|
|
15
|
+
globals: {
|
|
16
|
+
'@supermousejs/core': 'SupermouseCore'
|
|
17
|
+
, '@supermousejs/utils': 'SupermouseUtils'}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
plugins: [dts({ rollupTypes: true })]
|
|
22
22
|
});
|