无法在Electron(javascript)中制作透明窗口 [英] Can't succeed in making transparent window in Electron (javascript)

查看:495
本文介绍了无法在Electron(javascript)中制作透明窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ElectronJs制作透明窗口,但是我得到了黑色背景。

I am trying to make a transparent window with ElectronJs but I obtain a black background.

我在Linux(Debian Jessie)上

I am on Linux (Debian Jessie)

我尝试了不同的版本:最新版本,测试版和每夜版本,结果都相同。

I have tried different versions : latest, beta and nightly with the same result.

我有一个适用于NW.js的版本

I have a version for NW.js that works on the same machine, so I expect it is a Electron problem.

这是我的 main.js 代码:

const {app, BrowserWindow} = require('electron');
let mainWindow;
function createWindow () {
  mainWindow = new BrowserWindow({width: 920, height: 300,  frame:true, transparent:true, backgroundColor: '#00FFFFFF'});
  mainWindow.loadFile('index.html');
  mainWindow.on('closed', function () {
    mainWindow = null;
  });
}
app.on('ready', createWindow);

这是我的 index.html 的代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body style="background-color:rgba(255,255,255,0); color:lightgrey;">
    <h1>Hello World!</h1>
    <!-- All of the Node.js APIs are available in this renderer process. -->
    We are using Node.js <script>document.write(process.versions.node)</script>,
    Chromium <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.

    <script>
      // You can also require other files to run in this process
      // require('./renderer.js')
    </script>
  </body>
</html>

问题在于背景不是透明的而是黑色的:

The problem is that background is not transparent but black :

尝试了不同的尝试,但均未成功(例如 app.disableHardwareAcceleration( )

Have tried different things without success (for example app.disableHardwareAcceleration())

有人知道解决方案或有完整的示例吗?

Thx

-

编辑1:
也尝试过有和没有 --enable-transparent-visuals --disable-gpu
此处

推荐答案

这是Electron项目中非常老的回归错误。

It's a very old regression bug in Electron project.

请参见 https://github.com/electron / electron / issues / 15947

要绕过该问题,只需降级为 1.4.16 2.0.16 ,最后一个工作版本。

To bypass that problem, just downgrade to 1.4.16 2.0.16, the last working version.

编辑1:如果在就绪事件打开窗口后等待至少300ms,它将起作用正确

EDIT 1 : if you wait at least 300ms after ready event to open windows it will work correctly

app.on('ready', () => setTimeout(onAppReady, 300));

您不需要-disable-gpu 选项您的package.json

And you do not need --disable-gpu option in your package.json

"start": "electron --enable-transparent-visuals ."




编辑2:
要使其开箱即用,请使用此仓库: https://gitlab.com/doom-fr/electron-transparency-demo

git clone https://gitlab.com/doom-fr/electron-transparency-demo
cd electron-transparency-demo
npm install
npm start
# or npm run startWithTransparentOption
# or npm run startWithAllOptions


对我来说

npm start npm run startWithTransparentOption

编辑3:
也请看看 @ Thalinda Bandara的回答
可能是令人感兴趣的(未经测试,但已在其他地方看到过)。

EDIT 3 : Please have a look also to @Thalinda Bandara answer below : It might be interresting (not tested but already seen it elsewhere).

这篇关于无法在Electron(javascript)中制作透明窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