HTML5: 利用SVG动画动态绘制文字轮廓边框线条

 
简要教程

这是一款很酷的html5 svg线条动态绘制文字轮廓边框动画特效。SVG路径动画在网页设计中是一项热门的技术,它允许我们绘制各种简单、精美的图标和文字。关于使用SVG制作图标方面的知识,请参考阅读ESSENTIAL ICONS

 

制作流程

先用一张gif图片来看看效果:

HTML5: 利用SVG线条动态绘制文字轮廓边框动画

制作SVG

1、首先你要下载和安装一款矢量图形编辑器,推荐使用Inkscape

2、打开Inkscape,创建一个新的文档。在文档的左上角创建你的文字。

HTML5: 利用SVG线条动态绘制文字轮廓边框动画

3、使用选择工具选取你创建的文字。

4、在Path菜单中,选择Object --> Path。然后保存为SVG。

5、使用文本编辑器打开你刚才保存的SVG。将一些不需要的标签去掉,每个路径只保留path部分即可,见下图:

HTML5: 利用SVG线条动态绘制文字轮廓边框动画

CSS

1、创建一个div作为包裹容器包住SVG。给这个div一个id,然后添加下面的css代码:

#YOUR-SVG-CONTAINER { //ADJUST NAME TO MATCH YOUR ID
  position: relative;
  width: 360px;   //ADJUST WIDTH TO MATCH WIDTH OF YOUR TEXT
  height: 110px;  //ADJUST HEIGHT TO MATCH HEIGHT OF YOUR TEXT
  margin: 40vh auto 0 auto;
}

 

2、给你的SVG元素一个id,然后添加下面的CSS代码:

#svg-canvas { //ADJUST NAME TO MATCH YOUR ID
  position: relative;
  width: 360px; //ADJUST WIDTH TO MATCH WIDTH OF YOUR TEXT
  height: 110px; //ADJUST HEIGHT TO MATCH HEIGHT OF YOUR TEXT
}

 

3、确保你的每个路径元素看起来像下面这样:

<path class="title" fill-opacity="0" stroke="#000" stroke-width="1.5" d="" />

 

4、在你的样式表中添加下面的代码。下面的css代码将以线条绘制文字轮廓。关于下面代码的说明请点击这里

.title {
    stroke-dasharray: 500;
            stroke-dashoffset: 500;
    animation: draw 5s linear forwards;
    -webkit-animation: draw 8s linear forwards;
    -moz-animation: draw 8s linear forwards;
    -o-animation: draw 8s linear forwards;
    font-weight: bold;
    font-family: Amatic SC;
    -inkscape-font-specification: Amatic SC Bold"
    }
    @keyframes draw {
    to {
        stroke-dashoffset: 0;
        }
    }
    @-webkit-keyframes draw {
    to {
        stroke-dashoffset: 0;
        }
    }
 
    @-moz-keyframes draw {
    to {
        stroke-dashoffset: 0;
        }
    }
    @-o-keyframes draw {
    to {
        stroke-dashoffset: 0;
    }
}

 

 

出处:http://www.htmleaf.com/html5/html5donghua/20141203663.html

DEMO: 点击这里看效果

 

完整代码:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>text svg</title>
	<style type="text/css">
		body {
			background-color: #212A3F;
		}

		#svg-container {
			position: relative;
			width: 940px;
			height: 110px;
			margin: 40vh auto 0 auto;
		}
		#svg-canvas {
			position: relative;
			width: 940px;
			height: 110px;
		}
		.title {
			stroke-dasharray: 500;
			stroke-dashoffset: 500;
			animation: draw 5s linear forwards;		
			-webkit-animation: draw 5s linear forwards;
			-moz-animation: draw 5s linear forwards;
			-o-animation: draw 5s linear forwards;
			font-style:normal;
			font-variant:normal;
			font-weight:normal;
			font-stretch:semi-expanded;
			font-size:69.40313721px;
			font-family:Ravie;
			-inkscape-font-specification:'Ravie Semi-Expanded'
		}
		@keyframes draw {
		to {
			stroke-dashoffset: 0;
			}
		}
		@-webkit-keyframes draw {
		to {
			stroke-dashoffset: 0;
			}
		}
		
		@-moz-keyframes draw {
		to {
			stroke-dashoffset: 0;
			}
		}
		@-o-keyframes draw {
		to {
			stroke-dashoffset: 0;
			}
		}
	</style>
</head>
<body>

	<div id="svg-container">
		<svg id="svg-canvas">
			<path class="title" fill-opacity="0" stroke="#FFF" stroke-width="1.5"
			d="m 58.963004,17.880108 q 0,2.270513 -5.21879,3.863261 -4.608802,1.423306 -9.759816,1.423306 -4.812132,0 -7.895963,-1.796077 -3.558266,-2.101072 -3.558266,-6.032109 0,-5.083237 11.725335,-5.083237 6.506544,0 10.7425
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值