Git Product home page Git Product logo

img-with-aspect-ratio's Introduction

img-with-aspect-ratio

An unconventional but great JavaScript solution for "image resize and keep aspect ratio".

Demo

Check out the demo!

How it works

It generates an image with certain aspect ratio in memory, uses the image to make the browser calculate the width/height of the element and recalculate & apply them automatically when resized (keeping the aspect ratio).

Advantages

  • Once set up, no JavaScript is needed any more, doesn't rely on JS watchers or callbacks.
  • No obscure css, plays well with other css (e.g. max-width/min-width/media query).
  • Good compatibility across browsers, supports IE8.
  • Customizable crop options (crop/contain/position).
  • Supports all css units, 200px or 100%.
  • Reasonably good performance.
  • Only 1.8kb gzipped.

Installation

Download dist/img-with-aspect-ratio.min.js and include it in the HTML.

API

window.ImgWithAspectRatio.create
               (element, imageSrc, aspectWidth, aspectHeight,
                   primaryAttribute = "width", primaryAttributeValue = "100%",
                   { size = "contain", positionX = "center", positionY = "center", repeat = "no-repeat" } = {}
               );

element

DOM element, e.g.

window.document.getElementById('myImg');

imageSrc

URL of the image.

aspectWidth, aspectHeight

Both are numbers, together they determine the aspect ratio.

primaryAttribute

According to which attribute should the image resize. Must be set to either width or height.

The attribute specified in the primaryAttribute can later be modified by css or js and produce correct results.

Default value is width.

primaryAttributeValue

The value of the attribute specified in the primaryAttribute, supports all css length units (e.g. 50%, 200px).

Default value is 100%.

Can be set to "" (empty string) if you wish to specify it in CSS and don't wish to use !important.

imageOptions (last parameter)

The same as corresponding css background properties.

Check out w3schools css reference if you are not familiar with them.

Performance tips

Since in-memory generation of an image is involved, performance is affected by the total pixels of the image.

For the best performance, make aspectWidth/gcd and aspectHeight/gcd as small as possible (where gcd stands for Greatest Common Divisor of aspectWidth and aspectHeight), so that only an image of (aspectWidth/gcd) * (aspectHeight/gcd) px is needed.

In other words, make their gcd as big as possible.

Remember, aspectWidth=10000,aspectHeight=6000 is 4,000,000 times faster and more memory-efficient than aspectWidth=10000,aspectHeight=6001 !

TODOs

  • JavaScript reconfiguration API.
  • jQuery plugin.
  • AngularJS directive.
  • Angular 2 component.
  • React component.

See also

Experimental project: div-fixed-aspect-ratio

img-with-aspect-ratio's People

Contributors

kevinwang15 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

rickiswright

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.