Skip to content
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
28 changes: 11 additions & 17 deletions src/io/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ class Video {
*/
this._skinId = -1;

/**
* The Scratch Renderer Skin object.
* @type {Skin}
*/
this._skin = null;

Comment on lines -24 to -29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎

/**
* Id for a drawable using the video's skin that will render as a video
* preview.
Expand Down Expand Up @@ -141,8 +135,8 @@ class Video {
}

_disablePreview () {
if (this._skin) {
this._skin.clear();
if (this._skinId !== -1) {
this.runtime.renderer.updateBitmapSkin(this._skinId, new ImageData(...Video.DIMENSIONS), 1);
this.runtime.renderer.updateDrawableProperties(this._drawable, {visible: false});
}
this._renderPreviewFrame = null;
Expand All @@ -152,9 +146,8 @@ class Video {
const {renderer} = this.runtime;
if (!renderer) return;

if (this._skinId === -1 && this._skin === null && this._drawable === -1) {
this._skinId = renderer.createPenSkin();
this._skin = renderer._allSkins[this._skinId];
if (this._skinId === -1 && this._drawable === -1) {
this._skinId = renderer.createBitmapSkin(new ImageData(...Video.DIMENSIONS), 1);
this._drawable = renderer.createDrawable(StageLayering.VIDEO_LAYER);
renderer.updateDrawableProperties(this._drawable, {
skinId: this._skinId
Expand All @@ -176,16 +169,17 @@ class Video {

this._renderPreviewTimeout = setTimeout(this._renderPreviewFrame, this.runtime.currentStepTime);

const canvas = this.getFrame({format: Video.FORMAT_CANVAS});
const imageData = this.getFrame({
format: Video.FORMAT_IMAGE_DATA,
cacheTimeout: this.runtime.currentStepTime
});

if (!canvas) {
this._skin.clear();
if (!imageData) {
renderer.updateBitmapSkin(this._skinId, new ImageData(...Video.DIMENSIONS), 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to cache and reuse this "empty frame" ImageData to reduce garbage collection load.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Done!

return;
}

const xOffset = Video.DIMENSIONS[0] / -2;
const yOffset = Video.DIMENSIONS[1] / 2;
this._skin.drawStamp(canvas, xOffset, yOffset);
renderer.updateBitmapSkin(this._skinId, imageData, 1);
this.runtime.requestRedraw();
};

Expand Down