Skip to content

Add animated tray duck icon. #4

Description

@exclushivity

Absolutely! Let’s add an animated duck tray icon to your Electron app. (I assume you want the icon to animate—like a walking duck—when in the Windows system tray.)

High-Level Approach

Electron’s [Tray](https://www.electronjs.org/docs/latest/api/tray) API lets us dynamically update the icon. Animation involves changing the icon at short intervals ("frame-by-frame").

Step-by-Step Guide

1. Prepare Your Icon Frames

Create a set of PNG images (transparent background, ideally 16x16 or 32x32) representing animation frames (e.g., duck1.png, duck2.png, duck3.png...), placed in your assets/ folder.

2. Setup Tray in main.js

const { app, Tray, nativeImage } = require('electron');
const path = require('path');

let tray = null;
let animationInterval;
let currentFrame = 0;

// List of animation frames
const trayIcons = [
  path.join(__dirname, 'assets/duck1.png'),
  path.join(__dirname, 'assets/duck2.png'),
  path.join(__dirname, 'assets/duck3.png'),
  path.join(__dirname, 'assets/duck4.png'),
];

function updateTrayIcon() {
  const icon = nativeImage.createFromPath(trayIcons[currentFrame]);
  tray.setImage(icon);
  currentFrame = (currentFrame + 1) % trayIcons.length;
}

app.whenReady().then(() => {
  tray = new Tray(trayIcons[0]);
  tray.setToolTip('Quack! 🦆');

  // Start animation (change frame every 150ms)
  animationInterval = setInterval(updateTrayIcon, 150);

  // Optionally stop animation on click (you can add pause/play logic)
  tray.on('click', () => {
    if (animationInterval) {
      clearInterval(animationInterval);
      animationInterval = null;
    } else {
      animationInterval = setInterval(updateTrayIcon, 150);
    }
  });
});

app.on('window-all-closed', () => {
  // On Windows, apps typically stay active in the tray
});

3. Place PNGs

  • Place duck1.png, duck2.png, ..., in your assets/ directory.

4. Run!

Start your Electron app. You’ll see a duck quacking/dancing/walking, depending on your frame art, in the system tray!

Tips

  • Draw or download animated GIF and split it into PNG frames (e.g., via free tools or [ezgif.com](https://ezgif.com/split)).
  • You can adjust the frame rate by changing the interval value.
  • Use .ico files for Windows if you want crisp icons, but PNG works well for this use case.

Let me know if you need help with the art, or want a sample set of duck PNGs!

Metadata

Metadata

Labels

No labels
No labels

Fields

No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions