js實(shí)現(xiàn)GIF動(dòng)圖分解成多幀圖片上傳
在項(xiàng)目中遇到需要支持上傳gif圖片,并把其分解的幀圖片一次展示給用戶。話不多說直接上代碼
分解gif圖片需要使用libgif-js這個(gè)庫!
1. 引入Git庫
import SuperGif from './libgif.js'
2. 分解Gif為png圖片
const GifDecomposer = { structureGifObject (gifFiles, cb) { // gifFiles 獲取的文件對(duì)象 e.target.files[0] const gifImg = document.createElement('img'); gifImg.setAttribute('rel:animated_src', URL.createObjectURL(gifFiles)); gifImg.setAttribute('rel:auto_play', '0'); // Modified pictures must be added to the body document.body.appendChild(gifImg); // Construction example var rub = new SuperGif({ gif: gifImg }); rub.load(() => { var img_list = []; for (let i=1; i <= rub.get_length(); i++) { // Traversing through each frame of a GIF instance rub.move_to(i); // Converting each frame of canvas into a file object let cur_file = this.convertCanvasToImage(rub.get_canvas(), gifFiles.name.replace('.gif', '') + `-${i}`) img_list.push({ file_name: cur_file.name, url: URL.createObjectURL(cur_file), file: cur_file, }) } cb(img_list) }); }, dataURLtoFile (dataurl, filename) { const arr = dataurl.split(','); const mime = arr[0].match(/:(.*?);/)[1]; const bstr = atob(arr[1]); var n = bstr.length; const u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new File([u8arr], filename, {type:mime}); }, convertCanvasToImage (canvas, filename) { return this.dataURLtoFile(canvas.toDataURL('image/png'), filename); } }
3. 上傳每一張圖片
/** * costume upload GIF decomposer */ const filesImg = function (list, storage, costumeFormat, assetType, handleCostume) { let proDataList = list.map((item, index) => { return new Promise(function(resolve, reject) { let reader = new FileReader(); reader.readAsArrayBuffer(item.file); reader.onload = () => { let data = {result: reader.result, type: item.file.type, name: item.file.name} resolve(data); }; reader.onerror = (error) => {reject(error)}; }) }) Promise.all(proDataList).then(res => { res.forEach(item => { // 上傳 }) }).catch(data => {console.log(data)}) }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript中Number.isNaN 和 isNaN 的區(qū)別詳解
本文和大家分享一個(gè)前幾天寫代碼踩的坑,筆者在業(yè)務(wù)邏輯中需要對(duì)一個(gè)值進(jìn)行NaN的判斷,由于筆者的不嚴(yán)謹(jǐn),使用了isNaN,從而引起B(yǎng)ug,也正是因?yàn)檫@個(gè),筆者才知道了isNaN和Number.isNaN的區(qū)別,所以本文就和大家聊聊它們的區(qū)別2023-09-09JavaScript中的稀疏數(shù)組與密集數(shù)組[譯]
一般來說,JavaScript中的數(shù)組是稀疏的,也就是說,數(shù)組中的元素之間可以有空隙,因?yàn)橐粋€(gè)數(shù)組其實(shí)就是一個(gè)鍵值映射.本文解釋了如何創(chuàng)建稀疏數(shù)組和不稀疏的數(shù)組2012-09-09Canvas 制作動(dòng)態(tài)進(jìn)度加載水球詳解及實(shí)例代碼
這篇文章主要介紹了Canvas 制作動(dòng)態(tài)進(jìn)度加載水球詳解及實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12基于JavaScript實(shí)現(xiàn)移動(dòng)端無限加載分頁
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)移動(dòng)端無限加載分頁,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03c#和Javascript操作同一json對(duì)象的實(shí)現(xiàn)代碼
剛開始學(xué)Javascript,接觸到j(luò)son對(duì)象,json可以看作是用于客戶端數(shù)據(jù)實(shí)體對(duì)象的載體。json對(duì)象一般都是通過ajax方式傳送給服務(wù)層2012-01-01JavaScript數(shù)據(jù)類型轉(zhuǎn)換實(shí)例(其他類型轉(zhuǎn)字符串、數(shù)值型、布爾類型)
這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)據(jù)類型轉(zhuǎn)換的相關(guān)資料,本文分別介紹了其他類型轉(zhuǎn)為字符串、其他類型轉(zhuǎn)為數(shù)值型以及其他類型轉(zhuǎn)為布爾類型的方法,需要的朋友可以參考下2021-07-07從js向Action傳中文參數(shù)出現(xiàn)亂碼問題的解決方法
Action獲取jsp表單中的中文參數(shù),只要整個(gè)項(xiàng)目都采用UTF-8編碼格式都不會(huì)出現(xiàn)亂碼問題;但JSP中用到JS,并從JS向Action傳中文參數(shù),就會(huì)出現(xiàn)中文亂的現(xiàn)象2013-12-12