【发布时间】:2020-02-08 08:35:22
【问题描述】:
Angular 材质文档应用程序有一个 SVG 查看器可以执行以下操作:
inlineSvgContent(template) {
this.elementRef.nativeElement.innerHTML = template;
if (this.scaleToContainer) {
let svg = this.elementRef.nativeElement.querySelector('svg');
svg.setAttribute('width', '100%');
svg.setAttribute('height', '100%');
svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
}
}
IIUC 使 SVG 缩放到容器所需要做的就是使用正确的 viewBox 参数 as explained here 保存它。
这是来自答案的演示圈:
<svg viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" fill="gold"/>
<rect x="10" y="50" width="20" height="50" fill="gold"/>
<rect x="70" y="50" width="20" height="50" fill="gold"/>
<circle cx="35" cy="45" r="5"/>
<circle cx="65" cy="45" r="5"/>
</svg>
该圆圈将适合任何容器,无需设置width=100% 和height=100%。
所以我假设 Angular 团队这样做的原因是因为想将 width 和 height 设置为另一个用例的某个固定值?
一般来说,我只是对设计以及我们是否从中受益感到好奇。换句话说,这是 UX 开发人员的常见模式吗?为什么?
【问题讨论】:
-
这将是为了响应。见这篇文章:webdesign.tutsplus.com/tutorials/…
-
即:
We’ll start by looking at “zooming”, which we can do with the last two viewBox parameters: width and height respectively. We’ll leave the first two parameters at 0 0 for now. If those last two parameters have the same dimensions as the viewport, there’s no zooming in or out so nothing changes.
标签: javascript html css angular svg