ISSUE-053 - To avoid messing with code that already works I dropped in a function-function that will convert pixel values to percent values. This fix adds a little bit of overhead, I guess, but it works without munging up any clean looking code.
This commit is contained in:
1 parent
b48579ac54
commit
a11f5b8eb6
5 files changed
+64
-40
No files matched your search
+59
-35
@@ -1,7 +1,7 @@
|
||||
import { css }from "styled-components/macro";
|
||||
import { clamp } from "lodash";
|
||||
|
||||
import { Corner, Fraction, Placement, Px, TransitionArgs } from "../types/style";
|
||||
import { Corner, Fraction, Percent, Placement, Px, TransitionArgs } from "../types/style";
|
||||
import { Seconds } from "../types/util";
|
||||
|
||||
export function placementStyleRules(pos: Placement) {
|
||||
@@ -14,6 +14,21 @@ export function placementStyleRules(pos: Placement) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Call to create a function that can be re-used to generate a pair of percent
|
||||
* values to use on a clip-path.
|
||||
* This was necessary to deal with double-applied scaling issues when using
|
||||
* element measurement hooks.
|
||||
* @param width the width of the element to be clipped in pixels
|
||||
* @param height the height of the element to be clipped in pixels
|
||||
* @returns A function that can be used to convert pixel values into percent values
|
||||
*/
|
||||
const clipConvertToPercent = (width: Px, height: Px) =>
|
||||
(coords: [Px, Px]): [Percent, Percent] => [
|
||||
coords[0] / width * 100,
|
||||
coords[1] / height * 100,
|
||||
];
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -23,7 +38,7 @@ export function placementStyleRules(pos: Placement) {
|
||||
* @param progress fraction from 0 to 1
|
||||
* @param startingCorner Which corner the wipe originates from
|
||||
* @param degrees The angle in degrees of the wipe
|
||||
* @returns Array of six numbers representing pixel values of the clip-path
|
||||
* @returns Array of three pairs of percentages to the coordinates of the clip-path
|
||||
*/
|
||||
export function wipeCustomDegClip(
|
||||
width: Px,
|
||||
@@ -31,33 +46,34 @@ export function wipeCustomDegClip(
|
||||
progress: Fraction,
|
||||
startingCorner: Corner,
|
||||
degrees: number,
|
||||
): [[Px, Px], [Px, Px], [Px, Px]] {
|
||||
): [[Percent, Percent], [Percent, Percent], [Percent, Percent]] {
|
||||
const tangent = Math.tan(clamp(degrees, 0.01, 90) * Math.PI / 180);
|
||||
const convert = clipConvertToPercent(width, height);
|
||||
|
||||
switch (startingCorner) {
|
||||
case "topLeft":
|
||||
return [
|
||||
[0, 0],
|
||||
[(width + height / tangent) * progress, 0],
|
||||
[0, (height + width * tangent) * progress ],
|
||||
convert([0, 0]),
|
||||
convert([(width + height / tangent) * progress, 0]),
|
||||
convert([0, (height + width * tangent) * progress ]),
|
||||
];
|
||||
case "topRight":
|
||||
return [
|
||||
[width, 0],
|
||||
[width, (height + width * tangent) * progress],
|
||||
[width - (width + height / tangent) * progress, 0],
|
||||
convert([width, 0]),
|
||||
convert([width, (height + width * tangent) * progress]),
|
||||
convert([width - (width + height / tangent) * progress, 0]),
|
||||
];
|
||||
case "bottomRight":
|
||||
return [
|
||||
[width, height],
|
||||
[width, height - (height + width * tangent) * progress],
|
||||
[width - (width + height / tangent) * progress, height],
|
||||
convert([width, height]),
|
||||
convert([width, height - (height + width * tangent) * progress]),
|
||||
convert([width - (width + height / tangent) * progress, height]),
|
||||
];
|
||||
case "bottomLeft":
|
||||
return [
|
||||
[0, height],
|
||||
[0, height - (height + width * tangent) * progress],
|
||||
[(width + height / tangent) * progress, height],
|
||||
convert([0, height]),
|
||||
convert([0, height - (height + width * tangent) * progress]),
|
||||
convert([(width + height / tangent) * progress, height]),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -114,7 +130,7 @@ export function commonDirectionalTransition(
|
||||
* @param progress fraction from 0 to 1
|
||||
* @param outlineThickness the expected max width of the outline
|
||||
* @param interiorThickness the expected max width of the outline internally
|
||||
* @returns
|
||||
* @returns Array of six pairs of percentages to the coordinates of the clip-path
|
||||
*/
|
||||
export function outlineClipPath(
|
||||
width: Px,
|
||||
@@ -122,45 +138,53 @@ export function outlineClipPath(
|
||||
progress: Fraction,
|
||||
outlineThickness: Px,
|
||||
interiorThickness: Px = 0,
|
||||
): [[Px, Px], [Px, Px], [Px, Px], [Px, Px], [Px, Px], [Px, Px]] {
|
||||
): [
|
||||
[Percent, Percent],
|
||||
[Percent, Percent],
|
||||
[Percent, Percent],
|
||||
[Percent, Percent],
|
||||
[Percent, Percent],
|
||||
[Percent, Percent],
|
||||
] {
|
||||
const travelDistance = width * 2 + height;
|
||||
const pt1End = width / travelDistance;
|
||||
const pt2End = (width + height) / travelDistance;
|
||||
const convert = clipConvertToPercent(width, height);
|
||||
|
||||
// Part 1: Reveal the bottom line.
|
||||
if (progress <= pt1End) {
|
||||
const x = width * progress / pt1End;
|
||||
return [
|
||||
[0, height - interiorThickness],
|
||||
[0, height - interiorThickness],
|
||||
[0, height - interiorThickness],
|
||||
[x, height - interiorThickness],
|
||||
[x, height + outlineThickness],
|
||||
[0, height + outlineThickness],
|
||||
convert([0, height - interiorThickness]),
|
||||
convert([0, height - interiorThickness]),
|
||||
convert([0, height - interiorThickness]),
|
||||
convert([x, height - interiorThickness]),
|
||||
convert([x, height + outlineThickness]),
|
||||
convert([0, height + outlineThickness]),
|
||||
];
|
||||
|
||||
// Part 2: Reveal the right line.
|
||||
} else if (progress > pt1End && progress <= pt2End) {
|
||||
const y = height - (height + outlineThickness) * ((progress - pt1End) / (pt2End - pt1End));
|
||||
return [
|
||||
[0, height - interiorThickness],
|
||||
[width - Math.max(10, interiorThickness), height - interiorThickness],
|
||||
[width - interiorThickness, y],
|
||||
[width + outlineThickness, y],
|
||||
[width + outlineThickness, height + outlineThickness],
|
||||
[0, height + outlineThickness],
|
||||
convert([0, height - interiorThickness]),
|
||||
convert([width - Math.max(10, interiorThickness), height - interiorThickness]),
|
||||
convert([width - interiorThickness, y]),
|
||||
convert([width + outlineThickness, y]),
|
||||
convert([width + outlineThickness, height + outlineThickness]),
|
||||
convert([0, height + outlineThickness]),
|
||||
];
|
||||
|
||||
// Part 3: Reveal the top line.
|
||||
} else {
|
||||
const x = width - width * ((progress - pt2End) / (1 - pt2End));
|
||||
return [
|
||||
[0, height - interiorThickness],
|
||||
[x - Math.max(10, interiorThickness), height - interiorThickness],
|
||||
[x - interiorThickness, -outlineThickness],
|
||||
[width + outlineThickness, -outlineThickness],
|
||||
[width + outlineThickness, height + outlineThickness],
|
||||
[0, height + outlineThickness],
|
||||
convert([0, height - interiorThickness]),
|
||||
convert([x - Math.max(10, interiorThickness), height - interiorThickness]),
|
||||
convert([x - interiorThickness, -outlineThickness]),
|
||||
convert([width + outlineThickness, -outlineThickness]),
|
||||
convert([width + outlineThickness, height + outlineThickness]),
|
||||
convert([0, height + outlineThickness]),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user