::: use electron.js

ยอดเยี่ยมครับ! ถ้าคุณต้องการสร้าง Desktop App สำหรับเปิดภาพ ด้วย Electron.js, นี่คือตัวอย่าง ครบขั้นตอน ตั้งแต่ติดตั้ง จนถึงเปิดภาพด้วย HTML+JavaScript ผ่าน Electron


🧱 สิ่งที่ต้องมี

  • ติดตั้ง Node.js ก่อน: https://nodejs.org
  • ตัวแก้ไขโค้ด เช่น VS Code

✅ ขั้นตอนการสร้าง Electron App เปิดภาพ

📁 1. สร้างโปรเจกต์

bash
mkdir image-viewer
cd image-viewer
npm init -y
npm install electron --save-dev

🗂️ 2. สร้างโครงสร้างไฟล์

arduino
image-viewer/
├── main.js ← ตัว Electron main process
├── index.html ← UI หลัก
├── renderer.js ← JS ฝั่ง UI
├── package.json

🧠 3. เขียน main.js (เริ่ม Electron App)

js
const { app, BrowserWindow } = require('electron');
const path = require('path');

function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'renderer.js'),
nodeIntegration: true,
contextIsolation: false
}
});

win.loadFile('index.html');
}

app.whenReady().then(createWindow);

🖼️ 4. เขียน index.html (UI)

html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Image Viewer</title>
</head>
<body>
<h1>Simple Image Viewer</h1>
<button id="openBtn">Open Image</button>
<br><br>
<img id="imgDisplay" src="" style="max-width: 100%; max-height: 500px;" />
<script src="renderer.js"></script>
</body>
</html>

💻 5. เขียน renderer.js (ควบคุม UI + Dialog)

js
const { dialog } = require('electron').remote;
const fs = require('fs');

document.getElementById('openBtn').addEventListener('click', () => {
dialog.showOpenDialog({
title: 'Select an image',
filters: [{ name: 'Images', extensions: ['jpg', 'png', 'gif', 'bmp'] }],
properties: ['openFile']
}).then(result => {
if (!result.canceled) {
const imagePath = result.filePaths[0];
document.getElementById('imgDisplay').src = imagePath;
}
});
});

⚙️ 6. แก้ไข package.json

เพิ่ม main และ start script:

jsonCopyEdit{
  "name": "image-viewer",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "devDependencies": {
    "electron": "^29.0.0"
  }
}

▶️ 7. รันแอป

bashCopyEditnpm start

📦 เพิ่มเติม (ถ้าต้องการ)

  • สร้าง build .exe → ใช้ electron-packager หรือ electron-builder
  • เพิ่ม Drag & Drop
  • รองรับเปิดหลายภาพ
  • เพิ่ม Zoom / Rotate / Save

ถ้าคุณต้องการให้ผมสร้างเป็นเทมเพลต zip สำหรับคุณดาวน์โหลด หรือเพิ่มฟีเจอร์พิเศษ บอกได้เลยครับ!

อธิบาย code , main.js

แน่นอนครับ! ด้านล่างคือคำอธิบายทีละบรรทัดของ main.js ในโปรเจกต์ Electron.js ที่ใช้เปิดภาพ (Image Viewer):


📁 main.js – เป็น Main Process ของ Electron

js
const { app, BrowserWindow } = require('electron');

👉 ดึง โมดูลหลัก ของ Electron:

  • app = ตัวแทนของแอปพลิเคชัน
  • BrowserWindow = ใช้สร้างหน้าต่าง (เหมือนเบราว์เซอร์)

js
const path = require('path');

👉 ใช้โมดูล path ของ Node.js เพื่อจัดการ path ของไฟล์ให้ปลอดภัย (ไม่ต้องเขียน path เป็น string เอง)


js
function createWindow () {

👉 ฟังก์ชันสำหรับ สร้างหน้าต่างหลัก ของแอป


js
const win = new BrowserWindow({
width: 800,
height: 600,

👉 สร้างวัตถุหน้าต่าง (win) ขนาด 800×600 px


js
webPreferences: {
preload: path.join(__dirname, 'renderer.js'),
nodeIntegration: true,
contextIsolation: false
}

🔍 webPreferences คือการกำหนดสิ่งที่ เบราว์เซอร์ในแอป จะทำได้:

คีย์ความหมาย
preloadโหลดไฟล์ JS ล่วงหน้า เช่น renderer.js
nodeIntegration: trueให้ HTML/JS สามารถใช้ Node.js (เช่น fs, path) ได้
contextIsolation: false(ไม่ปลอดภัย แต่สะดวก) ให้ JS ฝั่งหน้าต่างเข้าถึง global context ได้

⚠️ สำหรับโปรดักชัน: ควรใช้ contextIsolation: true แล้วใช้ IPC แทน


js
});

win.loadFile('index.html');

👉 โหลดไฟล์ HTML ที่จะใช้เป็น UI หลัก


js
}

🔚 จบฟังก์ชัน createWindow


js
app.whenReady().then(createWindow);

👉 สั่งให้ Electron สร้างหน้าต่าง เมื่อแอปพร้อมแล้ว (event-driven lifecycle)


💡 สรุปหน้าที่ของ main.js:

  • โหลด Electron
  • สร้างหน้าต่าง
  • ตั้งค่าการทำงานเบื้องต้น
  • โหลด UI (index.html) ขึ้นมาให้ผู้ใช้ใช้งาน

ถ้าคุณต้องการให้ main.js รองรับหลายหน้าต่าง, สื่อสารกับ renderer.js, หรือมีเมนู ฯลฯ ผมสามารถขยายให้ได้ตามต้องการครับ.