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文件夹,所在的字体文件为压缩提取后的结果。

发布了92 篇原创文章 · 获赞 55 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/elie_yang/article/details/90898643
今日推荐