一、基于SVG文档的文字居中

text-anchor: middle; //水平方向居中
dominant-baseline: middle; //垂直居中
1.使用内联样式配置居中
<svg style=' border:1px solid blue;width:300px;height:300px;'>
<path d='M0,150 300,150 M150,0 L150,300 ' fill='none' stroke='green'/>
<text fill='red' x='150' y='150'
style='dominant-baseline:middle;text-anchor:middle;'>
中文内容,中文内容
</text>
</svg>

2.css中配置居中

svg {
width: 300px;
height: 150px;
border:1px solid red;
} path {
fill: #4682B4;
} text {
stroke: #fff;
stroke-width:;
font-size: 20px; text-anchor: middle;
/* 文本水平居中 */
dominant-baseline: middle;
/* 文本垂直居中 */
}

svg代码:

<svg>
<path d="M150 0 L75 200 L225 200 Z" />
<text x='150' y='75'>1233</text>
</svg>

二、在TextPath中设置居中

1.定义直线path居中处理

<svg style=' border:1px solid blue;'>
<defs>
<!-- 相对group 高度是容器的一半,宽度和容器相同 -->
<path id="center" d="M0 20,200,20" style="stroke: white; fill: none;" />
</defs>
<g transform='translate(50,50)'>
<rect width='200' height='40' fill='blue' />
<text style="font-size: 15;stroke:red;text-anchor:middle;dominant-baseline:middle">
<textPath xlink:href="#center" startOffset="50%">
中文测试内容
</textPath>
</text>
</g>
</svg>

2.定义曲线path,居中处理

<svg style='stroke:green; border:1px solid blue;' >
<defs>
<path id="short-corner" transform="translate(40,40)" d="M0 0 L 30 0 A 30 30 0 0 1 60 30 L 60 60"
style="stroke: gray; fill: none;" /> <path id="long-corner" transform="translate(140,40)" d="M0 0 L 50 0 A 30 30 0 0 1 80 30 L 80 80"
style="stroke: gray; fill: none;" />
</defs> <use xlink:href="#short-corner" />
<text style="font-size: 14;">
<textPath xlink:href="#short-corner">
This text is
</textPath>
</text> <use xlink:href="#long-corner" />
<text style="font-size: 14; text-anchor: middle;">
<textPath xlink:href="#long-corner" startOffset="50%">
centered
</textPath>
</text>
</svg>

3.使用SVG.js 设置居中处理

var draw = SVG('container').size(300, 300);
draw.style({
border: '1px solid red'
}); var group = draw.group();
var rect = draw.rect(100, 60).fill('green');
group.add(rect);
var text = draw.text('测试按钮');
text.style({
fill: 'red',
});
text.path('M0,10 100,10').attr({
fill:'none'
}); text.textPath().attr({
startOffset: '50%',
'text-anchor':'middle',
'dominant-baseline':'middle'
});
group.add(text); group.move(150, 100);
group.on('mouseenter', function () {
rect.fill('blue');
}).on('mouseleave', function () {
rect.fill('green');
});

相关文章:http://www.php.cn/html5-tutorial-35158.html http://www.cnblogs.com/lovesong/p/5998650.html   http://blog.csdn.net/lcy132100/article/details/9722543

更多:

SVG.js 文本绘制整理

SVG Path路径使用(一)

SVG 使用marker画箭头(一)

SVG 文字居中整理的更多相关文章

  1. css实现一行文字居中,多行文字左对齐

    问题及场景: 当内容能一行显示在盒子内时,文字居中对齐. 当内容过多换行后显示在盒子内时,文字左对齐. 其实这种视觉上的需求还是蛮常见的.比如用于弹出提示框,当提示内容比较少时,内容居中显示在弹出框, ...

  2. Android TextView 文字居中

    有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...

  3. 完美解决移动Web小于12px文字居中的问题

    前几天的一篇博文:移动Web单行文字垂直居中的问题,提到了移动web里小于12px的文字居中异常的问题,最后还是改为12px才近乎解决了问题.但是有时候或许并不是那么乐观,你并不能将原本定为10px的 ...

  4. 设置TextView文字居中

    有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...

  5. 【转】设置TextView文字居中

    原文网址:http://blog.csdn.net/lanpy88/article/details/6616924 有2种方法可以设置TextView文字居中: 一:在xml文件设置:android: ...

  6. iOS UITableViewCell透明度 和 cell文字居中

    1.创建UITableViewCell时,的模式用UITableViewCellStyleValue1时,透明度直接将UITableView的透明度设置以下就搞定拉,但是文字居中难以实现. 2.创建U ...

  7. 如何设置select和option的文字居中?

    今天在设置option文字居中时发现,给select设置text-align:center在火狐浏览器下ok,但是在chrome浏览器无效,然后option在两个浏览器下设置text-align:ce ...

  8. css文字居中、图片居中、div居中解决方案

    一.文字居中 若文字只有一行 <!--html代码--> <div class="box"> <p class="text"> ...

  9. 微信小程序 canvas 文字居中

    drawCanvas: function(ctx) { //... // 昵称 ctx.setFontSize(16) //字体大小 ctx.setFillStyle('#fff') //字体颜色 c ...

随机推荐

  1. LOOPS 概率dp

    题意:迷宫是一个R*C的布局,每个格子中给出停留在原地,往右走一个,往下走一格的概率,起点在(1,1),终点在(R,C),每走一格消耗两点能量,求出最后所需要的能量期望 简单概率dp 注意   原地不 ...

  2. 7-10Editing aBook uva11212(迭代加深搜索 IDA*)

    题意:  给出n( 2<=n<=9) 个乱序的数组  要求拍成升序  每次 剪切一段加上粘贴一段算一次  拍成1 2 3 4 ...n即可     求排序次数 典型的状态空间搜索问题   ...

  3. OLAP和OLTP基础知识

    数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical Processing).O ...

  4. HashMap几个需要注意的知识点

    HashMap简介 HashMap 是java集合框架的一部分. key value都允许null值 (除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同) 不保 ...

  5. babel更新之后的 一些坑

    最近在使用babel-loader的时候,发生了一些错误,现在的babel-loader版本已经是8.0.0,更新到这个版本之后,如果还按照以前的安装依赖的方法: cnpm install --sav ...

  6. 一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载)

    目录 一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载) Http协议简述 HttpRequest类设计 请求部分 接收部分 关于上传和下载 Cpp实现 关于源码中的Lo ...

  7. ARP监控工具ARPalert

    ARP监控工具ARPalert   ARP协议用于IP转化为Mac地址.由于ARP协议的缺陷,导致被用于中间人攻击.为了防止ARP攻击,Kali Linux提供了专用工具ARPalert.该工具可以对 ...

  8. Codeforces.786B.Legacy(线段树优化建图 最短路Dijkstra)

    题目链接 \(Description\) 有\(n\)个点.你有\(Q\)种项目可以选择(边都是有向边,每次给定\(t,u,v/lr,w\)): t==1,建一条\(u\to v\)的边,花费\(w\ ...

  9. Java_Certificates does not conform to algorithm constraints

    java.security.cert.CertificateException: Certificates does not conform to algorithm constraints SSL证 ...

  10. JS实现各种复制到剪贴板

    一.实现点击按钮,复制文本框中的的内容                         <script type="text/javascript"> function ...