File size: 5,830 Bytes
82a88c5 66a740c 63b089b e3d3deb 9bde4cc aa3b624 f86a242 e5cb552 aa3b624 9bde4cc aa3b624 f86a242 aa3b624 e459aae e5cb552 aa3b624 9bde4cc aa3b624 e5cb552 9bde4cc e459aae aa3b624 e459aae e5cb552 e459aae 9bde4cc aa3b624 e459aae aa3b624 e459aae aa3b624 e459aae 66a740c e459aae 66a740c 63b089b 66a740c 9bde4cc e459aae 66a740c e459aae 9bde4cc 75e70b4 e459aae aa3b624 e459aae aa3b624 9bde4cc aa3b624 e459aae aa3b624 9dbe470 aa3b624 9bde4cc aa3b624 9bde4cc aa3b624 9d696c3 9bde4cc aa3b624 9bde4cc aa3b624 9bde4cc aa3b624 9bde4cc edd7dca aa3b624 edd7dca aa3b624 edd7dca aa3b624 e459aae aa3b624 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
import { easeInOutQuad } from "./utils";
import { onDriverClick } from "./events";
import { emit } from "./emitter";
import { getConfig } from "./config";
import { getState, setState } from "./state";
export type StageDefinition = {
x: number;
y: number;
width: number;
height: number;
};
// This method calculates the animated new position of the
// stage (called for each frame by requestAnimationFrame)
export function transitionStage(elapsed: number, duration: number, from: Element, to: Element) {
let activeStagePosition = getState("__activeStagePosition");
const fromDefinition = activeStagePosition ? activeStagePosition : from.getBoundingClientRect();
const toDefinition = to.getBoundingClientRect();
const x = easeInOutQuad(elapsed, fromDefinition.x, toDefinition.x - fromDefinition.x, duration);
const y = easeInOutQuad(elapsed, fromDefinition.y, toDefinition.y - fromDefinition.y, duration);
const width = easeInOutQuad(elapsed, fromDefinition.width, toDefinition.width - fromDefinition.width, duration);
const height = easeInOutQuad(elapsed, fromDefinition.height, toDefinition.height - fromDefinition.height, duration);
activeStagePosition = {
x,
y,
width,
height,
};
renderOverlay(activeStagePosition);
setState("__activeStagePosition", activeStagePosition);
}
export function trackActiveElement(element: Element) {
if (!element) {
return;
}
const definition = element.getBoundingClientRect();
const activeStagePosition: StageDefinition = {
x: definition.x,
y: definition.y,
width: definition.width,
height: definition.height,
};
setState("__activeStagePosition", activeStagePosition);
renderOverlay(activeStagePosition);
}
export function refreshOverlay() {
const activeStagePosition = getState("__activeStagePosition");
const overlaySvg = getState("__overlaySvg");
if (!activeStagePosition) {
return;
}
if (!overlaySvg) {
console.warn("No stage svg found.");
return;
}
const windowX = window.innerWidth;
const windowY = window.innerHeight;
overlaySvg.setAttribute("viewBox", `0 0 ${windowX} ${windowY}`);
}
function mountOverlay(stagePosition: StageDefinition) {
const overlaySvg = createOverlaySvg(stagePosition);
document.body.appendChild(overlaySvg);
onDriverClick(overlaySvg, e => {
const target = e.target as SVGElement;
if (target.tagName !== "path") {
return;
}
emit("overlayClick");
});
setState("__overlaySvg", overlaySvg);
}
function renderOverlay(stagePosition: StageDefinition) {
const overlaySvg = getState("__overlaySvg");
// TODO: cancel rendering if element is not visible
if (!overlaySvg) {
mountOverlay(stagePosition);
return;
}
const pathElement = overlaySvg.firstElementChild as SVGPathElement | null;
if (pathElement?.tagName !== "path") {
throw new Error("no path element found in stage svg");
}
pathElement.setAttribute("d", generateStageSvgPathString(stagePosition));
}
function createOverlaySvg(stage: StageDefinition): SVGSVGElement {
const windowX = window.innerWidth;
const windowY = window.innerHeight;
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.classList.add("driver-overlay", "driver-overlay-animated");
svg.setAttribute("viewBox", `0 0 ${windowX} ${windowY}`);
svg.setAttribute("xmlSpace", "preserve");
svg.setAttribute("xmlnsXlink", "http://www.w3.org/1999/xlink");
svg.setAttribute("version", "1.1");
svg.setAttribute("preserveAspectRatio", "xMinYMin slice");
svg.style.fillRule = "evenodd";
svg.style.clipRule = "evenodd";
svg.style.strokeLinejoin = "round";
svg.style.strokeMiterlimit = "2";
svg.style.zIndex = "10000";
svg.style.position = "fixed";
svg.style.top = "0";
svg.style.left = "0";
svg.style.width = "100%";
svg.style.height = "100%";
const stagePath = document.createElementNS("http://www.w3.org/2000/svg", "path");
stagePath.setAttribute("d", generateStageSvgPathString(stage));
stagePath.style.fill = getConfig("overlayColor") || "rgb(0,0,0)";
stagePath.style.opacity = `${getConfig("overlayOpacity")}`;
stagePath.style.pointerEvents = "auto";
stagePath.style.cursor = "auto";
svg.appendChild(stagePath);
return svg;
}
function generateStageSvgPathString(stage: StageDefinition) {
const windowX = window.innerWidth;
const windowY = window.innerHeight;
const stagePadding = getConfig("stagePadding") || 0;
const stageRadius = getConfig("stageRadius") || 0;
const stageWidth = stage.width + stagePadding * 2;
const stageHeight = stage.height + stagePadding * 2;
// prevent glitches when stage is too small for radius
const limitedRadius = Math.min(stageRadius, stageWidth / 2, stageHeight / 2);
// no value below 0 allowed + round down
const normalizedRadius = Math.floor(Math.max(limitedRadius, 0));
const highlightBoxX = stage.x - stagePadding + normalizedRadius;
const highlightBoxY = stage.y - stagePadding;
const highlightBoxWidth = stageWidth - normalizedRadius * 2;
const highlightBoxHeight = stageHeight - normalizedRadius * 2;
return `M${windowX},0L0,0L0,${windowY}L${windowX},${windowY}L${windowX},0Z
M${highlightBoxX},${highlightBoxY} h${highlightBoxWidth} a${normalizedRadius},${normalizedRadius} 0 0 1 ${normalizedRadius},${normalizedRadius} v${highlightBoxHeight} a${normalizedRadius},${normalizedRadius} 0 0 1 -${normalizedRadius},${normalizedRadius} h-${highlightBoxWidth} a${normalizedRadius},${normalizedRadius} 0 0 1 -${normalizedRadius},-${normalizedRadius} v-${highlightBoxHeight} a${normalizedRadius},${normalizedRadius} 0 0 1 ${normalizedRadius},-${normalizedRadius} z`;
}
export function destroyOverlay() {
const overlaySvg = getState("__overlaySvg");
if (overlaySvg) {
overlaySvg.remove();
}
}
|