From c07d364584479edd7b53402288ebec1c2baca30f Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Mon, 7 Feb 2022 12:33:42 -0500 Subject: [PATCH] ISSUE-045 - Venetian Blinds - After some trial and error I've got a performant and sensible wipe solution using a clip-path mask. Figuring this out of great because these kinds of clip paths are used in other places as well so this is good practice to start with. --- .../VenetianBlindsTransition.tsx | 61 ++++++++++++++++--- src/types/style.d.ts | 5 ++ src/utils/styling.ts | 50 ++++++++++++++- 3 files changed, 105 insertions(+), 11 deletions(-) diff --git a/src/components/BroadcastGraphics/Common/VenetianBlindsTransition/VenetianBlindsTransition.tsx b/src/components/BroadcastGraphics/Common/VenetianBlindsTransition/VenetianBlindsTransition.tsx index 799bfc9..7565987 100644 --- a/src/components/BroadcastGraphics/Common/VenetianBlindsTransition/VenetianBlindsTransition.tsx +++ b/src/components/BroadcastGraphics/Common/VenetianBlindsTransition/VenetianBlindsTransition.tsx @@ -1,17 +1,58 @@ +import { useState } from "react"; +import { useSpring, animated } from "@react-spring/web"; +import useMeasure from "react-use-measure"; + import { VenetianBlindsFilter } from "./styles"; +import { wipe45DegClip } from "../../../../utils/styling"; type VenetianBlindsTransitionProps = { color: string, }; export const VenetianBlindsTransition = ( { color, children }: React.PropsWithChildren, -) => ( - - {children} - -); +) => { + const [childRef, { + height: childHeight, + width: childWidth, + }] = useMeasure(); + const deg = -45; + + const [state, toggle] = useState(true); + + const { x } = useSpring({ + from: { x: 0 }, + x: state ? 1 : 0, + config: { duration: 300 }, + }); + + return ( +
{ + console.log(!state); + toggle(!state); + }}> + { + const [a1, a2, b1, b2, c1, c2] = wipe45DegClip( + childWidth, + childHeight, + x, + "bottomRight", + ); + return `polygon(${a1}px ${a2}px, ${b1}px ${b2}px, ${c1}px ${c2}px)`; + }), + }} + > + + {children} + + +
+ ); +}; diff --git a/src/types/style.d.ts b/src/types/style.d.ts index d1af8a2..42a10b8 100644 --- a/src/types/style.d.ts +++ b/src/types/style.d.ts @@ -8,6 +8,11 @@ export type Px = number; */ export type Percent = number; +/** + * Type to remind you that we're dealing with fraction (0-1). + */ +export type Fraction = number; + /** * Object to cleanly define relative/absolute placement for a styled component. */ diff --git a/src/utils/styling.ts b/src/utils/styling.ts index b316106..a843421 100644 --- a/src/utils/styling.ts +++ b/src/utils/styling.ts @@ -1,6 +1,6 @@ import { css }from "styled-components"; -import { Placement } from "../types/style"; +import { Fraction, Placement, Px } from "../types/style"; export function placementStyleRules(pos: Placement) { return css` @@ -10,3 +10,51 @@ export function placementStyleRules(pos: Placement) { ${pos.left ? `left: ${pos.left};` : ""} `; } + + +/** + * Provides the x, y coordinates of a three point polygon for use in a clip-path + * mask. By taking a fraction to indicate the progress through the animation it + * will return the six coordinates that will generate a 45 degree wipe effect. + * @param width element width in pixels + * @param height element height in pixels + * @param progress fraction from 0 to 1 + * @param startingCorner Which corner the wipe originates from + * @returns Array of six numbers representing pixel values of the clip-path + */ +export function wipe45DegClip( + width: Px, + height: Px, + progress: Fraction, + startingCorner: "topLeft" | "topRight" | "bottomRight" | "bottomLeft", +): [Px, Px, Px, Px, Px, Px] { + // Long edge multiplied by 2. + const l = Math.max(width, height) * 2; + + switch(startingCorner) { + case "topLeft": + return [ + 0, 0, + l * progress, 0, + 0, l * progress, + ]; + case "topRight": + return [ + width, 0, + width, l * progress, + width - l * progress, 0, + ]; + case "bottomRight": + return [ + width, height, + width, height - l * progress, + width - l * progress, height, + ]; + case "bottomLeft": + return [ + 0, height, + 0, height - l * progress, + l * progress, height, + ]; + } +}