-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be6ef73
commit d26a7aa
Showing
4 changed files
with
125 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as React from "react" | ||
import { useRef } from "react" | ||
import { motion, useScroll } from "framer-motion" | ||
|
||
export const App = () => { | ||
const rect = useRef(null) | ||
const svg = useRef(null) | ||
|
||
const rectValues = useScroll({ | ||
target: rect, | ||
offset: ["start end", "end start"], | ||
}) | ||
|
||
const svgValues = useScroll({ | ||
target: svg, | ||
offset: ["start end", "end start"], | ||
}) | ||
|
||
return ( | ||
<> | ||
<div style={{ paddingTop: 400, paddingBottom: 400 }}> | ||
<svg ref={svg} viewBox="0 0 200 200" width="200" height="200"> | ||
<rect | ||
ref={rect} | ||
width="100" | ||
height="100" | ||
x="50" | ||
y="50" | ||
fill="red" | ||
/> | ||
</svg> | ||
</div> | ||
<motion.div style={{ ...fixed }} id="rect-progress"> | ||
{rectValues.scrollYProgress} | ||
</motion.div> | ||
<motion.div style={{ ...fixed, top: 50 }} id="svg-progress"> | ||
{svgValues.scrollYProgress} | ||
</motion.div> | ||
</> | ||
) | ||
} | ||
|
||
const fixed: React.CSSProperties = { | ||
position: "fixed", | ||
top: 10, | ||
left: 10, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 31 additions & 19 deletions
50
packages/framer-motion/src/render/dom/scroll/offsets/inset.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,37 @@ | ||
export function calcInset(element: Element, container: HTMLElement) { | ||
let inset = { x: 0, y: 0 } | ||
const inset = { x: 0, y: 0 } | ||
|
||
let current: Element | null = element | ||
while (current && current !== container) { | ||
if (current instanceof HTMLElement) { | ||
inset.x += current.offsetLeft | ||
inset.y += current.offsetTop | ||
current = current.offsetParent | ||
} else if (current instanceof SVGGraphicsElement && "getBBox" in current) { | ||
const { top, left } = current.getBBox() | ||
inset.x += left | ||
inset.y += top | ||
let current: Element | null = element | ||
while (current && current !== container) { | ||
if (current instanceof HTMLElement) { | ||
inset.x += current.offsetLeft | ||
inset.y += current.offsetTop | ||
current = current.offsetParent | ||
} else if (current.tagName === "svg") { | ||
const svgBoundingBox = current.getBoundingClientRect() | ||
current = current.parentElement! | ||
const parentBoundingBox = current.getBoundingClientRect() | ||
inset.x += svgBoundingBox.left - parentBoundingBox.left | ||
inset.y += svgBoundingBox.top - parentBoundingBox.top | ||
} else if ( | ||
current instanceof SVGGraphicsElement && | ||
"getBBox" in current | ||
) { | ||
const { x, y } = current.getBBox() | ||
inset.x += x | ||
inset.y += y | ||
|
||
/** | ||
* Assign the next parent element as the <svg /> tag. | ||
*/ | ||
while (current && current.tagName !== "svg") { | ||
current = current.parentNode as SVGElement | ||
} | ||
let svg: SVGElement | null = null | ||
let parent: SVGElement = current.parentNode as SVGElement | ||
while (!svg) { | ||
if (parent.tagName === "svg") { | ||
svg = parent | ||
} | ||
parent = current.parentNode as SVGElement | ||
} | ||
current = svg | ||
} | ||
} | ||
} | ||
|
||
return inset | ||
return inset | ||
} |