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

Stores the width and height values before any adjustments are made, Solves #6581 #6588

Merged
merged 5 commits into from Nov 30, 2023
Merged

Conversation

diyaayay
Copy link
Contributor

@diyaayay diyaayay commented Nov 25, 2023

Resolves #6581

Changes:

In this modification, I added originalWidth and originalHeight variables to store the width and height values before any adjustments are made. Later in the function, when setting the canvas width and height, I use these original values to update the canvas dimensions. This prevents the issue where the width and height are evaluated as 0 when the element is hidden. The snippet works just fine after the change.

Snippet:

let movie;

function preload() {
	// my video is 640 x 360
	movie = createVideo("flyboard.mp4");
    movie.volume(0);
}

function setup() {
	createCanvas(640, 360);
	pixelDensity(1);
  
	movie.play();
    // Commenting this out makes it work
	movie.hide();
}

function draw() {
	background(220);
    // Commenting this out makes it work
    // (or moving it to setup it seems)
    movie.size(width, height);
    debugger
	image(movie, 0, 0);
}

PR Checklist

@diyaayay diyaayay closed this Nov 25, 2023
@diyaayay diyaayay reopened this Nov 25, 2023
@diyaayay diyaayay changed the title stores width and height values in local variables before any adjustment Solves #6581 Nov 25, 2023
@diyaayay diyaayay changed the title Solves #6581 Stores the width and height values before any adjustments are made, Solves #6581 Nov 25, 2023
Copy link
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on, this looks like the right approach! I just have a few questions on the implementation.

src/dom/dom.js Outdated
@@ -3283,14 +3285,14 @@ p5.Element.prototype.size = function (w, h) {
this.elt.height = aH;
}

this.width = this.elt.offsetWidth;
this.height = this.elt.offsetHeight;
originalWidth = aW;
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like we don't set this.width or this.height any more here, was that on purpose?

Copy link
Contributor Author

@diyaayay diyaayay Nov 26, 2023

Choose a reason for hiding this comment

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

I looked into my approach again and tried changing it to:
this.elt.width = aW;
this.elt.height = aH;

this._pInst._setProperty('width', aW);
this._pInst._setProperty('height', aH);

still doesn't work though, I'm kind of stuck now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to also set this.width = aW and this.height = aH?

src/dom/dom.js Outdated

if (this._pInst && this._pInst._curElement) {
// main canvas associated with p5 instance
if (this._pInst._curElement.elt === this.elt) {
this._pInst._setProperty('width', this.elt.offsetWidth);
this._pInst._setProperty('height', this.elt.offsetHeight);
this._pInst._setProperty('width', originalWidth);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason for having originalWidth/Height as separate variables, or can we simplify this a bit and just use aW and aH here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes! I realised it later that aW and aH can be used instead of originalWidth/Height.

src/dom/dom.js Outdated
@@ -3272,25 +3274,31 @@ p5.Element.prototype.size = function (w, h) {
this.elt.setAttribute('height', aH * this._pInst._pixelDensity);
this.elt.style.width = aW + 'px';
this.elt.style.height = aH + 'px';
this.elt.style.width = styleWidth;
Copy link
Contributor

Choose a reason for hiding this comment

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

This might be resetting the width/height to null here, since in this branch, nothing has assigned styleWidth and styleHeight yet.

src/dom/dom.js Outdated
}

this.width = this.elt.offsetWidth;
this.height = this.elt.offsetHeight;
this.elt.width = styleWidth; // Set elt.width to the recorded value
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is setting the width/height to null in some cases. I think we can probably just use aW and aH instead of styleHeight since that is defined everywhere

Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect that's the cause of the error I get when trying it out: https://editor.p5js.org/davepagurek/sketches/UX8Yk9fnr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@davepagurek I have made changes by removing styleWidth and styleHeight due to the error, opting to use aW and aH instead. While finding the solutions, I came across two potential solutions. The first involves making minor adjustments in the sketch.js file, which isn't the preferred route. The second option is to modify the size function which is what I've been doing. However, I am unsure if I am on the right track with the latter. Am I?

@davepagurek
Copy link
Contributor

I updated the build in the p5 editor link and it seems to work now, thanks for making the changes!

@davepagurek davepagurek merged commit 07d5b98 into processing:main Nov 30, 2023
2 checks passed
@diyaayay diyaayay deleted the bugfix/6581 branch November 30, 2023 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

p5.MediaElement does not draw on canvas via image() after calling media.hide() and media.size()
2 participants