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: playback rate #1642

Merged
merged 3 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
51 changes: 47 additions & 4 deletions extensions/Lily/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@
},
},
},
{
opcode: "setPlaybackRate",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate(
"set playback rate of video [NAME] to [RATE]"
),
arguments: {
NAME: {
type: Scratch.ArgumentType.STRING,
defaultValue: "my video",
},
RATE: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "2",
},
},
},
],
menus: {
targets: {
Expand Down Expand Up @@ -349,6 +366,10 @@
text: Scratch.translate("height"),
value: "height",
},
{
text: Scratch.translate("playback rate"),
value: "playback rate",
},
],
},
},
Expand Down Expand Up @@ -475,6 +496,8 @@
return videoSkin.size[0];
case "height":
return videoSkin.size[1];
case "playback rate":
return videoSkin.videoElement.playbackRate;
default:
return 0;
}
Expand All @@ -492,9 +515,14 @@
return "";
}

canvas.width = videoSkin.videoElement.videoWidth;
canvas.height = videoSkin.videoElement.videoHeight;
ctx.drawImage(videoSkin.videoElement, 0, 0);
const videoElement = videoSkin.videoElement;
if (videoElement.videoWidth === 0 || videoElement.videoHeight === 0) {
return "";
}

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

Expand Down Expand Up @@ -528,13 +556,28 @@

setVolume(args) {
const videoName = Cast.toString(args.NAME);
const value = Cast.toNumber(args.VALUE);
const videoSkin = this.videos[videoName];
if (!videoSkin) return;

const value = Cast.toNumber(args.VALUE);
videoSkin.videoElement.volume = Math.min(1, Math.max(0, value / 100));
}

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

try {
const value = Cast.toNumber(args.RATE);
// Supposedly negative values will work in Safari but people probably shouldn't rely
// on that since others don't.
videoSkin.videoElement.playbackRate = Math.max(0, value);
} catch (e) {
console.warn(e);
}
}

/** @returns {VM.Target|undefined} */
_getTargetFromMenu(targetName, util) {
if (targetName === "_myself_") return util.target;
Expand Down