当前位置:网站首页>[electron] basic learning
[electron] basic learning
2022-06-27 23:00:00 【Guardian of loving angels】
List of articles
quick-start
// index.js
const {
app, BrowserWindow } = require('electron');
app.on('ready', function() {
console.log('ready');
const mainWin = new BrowserWindow({
width: 800,
height: 600
});
mainWin.loadFile('index.html');
})
Main process and rendering process
electron function package.jsonmain The process of the script is called the main process , Scripts running in the main process are created by web Page to show the user interface .
The main process runs on nodejs Environment
The difference between the main process and the rendering process
The main process uses BrowserWindow Instance creation page , Every BrowserWindow The instance runs the page in its own rendering process , When one BrowserWindow After the instance is destroyed , The corresponding rendering process will also be terminated .
The main process manages all web Pages and their rendering process . Each rendering process is independent , It only cares about what it runs web page .
Call in page GUI Related primitives API It's not allowed , Because in web Page operation native GUI Resources are very dangerous , Easy to cause resource leakage . If you want to web Use in page GUI operation , Its corresponding rendering process must communicate with the main process , Request the main process to perform relevant operations .
app modular
ready Application initialization complete
browser-ready-created Window creation completion triggers
before-quit Before the window closes
will-quit The window closed , But the program didn't close , About to close
quit Application shutdown triggers
whenReady() then()
requestSingleInstanceLock() Limit only one application to open true perhaps false
second-instance event
BrowserWindow modular
Used to create a control application window
app.on('ready', function() {
console.log('ready');
const mainWin = new BrowserWindow({
width: 800,
height: 600,
// frame: false,
// maxWidth: 1000,
// maxHeight: 800,
// minWidth: 800,
// minHeight: 600,
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWin.loadURL('http://www.baidu.com');
// mainWin.setBounds({
// x: 500,
// y: 500
// })
mainWin.once('ready-to-show', function() {
mainWin.show();
})
mainWin.on('show', function() {
mainWin.maximize();
})
})
loadFile Method
Used to load local files , You can use relative paths , You can also use absolute paths .
You can load files that are not in the project .
More than can be loaded html file , You can also load other files
loadURL Method
Used to load remote files
frame To configure
Set whether the window has a border and a menu bar , The default is true
resizeable To configure
Set whether the window can be resized , The default is true
maxWidth, maxHeight, minWidth, minHeight
Set the maximum width 、 Maximum height 、 Minimum width 、 Minimum height
show To configure
Set whether the window displays , The default is true
ready-to-show event
This event is triggered when the rendering process renders the page for the first time , Trigger only once
And show Use in combination with configuration :
const mainWin = new BrowserWindow({
show: false
});
mainWin.once('ready-to-show', function() {
mainWin.show();
})
show Method , Control the display of the window
webReference To configure
A configuration item is an object
- nodeIntegration
Open or not node Support , Default false
- contextIsolation
Whether to enable context isolation , Default true
setBounds Method
Set window size or move position
maximize Method
Set window maximization
Process of communication
The main process uses ipcMain
ipcMain.on('renderer-send', (event, data) => {
console.log(data);
event.reply('main-send', ' The main process replies with a message ')
})
The rendering process uses ipcRenderer
const {
ipcRenderer
} = require('electron');
document.querySelector('button').addEventListener('click', function(e) {
ipcRenderer.send('renderer-send', ' Information passed by the rendering process ');
})
ipcRenderer.on('main-send', (event, data) => {
console.log(data);
})
System tray
Tray: Add icons and context menus to the system notification area
process : The main process
const tray = new Tray('icon.jpg');
// Set display text
tray.setToolTip('wcy Recording screen ');
Menu
Menu: Create native menus and application context menus
process : The main process
const tray = new Tray('icon.jpg');
tray.setToolTip('wcy Recording screen ');
const menu = Menu.buildFromTemplate([{
label: ' sign out ',
click: () => {
console.log(' Click to exit ')
}
}])
Screen sharing and camera shooting
MediaDevices
WebRtc
MediaDevices The interface provides access to devices connected to media input , Such as camera and microphone , And screen sharing , It allows you to get media data from any hardware resource
- getUserMedia
With the user's permission , Turn on the camera or screen share and microphone on the system , And provide MediaStream Contains the input of video track and audio track
<video src=""></video>
<script> const video = document.querySelector('video'); const init = async() => {
const stream = await navigator.mediaDevices.getUserMedia({
video: {
width: 600, height: 500 } }); video.srcObject = stream; video.play(); }; init(); </script>
- getDisplayMedia
Prompts the user to select a display or part of a display to capture MediaStream For sharing or recording , Return resolved to MediaStream Of Promise
<video src=""></video>
<script> const video = document.querySelector('video'); const init = async() => {
const stream = await navigator.mediaDevices.getDisplayMedia({
video: true }); video.srcObject = stream; video.play(); }; init(); </script>
Native screen recording
MediaRecorder
<body>
<p>Media</p>
<button class="start"> Start recording </button>
<button class="end"> Stop recording </button>
<button class="play"> Play </button>
<video src="" class="video"></video>
<video src="" class="playVideo"></video>
<script> const video = document.querySelector('.video'); const playVideo = document.querySelector('.playVideo'); const startBtn = document.querySelector('.start'); const endBtn = document.querySelector('.end'); const playBtn = document.querySelector('.play'); let stream = null; let record = null; let data = []; const init = async() => {
stream = await navigator.mediaDevices.getUserMedia({
video: {
width: 600, height: 500 } }); video.srcObject = stream; video.play(); }; init(); const startRedord = startRecord = () => {
console.log(stream); record = new MediaRecorder(stream, {
mimeType: 'video/webm' }); if (record) {
record.start(); record.addEventListener('dataavailable', function(e) {
data.push(e.data); }); // record.addEventListener('stop', function(e) {
// console.log(data); // }) } }; startBtn.addEventListener('click', function(e) {
startRedord(); }); endBtn.addEventListener('click', function(e) {
record.stop(); }); playBtn.addEventListener('click', function(e) {
const blob = new Blob(data, {
type: 'video/mp4' }); const videoUrl = URL.createObjectURL(blob); console.log(videoUrl); playVideo.src = videoUrl; playVideo.play(); }); </script>
</body>
边栏推荐
- Learn rnaseq analysis by following the archiving tutorial (I)
- 资深猎头团队管理者:面试3000顾问,总结组织出8大共性(茅生)
- Zabbix6.0 upgrade Guide - how to synchronize database upgrades?
- [网络]常见的请求方法
- Spatial relation query and graph based query in secondary development of ArcGIS Engine
- [electron] 基础学习
- 移动端避免使用100vh[通俗易懂]
- 改善深层神经网络:超参数调试、正则化以及优化(三)- 超参数调试、Batch正则化和程序框架
- 云辅助隐私集合求交(Server-Aided PSI)协议介绍:学习
- 微服務之服務網關
猜你喜欢

微服务之服务网关

Is the dog virtue training with a monthly salary of 30000 a good business?

Livox Lidar+海康Camera 基于loam的实时三维重建生成RGB彩色点云

Spark bug Practice (including bug: classcastexception; connectexception; noclassdeffounderror; runtimeexceptio, etc.)

Ice cream or snow "high"?

Crawler notes (3) -selenium and requests

netERR_CONNECTION_REFUSED 解决大全

爬虫笔记(2)- 解析

Summary of various loams (laser SLAM)

【经典干货书】数据科学中的信息理论方法,561页pdf
随机推荐
ABAP essay-excel-3-batch import (breaking through 9999 lines of standard functions)
[suggested collection] ABAP essays-excel-4-batch import recommended
Livox Lidar+海康Camera实时生成彩色点云
元气森林的5元有矿之死
雪糕还是雪“高”?
STM32与RC522简单公交卡系统的设计
向量召回和字面召回的选择与权衡
The "business and Application Security Development Forum" held by the ICT Institute was re recognized for the security capability of Tianyi cloud
Hiplot 在线绘图工具的本地运行/开发库开源
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
About the SQL injection of davwa, errors are reported: analysis and verification of the causes of legal mix of settlements for operation 'Union'
「R」 Using ggpolar to draw survival association network diagram
Service gateway of microservices
Common APIs (Methods) for scope -number and string
Liuleifeng, a "good man in Guangzhou" in the first quarter of 2022, has a strong sense of integrity and food safety
Structured machine learning project (I) - machine learning strategy
go语言切片Slice和数组Array对比panic: runtime error: index out of range问题解决
Spatial relation query and graph based query in secondary development of ArcGIS Engine
同花顺炒股软件可靠吗??安全嘛?
Zabbix6.0 upgrade Guide - how to synchronize database upgrades?