Coder Social home page Coder Social logo

eladcandroid / vue-responsive-image Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hcesrl/vue-responsive-image

0.0 1.0 0.0 229 KB

A Vue component that lets you quickly create responsive image tags with an optimal number of image sources for all devices.

License: MIT License

JavaScript 46.37% Vue 44.16% HTML 9.47%

vue-responsive-image's Introduction

Vue Responsive Image

Vue Responsive Image is a Vue component that allows you to quickly insert responsive image tags in your Vue project, provided you have some automated system that produces the various sizes of images required. The component calculates all source sizes and, depending on the parameters passed, generates the appropriate <img> tag and its srcset attribute and, if needed, separate <source> tags for tablet portrait and smartphone views.

Requirements

You need to have some system that automatically generates the images for you when an URL with the appropriate format is called, for example: http://via.placeholder.com/1920x1080

Standard Installation

npm

npm install --save-dev vue-responsive-image

Vue

Import the component in the *.vue template that uses it

import VueResponsiveImage from 'vue-responsive-image'

Fast installation

Just copy the ResponsiveImage.vue component somewhere in your project and import it in the templates that need it. Remember that this way your component won't be automatically upgraded with bugfixes and improvements, though!

Using a .js file

You can also include the script you find in the dist/ folder called vue-responsive-image-web.min.js. This way you can use it directly in simple Vue-based pages. To find out how to include it, check the HTML file in web-test/web-test.html. Keep in mind that in this case your component won't be automatically upgraded unless you manually update the script.

Usage

VueResponsiveImage must be used as a component in your template.

Minimal usage:

<vue-responsive-image
    :image-url="'http://via.placeholder.com/%width%x%height%'"
    :image-ratio="16/9"
  ></vue-responsive-image>

Complete set of options:

<vue-responsive-image
    :image-url="'http://via.placeholder.com/%width%x%height%'"
    :error-image-url="'https://via.placeholder.com/160x90.png/09f/fff?text=Not+Found'"
    :show-error-image="true"
    :width-on-screen="50"
    :width-on-screen-tablet="75"
    :width-on-screen-smartphone="100"
    :image-ratio="16/9"
    :alt="'test1'"
    :image-class="'vw50'"
    :mode="'all'"
  ></vue-responsive-image>

Parameters

It currently supports the following parameters:

ParameterDefault value
image-urlN/aThe base url to create images, with a width and height placeholder which will be replaced by the component. Example:http://via.placeholder.com/%width%x%height%. %width% and %height% will be replaced with the calculated width and height for each image.
show-error-imagefalseWhether to show the error image instead of the broken image if the current image fails to load
error-image-urlN/aThe URL for an image to show in case of failure to load the image. It can be an inline SVG. The component now detects image load failures and instead of showing broken images shows an error image. The default is a grey-background svg
width-on-screen100The percentage of the screen the image will occupy in the layout. Used to determine the various s0izes for the srcset of the image
width-on-screen-tablet-The percentage of the screen the image will occupy in the layout on tablet-portrait mode, only if different from default
width-on-screen-smartphone-The percentage of the screen the image will occupy in the layout on smartphone mode, only if different from default
image-ratio16/9The width to height ratio of the image, used to determine the height of the final images
alt''The alt attribute for the image
class''The extra class(es) to be added to the image. The class "vue-reponsive-image" is added by default
mode'all'Possible values: 'all', 'tablet', 'smartphone', 'mobile'. The desidered mode of operation for the main image, if only "mobile" is passed, then desktop sizes will not be generated, etc.

Demo

You can find a simple demo here: https://codepen.io/paolomioni/pen/PeNoQg

Principles

This plugin is based on the analyses I made for my clients, and explained in an article I wrote which you can find here:

Applying srcset: choosing the right sizes for responsive images at different breakpoints

Assumptions

This plugin assumes that you are using the same image, with the same proportions (the same width and height ratio), for all breakpoints. It might support different ratio images in the future.

It also assumes that you will use the same image width for desktop and tablet landscape, but that you might need a different overall image width for tablet portrait and smartphone.

For example, your image might occupy 33% of the screen on desktop, 50% on tablet portrait, and 100% on smartphones. This requires completely different sizes for the final images in the HTML, and the plugin calculates those for you. This is by far the most common use case according to my experience.

Testing

There is a unit test suite made with mocha-webpack. Just clone the project, install with npm, and run npm run test.

Roadmap

  • More sophisticated options for different breakpoints and different ratios
  • Automatic lazy loading using the Intersection Observer API
  • Integration with some routine to produce the images with server-side tools according to the plugin's logic
  • A more thorough unit test suite
  • Switch to a single config parameter? Not sure if it's needed

Feel free to contact me with proposal, requests, and pull requests. Bugs and issues should be submitted on the GitHub page of the project.

License

MIT

vue-responsive-image's People

Contributors

pmioni avatar eladcandroid avatar zearin avatar

Watchers

James Cloos 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.