1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <!DOCTYPE html> <html> <head> <title>Canvas</title> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> </head>
<body> <h1>Video</h1> <video class="video" src="./horse.mp4" autoplay muted loop></video> <canvas class="canvas" width="500" height="300"></canvas>
<script> const canvas = document.querySelector(".canvas"); const ctx = canvas.getContext("2d"); ctx.font = "bold 50px serif"; ctx.fillStyle = "white";
const subtitle = [ { time: 1, message: "Melody Lane", x: 270, y: 70 }, { time: 3, message: "Son of Orfevre", x: 270, y: 170 }, { time: 5, message: "8th May", x: 270, y: 920 }, ];
let canPlayState = false;
ctx.textAlign = "center"; ctx.fillText("loading video...", 250, 150);
const videoElem = document.querySelector(".video"); videoElem.addEventListener("canplaythrough", render);
function render() { ctx.drawImage(videoElem, 0, 0, 540, 960);
for (let i = 0; i < subtitle.length; i++) { if (videoElem.currentTime > subtitle[i].time) { ctx.fillText(subtitle[i].message, subtitle[i].x, subtitle[i].y); } }
requestAnimationFrame(render); } </script> </body> </html>
|