1e41f4b71Sopenharmony_ci# textPath 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci沿路径绘制文本。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci> **说明:** 7e41f4b71Sopenharmony_ci> - 该组件从API version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> - 按指定的路径绘制文本,可嵌套子标签tspan分段。 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> - 只支持被父元素标签text嵌套。 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## 权限列表 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci无 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## 子组件 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci[tspan](js-components-svg-tspan.md)。 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## 属性 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci支持以下表格中的属性。 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 描述 | 30e41f4b71Sopenharmony_ci| -------------- | ---------------------------------- | ------ | ------------------------------------------------------------ | 31e41f4b71Sopenharmony_ci| id | string | - | 组件的唯一标识。 | 32e41f4b71Sopenharmony_ci| path | string | 0 | 设置路径的形状。<br/>字母指令表示的意义如下:<br/>- M = moveto<br/>- L = lineto<br/>- H = horizontal lineto<br/>- V = vertical lineto<br/>- C = curveto<br/>- S = smooth curveto<br/>- Q = quadratic Belzier curve<br/>- T = smooth quadratic Belzier curveto<br/>- A = elliptical Arc<br/>- Z = closepath<br/>默认值:0 | 33e41f4b71Sopenharmony_ci| startOffset | <length>\|<percentage> | 0 | 设置文本沿path绘制的起始偏移。<br/>默认值:0 | 34e41f4b71Sopenharmony_ci| font-size | <length> | 30px | 设置文本的尺寸。<br/>默认值:30px | 35e41f4b71Sopenharmony_ci| fill | <color> | black | 字体填充颜色。<br/>默认值:black | 36e41f4b71Sopenharmony_ci| by | number | - | 相对被指定动画的属性偏移值,from默认为原属性值。 | 37e41f4b71Sopenharmony_ci| opacity | number | 1 | 元素的透明度,取值范围为0到1,1表示为不透明,0表示为完全透明。支持属性动画。<br/>默认值:0 | 38e41f4b71Sopenharmony_ci| fill-opacity | number | 1.0 | 字体填充透明度。<br/>默认值:1.0 | 39e41f4b71Sopenharmony_ci| stroke | <color> | black | 绘制字体边框并指定颜色。<br/>默认值:black | 40e41f4b71Sopenharmony_ci| stroke-width | number | 1px | 字体边框宽度。<br/>默认值:1px | 41e41f4b71Sopenharmony_ci| stroke-opacity | number | 1.0 | 字体边框透明度。<br/>默认值:1.0 | 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci## 示例 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_citextspan属性示例,textpath文本内容沿着属性path中的路径绘制文本,起点偏移20%的path长度。(绘制的元素<path>曲线仅做参照) 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci```html 49e41f4b71Sopenharmony_ci<!-- xxx.hml --> 50e41f4b71Sopenharmony_ci<div class="container"> 51e41f4b71Sopenharmony_ci <svg fill="#00FF00" x="50"> 52e41f4b71Sopenharmony_ci <path d="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" stroke="red" fill="none"></path> 53e41f4b71Sopenharmony_ci <text> 54e41f4b71Sopenharmony_ci <textpath fill="blue" startOffset="20%" path="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" font-size="30px"> 55e41f4b71Sopenharmony_ci This is textpath test. 56e41f4b71Sopenharmony_ci </textpath> 57e41f4b71Sopenharmony_ci </text> 58e41f4b71Sopenharmony_ci </svg> 59e41f4b71Sopenharmony_ci</div> 60e41f4b71Sopenharmony_ci``` 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci```css 63e41f4b71Sopenharmony_ci/* xxx.css */ 64e41f4b71Sopenharmony_ci.container { 65e41f4b71Sopenharmony_ci flex-direction: row; 66e41f4b71Sopenharmony_ci justify-content: flex-start; 67e41f4b71Sopenharmony_ci align-items: flex-start; 68e41f4b71Sopenharmony_ci height: 1200px; 69e41f4b71Sopenharmony_ci width: 600px; 70e41f4b71Sopenharmony_ci} 71e41f4b71Sopenharmony_ci``` 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_citextpath与tspan组合示例与效果图 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci```html 79e41f4b71Sopenharmony_ci<!-- xxx.hml --> 80e41f4b71Sopenharmony_ci<div class="container"> 81e41f4b71Sopenharmony_ci <svg fill="#00FF00" x="50"> 82e41f4b71Sopenharmony_ci <path d="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" stroke="red" fill="none"></path> 83e41f4b71Sopenharmony_ci <text> 84e41f4b71Sopenharmony_ci <textpath fill="blue" startOffset="20%" path="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" font-size="15px"> 85e41f4b71Sopenharmony_ci <tspan dx="-50px" fill="red">This is tspan onTextPath.</tspan> 86e41f4b71Sopenharmony_ci <tspan font-size="25px">Let's play.</tspan> 87e41f4b71Sopenharmony_ci <tspan font-size="30px" fill="#00FF00">12345678912354567891234567891234567891234567891234567890</tspan> 88e41f4b71Sopenharmony_ci </textpath> 89e41f4b71Sopenharmony_ci </text> 90e41f4b71Sopenharmony_ci </svg> 91e41f4b71Sopenharmony_ci</div> 92e41f4b71Sopenharmony_ci``` 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci```html 97e41f4b71Sopenharmony_ci<!-- xxx.hml --> 98e41f4b71Sopenharmony_ci<div class="container"> 99e41f4b71Sopenharmony_ci <svg fill="#00FF00" x="50"> 100e41f4b71Sopenharmony_ci <path d="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" stroke="red" fill="none"></path> 101e41f4b71Sopenharmony_ci <text> 102e41f4b71Sopenharmony_ci <textpath fill="#D2691E" path="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" font-size="30px" stroke="black" stroke-width="1" > 103e41f4b71Sopenharmony_ci This is TextPath. 104e41f4b71Sopenharmony_ci <tspan font-size="20px" fill="red">This is tspan onTextPath.</tspan> 105e41f4b71Sopenharmony_ci <tspan font-size="30px">Let's play.</tspan> 106e41f4b71Sopenharmony_ci <tspan font-size="40px" fill="#00FF00" stroke="blue" stroke-width="2">12345678912354567891234567891234567891234567891234567890 107e41f4b71Sopenharmony_ci </tspan> 108e41f4b71Sopenharmony_ci </textpath> 109e41f4b71Sopenharmony_ci </text> 110e41f4b71Sopenharmony_ci </svg> 111e41f4b71Sopenharmony_ci</div> 112e41f4b71Sopenharmony_ci``` 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci```html 117e41f4b71Sopenharmony_ci<!-- xxx.hml --> 118e41f4b71Sopenharmony_ci<div class="container"> 119e41f4b71Sopenharmony_ci <svg fill="#00FF00" x="50"> 120e41f4b71Sopenharmony_ci <path d="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" stroke="red" fill="none"> 121e41f4b71Sopenharmony_ci </path> 122e41f4b71Sopenharmony_ci <!-- 数值百分比 --> 123e41f4b71Sopenharmony_ci <text> 124e41f4b71Sopenharmony_ci <textpath fill="#D2691E" path="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" font-size="30px"> 125e41f4b71Sopenharmony_ci This is TextPath. 126e41f4b71Sopenharmony_ci <tspan x="50" fill="blue">This is first tspan.</tspan> 127e41f4b71Sopenharmony_ci <tspan x="50%">This is second tspan.</tspan> 128e41f4b71Sopenharmony_ci <tspan dx="10%">12345678912354567891234567891234567891234567891234567890</tspan> 129e41f4b71Sopenharmony_ci </textpath> 130e41f4b71Sopenharmony_ci </text> 131e41f4b71Sopenharmony_ci </svg> 132e41f4b71Sopenharmony_ci</div> 133e41f4b71Sopenharmony_ci``` 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_cistartOffset属性动画,文本绘制时起点偏移从10%运动到40%,不绘制超出path长度范围的文本。 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci```css 140e41f4b71Sopenharmony_ci/* xxx.css */ 141e41f4b71Sopenharmony_ci.container { 142e41f4b71Sopenharmony_ci flex-direction: row; 143e41f4b71Sopenharmony_ci justify-content: flex-start; 144e41f4b71Sopenharmony_ci align-items: flex-start; 145e41f4b71Sopenharmony_ci height: 3000px; 146e41f4b71Sopenharmony_ci width: 1080px; 147e41f4b71Sopenharmony_ci} 148e41f4b71Sopenharmony_ci``` 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci```html 151e41f4b71Sopenharmony_ci<!-- xxx.hml --> 152e41f4b71Sopenharmony_ci<div class="container"> 153e41f4b71Sopenharmony_ci <svg fill="#00FF00"> 154e41f4b71Sopenharmony_ci <path d="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" stroke="red" fill="none"></path> 155e41f4b71Sopenharmony_ci <text> 156e41f4b71Sopenharmony_ci <textpath fill="blue" startOffset="10%" path="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" font-size="15px"> 157e41f4b71Sopenharmony_ci <tspan dx="-50px" fill="red">This is tspan onTextPath.</tspan> 158e41f4b71Sopenharmony_ci <tspan font-size="25px">Let's play.</tspan> 159e41f4b71Sopenharmony_ci <tspan font-size="30px" fill="#00FF00">12345678912354567891234567891234567891234567891234567890</tspan> 160e41f4b71Sopenharmony_ci <animate attributeName="startOffset" from="10%" to="40%" dur="3s" repeatCount="indefinite"></animate> 161e41f4b71Sopenharmony_ci </textpath> 162e41f4b71Sopenharmony_ci </text> 163e41f4b71Sopenharmony_ci </svg> 164e41f4b71Sopenharmony_ci</div> 165e41f4b71Sopenharmony_ci``` 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_citextpath与tspan组合属性动画与效果图 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci```html 172e41f4b71Sopenharmony_ci<!-- xxx.hml --> 173e41f4b71Sopenharmony_ci<div class="container"> 174e41f4b71Sopenharmony_ci <svg fill="#00FF00"> 175e41f4b71Sopenharmony_ci <path d="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" stroke="red" fill="none"> 176e41f4b71Sopenharmony_ci </path> 177e41f4b71Sopenharmony_ci <text> 178e41f4b71Sopenharmony_ci <textpath fill="#D2691E" path="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" font-size="30px"> 179e41f4b71Sopenharmony_ci This is TextPath. 180e41f4b71Sopenharmony_ci <tspan x="50" fill="blue"> 181e41f4b71Sopenharmony_ci tspan attribute x|rotate 182e41f4b71Sopenharmony_ci <animate attributeName="x" from="50" to="100" dur="5s" repeatCount="indefinite"></animate> 183e41f4b71Sopenharmony_ci <animate attributeName="rotate" from="0" to="360" dur="5s" repeatCount="indefinite"></animate> 184e41f4b71Sopenharmony_ci </tspan> 185e41f4b71Sopenharmony_ci <tspan x="30%">tspan static.</tspan> 186e41f4b71Sopenharmony_ci <tspan> 187e41f4b71Sopenharmony_ci tspan attribute dx|opacity 188e41f4b71Sopenharmony_ci <animate attributeName="dx" from="0%" to="30%" dur="3s" repeatCount="indefinite"></animate> 189e41f4b71Sopenharmony_ci <animate attributeName="opacity" from="0.01" to="0.99" dur="3s" repeatCount="indefinite"></animate> 190e41f4b71Sopenharmony_ci </tspan> 191e41f4b71Sopenharmony_ci <tspan dx="5%">tspan move</tspan> 192e41f4b71Sopenharmony_ci </textpath> 193e41f4b71Sopenharmony_ci </text> 194e41f4b71Sopenharmony_ci </svg> 195e41f4b71Sopenharmony_ci</div> 196e41f4b71Sopenharmony_ci``` 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci(1) "tspan attribute x|rotate" 文本绘制起点偏移从50px运动到100px,顺时针旋转0度到360度。 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci(2) "tspan attribute dx|opacity" 在 "tspan static." 绘制结束后再开始绘制,向后偏移量从0%运动到30%,透明度从浅到深变化。 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci(3) "tspan move" 在上一段tspan绘制完成后,向后偏移5%的距离进行绘制,呈现跟随前一段tspan运动的效果。 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_citextpath与tspan组合属性动画与效果图 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci```html 209e41f4b71Sopenharmony_ci<!-- xxx.hml --> 210e41f4b71Sopenharmony_ci<div class="container"> 211e41f4b71Sopenharmony_ci <svg fill="#00FF00"> 212e41f4b71Sopenharmony_ci <path d="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" stroke="red" 213e41f4b71Sopenharmony_ci fill="none"> 214e41f4b71Sopenharmony_ci </path> 215e41f4b71Sopenharmony_ci <text> 216e41f4b71Sopenharmony_ci <textpath fill="#D2691E" path="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" font-size="30px"> 217e41f4b71Sopenharmony_ci This is TextPath. 218e41f4b71Sopenharmony_ci <tspan dx="20" fill="blue"> 219e41f4b71Sopenharmony_ci tspan attribute fill|fill-opacity 220e41f4b71Sopenharmony_ci <animate attributeName="fill" from="blue" to="red" dur="3s" repeatCount="indefinite"></animate> 221e41f4b71Sopenharmony_ci <animate attributeName="fill-opacity" from="0.01" to="0.99" dur="3s" repeatCount="indefinite"></animate> 222e41f4b71Sopenharmony_ci </tspan> 223e41f4b71Sopenharmony_ci <tspan dx="20" fill="blue"> 224e41f4b71Sopenharmony_ci tspan attribute font-size 225e41f4b71Sopenharmony_ci <animate attributeName="font-size" from="10" to="50" dur="3s" repeatCount="indefinite"></animate> 226e41f4b71Sopenharmony_ci </tspan> 227e41f4b71Sopenharmony_ci </textpath> 228e41f4b71Sopenharmony_ci <tspan font-size="30">Single tspan</tspan> 229e41f4b71Sopenharmony_ci </text> 230e41f4b71Sopenharmony_ci </svg> 231e41f4b71Sopenharmony_ci</div> 232e41f4b71Sopenharmony_ci``` 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci(1) "This is TextPath." 在path上无偏移绘制第一段文本内容,大小30px,颜色"\#D2691E"。 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci(2) "tspan attribute fill|fill-opacity" 相对上一段文本结束后偏移20px,颜色从蓝到红,透明度从浅到深。 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci(3) "tspan attribute font-size" 绘制起点相对上一段结束后偏移20px,起点静止,字体大小从10px到50px,整体长度持续拉长。 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci(4) "Single tspan" 在上一段的尾部做水平绘制,呈现跟随上一段运动的效果。 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_citextpath与tspan组合属性动画与效果图 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci```html 247e41f4b71Sopenharmony_ci<!-- xxx.hml --> 248e41f4b71Sopenharmony_ci<div class="container"> 249e41f4b71Sopenharmony_ci <svg fill="#00FF00"> 250e41f4b71Sopenharmony_ci <path d="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" stroke="red" 251e41f4b71Sopenharmony_ci fill="none"> 252e41f4b71Sopenharmony_ci </path> 253e41f4b71Sopenharmony_ci <text> 254e41f4b71Sopenharmony_ci <textpath fill="#D2691E" path="M40,360 Q360,360 360,180 Q360,40 200,40 Q40,40 40,160 Q40,280 180,280 Q280,280 300,200" font-size="30px"> 255e41f4b71Sopenharmony_ci This is TextPath. 256e41f4b71Sopenharmony_ci <tspan dx="20" fill="blue"> 257e41f4b71Sopenharmony_ci tspan attribute stroke 258e41f4b71Sopenharmony_ci <animate attributeName="stroke" from="red" to="#00FF00" dur="3s" repeatCount="indefinite"></animate> 259e41f4b71Sopenharmony_ci </tspan> 260e41f4b71Sopenharmony_ci <tspan dx="20" fill="white" stroke="red"> 261e41f4b71Sopenharmony_ci tspan attribute stroke-width-opacity 262e41f4b71Sopenharmony_ci <animate attributeName="stroke-width" from="1" to="5" dur="3s" repeatCount="indefinite"></animate> 263e41f4b71Sopenharmony_ci <animate attributeName="stroke-opacity" from="0.01" to="0.99" dur="3s" repeatCount="indefinite"></animate> 264e41f4b71Sopenharmony_ci </tspan> 265e41f4b71Sopenharmony_ci </textpath> 266e41f4b71Sopenharmony_ci </text> 267e41f4b71Sopenharmony_ci </svg> 268e41f4b71Sopenharmony_ci</div> 269e41f4b71Sopenharmony_ci``` 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci(1) "tspan attribute stroke" 轮廓颜色从红色逐渐转变成绿色。 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci(2) "tspan attribute stroke-width-opacity" 轮廓宽度从细1px转变粗5px,透明度从浅到深。 276