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

Bug: Transparent Background doesn't work #10162

Open
achubutkin opened this issue Jan 28, 2024 · 2 comments
Open

Bug: Transparent Background doesn't work #10162

achubutkin opened this issue Jan 28, 2024 · 2 comments
Labels
📢 Accepting PRs Would welcome a PR from the community. 🤩 Good First PR Indicating to new contributors a PR is simple/easy. Things like documentation or type fixes.
Milestone

Comments

@achubutkin
Copy link

achubutkin commented Jan 28, 2024

Current Behavior

The background transparency is not set further if I initialize the application with an opaque background.

Expected Behavior

The background transparency should be set further if I initialize the application with an opaque background.

Steps to Reproduce

  1. Open example here https://pixijs.com/examples/basic/transparent-background
  2. Remove backgroundAlpha from constructor options.
    const app = new PIXI.Application({ backgroundColor: 'green', resizeTo: window });
    // ... some code
  3. Set a new background (red + 0.5 alpha)
    app.renderer.background.color = 'rgba(255, 0, 0, 0.5)'; // doesn't work

Note: If backgroundAlpha is set to 0 in constructor then step 3 is works.

Environment

Possible Solution

No response

Additional Information

No response

@naramdash
Copy link
Contributor

I have a same issue on my private project.

    this.#pixiApp.renderer.background.color = "rgb(255,0,0)";
    this.#pixiApp.renderer.background.alpha = 0.5;

background is applied without alpha.
image

Environment

  • pixi.js version: 7.4.0
  • Browser & Version: Ubuntu 22.04 Edge 121.0.2277.83 (Official build) (64-bit)

@bigtimebuddy bigtimebuddy added this to the v7.4.1 milestone Jan 31, 2024
@bigtimebuddy
Copy link
Member

I looked into this, the reason this is happening is because alpha is only used on the canvas when it is initialized. There are some performance implication by having alpha or not enabled on the canvas, for many browser opaque canvases are faster. Once options are set using canvas.getContext, they cannot be changed. Here's a simple example: https://jsfiddle.net/bigtimebuddy/m9kehdqx/

Here are the initialization methods for each renderer if you'd like to see how it's being used.

Renderer

const alpha = this.renderer.background.alpha < 1;

CanvasRenderer

const alpha = this.renderer.background.alpha < 1;

The workaround is make sure you set alpha to be < 0 when you create the Application or Renderer.

One small suggestion we could make is if the canvas is opaque (because of how you initialized), we could provide a console warning when trying to change the alpha after the fact. Something like:

The canvas has already been initialized as non-transparent. If you'd like to adjust alpha of the canvas after initializing the renderer, please set the initial value less than 1.

Some documentation might also clarify this behavior.

@bigtimebuddy bigtimebuddy added 📢 Accepting PRs Would welcome a PR from the community. 🤩 Good First PR Indicating to new contributors a PR is simple/easy. Things like documentation or type fixes. labels Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📢 Accepting PRs Would welcome a PR from the community. 🤩 Good First PR Indicating to new contributors a PR is simple/easy. Things like documentation or type fixes.
Projects
None yet
Development

No branches or pull requests

3 participants