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

object-fit-math

v1.0.0

Published

Just the math behind object-fit and object-position

Downloads

6,159

Readme

object-fit-math

Just the math behind object-fit and object-position

It has no dependencies and just exposes a few simple functions

Allows you to use the same math as the browser uses for object-fit and object-position

Supports the following object-fit properties:

contain, cover, fill, none, scale-down

Supports object-position specified in px or % only

Use cases:

  • using the same algorithm as the browser elsewhere, like on an HTML canvas or server side image rendering
  • finding out the bounds for an object that's using object-fit and object-position - this is what I actually wrote this for, so I could translate the mouse/touch event locations to the actual pixel location on a fitted img/canvas/video element
  • you could use it to make a polyfill I guess

usage

npm install object-fit-math

basic usage

const { fit, position, fitAndPosition } = require( 'object-fit-math' )

const parentSize = { width: 320, height: 240 }
const childSize = { width: 90, height: 160 }

const fitted = fit( parentSize, childSize, 'contain' )

// { width: 135, height: 240 }
console.log( fitted )

const positioned = position( parentSize, fitted, '50%', '50%' )

// { x: 92.5, y: 0 }
console.log( positioned )

const rect = fitAndPosition( parentSize, childSize, 'contain', '50%', '50%' )

// { x: 92.5, y: 0, width: 135, height: 240 }
console.log( rect )

translate a point on the fitted element to original pixel location

const { transformFittedPoint } = require( 'object-fit-math' )

// get the object eg a canvas that uses object-fit from the DOM here

canvas.addEventListener( 'click', e => {
  const { offsetX: x, offsetY: y } = e

  const canvasSize = canvas.getBoundingClientRect()
  const { objectFit, objectPosition } = getComputedStyle( canvas )
  const [ left, top ] = objectPosition.split( ' ' )

  const childPoint = transformFittedPoint(
    { x, y }, canvasSize, canvas, objectFit, left, top
  )

  console.log( childPoint )
})

types

See /src/types.ts for Size, Point etc

exported functions

fit = ( parent: Size, child: Size, fitMode: FitMode = 'fill' ) => Size

position = ( parent: Size, child: Size, left = '50%', top = '50%' ) => Point

fitAndPosition = (
  parent: Size, child: Size,
  fitMode: FitMode = 'fill', left = '50%', top = '50%'
) => Rect

transformFittedPoint = (
  fittedPoint: Point, parent: Size, child: Size,
  fitMode: FitMode = 'fill', left = '50%', top = '50%'
) => Point

isFit = ( value: any ): value is FitMode => boolean

examples

transform event point to pixel

The transform example creates a small canvas then fits it to its parent element - you can then click anywhere on the canvas to get the actual pixel location. Useful for finding out the actual pixel that was clicked on a video/canvas/img which uses object-fit

compare

The compare example generates a long list of combinations of parent and child sizes, fit modes, positions etc

Each comparison shows the result of the DOM's built in object-fit and object-position on the left, and on the right it uses this module to get the rectangle and uses absolute positioning and sizing to replicate the effect

building

You will need typescript installed globally to build the module, and browserify installed globally to build the examples

npm install typescript -g

npm run build

npm install browserify -g

npm run build-examples

contributing

Please try to follow the existing style (sorry, no linting etc at the moment) and keep your pull requests small and focussed

testing your changes

The easiest way to tell if your changes are correct is to build the examples and then scroll through the compare example - yes, this file is huge, but it ensures that a good combination of inputs is represented. Once you verify visually that the math matches the DOM behaviour, there is a <pre> element at the bottom that contains the fixture data used by the tests, copy and paste this to /src/test/fixtures.json and run the tests.

license

MIT License

Copyright (c) 2020 Nik Coughlin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.