Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Text SVG:rect中的文本_Text_Svg_Rect - Fatal编程技术网

Text SVG:rect中的文本

Text SVG:rect中的文本,text,svg,rect,Text,Svg,Rect,我想在SVGrect中显示一些文本。可能吗 我试过了 <svg xmlns="http://www.w3.org/2000/svg"> <g> <rect x="0" y="0" width="100" height="100" fill="red"> <text x="0" y="10" font-family="Verdana" font-size="55" fill="blue"> Hello </text>

我想在SVG
rect
中显示一些文本。可能吗

我试过了

<svg xmlns="http://www.w3.org/2000/svg">
  <g>
    <rect x="0" y="0" width="100" height="100" fill="red">
      <text x="0" y="10" font-family="Verdana" font-size="55" fill="blue"> Hello </text>
    </rect>
  </g>
</svg>

你好

但它不起作用。

这是不可能的。如果您想在一个rect元素中显示文本,您应该将它们放在一个组中,文本元素位于rect元素之后(因此它显示在顶部)


你好
以编程方式使用:


我的代码,你的成就。。。。。。。

您可以使用foreignobject进行更多控制,并将丰富的HTML内容放置在rect或circle上


这里有一个长文本,它不止一行,可以作为一个段落

这是第一行
这是大胆的
这是斜体的
使用基本Javascript在rect上以编程方式显示文本

var svg=document.getelementsbytagnames('http://www.w3.org/2000/svg','svg')[0];
var text=document.createElements('http://www.w3.org/2000/svg","文本",;
text.setAttribute('x',20);
text.setAttribute('y',50);
text.setAttribute('width',500);
text.style.fill='red';
text.style.fontFamily='Verdana';
text.style.fontSize='35';
text.innerHTML=“一些文本行”;
appendChild(文本);
var text2=document.createElements('http://www.w3.org/2000/svg","文本",;
text2.setAttribute('x',20);
text2.setAttribute('y',100);
text2.setAttribute('width',500);
text2.style.fill='绿色';
text2.style.fontFamily='Calibri';
text2.style.fontSize='35';
text2.style.fontStyle='italic';
text2.innerHTML=“一些斜体线”;
appendChild(text2);
var text3=document.createElements('http://www.w3.org/2000/svg","文本",;
text3.setAttribute('x',20);
text3.setAttribute('y',150);
text3.setAttribute('width',500);
text3.style.fill='绿色';
text3.style.fontFamily='Calibri';
text3.style.fontSize='35';
text3.style.fontwweight=700;
text3.innerHTML=“一些粗体线条”;
svg.appendChild(text3)


这是否回答了您的问题?有没有办法不必手动设置矩形的高度和宽度?取决于具体情况以及“手动”的含义。如果您愿意,可以使用我的html知识用JavaScript编写脚本(见下面的narendra的答案)——这可能不适用于这里——这里的
g
元素似乎有一个隐式大小,我希望矩形扩展到它的大小。组适合它的内容,而不是其他方式。我认为这些元素仍然是相对于父svg的。group元素在这里重要吗?这会生成像OP想要的那样显示的标记,但它不会执行OP试图执行的操作(这是不合法的)。这仍然会产生
.Javascript!=SVG。问题用svg、文本和rect标记。没有任何内容表明用户可以访问编程语言。(我来这里是为了寻找一个静态的解决方案,所以说这句话。)虽然这是真的,但这与我的问题无关,显然还有很多其他人来这里是为了D3,有没有可能将rect自动调整到矩形的宽度text@ColinD那也是我要找的。但似乎不可能期望它自动完成。相反,我们必须自己手动实现这一目标。它需要测量两个元素(rect和text)的尺寸(宽度和/或高度)。与
text
-tags only选项不同,此选项实际上将文本放置在路径中,而不是将其隐藏在路径上方的某个不可见空间中!
x
y
属性对我来说是不必要的,但是
宽度和
高度是不必要的,或者它也不存在!这是一个奇妙的答案。唯一的缺点是IE11不支持foreignObject;但对该浏览器的支持正在消失,因此这可能不是问题。这比使用一堆tspan要简单得多。
body = d3.select('body')
svg = body.append('svg').attr('height', 600).attr('width', 200)
rect = svg.append('rect').transition().duration(500).attr('width', 150)
                .attr('height', 100)
                .attr('x', 40)
                .attr('y', 100)
                .style('fill', 'white')
                .attr('stroke', 'black')
text = svg.append('text').text('This is some information about whatever')
                .attr('x', 50)
                .attr('y', 150)
                .attr('fill', 'black')
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    <g>
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(145,200,103);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(132,168,86);stop-opacity:1" />
    </linearGradient>
  </defs>
  <rect width="220" height="30" class="GradientBorder" fill="url(#grad1)" />
  <text x="60" y="20" font-family="Calibri" font-size="20" fill="white" >My Code , Your Achivement....... </text>
  </g>
</svg>