简体   繁体   English

如何更新SVG文本并保持对齐?

[英]How to update SVG text and maintain alignment?

In my application I have some SVGs that are preloaded and I clone them and update the text elements with different values. 在我的应用程序中,我预先加载了一些SVG,并克隆了它们,并使用不同的值更新了文本元素。 The original SVGs render correctly and the text is left aligned. 原始SVG可以正确渲染,并且文本保持左对齐。 After the update the new text is wrongly placed. 更新后,新文本将错误放置。

path.text = randomString.substr(0, randomInt) + randomString.substr(0,randomInt);

Example fiddle: https://jsfiddle.net/1t4qkzpc/1/ 小提琴示例: https : //jsfiddle.net/1t4qkzpc/1/

How could I update these SVGs without messing up the text? 如何在不弄乱文本的情况下更新这些SVG?

Regards, Alex 问候,亚历克斯

The left property of text is picked up to mantain position to that particular text. 拾取文本的left属性以保持该特定文本的位置。 Given the fact that text is by default left aligned, remove the current half width from the left, and after recalculating the dimensions with the new text, add half width back. 考虑到默认情况下文本左对齐的事实,请从左侧删除当前的半角宽度,并在使用新文本重新计算尺寸后,向后添加半角宽度。

This can help in this situation. 这可以在这种情况下提供帮助。 I do not think it will work with rotated text 我认为它不适用于旋转文字

 var svgEl = document.body.getElementsByTagName('svg')[0]; var serializer = new XMLSerializer(); var svgStr = serializer.serializeToString(svgEl); var canvas = new fabric.Canvas('c'); canvas.backgroundColor = 'rgb(150,150,150)'; fabric.Object.prototype.objectCaching = false; var path = fabric.loadSVGFromString(svgStr,function(objects, options) { var obj = fabric.util.groupSVGElements(objects, options); obj.scaleToHeight(200) .set({ left: 20, top: 20 }) .setCoords(); canvas.add(obj); }); var path = fabric.loadSVGFromString(svgStr,function(objects, options) { var obj = fabric.util.groupSVGElements(objects, options); obj.scaleToHeight(200) .set({ left: 20, top: 250 }) .setCoords(); obj.paths.forEach(function(path) { var randomString = Math.random().toString(36).slice(2); var randomInt = Math.floor(Math.random()*(40+1-1)) + 1; path.text = randomString.substr(0, randomInt) + randomString.substr(0,randomInt); if (path.type === 'text') { path.left -= path.width / 2 path._initDimensions() path.left += path.width / 2 } }) canvas.add(obj); }); /*var url = "https://www.w3.org/TR/SVG2/images/text/textdecoration01.svg"; fabric.loadSVGFromURL(url, function (objects, options) { var object = fabric.util.groupSVGElements(objects, options); object.set({left: 20, top: 20}); canvas.add(object).renderAll(); }); fabric.loadSVGFromURL(url, function (objects, options) { var object = fabric.util.groupSVGElements(objects, options); object.paths.forEach(function (path) { path.text = "MUIE CU LAMAIE"; }); object.set({left: 20, top: 220}); canvas.add(object).renderAll(); }); */ 
 svg{ display:none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.16/fabric.min.js"></script> <canvas id="c" width="800px" height="500px"></canvas> <svg version="1.1" id="CE" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="200" height="100" viewBox="0 0 200 100" style="enable-background:new 0 0 200 100;" xml:space="preserve"> <style type="text/css"> .st0{fill:#20AEE6;} .st1{fill:#FFFFFF;} .st2{font-family: Roboto, Helvetica, Arial, sans-serif;} .st3{font-size:12px;} .st4{fill:#E2E2E2;} .st5{fill:#E3E7E8;} </style> <rect class="st0" width="200" height="100"/> <text x="5" y="61" class="st1 st2 st3">LOOOOOOOOOOOOOOOOOONG</text> <text x="5" y="76" class="st1 st2 st3">MEEEEDIUUUUUUM</text> <text x="5" y="91" class="st1 st2 st3">SHORT</text> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM