npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

actual-client-rect

v0.2.2

Published

gets an element's actual client rect with transform. ALPHA

Downloads

59

Readme

getActualClientRect

ALPHA

npm i actual-client-rect --save

Problem

It was 2023 and web developers still didn't have any good options for obtaining the position and shape of an element on a page.

  • People often use getBoundingClientRect(), which by nature obscures any transforms affecting the element.
  • HTMLElement's offset api is annoying to work with, and the specs require the values to be rounded to the nearest pixel.
  • Some libraries may have solved this problem internally, but to my knowledge no library exists that provides a direct solution, until now.

Solution

getActualClientRect() returns the element's basis DOMRect relative to the viewport, its computed transform origin, and its accumulated transform in 3 formats: css transform string, css transform's matrix3d substring, and a glMatrix mat4

API / Types / Documentation

export function getActualClientRect(element: HTMLElement, options?: ACROptions): ActualClientRect;

export type ActualClientRect = {
  basis: DOMRect;
  transformOrigin: string;
  transform: string; // `matrix3d(${matrix3d})`
  matrix3d: string;
  transformMat4: mat4; // row-major formatted array
};

export type ACROptions = {
  // optimal for animations. sometimes causes subpixel differences due to
  // rendering differences between offsets and transforms
  bakePositionIntoTransform?: boolean;

  // return the transform relative to this origin on the element,
  // rather than the element's own origin.
  useTransformOrigin?: string;
};

Example.svelte

(getActualClientRect does not require Svelte, but Svelte rocks!)

<script lang="ts">
  import { onMount } from 'svelte';
  import { getActualClientRect } from 'actual-client-rect';

  let baseElement: HTMLElement;
  let matchingElement: HTMLElement;

  onMount(() => {
    match();
  });

  function match(): void {
    const acr = getActualClientRect(baseElement);

    matchingElement.style.left = `${acr.basis.left}px`;
    matchingElement.style.top = `${acr.basis.top}px`;
    matchingElement.style.width = `${acr.basis.width}px`;
    matchingElement.style.height = `${acr.basis.height}px`;
    matchingElement.style.transformOrigin = acr.transformOrigin;
    matchingElement.style.transform = acr.transform;
  }
</script>

<div bind:this={baseElement} class="base" />
<div bind:this={matchingElement} class="matching" />

<style lang="scss">
  .base, .matching {
    outline: solid 2px;
  }

  .base {
    position: relative;
    top: 5em;
    left: 5em;
    width: 5em;
    height: 5em;

    transform: rotate(15deg);

    color: yellow;
  }

  .matching {
    position: fixed; // absolute works if offset parent is flush with viewport

    color: green;
  }
</style>

Example.svelte render

Limitations

  • getActualClientRect will not attempt to match, emulate, or mitigate bugs in rendering engines
    • stackoverflow: -webkit-transform-style: preserve-3d not working
    • some engines don't follow the preserve-3d used value specs, and still use preserve-3d even when combined with certain grouping properties:
      • Chrome v123 / Blink -- contain: strict | content | paint, content-visibility: auto
      • Firefox v124 / Gecko -- will-change: filter
      • Safari v17.4 / Webkit -- will-change: filter | opacity
      • (Properties not yet supported by particular browsers omitted from their respective lists)
  • gACR performance has not yet been profiled

Contribute

All contributions are greatly appreciated!

<3 anxpara