Skip to content

Instantly share code, notes, and snippets.

@felixrieseberg
Created April 10, 2024 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixrieseberg/9a699d9622151ad769ad8c6f53e1d639 to your computer and use it in GitHub Desktop.
Save felixrieseberg/9a699d9622151ad769ad8c6f53e1d639 to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Color Change Boxes</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
display: flex;
flex-wrap: wrap;
width: 100vw;
height: 100vh;
}
.box {
width: calc(100vw / 20); /* Adjust number of boxes per row */
height: calc(100vw / 20); /* Adjust to keep boxes square */
transition: background-color 0.5s; /* Smooth color transition */
}
</style>
</head>
<body>
<div class="container">
<!-- Boxes will be added by JavaScript -->
</div>
<script>
const container = document.querySelector('.container');
// Adjust the totalBoxes value to add or reduce the number of boxes
const totalBoxes = 400; // Example: 20x20 grid
for (let i = 0; i < totalBoxes; i++) {
const box = document.createElement('div');
box.classList.add('box');
box.style.backgroundColor = '#f0f0f0'; // Default box color
// Change color on hover
box.addEventListener('mouseover', () => box.style.backgroundColor = getRandomColor());
box.addEventListener('mouseleave', () => box.style.backgroundColor = '#f0f0f0');
container.appendChild(box);
}
// Function to get a random color
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
</script>
</body>
</html>
// Modules to control application life and create native browser window
const { app, BrowserWindow, BrowserView } = require('electron')
const path = require('node:path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
titleBarStyle: 'hiddenInset',
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
const browserView = new BrowserView()
browserView.setBounds({ x: 0, y: 0, width: 300, height: 300 })
mainWindow.addBrowserView(browserView)
// and load the index.html of the app.
browserView.webContents.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
{
"name": "emotional-morning-swim-oekzi",
"productName": "emotional-morning-swim-oekzi",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "felix",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "29.3.0"
}
}
/**
* The preload script runs before `index.html` is loaded
* in the renderer. It has access to web APIs as well as
* Electron's renderer process modules and some polyfilled
* Node.js functions.
*
* https://www.electronjs.org/docs/latest/tutorial/sandbox
*/
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
/**
* This file is loaded via the <script> tag in the index.html file and will
* be executed in the renderer process for that window. No Node.js APIs are
* available in this process because `nodeIntegration` is turned off and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
/* styles.css */
/* Add styles here to customize the appearance of your app */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment