Code Monkey home page Code Monkey logo

retinajs's Introduction

JavaScript, LESS helper for rendering high-resolution image variants

retina.js makes it easy to serve high-resolution images to devices with retina displays

How it works

When your users load a page, retina.js checks each image on the page to see if there is a high-resolution version of that image on your server. If a high-resolution variant exists, the script will swap in that image in-place.

The script assumes you use Apple's prescribed high-resolution modifier (@2x) to denote high-resolution image variants on your server. It is also possible to override this by manually specifying the URL for the @2x images using data-at2x attributes.

For example, if you have an image on your page that looks like this:

<img src="/images/my_image.png" />

The script will check your server to see if an alternative image exists at /images/[email protected]

However, if you have:

<img src="/images/my_image.png" data-at2x="http://example.com/[email protected]" />

The script will use http://example.com/[email protected] as the high-resolution image. No checks to the server will be performed.

How to use

JavaScript

The JavaScript helper script automatically replaces images on your page with high-resolution variants (if they exist). To use it, download the script and include it at the bottom of your page.

  1. Place the retina.js file on your server
  2. Include the script on your page (put it at the bottom of your template, before your closing </body> tag)
<script type="text/javascript" src="/scripts/retina.js"></script>

You can also exclude images that have no high-res version. Simply add the data-no-retina attribute.

<img src="/path/to/image" data-no-retina />

###LESS & SASS

The LESS & SASS CSS mixins are helpers for applying high-resolution background images in your stylesheet. You provide it with an image path and the dimensions of the original-resolution image. The mixin creates a media query specifically for Retina displays, changes the background image for the selector elements to use the high-resolution (@2x) variant and applies a background-size of the original image in order to maintain proper dimensions. To use it, download the mixin, import or include it in your LESS or SASS stylesheet, and apply it to elements of your choice. The SASS versions require you pass the extension separately from the path.

Syntax:

.at2x(@path, [optional] @width: auto, [optional] @height: auto);
@include at2x($path, [option] $ext: "jpg", [optional] $width: auto, [optional] $height: auto);

Steps:

  1. LESS - Add the .at2x() mixin from retina.less to your LESS stylesheet (or reference it in an @import statement) SASS - Add the @mixin at2x() from retina.scss or retina.sass to your SASS stylesheet (or reference it in an @import)
  2. LESS - In your stylesheet, call the .at2x() mixin anywhere instead of using background-image SASS - In your stylesheet, call @include at2x() anywhere instead of using background-image

This:

.logo {
  .at2x('/images/my_image.png', 200px, 100px);
}
.logo {
  @include at2x('/images/my_image', png, 200px, 100px);
}

Will compile to:

.logo {
  background-image: url('/images/my_image.png');
}

@media all and (-webkit-min-device-pixel-ratio: 1.5) {
  .logo {
    background-image: url('/images/[email protected]');
    background-size: 200px 100px;
  }
}

retinajs's People

Contributors

vojtesaak avatar

Watchers

James Cloos avatar  avatar

Forkers

storyous

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.