vue.js自定義LCD字體及字體壓縮

大屏監控中常用到液晶字體效果,如下圖所示: 

首先下載lcdd.ttf字體;

在 webpack.config.js中設置對.ttf字體模塊的處理,webpack視一切文件爲模塊,.ttf字體文件也不例外

            {
                test: /\.(svg|woff|ttf|eot)\??.*$/,
                use: [
                    {
                        loader: "file-loader",
                        options: {
                            name: "assest/font/[name].[ext]",                           
                            publicPath: "../../"
                        }
                    }
                ]
            }

 在項目中組織好lcdd.tff文件的位置,在index.css中寫入自定義字體樣式

 在需要的地方使用這個字體樣式

.num-text
    color #edd327
    font-family 'lcdd'

 液晶字體通常是0到9數字,小數點和“%”等,如果中文字體也包含進來的情況下,加大了網絡下載開銷,於是優化壓縮處理

方法如下:這裏使用了一個第三方工具:字蛛 http://font-spider.org/ 

步驟1:全局安裝font-spider包: npm i -g font-spider

步驟2:新建一個index.html頁面

寫入簡單的html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <style>
        @font-face {
            font-family: 'lcdd';
            src: url('./lcdd.TTF');
            src: url('./lcdd.TTF') format('truetype');
            font-weight: normal;
            font-style: normal;
        }

        #test {
            font-family: lcdd;
        }
    </style>
</head>
<body>
    <h2 id="test">12345678.980%</h2>
</body>
</html>

步驟3:PowerShell 進入index.html所在頁面 ,執行font-spider index.html,如下圖所示:

壓縮後只有5.756KB. 

在當前路徑下會生成一個.spider文件夾,所在的字體文件爲壓縮提取後的結果。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章