Advertisement
Guest User

HTML Java script MYSQL programmers dev...(elmo)

a guest
Jan 29th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.32 KB | None | 0 0
  1. Skip to content
  2.  
  3. Search
  4.  
  5. Picture in Picture Pomodoro Clock with Canvas
  6.  boywithsilverwings profile image Agney Menon   Jan 282 min read
  7. #javascript #beginners #showdev
  8. Caution: This currently works only on Chrome. Firefox does not have support for programmatic PiP yet.
  9.  
  10. Can I Use - Picture in Picture
  11.  
  12. The Normal Pomodoro Timer
  13.  
  14. I'm starting off with a normal countdown pomodoro timer. I will be using AlpineJS. For the uninitiated, AlpineJS is a small JavaScript library that uses Vue/Angular like syntax to bring reactivity to the DOM. If you are familiar with Vue or Angular, then you should feel right at home with the syntax.
  15. <main x-data="timer()" x-init="init()">
  16.  <div>
  17.    <p x-text="text"></p>
  18.  </div>
  19.  <div class="button-container">
  20.    <button
  21.      x-text='interval ? "Pause" : "Start"'
  22.      @click="interval ? pause() : start()"
  23.    >
  24.      Start
  25.    </button>
  26.    <button @click="stop()">Reset</button>
  27.  </div>
  28. </main>
  29. JavaScript:
  30. const zeroPad = (num, places) => String(num).padStart(places, '0');
  31.  
  32. const formatTime({minutes, seconds}) = () => `${zeroPad(minutes, 2)} : ${zeroPad(seconds, 2)}`;
  33.  
  34. function timer() {
  35.  return {
  36.    interval: null,
  37.    text: '',
  38.    time: {
  39.      minutes: 25,
  40.      seconds: 0,
  41.    },
  42.    init() {
  43.      this.text = formatTime(this.time);
  44.    },
  45.    start() {
  46.      this.interval = setInterval(() => {
  47.        const time = calculateTime(this.time);
  48.        this.time = time;
  49.        if(time.minutes === 0 && time.seconds === 0) {
  50.          this.stop();
  51.        }
  52.        this.text = formatTime(time);
  53.      }, 1000);
  54.    },
  55.    stop() {
  56.      clearInterval(this.interval);
  57.      this.interval = null;
  58.      this.time = {
  59.        minutes: 25,
  60.        seconds: 0,
  61.      }
  62.      this.text = formatTime(this.time);
  63.    },
  64.    pause() {
  65.      clearInterval(this.interval);
  66.      this.interval = null;
  67.    }
  68.  }
  69. }
  70. You need AlpineJS needed for this. You can load it from a CDN
  71.  
  72. Picture in Picture
  73.  
  74. As a sidenote, let us talk about Picture in Picture (This is where we are going at the end, but still).
  75.  
  76. Google Developers pages have some great tutorials on picture in picture.
  77.  
  78. But by essence, you can request a video to play picture in picture with:
  79. const videoEl = document.querySelector('#video');
  80. videoEl.requestPictureInPicture();
  81. You can see if a device supports Picture in Picture with:
  82. if (!('pictureInPictureEnabled' in document)) {
  83.   console.log('The Picture-in-Picture Web API is not available.');
  84. } else if (!document.pictureInPictureEnabled) {
  85.   console.log('The Picture-in-Picture Web API is disabled.');
  86. }
  87. Bringing Canvas into the Picture
  88. How do we bring the canvas into this? Well, Chrome allows for any video to play Picture in Picture and luckily for us, Canvas comes with a captureStream API that allows us to capture any stream on a canvas and capture it on video all in real time.
  89.  
  90. Here is how we would do that:
  91. const canvas = document.createElement('canvas');
  92. // Draw something to canvas.
  93. canvas.getContext('2d').fillRect(0, 0, canvas.width, canvas.height);
  94.  
  95. const video = document.createElement('video');
  96. video.muted = true;
  97. video.srcObject = canvas.captureStream();
  98. video.play();
  99. All Together now
  100.  
  101.  
  102. heart   unicorn   reading list   dropdown menu icon
  103.  boywithsilverwings profile
  104. Agney Menon
  105. Software Developer at BigBinary. Engineer 👨‍💻 Sleeping if not Online 🤖
  106. @boywithsilverwings   twitter agneymenon github agneym   link agney.dev
  107.  
  108. Add to the discussion
  109. markdown guide upload image
  110.  PREVIEW  SUBMIT
  111. code of conduct - report abuse
  112. Classic DEV Post from May 8 '19
  113. Is “Defensive Programming” actually healthy?
  114. cubiclebuddha profile image Cubicle Buddha
  115. I can’t solve this one, and I think I need the help of the DEV Community. So, a d...
  116.  
  117. Reactions 155  Reactions 123
  118. sarthology profile image  
  119. How I CRUSHED All of my 2019 Resolutions and You Can Too! 💪🏽🎉🍾
  120. Sarthak Sharma - Jan 28
  121. blacksonic profile image  
  122. A simple Node.js Docker workflow
  123. Gábor Soós - Jan 28
  124. gc_psk profile image  
  125. Top Reasons Why Your Angular App Is Slow
  126. Giancarlo Buomprisco - Jan 28
  127. softchris profile image  
  128. Learn how YOU can add CI/CD to your app
  129. Chris Noring - Jan 28
  130. Home About Privacy Policy Terms of Use Contact Code of Conduct
  131. DEV Community copyright 2016 - 2020  🔥
  132.  
  133. Resources enoslay.elmo@yahoo.com.ph
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement