GithubHelp home page GithubHelp logo

hiteshm01 / image-focus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from third774/image-focus

0.0 1.0 0.0 9.95 MB

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

Home Page: http://npmjs.com/package/image-focus

License: MIT License

TypeScript 71.11% HTML 27.72% JavaScript 1.17%

image-focus's Introduction

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.

image-focus's People

Contributors

third774 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.