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

image-focus

v1.2.1

Published

A dependency free utility for cropping images based on a focal point ~2.13kB gzipped

Downloads

11,727

Readme

image-focus 📷

A dependency free utility for cropping images based on a focus point ~2.13kB gzipped

npm version npm downloads MIT

Demo

Demo

Check out the demo and then play with editing it

Docs

Docs generated by typedoc

Usage

Focus Coordinates range between -1 and 1 for both x and y axes. The FocusPicker class will help enable users to select the focus point to be used with an image.

FocusedImage

There are two ways to supply the coordinates when initializing the FocusedImage class

Data Tags

<div class="focused-image-container">
  <img class="focused-image" src="https://picsum.photos/2400/1400" data-focus-x="0.34" data-focus-y="-0.21">
</div>
import { FocusedImage } from "image-focus"

const img = document.querySelector('.focused-image') as HTMLImageElement
const focusedImage = new FocusedImage(img)

Using focus Option

<div class="focused-image-container">
  <img class="focused-image" src="https://picsum.photos/2400/1400">
</div>
import { FocusedImage } from "image-focus"

const img = document.querySelector('.focused-image') as HTMLImageElement
const focusedImage = new FocusedImage(img, {
  focus: {
    x: 0.34,
    y: -0.21
  }
})

FocusPicker

Provide an onChange callback that will receive a Focus object that has x and y properties for the newly selected coordinates. Optionally supply a focus to initialize with, or a retina src to use instead of the default white ring SVG.

import {FocusedImage, FocusPicker} from "image-focus"

const imgEl = document.getElementById("focused-image") as HTMLImageElement
const focusedImage = new FocusedImage(imgEl)

const focusPickerEl = document.getElementById("focus-picker-img") as HTMLImageElement
const focusPicker = new FocusPicker(focusPickerEl, {
  onChange: focus => focusedImage.setFocus(focus),
  focus: startingFocus,
})

What's going on?

The <img/> element is being set to position: absolute; and having its top and left properties adjusted based on some calculations using the image and parent containers' aspect ratios and dimensions. The <img/>'s parent container gets set to position: relative; and overflow: hidden; to create the effect. There are a few other inline styles that get applied, so if anything appears to be behaving unexpectedly, be sure to check that the inline styles on both the <img/> and its parent aren't being overridden by CSS on your page (especially from rules using !important).

Additionally, because the FocusedImage is positioned absolutely so it can shift as needed, its container needs to manage its own height and width. If you aren't seeing an image appear at all, it is likely that the parent div's height is fully collapsed.

What if I'm not using npm and a build process?

That's okay! unpkg has you covered. Just add this script tag to your page and the image-focus module is exposed in the global namespace under window.imageFocus.

<script src="https://unpkg.com/[email protected]"></script>

Then in some script that loads after the above script tag:

var imgEl = document.querySelector('img.focused-image');
var focusedImage = new imageFocus.FocusedImage(imgEl, {x: 0.25, y: -0.3});

Attributions

This project was largely inspired by and adapted from jquery-focuspoint by jonom and used typescript-library-starter to scaffold the build process.