Coder Social home page Coder Social logo

bkwld / vue-visual Goto Github PK

View Code? Open in Web Editor NEW
59.0 6.0 7.0 14.53 MB

Vue 2 image and video loader supporting lazy loading, background videos, fixed aspect ratios, low rez poster images, transitions, loaders, slotted content and more.

Home Page: https://bkwld.github.io/vue-visual

License: MIT License

JavaScript 74.86% CoffeeScript 15.44% Vue 7.91% CSS 1.79%
vue video-player loader lazy-loading srcset intersectionobserver

vue-visual's Introduction

Vue Visual npm

Vue 2 image and video loader supporting lazy loading. Visual 2.x is a simplification of Version 1.x with a greater reliance on modern browser features (IntersectionObserver, object-fit, srcset, sizes, etc).

Examples at https://bkwld.github.io/vue-visual.

Installation

  1. Install the package: npm install --save vue-visual or yarn add vue-visual
  2. Register the component:
    import Vue from 'vue'
    import Visual from 'vue-visual'
    Vue.component('visual', Visual)
    import 'vue-visual/index.css'
  3. These polyfills are recommended for older browsers:

Usage

See the Storybook.

Props

A list of the component properties that may be set on the Visual component.

Assets

  • image (string) : The URL of an image to load.

  • srcset (string) : An img srcset, used in addition to the image. Both are recommended.

  • webp-srcset (string) : A srcset that will be added to a source inside of a picture element with a type of image/webp.

  • video (string|array) : A video that is loaded after the image is loaded if the device supports video. If a string, should be the URL to a source video. If an array, a list of video URLs that will be added as difference <source>s.

Size

  • width (number|string) : This width will be applied to the asset element. If a number, it's assumed to be a px value.

  • height (number|string) : See width

  • max-width (number|string) : This value will be applied to the asset element as the css max-width. If a number, it's assumed to be a px value.

  • sizes (string) : Specify the img sizes attribute.

  • aspect (number) : Force the Visual to a specific aspect ratio. This works by making the asset position:absolute and then using an inner div with a padding-top set to a percentage.

  • expand (boolean) : Make the Visual fill it's container via CSS using absolute positioning.

Style

  • object-fit (string) - Default cover. Like the CSS property.

  • object-position (string) - Default center center. Like the CSS property.

  • align (string) - Default center middle.. Used in conjunction with slots to position the slot content. May be any combination of one horizontal (left, center, right) and one vertical (top, middle, bottom) choice, space-delimited.

Loading

  • preload (boolean) - Requires Nuxt framework. If true will add <link rel="preload"/> tags to the <head> for the image assets.

  • autoload (boolean) - Default: true. If true, assets are loaded immediately unless lazyload.

  • lazyload (boolean) - Waits until the Visual enters the viewport to trigger loading. Overrides autoload.

  • intersection-options (object) - IntersectionObserver options. Used with lazyload and autopause.

  • placeholder-color - Sets a background color behind the assets. Most useful in conjunction with an aspect value.

  • transition (string, boolean) - Default: 'vv-fade'. A Vue transition name that is applied when an asset is loaded. Set to an empty string to immediately render assets rather than waiting for loading.

Video

  • autoplay (boolean) - If true, begins playing immediately.

  • autopause (boolean) - If true, begins playing when the Visual enters the viewport and stops when it leaves. Overrides autoplay.

  • loop (boolean) - Sets <video> loop

  • muted (boolean) - Sets <video> muted

  • controls (boolean) - Sets <video> controls

Accessibility

  • alt (string) - Sets the alt attribute or aria-label value, depending on context.

Slots

  • default: Markup is added after the assets and before the loader
  • image-source: Adds <source> tags to the <picture> element.
  • video-source: Adds <source> tags to the <video> element.

Methods

  • load() - Manually initiate loading.
  • play() - Tell video to play.
  • pause() - Tell video to pause.
  • restart() - Tell video to restart playback from beginning.

Events

  • loaded:image - Image asset has finished loading
  • loaded:video - Video asset has finished loading
  • loaded - All assets hvae loaded

Contributing

  • Boot up the Storybook with yarn storybook and use that as your HMR friendly dev environment
  • Use npm version ... to build, tag, and update the poblished storybook

Changes from 1.x

  • Dropped props:
    • poster
    • fallback
    • per-asset variants for load, etc
  • Prop changes
    • backgroundobject-fit
    • background-positionobject-position
    • fillexpand
  • Not testing for video support on device
  • Video playing state not stored in Vue state
  • Image and video loaded simultenously, not in series
  • Removed setDefaults for setting default options. See custom-defaults for an example of how to implement this functionality using a functional component.

migrate-1.x.coffee shows an example of a functional component that migrates the old API to the new API.

vue-visual's People

Contributors

dependabot[bot] avatar jonjahr avatar lemoswilson avatar thelucre avatar weotch 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

vue-visual's Issues

v0.1 todos

  • Pausing before testing render='load'
  • Cleanup listeners on img and video for loading on component destroy .. or can i use the on... style listeners and not have to cleanup. Like oncanplaythrough
  • Implement setting aspect by measuring the asset ... or ditch that feature Ditching
  • Measure the dimensions of the container for containerAspect ... we may need to keep this updated for biding to work. Maybe there should always be listeners added on resize to make this simple to understand.
  • Add support for visible to load, autoplay, and autoause
  • Add transitions
  • Add slots with vertical align
  • Add alt
  • Add spinner
  • Add render='visible' ... maybe this means working like video where I have a v-show and a v-if on each asset.
    • v-if is triggered by shouldLoad, v-show is triggered by shouldRender
    • Maybe shouldRender should change to shouldShow ... like maybe all loading always adds to the DOM immediately.
    • Note, when a later-loading asset is loaded, the removing asset should be toggled away via v-show (for now) because of vuejs/vue#4360
    • Actually, I got rid of it altogether
  • Refactor into multiple src files.
    • Like src/loading.coffee, src/render.coffee.
    • Each returns an object with computed, methods, etc. Also, will return it's own data key. Index.vue will loop through each object, remove data (merging it into a different data object) and then export a new object with the merged configs (sans data) as well as a data function that returns the data object.
  • Go through examples and make sure its all up to date.
  • Add Travis-CI integration

