Code Monkey home page Code Monkey logo

canvid's Introduction

canvid.js

CDNJS

canvid is a tiny dependency free library for playback of relatively short videos on canvas elements.

  • Why not just use HTML5 video?
    Because you can't until Oct 2016 you could not embed and autoplay HTML5 videos on iOS! Yeah, that sucked.

  • Why is this better than using an animated GIF?
    Videos look kind of gross when converted to animated GIFs because of the colors sampling. Also the file size of video GIFs can get pretty huge. GIF is just not made for videos. JPG does a much better job of compressing video frames. Also, animated GIFs don't give you any playback controls. You can't pause a GIF or delay it's playback. With canvid you can do that.

  • Why only "relatively short" videos?
    As you see further down, the container format for canvid is a big image sprite of all the frames of each clip. Sadly, iOS limits the maximum image size (bigger image get sampled down), so that puts a limit on the maximum frames you can store.

  • Why is there no audio?
    canvid doesn't support audio for the same reason animated GIF doesn't support audio either: because that's not what it was built for. (if you need audio, try iphone-inline-video)

Installation

npm

$ npm install --save canvid

git clone

$ git clone [email protected]:gka/canvid.git

Usage

You can use canvid.js with AMD, CommonJS and browser globals.

var canvidControl = canvid({
    selector : '.video',
    videos: {
        clip1: { src: 'clip1.jpg', frames: 38, cols: 6, loops: 1, onEnd: function(){
          console.log('clip1 ended.');
          canvidControl.play('clip2');
        }},
        clip2: { src: 'clip2.jpg', frames: 43, cols: 6, fps: 24 }
    },
    width: 500,
    height: 400,
    loaded: function() {
        canvidControl.play('clip1');
        // reverse playback
        // canvidControl.play('clip1', true);
    }
});

If you want to use canvid with React you can check this simple react + canvid demo to see how it works.

Options

  • videos required
    Video/Sprite objects (videoKey : videoOptions).

    • src required
      Path of the sprite image.

    • frames required
      Number of frames.

    • cols required
      Number of columns.

    • loops optional
      Number of loops.

    • fps optional (default: 15)
      Frames per second.

    • onEnd optional
      Function that gets called when the clip ended.

  • selector optional
    The selector of the element where the video gets displayed. You can also pass a DOM element as a selector. default: '.canvid-wrapper'

  • width optional
    Width of the element where the video gets displayed.
    default: 800

  • height optional
    Height of the element where the video gets displayed.
    default: 450

  • loaded optional
    Function that gets called when all videos are loaded.

  • srcGif optional
    Path of the fallback gif, if canvas is not supported.

Methods

The canvid function returns an object to control the video:

var canvidControl = canvid(canvidOptions);

play
Plays video of the passed videoKey. The parameters isReverse (default: false) and fps (default: 15) are optional.

canvidControl.play(videoKey [,isReverse, fps]);

pause
Pause current video.

canvidControl.pause();

resume
Resume current video.

canvidControl.resume();

destroy
Stops video and removes the canvas of the current canvid element from the DOM.

canvidControl.destroy();

isPlaying
Returns true or false whether the video is playing or not.

canvidControl.isPlaying();

getCurrentFrame
Returns the current frame number.

canvidControl.getCurrentFrame();

setCurrentFrame
Sets the current frame number.

canvidControl.setCurrentFrame(0);

How to convert your video to a JPG sprite

First, convert you video into single frames using ffmpeg:

mkdir frames
ffmpeg -i myvideo.mp4 -vf scale=375:-1 -r 5 frames/%04d.png

Then, use ImageMagicks montage to stich all the frames into one big image:

