justinribeiro / lite-youtube

The fastest little YouTube web component on this side of the internet. The shadow dom web component version of Paul's lite-youtube-embed.
https://www.npmjs.com/package/@justinribeiro/lite-youtube
MIT License
948 stars 70 forks source link

Can this generate a background video? #48

Closed jiang-yuan closed 2 years ago

jiang-yuan commented 2 years ago

Would you like to introduce an option with all the following features into your package? like auto loop play, removing controls. The demo and code: https://codepen.io/adamgreenough/pen/bGrgoNb?editors=1100

justinribeiro commented 2 years ago

There's no feature to add. You can achieve a similar effect as the linked demo with nothing more than than the autoload feature and the YouTube IFrame API. Off the cuff example that works about the same (you could tweak to your hearts content).

    <script src="https://www.youtube.com/iframe_api" id="iframe-demo"></script>
    <script type="module" src="../lite-youtube.js"></script>
    <style>
      #test-jsapi {
        pointer-events: none;
      }
    </style>
    <lite-youtube
      id="test-jsapi"
      videoid="WhY7uyc56ms"
      autoload
      params="autoplay=1&mute=1&loop=1&controls=0&modestbranding=1&playsinline=1&rel=0&enablejsapi=1"
    ></lite-youtube>

    <script type="text/javascript">
      let player;

      function setupYT() {
        player = new YT.Player(
          document
            .querySelector('#test-jsapi')
            .shadowRoot.querySelector('iframe'),
          {
            events: {
              onReady: onPlayerReady,
            },
          }
        );
      }

      function onPlayerReady() {
        player.playVideo();
      }

      document.addEventListener('liteYoutubeIframeLoaded', () => {
        try {
          setupYT();
        } catch(e) {
          // in case we're too fast
          setTimeout(setupYT, 100);
        }
      }, false);
    </script>
  </body>
</html>
jiang-yuan commented 2 years ago

Thank you. I will try it.