我正在尝试使用 ElectronJs 制作一个透明窗口,但我获得了黑色背景。
我在 Linux (Debian Jessie)
我尝试了不同的版本:最新版、测试版和每晚版,结果相同。
我有一个适用于同一台机器的 NW.js 版本,所以我认为这是一个 Electron 问题。
这是我的代码 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>
问题是背景不是透明的而是黑色的:
尝试过不同的事情但没有成功(例如 app.disableHardwareAcceleration()
)
有人知道解决方案或有完整的工作示例吗?
谢谢
-
编辑 1:也尝试过使用和不使用 --enable-transparent-visuals --disable-gpu
如此 处 所述
原文由 doom 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是 Electron 项目中一个非常古老的回归错误。
请参阅 https://github.com/electron/electron/issues/15947
要绕过这个问题,只需降级到
1.4.16
2.0.16
,最后一个工作版本。编辑 1:如果您在就绪事件打开窗口后至少等待 300 毫秒,它将正常工作
而且你不需要 package.json 中的
--disable-gpu
选项编辑 2:要使其开箱即用,请使用此存储库: https ://gitlab.com/doom-fr/electron-transparency-demo
对我来说与
npm start
和npm run startWithTransparentOption
工作编辑 3:也请查看 下面的@Thalinda Bandara 回答:它可能很有趣(未测试但已在别处看到)。