montage -border 0 -geometry 375x -tile 6x -quality 60% frames/*.png myvideo.jpg

Is canvid responsive?

Yes it is, thanks to a nice little trick. Regardless of what width and height parameters you set in the canvid constructor, you can use style="width:100%" on the canvas element and it will get scaled to the outer container and preserve its original aspect ratio.

canvas.canvid {
  width: 100%;
}

Known Issues

Some users encountered problems on mobile devices with large sprites. A workaround is to split the sprite into multiple sprites.

If you're experiencing problems with montage (e.g. "unable to read font") try updating imagemagick to the latest version.

Contributors

canvid's People

Contributors

gka avatar kennynaoh avatar moklick avatar ryancurtin avatar thomascharbit 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

canvid's Issues

Control video through scrolling

Hey,

thanks for this library. Just playing around because I'm searching right now for a solution for an animation of mine. Have you ever tried to control the video through scrolling? I'm thinking of a like parallax effect. The video "plays" while scrolling. Back or forward. Do you think this is possible?

Greets

about android

it can run pc and ios ,but in android ,it can't run.

why ?

Broken in Chrome 48 for Android

The image playback has an issue with the origin position.
Tested in Chrome version 48 on Android 6.0. Maybe a Chrome issue.

Integrate a canvid within a canvas

Hi, thanks for canvid, it's a great idea. I have very little experience of canvas and I was wondering if it was possible to integrate a canvid video within the wider "context" (in the ordinary sense) of a canvas element, so the canvas could contain other stuff along with the canvid(s).

Pass more styles to canvid beyond width and height

I could be wrong but there doesn't seem to be a way to do this right now. As a workaround, in my own CSS, I added the .canvid class and added the extra styling that I wanted, but I'd prefer to be able to pass through the canvid creation function instead.

Is there a way to load clip on demand for saving web flow data?

The video clips like:

{
  clip00: {src: 'static/video/00.jpg', frames: 24, cols: 6, fps: 12},
  clip01: {src: 'static/video/01.jpg', frames: 24, cols: 6, fps: 12},
  clip02: {src: 'static/video/02.jpg', frames: 24, cols: 6, fps: 12},
  clip04: {src: 'static/video/04.jpg', frames: 24, cols: 6, fps: 12},
  clip05: {src: 'static/video/05.jpg', frames: 24, cols: 6, fps: 12},
}

I visit the website and the canvid will switch the clip to a specific one in some condition. Is there a way for saving the transferred network data if some clips have no necessary being played?
Or I have to get the condition first then create the canvid dynamically?

Compress sprites

Just a quick tip you maybe could include in your docs.

Compress sprites

For a great ~85% reduction in filesize install jpeg-recompress

On a mac simply brew install jpeg-archive
and than jpeg-recompress --quality high clip1.jpg clip1.min.jpg

Janky when looping through sprites

There seems to be a jank between each sprite when doing this

var canvidControl = canvid({
    selector : '.video',
    videos: {
        clip1: { src: 'clip-1.min.jpg', frames: 32, cols: 6, fps: 30, loops: 1, onEnd: function(){
          canvidControl.play('clip2');
        }},
        clip2: { src: 'clip-2.min.jpg', frames: 32, cols: 6, fps: 30, loops: 1, onEnd: function(){
          canvidControl.play('clip3');
        }},
        clip3: { src: 'clip-3.min.jpg', frames: 32, cols: 6, fps: 30, loops: 1},
    },
    width: 640,
    height: 360,
    loaded: function() {
        canvidControl.play('clip1');
    }
});

Infinite loop ?

Is there a way to make infinite loop ? Configuring loop to -1, doesn't seem to work.

issue on Android with multiple sprites playing on a page

Facing issue on Android. When there is more than one canvid playing on the page, the sprite > 400 KB makes other sprites frame load slower.

Example: have 2 canvids less than 400kb loading fine on page. When I add the third one with size > 400KB. All three players become very slow. Kind of loading 1 frame per sec.

While if the sprite > 400KB is played alone on page, it works fine... But with other sprites it is causing issue.

I am aware there is a known issue on devices for bigger sprites.. But the one I am referring to is just 400 KB and has 102 frames. And there is no issue when the sprite is the only sprite loaded on page.

Support for Dom element as a selector

So I can do this:
var canvidControl = canvid({
selector : document.querySelector(".myelement"),
videos: {
clip1: { src: 'clip1.jpg', frames: 38, cols: 6 },
clip2: { src: 'clip2.jpg', frames: 43, cols: 6, loops: 3 }
},
width: 500,
height: 400,
loaded: function() {
canvidControl.play('clip1');
// reverse playback
// canvidControl.play('clip1', true);
}
});

I have multiple videos on page and all of them have same selectors.

Audio sync

If you'd add a method / property which returns current frame / time, it can be extended to sync with audio in the background.

Broken in Chrome 46 for Android

I've tried it on a Android 4.3 device with Chrome 46, and the canvas ends up larger than the frame. I don't see a video, but rather, a scrolling fly by over the sprite sheet.

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.