GithubHelp home page GithubHelp logo

gestixi / image-scale Goto Github PK

View Code? Open in Web Editor NEW
157.0 17.0 45.0 61 KB

Scale images to fit or fill any target container via two simple properties: scale and align

License: MIT License

JavaScript 100.00%

image-scale's Introduction

Image Scale

Scale images to fit or fill any target container via two simple properties: scale and align.

This plugin is greatly inspired from Sproutcore SC.ImageView.


Installation

image-scale depends on jQuery. To use it, include this in your page :

<script src="jquery.js" type="text/javascript"></script>
<script src="image-scale.js" type="text/javascript"></script>

Usage

If you want to identify the images that you want to scale, you can add a class to them. In this example we are adding a class call scale.

You can also set the data-scale and data-align attributes directly to the images if you want to override the default setting.

<div class="image-container">
  <img class="scale" data-scale="best-fit-down" data-align="center" src="img/example.jpg">
</div>

Now add this JavaScript code to your page :

$(function() {
  $("img.scale").imageScale();
});

You're done.


Properties

scale

Determines how the image will scale to fit within its containing space. Possible values:

  • fill - stretches or compresses the source image to fill the target frame
  • best-fill - fits the shortest side of the source image within the target frame while maintaining the original aspect ratio
  • best-fit - fits the longest edge of the source image within the target frame while maintaining the original aspect ratio
  • best-fit-down - same as best-fit but will not stretch the source if it is smaller than the target
  • none - the source image is left unscaled
Type: String
Default: best-fill

align

Align the image within its frame. Possible values:

  • left
  • right
  • center
  • top
  • bottom
  • top-left
  • top-right
  • bottom-left
  • bottom-right
Type: String
Default: center

parent

A jQuery Object against which the image size will be calculated. If null, the parent of the image will be used.

Type: jQuery Object
Default: null

hideParentOverflow

A boolean determining if the parent should hide its overflow.

Type: Boolean
Default: true

fadeInDuration

A duration in milliseconds determining how long the fadeIn animation will run when your image is scale for the firstTime.

Set it to 0 if you don't want any animation.

Type: Number or String
Default: 0

rescaleOnResize

A boolean indicating if the image size should be rescaled when the window is resized.

The window size is checked using requestAnimationFrame for good performance.

Example:

$images.imageScale({ 
  rescaleOnResize: true
});
Type: Boolean
Default: false

didScale

A function that will be call each time the receiver is scaled.

Example:

$images.imageScale({ 
  didScale: function(firstTime, options) {
    console.log('did scale img: ', this.element);
  }
});
Type: Function
Parameters: 
  - firstTime {Boolean} true if the image was scale for the first time.
  - options {Object} the options passed to the scale method.

debug

A number indicating the debug level :

  1. silent
  2. error
  3. error & warning
  4. error & warning & notice
Type: Number
Default: 0

Methods

scale

Main method. Used to scale the images.

When rescaleOnResize is set to true, this method is executed each time the windows size changes.

If rescaleOnResize is set to false, you may want to call it manually. Here is an example on how you should do it:

$image.imageScale('scale');

destroy

Removes the data for the element.

Here is an example on how you can call the destroy method:

$image.imageScale('destroy');

Demo

See it in action on our home page.

You can also check out the Sproutcore Automatic Image Scaling demo to understand the difference between all the different options.


Size

Original Size: 4.3KB gzipped (15.04KB uncompressed)

Compiled Size: 1.9KB gzipped (4.65KB uncompressed)


Author

Nicolas Badia


Copyright and license

Copyright 2013-2016 GestiXi under The MIT License (MIT).

image-scale's People

Contributors

nicolasbadia avatar ssavajols avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

image-scale's Issues

Conflicts with Jquery UI

any of you had a project that jquery ui and image-scale together did you experience conflicts between the two?

Script not working

