使用electron制作滿屏心特效的示例代碼
本文介紹了使用electron制作滿屏心特效的示例代碼,分享給大家,具體如下:
圖片被壓縮了 看起來(lái)很難看
主進(jìn)程代碼
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 style="position: absolute;width: 95%;height: 95%;border-radius: 10px;-webkit-app-region: drag"> <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)建畫(huà)布 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; }); // 初始化畫(huà)布大小 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 '愛(ài)你一輩子'; } 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è)問(wèn)題 就是這樣做界面不會(huì)正確響應(yīng) 我們可以使用進(jìn)程通信去解決
win.webContents.sen('windowSize',size);
之后再主進(jìn)程中監(jiān)聽(tīng)就行了
窗口點(diǎn)擊穿透
以上代碼會(huì)有一個(gè)問(wèn)題 就是一旦運(yùn)行 就不能關(guān)閉了
win.setIgnoreMouseEvents(true)
就可以讓界面只做展示使用 鼠標(biāo)可以點(diǎn)擊你窗口的任意區(qū)域
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- electron制作仿制qq聊天界面的示例代碼
- electron + vue項(xiàng)目實(shí)現(xiàn)打印小票功能及實(shí)現(xiàn)代碼
- electron中使用bootstrap的示例代碼
- Electron中實(shí)現(xiàn)大文件上傳和斷點(diǎn)續(xù)傳功能
- 從零開(kāi)始用electron手?jǐn)]一個(gè)截屏工具的示例代碼
- 使用electron將vue-cli項(xiàng)目打包成exe的方法
- 解決npm安裝Electron緩慢網(wǎng)絡(luò)超時(shí)導(dǎo)致失敗的問(wèn)題
- 詳解Webpack實(shí)戰(zhàn)之構(gòu)建 Electron 應(yīng)用
- 詳解Angular CLI + Electron 開(kāi)發(fā)環(huán)境搭建
- 關(guān)于node-bindings無(wú)法在Electron中使用的解決辦法
相關(guān)文章
js實(shí)現(xiàn)input密碼框提示信息的方法(附html5實(shí)現(xiàn)方法)
這篇文章主要介紹了js實(shí)現(xiàn)input密碼框提示信息的方法,涉及JavaScript頁(yè)面元素的獲取,屬性判斷及樣式設(shè)置等技巧,并附帶html5的相關(guān)實(shí)現(xiàn)方法,需要的朋友可以參考下2016-01-01Javascript 實(shí)現(xiàn)微信分享(QQ、朋友圈、分享給朋友)
這篇文章主要介紹了Javascript 實(shí)現(xiàn)微信分享(QQ、朋友圈、分享給朋友)的相關(guān)資料,需要的朋友可以參考下2016-10-10JavaScript實(shí)現(xiàn)日期格式化的操作詳解
在我們做業(yè)務(wù)開(kāi)發(fā)的漫長(zhǎng)歲月里,會(huì)多次跟時(shí)間打交道,相信大多數(shù)小伙伴對(duì)日期格式化也并不陌生,本文簡(jiǎn)單記錄了JavaScript實(shí)現(xiàn)日期格式化的過(guò)程,以及一些拓展,希望對(duì)大家有所幫助2023-05-05JavaScript 操作table,可以新增行和列并且隔一行換背景色代碼分享
這篇文章介紹了JavaScript操作table,可以新增行和列并且隔一行換背景色代碼,有需要的朋友可以參考一下2013-07-07微信小程序開(kāi)發(fā)中生命周期的詳細(xì)介紹
生命周期是指一個(gè)對(duì)象從創(chuàng)建→>運(yùn)行>銷毀的整個(gè)階段,強(qiáng)調(diào)的是一個(gè)時(shí)間段,文中介紹了小程序中組件的生命周期,需要的朋友可以參考下2023-03-03詳解js中構(gòu)造流程圖的核心技術(shù)JsPlumb
這篇文章主要介紹了js中構(gòu)造流程圖的核心技術(shù)JsPlumb,jsPlumb是一個(gè)強(qiáng)大的JavaScript連線庫(kù),它可以將html中的元素用箭頭、曲線、直線等連接起來(lái),適用于開(kāi)發(fā)Web上的圖表、建模工具等,需要的朋友可以參考下2015-12-12利用JavaScript實(shí)現(xiàn)仿京東放大鏡效果
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實(shí)現(xiàn)仿京東放大鏡特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03JavaScript實(shí)現(xiàn)獲取遠(yuǎn)程的html到當(dāng)前頁(yè)面中
今天做個(gè)項(xiàng)目,需要在當(dāng)前的html頁(yè)面中引用一個(gè)遠(yuǎn)程的html頁(yè)面,百度了一下,發(fā)現(xiàn)一個(gè)非常好用的代碼,這里分享給大家,有相同需求的小伙伴可以來(lái)看看2017-03-03