Skip to content

Commit

Permalink
Lily/Video: playback rate (#1642)
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin authored Aug 6, 2024
1 parent 532daff commit dfb5354
Showing 1 changed file with 47 additions and 4 deletions.
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

0 comments on commit dfb5354

Please sign in to comment.