项目作者: cbillowes

项目描述 :
A Gatsby plugin to add interactive animated gifs to markdown files.
高级语言: JavaScript
项目地址: git://github.com/cbillowes/gatsby-remark-interactive-gifs.git
创建时间: 2020-04-23T18:49:25Z
项目社区:https://github.com/cbillowes/gatsby-remark-interactive-gifs

开源协议:MIT License

下载


gatsby-remark-interactive-gifs

npm
Build Status
License: MIT

A Gatsby plugin to add interactive animated gifs to markdown files.
Check it out!

Install

npm install --save gatsby-remark-interactive-gifs

Overview

  • Add a gif to your markdown with the gif protocol in inline code. gif:<filename>.gif.
    There are options to customize it defined later on.
  • A still image is extracted and saved in the dest as still-<filename>.gif.
  • Click events will toggle between play and playing.

Gotchas: A fresh copy needs to be downloaded to play a gif from the beginning.

  • All caching strategies are bypassed.
  • This plugin is designed for images that fill a container. Custom styling is required to
    cater for gifs not of 100% width.

Tips:

  • Optimize your gifs!
  • For imagery, I use icons from www.flaticon.com and make sure I attribute them.
  • For spinners/loading indicators, I use loading.io.

Requirements

This plugin requires node >=10.

Configure

gatsby-config.json:

  1. {
  2. resolve: `gatsby-transformer-remark`,
  3. options: {
  4. plugins: [
  5. {
  6. resolve: `gatsby-remark-interactive-gifs`,
  7. options: {
  8. root: `${__dirname}`,
  9. src: `${__dirname}/src/gifs`,
  10. dest: `${__dirname}/public/static/gifs`,
  11. play: `${__dirname}/src/images/play.gif`,
  12. placeholder: `${__dirname}/src/images/placeholder.gif`,
  13. loading: `${__dirname}/src/images/loading.gif`,
  14. relativePath: `/static/gifs`
  15. },
  16. },
  17. ]
  18. },
  19. }
  • root - The root of your project.
  • src - Where all the gifs you want processed are stored. Absolute path.
  • dest - A path in public where your gifs are stored. Absolute path.
  • play - An image to indicate that the gif can be interacted with. Absolute path.
  • placeholder - An image to show when the gif is missing in action. Absolute path.
  • loading - An image which shows when the gif is downloading. Absolute path.
  • relativePath - The relative path served from public/.

How to query

Your animated gifs are available in GraphQL and the nodes can be access via allInteractiveGif.

  1. query MyQuery {
  2. allInteractiveGif {
  3. edges {
  4. node {
  5. height
  6. absolutePath
  7. base64
  8. relativePath
  9. sourcePath
  10. stillRelativePath
  11. width
  12. }
  13. }
  14. }
  15. }

How to use

Simply reference your gif file name (relative to the configured src directory) in the gif protocol in order to embed the interactive gif.

  1. `gif:dolphin.gif`

You can customize it by adding attributes. They are in no particular order and neither are mandatory.

  1. `gif:dolphin.gif:id=hitchikers-guide-to-the-galaxy;class=dolphin;caption=So long and thanks for all the fish`
  • id adds element an ids on the gif container and a still-<id> on the still container.
  • class adds a class to the parent interactive gif container.
  • caption adds text to the bottom of the image.

How to style

Below is sample styling in CSS to get you started.

  1. .interactive-gif {}
  2. /* Responsive flicker-less display */
  3. .interactive-gif .embedded {
  4. position: relative;
  5. width: 100%;
  6. height: auto;
  7. }
  8. .interactive-gif .loading,
  9. .interactive-gif .still-container,
  10. .interactive-gif .gif-container {
  11. position: absolute;
  12. top: 0;
  13. width: 100%;
  14. height: 100%;
  15. }
  16. .interactive-gif .still,
  17. .interactive-gif .gif {
  18. width: 100%;
  19. height: 100%;
  20. cursor: pointer;
  21. }
  22. /* Loading indicator */
  23. .interactive-gif .loading .indicator {
  24. position: absolute;
  25. bottom: 0;
  26. left: 0;
  27. width: 80px;
  28. filter: contrast(0.5);
  29. }
  30. /* Play button */
  31. .interactive-gif .still-container .play {
  32. cursor: pointer;
  33. filter: grayscale(100%);
  34. width: 20%;
  35. position: absolute;
  36. opacity: 0.9;
  37. left: 50%;
  38. top: 50%;
  39. transform: translateX(-50%) translateY(-50%);
  40. }
  41. /* Text underneath the gif */
  42. .interactive-gif .caption {
  43. font-size: 90%;
  44. font-style: italic;
  45. }
  46. /* Image displayed when the gif cannot be found */
  47. .interactive-gif .placeholder {
  48. filter: grayscale(100%);
  49. text-align: center;
  50. }
  51. .interactive-gif .placeholder img {
  52. width: 200px;
  53. }

You can convert the above CSS to
scss,
sass or
less.

Example

  1. `gif:nyancat.gif:caption=Nyanyanyanyanyanyanya`
  1. <div class="interactive-gif ">
  2. <div class="embedded"
  3. style="padding-top: 56.28517823639775%">
  4. <div id="loading-nyancat.gif"
  5. class="loading"
  6. style="background-size: cover; background-image: url('/static/gifs/still-nyancat.gif');">
  7. <img class="indicator" src="/static/gifs/loading.gif">
  8. </div>
  9. <div id="nyancat.gif"
  10. class="gif-container"
  11. style="display: block;"
  12. onclick="document.getElementById('nyancat.gif').style.display = 'none';
  13. document.getElementById('still-nyancat.gif').style.display = 'block';">
  14. <img id="image-nyancat.gif"
  15. class="gif"
  16. data-original="/static/gifs/nyancat.gif"
  17. src="/static/gifs/nyancat.gif?t=1587914555745">
  18. </div>
  19. <div id="still-nyancat.gif"
  20. class="still-container"
  21. onclick="var gif = document.getElementById('image-nyancat.gif');
  22. gif.src = gif.dataset.original + '?t=' + new Date().getTime();
  23. document.getElementById('still-nyancat.gif').style.display = 'none';
  24. document.getElementById('nyancat.gif').style.display = 'block';" style="display: none;">
  25. <img id="image-still-nyancat.gif" class="still" src="/static/gifs/still-nyancat.gif">
  26. <img class="play" src="/static/gifs/play.gif">
  27. </div>
  28. </div>
  29. <div class="caption">Nyanyanyanyanyanyanya</div>
  30. </div>

Note: padding-top is calculated using the aspect ratio of the image. The embedded container
is given enough room to responsively contain the children images and containers.

Troubleshooting

Run a gatsby clean when your source nodes are no longer generated.

Notes

The order of this plugin only matters when you use it together with gatsby-remark-prismjs. Prism transforms code blocks and I kind
of slapped a gif protocol in one which will confuse the daylight out of prism. Just reference this plugin somewhere above Prism.

Contribute

Read the guidelines to contribute to this plugin.

License

MIT, by Clarice Bouwer