Dragging work on the responsive preview but doesn't in the desktop view #341
-
Hi ! First off, thanks for your work on this lib. I'm messing around a bit with it to create a simple image pan/zoom component Here is a video of it, what you see in the console is the offset values : I'm without a doubt missing something but I can't figure out what, did anyone encountered the same issue ? or have and idea ? I'm using Chrome 91.0.4472.164 Pan.tsx import React, {useState, useRef} from 'react';
import {useDrag} from '@use-gesture/react'
type Props = {
imageSrc: string;
};
function ImagePanner( { imageSrc }: Props) {
let imageRef = useRef(null)
const [offset, setOffset] = useState({x:0,y:0})
useDrag(({offset: [dx,dy]} :{offset:Array<number>})=>{
setOffset({x:dx, y:dy})
console.log(dx,dy)
}, { target: imageRef }
)
const {x, y} = offset
return (
<>
<div className="pan-container">
<img src={imageSrc}
ref={imageRef}
style={{
transform: `translate(${x}px, ${y}px)`
}}
className="image-pan" />
</div>
</>
);
}
export default ImagePanner; Index.css .pan-container{
overflow: hidden;
width: 70rem;
height: 70rem;
margin: auto;
pointer-events: all;
touch-action: none;
}
.image-pan{
position: relative;
touch-action: none;
pointer-events: all;
cursor: pointer;
} App.tsx import './App.css';
import ImagePanner from './Pan';
import image from './image-test.jpg'
function App() {
return (
<div className="App">
<ImagePanner imageSrc={image} />
</div>
);
}
export default App; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Got it working by adding <img src={imageSrc}
ref={imageRef}
draggable={false}`
style={{
transform: `translate(${x}px, ${y}px)`
}}
className="image-pan" /> Wild guess is that the browser default to dragable to true, but that actually means that you can drag a semi-transparent copy of that image under the cursor. but there is nowhere to drop that image hence the cursor-cancel. And it work with the mobile preview since in that case the dragable default to false ? I would be curious to a have the full explanation if anyone knows. |
Beta Was this translation helpful? Give feedback.
Got it working by adding
draggable={false}
in the img attributes:Wild guess is that the browser default to dragable to true, but that actually means that you can drag a semi-transparent copy of that image under the cursor. but there is nowhere to drop that image hence the cursor-cancel. And it work with the mobile preview since in that case the dragable default to false ? I would be curious to a have the full explanation if anyone knows.