溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用electron怎么制作一個(gè)滿屏心特效

發(fā)布時(shí)間:2021-05-11 18:24:48 來源:億速云 閱讀:179 作者:Leah 欄目:web開發(fā)

這篇文章給大家介紹使用electron怎么制作一個(gè)滿屏心特效,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

import {BrowserWindow, app, ipcMain} from 'electron'

createWindow();

ipcMain.on('quitApp', () => {
  app.quit();
});

function createWindow() {

  const loginURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080` : `file://${__dirname}/index.html`;
  const win = new BrowserWindow({

    alwaysOnTop: true,
    modal: true,
    width: 1200,
    height: 6500,
    frame: false,
    show: false,
    darkTheme: true,
    resizable: false,
    minimizable: false,
    maximizable: false,
    transparent: true,
    type: 'toolbar',
    webPreferences: {
      devTools: false,
    }
  });
  win.loadURL(loginURL);
  win.once('ready-to-show', () => {
    win.show();
  });
}

渲染進(jìn)程代碼

界面代碼

<template>
  <div >
    <canvas ref="drawHeart" id="drawHeart"></canvas>
  </div>
</template>

js代碼

 mounted() {
      var hearts = [];
      var canvas = this.$refs.drawHeart;
      var wW = 1920;
      var wH = 1040;
      // 創(chuàng)建畫布
      var ctx = canvas.getContext('2d');
      // 創(chuàng)建圖片對(duì)象
      var heartImage = new Image();
      heartImage.src = img;
      var num = 100;

      init();

      window.addEventListener('resize', function(){
        wW = window.innerWidth;
        wH = window.innerHeight;
      });
      // 初始化畫布大小
      function init(){
        canvas.width = wW;
        canvas.height = wH;
        for(var i = 0; i < num; i++){
          hearts.push(new Heart(i%5));
        }
        requestAnimationFrame(render);
      }

      function getColor(){
        var val = Math.random() * 10;
        if(val > 0 && val <= 1){
          return '#00f';
        } else if(val > 1 && val <= 2){
          return '#f00';
        } else if(val > 2 && val <= 3){
          return '#0f0';
        } else if(val > 3 && val <= 4){
          return '#368';
        } else if(val > 4 && val <= 5){
          return '#666';
        } else if(val > 5 && val <= 6){
          return '#333';
        } else if(val > 6 && val <= 7){
          return '#f50';
        } else if(val > 7 && val <= 8){
          return '#e96d5b';
        } else if(val > 8 && val <= 9){
          return '#5be9e9';
        } else {
          return '#d41d50';
        }
      }

      function getText(){
        var val = Math.random() * 10;
        if(val > 1 && val <= 3){
          return '愛你一輩子';
        } else if(val > 3 && val <= 5){
          return '感謝你';
        } else if(val > 5 && val <= 8){
          return '喜歡你';
        } else{
          return 'I Love You';
        }
      }

      function Heart(type){
        this.type = type;
        // 初始化生成范圍
        this.x = Math.random() * wW;
        this.y = Math.random() * wH;

        this.opacity = Math.random() * .5 + .5;

        // 偏移量
        this.vel = {
          x: (Math.random() - .5) * 5,
          y: (Math.random() - .5) * 5
        }

        this.initialW = wW * .5;
        this.initialH = wH * .5;
        // 縮放比例
        this.targetScale = Math.random() * .15 + .02; // 最小0.02
        this.scale = Math.random() * this.targetScale;

        // 文字位置
        this.fx = Math.random() * wW;
        this.fy = Math.random() * wH;
        this.fs = Math.random() * 10;
        this.text = getText();

        this.fvel = {
          x: (Math.random() - .5) * 5,
          y: (Math.random() - .5) * 5,
          f: (Math.random() - .5) * 2
        }
      }

      Heart.prototype.draw = function(){
        ctx.save();
        ctx.globalAlpha = this.opacity;
        ctx.drawImage(heartImage, this.x, this.y, this.width, this.height);
        ctx.scale(this.scale + 1, this.scale + 1);
        if(!this.type){
          // 設(shè)置文字屬性
          ctx.fillStyle = getColor();
          ctx.font = 'italic ' + this.fs + 'px sans-serif';
          // 填充字符串
          ctx.fillText(this.text, this.fx, this.fy);
        }
        ctx.restore();
      }
      Heart.prototype.update = function(){
        this.x += this.vel.x;
        this.y += this.vel.y;

        if(this.x - this.width > wW || this.x + this.width < 0){
          // 重新初始化位置
          this.scale = 0;
          this.x = Math.random() * wW;
          this.y = Math.random() * wH;
        }
        if(this.y - this.height > wH || this.y + this.height < 0){
          // 重新初始化位置
          this.scale = 0;
          this.x = Math.random() * wW;
          this.y = Math.random() * wH;
        }

        // 放大
        this.scale += (this.targetScale - this.scale) * .1;
        this.height = this.scale * this.initialH;
        this.width = this.height * 1.4;

        // -----文字-----
        this.fx += this.fvel.x;
        this.fy += this.fvel.y;
        this.fs += this.fvel.f;

        if(this.fs > 50){
          this.fs = 2;
        }

        if(this.fx - this.fs > wW || this.fx + this.fs < 0){
          // 重新初始化位置
          this.fx = Math.random() * wW;
          this.fy = Math.random() * wH;
        }
        if(this.fy - this.fs > wH || this.fy + this.fs < 0){
          // 重新初始化位置
          this.fx = Math.random() * wW;
          this.fy = Math.random() * wH;
        }
      }

      function render(){
        ctx.clearRect(0, 0, wW, wH);
        for(var i = 0; i < hearts.length; i++){
          hearts[i].draw();
          hearts[i].update();
        }
        requestAnimationFrame(render);
      }

    }

擴(kuò)展功能

全屏展示

const size = screen.getPrimaryDisplay().workAreaSize;  //獲取顯示器的寬高
win.setSet(size.width size.height);
win.setPosition(0,0);

這樣就會(huì)讓窗口全屏 但是有一個(gè)問題 就是這樣做界面不會(huì)正確響應(yīng) 我們可以使用進(jìn)程通信去解決

win.webContents.sen('windowSize',size);

關(guān)于使用electron怎么制作一個(gè)滿屏心特效就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI