如何利用node.js開發(fā)一個(gè)生成逐幀動(dòng)畫的小工具
前言
在實(shí)際工作中我們已經(jīng)下下來不下于一萬個(gè)npm包了,像我們熟悉的 vue-cli,react-native-cli 等,只需要輸入簡(jiǎn)單的命令 vue init webpack project,即可快速幫我們生成一個(gè)初始項(xiàng)目。在實(shí)際開發(fā)項(xiàng)目中,我們也可以定制一個(gè)屬于自己的npm包,來提高自己的工作效率。
為什么要開發(fā)一個(gè)工具包?
- 減少重復(fù)性的工作,不再需要復(fù)制其他項(xiàng)目再刪除無關(guān)代碼,或者從零創(chuàng)建一個(gè)項(xiàng)目和文件。
- 根據(jù)交互動(dòng)態(tài)生成項(xiàng)目結(jié)構(gòu)和所需要的文件等。
- 減少人工檢查的成本。
- 提高工作效率,解放生產(chǎn)力。
這次以幀動(dòng)畫工具為例,來一步一步解析如何開發(fā)一個(gè)npm包。
開始前的準(zhǔn)備
以我們這次為例。由于目前在做一些活動(dòng)頁(yè)相關(guān)的工作,其中動(dòng)畫部分全都采用CSS3中的animation來完成,但是這樣每次開發(fā)都要計(jì)算百分比,手動(dòng)判斷動(dòng)畫的一些屬性值,十分耗時(shí)又很容易出錯(cuò),就想能不能寫個(gè)腳本,直接一行命令就可以搞定了呢?!答案當(dāng)然是肯定的。
理清思路
我們要做一個(gè)可以通過讀取圖片就可以自動(dòng)生成包含CSS animation的HTML頁(yè)面,以后需要生成相應(yīng)的CSS片段,直接執(zhí)行命令就可以了。
初始化
既然是npm包,那我們就需要在npmjs上注冊(cè)一個(gè)賬號(hào),注冊(cè)完成之后回到本地新建一個(gè)文件目錄fbf,進(jìn)入fbf目錄下執(zhí)行npm init -y。
{ "name": "fbf", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "bin": { "test": "index.js" }, "keywords": [], "author": "", "license": "ISC" }
這樣,你的package.json就建好了。
依賴的庫(kù)
來看看會(huì)用到哪些庫(kù)。
- commander.js,可以自動(dòng)的解析命令和參數(shù),用于處理用戶輸入的命令。
- chalk,可以給終端的字體加上顏色。
- create-html,創(chuàng)建HTML模版,用于生成HTML。
- image-size,獲取圖片大小。
npm install commander chalk create-html image-size -S
命令行操作
node.js 內(nèi)置了對(duì)命令行操作的支持,在 package.json 中的 bin 字段可以定義命令名和關(guān)聯(lián)的執(zhí)行文件。所以現(xiàn)在 package.json 中加上 bin 的內(nèi)容:
{ "name": "fbf", "version": "1.0.0", "description": "", "bin": { "fbf": "index.js" }, ... }
然后在 index.js 中來定義 start 命令:
#!/usr/bin/env node const program = require('commander'); program.version('1.0.0', '-v, --version') .command('start <name>') .action((name) => { console.log(name); }); program.parse(process.argv);
調(diào)用 version('1.0.0', '-v, --version') 會(huì)將 -v 和 --version 添加到命令中,可以通過這些選項(xiàng)打印出版本號(hào)。
調(diào)用 command('start <name>') 定義 start 命令,name 則是必傳的參數(shù),為文件名。
action() 則是執(zhí)行 start 命令會(huì)發(fā)生的行為,要生成項(xiàng)目的過程就是在這里面執(zhí)行的,這里暫時(shí)只打印出 name。
其實(shí)到這里,已經(jīng)可以執(zhí)行 start 命令了。我們來測(cè)試一下,在 fbf 的同級(jí)目錄下執(zhí)行:
node ./test/index.js start HelloWorld
可以看到命令行工具也打印出了 HelloWorld,那么很清楚, action((name) => {}) 這里的參數(shù) name,就是我們執(zhí)行 start 命令時(shí)輸入的項(xiàng)目名稱。
命令已經(jīng)完成,接下來就要針對(duì)圖片的操作了。
獲取圖片信息
這里我們默認(rèn)根據(jù)第一張圖片的尺寸信息作為外層DIV的默認(rèn)尺寸。
#!/usr/bin/env node const program = require('commander'); const fs = require('fs'); const path = require('path'); const createHTML = require('create-html'); const sizeOf = require('image-size'); const chalk = require('chalk'); program.version('1.0.0', '-v, --version') .command('start <dir>') .action((dir) => { //獲取圖片路徑 const imgPath = path.resolve(dir) let imgSize = null; fs.readdir(imgPath, (err, file) => { imgSize = sizeOf(dir + '/' +file[0]); //取第一個(gè)圖片的尺寸作為框尺寸 let cssString = ` .fbf-animation{ width: ${imgSize.width}px; height: ${imgSize.height}px; margin:auto; background-image: url(./${dir}/${file[0]}); background-size: ${imgSize.width}px ${imgSize.height}px; background-repeat: no-repeat; animation-name: keyframes-img; animation-duration: 0.36s; animation-delay: 0s; animation-iteration-count: infinite; animation-fill-mode: forwards; animation-timing-function: steps(1); } ` }) })
生成CSS代碼
然后根據(jù)圖片數(shù)量生成相應(yīng)的keyframes代碼
function toCss(dir, fileList){ let _css = ''; let start = 0; const per = Math.floor(100/fileList.length); fileList.map((path, i) => { if(i === fileList.length - 1){ _css += ` ${start + i*per}%, 100% { background:url(./${dir}/${path}) center center no-repeat; background-size:100% auto; } ` }else{ _css += ` ${start + i*per}% { background:url(./${dir}/${path}) center center no-repeat; background-size:100% auto; } ` } }) return _css; } let frameCss = toCss(dir, newFile) //取第一個(gè)圖片的尺寸作為框尺寸 let cssString = ` .fbf-animation{ width: ${imgSize.width}px; height: ${imgSize.height}px; margin:auto; background-image: url(./${dir}/${file[0]}); background-size: ${imgSize.width}px ${imgSize.height}px; background-repeat: no-repeat; animation-name: keyframes-img; animation-duration: 0.36s; animation-delay: 0s; animation-iteration-count: infinite; animation-fill-mode: forwards; animation-timing-function: steps(1); } @keyframes keyframes-img { ${frameCss} }
生成html文件
最后我們把生成的CSS放到HTML里。
//生成html let html = createHTML({ title: '逐幀動(dòng)畫', scriptAsync: true, lang: 'en', dir: 'rtl', head: '<meta name="description" content="example">', body: '<div class="fbf-animation"></div>' + css, favicon: 'favicon.png' }) fs.writeFile('fbf.html', html, function (err) { if (err) console.log(err) })
視覺美化
通過 chalk 來為打印信息加上樣式,比如成功信息為綠色,失敗信息為紅色,這樣子會(huì)讓用戶更加容易分辨,同時(shí)也讓終端的顯示更加的好看。
const chalk = require('chalk'); console.log(chalk.green('生成代碼成功!')); console.log(chalk.red('生成代碼失敗'));
完整示例
#!/usr/bin/env node const program = require('commander'); const fs = require('fs'); const path = require('path'); const createHTML = require('create-html'); const sizeOf = require('image-size'); const chalk = require('chalk'); //排序 const sortByFileName = files => { const reg = /[0-9]+/g; return files.sort((a, b) => { let imga = (a.match(reg) || []).slice(-1), imgb = (b.match(reg) || []).slice(-1) return imga - imgb }); } //刪除.DS_Store function deleteDS(file) { file.map((v, i) => { if(v === '.DS_Store'){ fs.unlink('img/.DS_Store', err => {}) } }) } // 生成keyframe function toCss(dir, fileList){ let _css = ''; let start = 0; const per = Math.floor(100/fileList.length); fileList.map((path, i) => { if(i === fileList.length - 1){ _css += ` ${start + i*per}%, 100% { background:url(./${dir}/${path}) center center no-repeat; background-size:100% auto; } ` }else{ _css += ` ${start + i*per}% { background:url(./${dir}/${path}) center center no-repeat; background-size:100% auto; } ` } }) console.log(chalk.green('css successed!')) return _css; } program.version('1.0.0', '-v, --version') .command('start <dir>') .action((dir) => { const imgPath = path.resolve(dir) let imgSize = null; fs.readdir(imgPath, (err, file) => { const newFile = sortByFileName(file) deleteDS(newFile) imgSize = sizeOf(dir + '/' +file[0]); let frameCss = toCss(dir, newFile) //取第一個(gè)圖片的尺寸作為框尺寸 let cssString = ` .fbf-animation{ width: ${imgSize.width}px; height: ${imgSize.height}px; margin:auto; background-image: url(./${dir}/${file[0]}); background-size: ${imgSize.width}px ${imgSize.height}px; background-repeat: no-repeat; animation-name: keyframes-img; animation-duration: 0.36s; animation-delay: 0s; animation-iteration-count: infinite; animation-fill-mode: forwards; animation-timing-function: steps(1); } @keyframes keyframes-img { ${frameCss} } ` let css = ` <style>${cssString}</style> ` //生成html let html = createHTML({ title: '逐幀動(dòng)畫', scriptAsync: true, lang: 'en', dir: 'rtl', head: '<meta name="description" content="example">', body: '<div class="fbf-animation"></div>' + css, favicon: 'favicon.png' }) fs.writeFile('fbf.html', html, function (err) { console.log(chalk.green('html successed!')) if (err) console.log(err) }) }) }); program.parse(process.argv);
代碼一共100行左右,可以說非常簡(jiǎn)單明了,有興趣的同學(xué)可以試試。
最后
完成之后,使用npm publish fbf就可以把腳手架發(fā)布到 npm 上面,通過 -g 進(jìn)行全局安裝,就可以在自己本機(jī)上執(zhí)行 fbf start [dir]來生成一個(gè)fbf.html文件,這樣便完成了一個(gè)簡(jiǎn)單的node工具了。
好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
- 利用JavaScript差集實(shí)現(xiàn)一個(gè)對(duì)比小工具
- Node.js 實(shí)現(xiàn)搶票小工具 & 短信通知提醒功能
- java9的JShell小工具和編譯器兩種自動(dòng)優(yōu)化方法
- Node.js實(shí)現(xiàn)JS文件合并小工具
- javascript制作sql轉(zhuǎn)換為stringBuffer的小工具
- js 創(chuàng)建書簽小工具之理論
- 用hta[javascript]寫的定時(shí)重啟或關(guān)機(jī)的小工具
- 國(guó)慶節(jié)到了,利用JS實(shí)現(xiàn)一個(gè)生成國(guó)慶風(fēng)頭像的小工具 詳解實(shí)現(xiàn)過程
相關(guān)文章
nodejs+mongodb aggregate級(jí)聯(lián)查詢操作示例
這篇文章主要介紹了nodejs+mongodb aggregate級(jí)聯(lián)查詢操作,結(jié)合實(shí)例形式分析了基于nodejs的mongodb數(shù)據(jù)庫(kù)級(jí)聯(lián)查詢相關(guān)操作技巧,需要的朋友可以參考下2018-03-03利用Node轉(zhuǎn)換Excel成JSON的詳細(xì)步驟
最近工作中遇到一個(gè)需求,大致需求就是將Excel文件在導(dǎo)入時(shí)解析為json格式轉(zhuǎn)換數(shù)據(jù)結(jié)構(gòu)再傳輸給后臺(tái),下面這篇文章主要給大家介紹了關(guān)于如何利用Node轉(zhuǎn)換Excel成JSON的詳細(xì)步驟,需要的朋友可以參考下2022-11-11nodejs16.15.0版本如何解決node-sass和sass-loader版本沖突問題
這篇文章主要介紹了nodejs16.15.0版本如何解決node-sass和sass-loader版本沖突問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08