html - 如何剪辑 SVG 图?

标签 html css svg svg.js

我的 SVG 元素是这样设置的:

svg.attr("viewBox", "0 0 " + chartView.displayWidth + " " + chartView.displayHeight);

我在里面画了一些图。有没有办法创建一个“透 window 口”,我可以在其中看到我的绘图?

我尝试使用:

svg.append("clipPath").append("rect")
                    .attr("x", 10)
                     .attr("y", 10)
                    .attr("width", 50)
                   .attr("height", 100);

但是没用

最佳答案

如评论中所述,您将使用 ClipPath。 您已经尝试使用 append 创建一个。据我所知,这是 D3.jsjQuery 语法。但是你标记了 svg.js。 在 svg.js 中你会这样做:

svg.viewbox(0, 0, chartView.displayWidth, chartView.displayHeight);

var plot = svg.path('your plots path data').stroke('#000')

var clipper = svg.rect(50, 100).move(10, 10)

// to clip the whole svg use svg instead of plot
plot.clipWith(clipper)

如果您使用 D3.js,您就快成功了。 您已经创建了剪辑路径。你现在必须给它一个 id,并在你的情节中引用这个 id。

var clip = svg.append("clipPath").attr("id", "clipper");
clip.append("rect").attr("x", 10)
                   .attr("y", 10)
                   .attr("width", 50)
                   .attr("height", 100);

yourplot.attr("clip-path", "url(#clipper)")

如果你想用你的矩形剪辑整个 svg,使用 svg.attr(...) 而不是 yourplot.attr(...)

确保阅读 svg 规范或一些关于 clipPaths 的教程

关于html - 如何剪辑 SVG 图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37111495/

相关文章:

html - A 链接内的 Div 框,希望最小化 A 链接光标框区域

css - 在没有换行符的情况下将 div 环绕在文本周围

html - Bootstrap 列居中

javascript - jQuery 使用向上/向下箭头对卡片进行排序

html - 悬停另一个div时div不显示

css - 由于使用样式,svg 中的标记图像在 FireFox 中不可见。怎么修?

javascript - 更改 SVG 的文本内容

c# - 我可以使用 SkiaSharp 将 SVG 渲染为 PNG 吗?

javascript - 使用 jquery 或 js 根据浏览器宽度动态更改 css 属性

PHP - 查询和循环以显示不同的数据