Demo page would be good

Maybe creating a demo page for the different properties would be a good move. After all, a picture is worth 1,000 words!

Great component though. Thanks for taking the trouble to create and share it.

Prop to disable dragging

Currently the only way to prevent user dragging of a visual from vue-visual is to use a deep selector. I believe it'd be cleaner to add a prop like prevent-dragging or draggable='false' to vue-visual to add it to the html structure of the visual rather than rely on CSS.

Add support for tracking progress

The current load progress would be passed to the loader component through a prop based interface. Like:

# loader-component.vue
module.exports =
  props: ['progress', 'total', 'asset']
  • progress - Fractional progress percentage
  • total - Total filesize in bytes
  • asset The asset being loaded (poster, image, etc)

Add `poster-from-image` prop

When true and when image is an object, use the smallest image src for the poster value. Useful when a CMS can easily generate multiple sizes, it will save you from having to set a separate prop for poster.

Add a slot align-strategy prop

  • vertical-align - The default
  • absolute - The transform apporach
  • table - Does display:table and display:table-cell on slot, useful when you want to use a min-height height on the visual.

videoLoaded never gets set to true in some instances when video is cached

In some instances, the videoLoaded boolean never gets set to true. When I tested this, I found out that it's because the video element's canplaythrough event never fired. I think this is because the video was cached, and the video element triggered the canplaythrough event so quickly, it fired before Vue could register the event handler. One solution I found is to check the video readyState when the component mounts:

@videoLoaded = true if @$refs?.video?.readyState > 3

Here's a stack overflow that talks about this.

Annoyingly, I can't reproduce this on Netlify, or even in vue-visual Storybook. I can only reproduce this when I run my Nuxt stack on localhost. Maybe Nuxt slows down the mounting/hydration (only in dev mode) just enough for visual to miss the canplaythrough event? I don't know.

I'll send a pull request with the fix in a sec.

Add support for setting size using native values

From previous version of docs:


Use native asset dimensions for the size

<visual
	image='image.png'
	background='cover'
	width='image'
	height='image'>
</visual>

Once the image image has loaded, the native image size will be used for the Visual dimensions. You may use poster as the value as well. Or, use the shorthand size='image' in place of width and height.


Also, from the size docs:

May also be a string matching one of the asset properties (poster, image, video, fallback) to use the native width of the asset. Note, these values cannot be read until the asset has loaded, so the Visual will be dimension-less until load has completed.


Also, from aspect docs:


You can also use the measured dimensions of the image or poster for the aspect, although these can't be determined until the image has actually fully loaded:

<visual
	image='image.png'
	background='cover'
	aspect='image'>
</visual>

Make a responsive-height-craft-visual component.

The point would be for 100vw x 100vh images where srcset alone isn't going to cut it. Take the case of a square viewport and a 16:9 image. It's going to get scaled up to fit the viewport. The solve would be something like this, where we create a bunch of source elements. It would pretty much requires an image CDN to work.

This would probably consume responsive-craft-visual. I don't have a clear line of site on the API for this.

Manual Load() method does not work

I feel that the documentation is not clear about how to use the manual load, pause, play and restart methods.

Screen Shot 2021-06-23 at 11 36 00 AM

I used the code snippet from here: https://bkwld.github.io/vue-visual/?path=/story/assets--video

However it seems that the load, pause, play and restart methods need to be manually created/defined in my component methods. In your example, all these methods are hidden, I don't know what they are supposed to be doing?

The documentation says to use load on a click event, but what is it supposed to do? how do I actually get the video/image to load after clicking on a button? It is not mentioned anywhere.

My assumption is that I have to add soemthing like this (see below). and then define what happens when you click on load. But i have no idea how to make it work. Please help?

methods: {
load() {
...
},
pause() {
...
},
play() {
...
},
restart() {
...
}
}

Your example code:

<div>
  <div style='margin-bottom: 0.5em'>
<!-- NONE OF THESE BUTTONS WORK CAUSE THE METHODS ARE NOT DEFINED, THE EXAMPLE ON THE WEBSITE DOES NOT SHOW HOW TO HANDLE THESE METHODS, HENCE IT IS NOT USEABLE -->
  <button @click='load'>Load</button>  
  <button @click='pause'>Pause</button>
    <button @click='play'>Play</button>
    <button @click='restart'>Restart</button>
  </div>
  <visual 
    ref='visual' 
    :video='video' 
    :autoload='autoload'
    :autoplay='autoplay'
    :loop='loop'
    muted
    width='100%' />
</div>

Render an initial asset during SSR

Currently, when vue-visual gets SSRed via Nuxt, there is no asset rendered. It would be nice if the rendering of the initial asset during SSG didn't depend in Nuxt hydrating the JS. This would also make it work better with lazy-hyrdate.

Add `watch-container-size` prop

When true, keeps the container size variables up to date even if there is no video.

In addition, if there is an object image config, the container size will be used for determining which image to show, not the window. Thus, if an image takes only 50% of the width, this will use a smaller image than it normally would.

  • Do a JS perf to see if reading only one dimension is faster.

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.