Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lily/Video -- Clamp Volume Between 0 and 1, & Add "get frame of video at (seconds)" #1595

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion extensions/Lily/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ID: lmsVideo
// Description: Play videos from URLs.
// By: LilyMakesThings <https://scratch.mit.edu/users/LilyMakesThings/>
// By: SharkPool
// License: MIT AND LGPL-3.0

// Attribution is not required, but greatly appreciated.
Expand Down Expand Up @@ -239,6 +240,19 @@
},
},
},
{
opcode: "getFrame",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate(
"screenshot of video [NAME] at current time"
),
arguments: {
NAME: {
type: Scratch.ArgumentType.STRING,
defaultValue: "my video",
},
},
},
"---",
{
opcode: "pause",
Expand Down Expand Up @@ -466,6 +480,24 @@
}
}

getFrame(args) {
const videoName = Cast.toString(args.NAME);
const videoSkin = this.videos[videoName];
if (!videoSkin) return "";

const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
if (!ctx) {
console.warn("2D rendering context not available");
return "";
}

canvas.width = videoSkin.videoElement.videoWidth;
canvas.height = videoSkin.videoElement.videoHeight;
ctx.drawImage(videoSkin.videoElement, 0, 0);
return canvas.toDataURL();
}

pause(args) {
const videoName = Cast.toString(args.NAME);
const videoSkin = this.videos[videoName];
Expand Down Expand Up @@ -500,7 +532,7 @@
const videoSkin = this.videos[videoName];
if (!videoSkin) return;

videoSkin.videoElement.volume = value / 100;
videoSkin.videoElement.volume = Math.min(1, Math.max(0, value / 100));
}

/** @returns {VM.Target|undefined} */
Expand Down