Commit 4517af39 authored by Noah Zinsmeister's avatar Noah Zinsmeister Committed by GitHub

don't jump on mouse exit (#1565)

parent b40163ce
import React, { SyntheticEvent, useCallback, useMemo, useRef, useState } from 'react' import React, { useCallback, useMemo, useRef, useState } from 'react'
import { NonfungiblePositionManager, Pool, Position } from '@uniswap/v3-sdk' import { NonfungiblePositionManager, Pool, Position } from '@uniswap/v3-sdk'
import { PoolState, usePool } from 'hooks/usePools' import { PoolState, usePool } from 'hooks/usePools'
...@@ -201,20 +201,11 @@ function getRatio( ...@@ -201,20 +201,11 @@ function getRatio(
} }
} }
function NFT({ image, height: targetHeight }: { image: string; height: number }) { // snapshots a src img into a canvas
const [animate, setAnimate] = useState(false) function getSnapshot(src: HTMLImageElement, canvas: HTMLCanvasElement, targetHeight: number) {
const canvasRef = useRef<HTMLCanvasElement>()
const imageRef = useRef<HTMLImageElement>()
const getSnapshot = (src: HTMLImageElement) => {
if (!canvasRef.current) return
const { current: canvas } = canvasRef
const context = canvas.getContext('2d') const context = canvas.getContext('2d')
if (!context) return if (context) {
let { width, height } = src let { width, height } = src
// src may be hidden and not have the target dimensions // src may be hidden and not have the target dimensions
...@@ -232,15 +223,39 @@ function NFT({ image, height: targetHeight }: { image: string; height: number }) ...@@ -232,15 +223,39 @@ function NFT({ image, height: targetHeight }: { image: string; height: number })
context.clearRect(0, 0, width, height) context.clearRect(0, 0, width, height)
context.drawImage(src, 0, 0, width, height) context.drawImage(src, 0, 0, width, height)
} }
}
const onLoad = (e: SyntheticEvent<HTMLImageElement>) => { function NFT({ image, height: targetHeight }: { image: string; height: number }) {
getSnapshot(e.target as HTMLImageElement) const [animate, setAnimate] = useState(false)
}
const canvasRef = useRef<HTMLCanvasElement>(null)
const imageRef = useRef<HTMLImageElement>(null)
return ( return (
<NFTGrid onMouseEnter={() => setAnimate(true)} onMouseLeave={() => setAnimate(false)}> <NFTGrid
<NFTCanvas ref={canvasRef as any} /> onMouseEnter={() => {
<NFTImage src={image} hidden={!animate} onLoad={onLoad} ref={imageRef as any} /> setAnimate(true)
}}
onMouseLeave={() => {
// snapshot the current frame so the transition to the canvas is smooth
if (imageRef.current && canvasRef.current) {
getSnapshot(imageRef.current, canvasRef.current, targetHeight)
}
setAnimate(false)
}}
>
<NFTCanvas ref={canvasRef} />
<NFTImage
ref={imageRef}
src={image}
hidden={!animate}
onLoad={() => {
// snapshot for the canvas
if (imageRef.current && canvasRef.current) {
getSnapshot(imageRef.current, canvasRef.current, targetHeight)
}
}}
/>
</NFTGrid> </NFTGrid>
) )
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment