我正在尝试将视频嵌入到 svg 中(该 svg 只能在网络上查看)。为此,我使用了 foreignObject 标签:
<svg version="1.1" class="center-block" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" width="800" height="600"
style="border: 1px solid black;">
<g>
<g transform="translate(151,104) scale(1,1)">
<rect x="0" y="0" width="300" height="200"></rect>
<foreignObject x="0" y="0" width="300" height="200">
<video width="300" height="200" controls="" style="position: fixed; left: 151px; top: 104px;">
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
</video>
</foreignObject>
</g>
</g>
</svg>Run Code Online (Sandbox Code Playgroud)
它在显示视频的意义上“有效”,但它相对于其父级有几个像素<g>。我尝试了几种组合:带样式的视频、不带样式、带命名空间的视频标签等。这在 Firefox 中效果更好,但在 Chrome(Mac 和 Linux)中完全中断。我不想在 svg 之外添加 html 标签,因为这会更麻烦(svg 是用 React 动态创建的)。
有没有人能够得到类似的工作?
R. *_*ini 10
你去吧:
Translate 将原点从左上角移动到指定的坐标。如果您在 0,0 处嵌入一个对象,它将被放置在新的原点。在这种情况下,您必须将其嵌入到 -translation 坐标中。
即便如此,我还是不得不增加宽度和高度。为什么?我不知道。它似乎不是 2 的比例。如果有人知道,我很想知道。
<svg version="1.1" class="center-block" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="800" height="600" style="border: 1px solid black;">
<g>
<g transform="translate(151,104) scale(1,1)">
<rect x="0" y="0" width="300" height="200"></rect>
<foreignObject x="-151" y="-104" width="500" height="400">
<video width="300" height="200" controls="" style="position: fixed; left: 151px; top: 104px;">
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
</video>
</foreignObject>
</g>
</g>
</svg>Run Code Online (Sandbox Code Playgroud)