Today I tried to use your script, but unsuccessfully. Could you explain what steps I should do?

  1. linked your js file (from local) and jquery (from the Net),

  2. added the script (in html head):

    $(function() {
    $("img.scale").imageScale();
    });
    
  3. added the html code -> class="scale" data-scale="best-fit-down" data-align="center" in the child element (an image)

But the child img element didn't fit to the parent element...

background-image

can this work on background-image ?
i'm new here, don't know if it's the right place to ask...

thanks

image-scale fiddles with opacity

Hello, i am using image-scale on images that need to be hidden until i fade them in via jQuery. But it seems like image-scale overwrites the opacity values i set in css.

I dont quite understand why image-scale does it in the first place. Removing the opacity lines in the .js file renders it unusable. Probably because i can't quite write js at all.

Hope you can help.

Exif Orientation leads to wrong scaling

Hi,
first I like your script. Very well done!
I have in my project

img {
	image-orientation: from-image;
}

With this enabled images are wrongly scaled when the image needs to be rotated because of the orientation. As I did not find a quick solution to read the exif information I did it quick and dirty change in row 213.

if (Math.round(img.naturalHeight/img.naturalWidth * 100) == Math.round(img.width/img.height * 100)) {
    	that.imgHeight = img.naturalWidth || img.width;
        that.imgWidth = img.naturalHeight || img.height;
    }
    else {
	    that.imgWidth = img.naturalWidth || img.width;
	    that.imgHeight = img.naturalHeight || img.height;
    }

And it works (at least in my cases) Would be great if you have a cleaner solution.
Thanks again.

UMD

Please use UMD. So we can import it with requirejs, webpack, browserify or globally.

It is not usable with webpack right now.

Add jquery dependency whitin your module declaration.

the height of div container is required?

It is great that install this script is easy, and the performance of the scale is perfect.
My question: I must set the height of the div container, or the image won't be displayed.
Is there a bug or I got some error?
Thanks.

Rescale On Resize

Could you show me a example how to insert the Boolean?
Thanks you in advance!

Negative offset

Hi there,

Been using this in something for awhile now. Works pretty great.

However I've noticed sometimes for some reason the container has 1 pixel width and height more than the image and the image gets an offset of -0.5px for top and left which I think at some window sizes the browser will round it to move a full pixel and sometimes not which causes it to cut off the first pixel of the top and left of the image which is quite noticeable when that is a 1px border around the image.

I don't know if this is the best way to fix it or not but I found that adding Math.ceil to the layout.x and layout.y values fixed it for me. I'm wondering if maybe there is a better way somewhere else?

$element.css({position: 'absolute', top: Math.ceil(layout.y) + 'px', left: Math.ceil(layout.x) + 'px', width: layout.width + 'px', height: layout.height + 'px', 'max-width': 'none'});

Thanks!

Not working in IE9

First, IE9 displays an inline style of opacity: 0 even after being loaded. The image is not centering or filling the parent.

All of my scripts are called before the ends.

My code:

jQuery(function($) {
  $(".grid img").imageScale({scale: "best-fill", rescaleOnResize: true, fadeInDuration: "0"});
});

Otherwise, IE11, Mac Safari, Chrome, Firefox—all good!

For video

This would be excellent for video. Am thinking I could just test for video option and then let remainder do its work ?

Smooth transition between original size and new calculated size

Hi, thanks for the great library. I have a problem when loading portions of a page using ajax and then applying image-scale. The problem is that the images jump from the original size to the new size and that makes it feel uncomfortable when watching things load. Is it possible to have an option to animate the transition from the original size to the new size?

I tried mitigating the issue by tweaking opacity in the whole div but the images load in different times and I don't want to wait for all of them to load before removing alpha.

Resonsive issue

It should be responsive while changing the browser windows.

Thanks

The description could be update with default css for .image-container class

Hey guys,

Thanks for a great component! It worked very well for me, but initially I got confused and I thought it doesn't work at all, because I didn't set the basic minimum of css properties for .image-container class. The most important one is that it's "position:" should be "relative". I think to avoid this confusion for other people you could update your example with this note.

Thanks for the great job!

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.