React-svg中的html标签 [英] React - html tags inside svg

查看:99
本文介绍了React-svg中的html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作圆形菜单,因此我使用SVG创建了一个圆形,现在我想在圆形的一部分内显示带有某些图像的链接.我该怎么办?我的代码-

I am making circle menu, so I use SVG to create a circle, and now I want to show a link with some image inside of part of the circle. How i can do it? My code -

render(){
    return(
       <svg id={"menuLevel" + index} width={200} height={200}>
          <path fill="white" stroke="rgba(0,0,0,0.2)" strokeWidth="2" d={"M"+width+","+width+" L"+previousX+", "+previousY+" A"+width+","+width+" 0 0,0 "+x+", "+y+" z"}></path>
      </svg>
    )
}

我尝试过这样的事情-

<path fill="white" stroke="rgba(0,0,0,0.2)" strokeWidth="2" d={"M"+width+","+width+" L"+previousX+", "+previousY+" A"+width+","+width+" 0 0,0 "+x+", "+y+" z"}>
     <foreignobject x="120" y="120" width="180" height="180">
         <Link ...><Image .../></Link>
     </foreignobject>
</path>

但是它不起作用,该异物仍然具有0宽度和0高度,并且内容不显示.

But it doesn't work, this foreign object have still 0 width and 0 height and content doesn't show.

更新

我需要将链接组件分配给所有路径对象

I need to assign link component to all path objects

<svg id={"menuLevel" + index} width={width*2+2} height={width*2+2}>
                    {arr.map(function(item){
                        let angleInRadians = -item * Math.PI / 180.0;
                        let previousX =  x;
                        let previousY = y;
                        x = width + width * Math.cos(angleInRadians);
                        y = width + width * Math.sin(angleInRadians);
                        return(
                                <path fill="white" stroke="rgba(0,0,0,0.2)" strokeWidth="2" d={"M"+width+","+width+" L"+previousX+", "+previousY+" A"+width+","+width+" 0 0,0 "+x+", "+y+" z"}>
                                </path>
                        )
                    })}
                </svg> 

推荐答案

请在此处 JSFiddle 进行检查.使用image元素将图像添加到SVG: https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/SVG_Image_Tag

Please check it here JSFiddle. Use image element to add the image to SVG: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/SVG_Image_Tag

<svg width="5cm" height="4cm" version="1.1"
     xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">
     <circle x="0" y="0" r="200"></circle>
    <image xlink:href="https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" x="0" y="0" height="200px" width="200px"/>
</svg>

请注意:

如果您未设置x或y属性,它们将被设置为0.

If you do not set the x or y attributes, they will be set to 0.

如果您未设置高度或宽度属性,则将它们设置为0.

If you do not set the height or width attributes, they will be set to 0.

将height或width属性设置为0将禁用图像渲染.

Having a height or width attribute of 0 will disable rendering of the image.


更新1

这是一个将React组件与图像一起添加的有效示例: JSFiddle .但是我将Link组件作为SVG的同级组件,然后使用absolute对其进行定位.不是一个完美的解决方案.


Update 1

Here is a working example to add a React component together with the image: JSFiddle. But I make the Link component as a sibling of the SVG, and then using absolute to position them. Not a perfect solution.

要使路径可点击: JSFiddle .

这是具有可点击路径的图像,与ReactJS集成在一起: JSFiddle :

This is an image with clickable paths, integrated with ReactJS: JSFiddle:

var Link = React.createClass({
  render: function() {
    return <a className="link" href={this.props.href} target="_blank">{this.props.children}</a>
  }
});

var Hello = React.createClass({ 
  render: function() {
    return <div id="container"><svg xmlns="http://www.w3.org/2000/svg" width="300px" height="300px">
     <Link href="http://www.google.com">
      <g transform="translate(100, 100)"><image href="https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" x="0" y="0" height="200px" width="200px"/></g>
     </Link>
     <Link href="http://www.facebook.com">
     <g><image href="https://www.facebook.com/images/fb_icon_325x325.png" x="0" y="0" height="100px" width="100px"/></g>
     </Link>
</svg></div>
  }
});

这篇关于React-svg中的html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