如何使用 Snap.svg 和 Angular 5 在悬停时缩放不同的 svg 图层



我是SVG的超级菜鸟,所以请耐心等待(并随时纠正我做错的地方)。我的应用程序是在 Angular 5 中构建的。我正在玩一个复杂的SVG,它有大约23个层。这是因为它是一个图表,其中包含代表各种输入的框,并且每个框都必须按hoverclick缩放,以向用户指示他/她正在选择相应的框。

在尝试使用简单的 CSS 转换(缩放、翻译)实现缩放后,我意识到 svg 矩阵和缩放需要我这边更多的研究和理解,因此决定尝试Snap.svg,我读到它非常擅长简化 svg 的工作。

所以,现在我的 Angular 5 组件看起来像这样:

import { Component, OnInit } from '@angular/core';
declare const Snap: any;

@Component({
...
export class SvgComponent implements OnInit {
constructor(){
}
ngOnInit(){
}
onHover(event) {
const layer = Snap(event.target.id);
layer.hover(function() {
this.animate({ transform: 's1.5,1.5' }, 500);
}, function() {
this.animate({ transform: 's1,1' }, 500);
});
}

HTML 看起来像这样(其中一个层):

<svg:g style="display:inline" class="svg-layer" id="layer1" (hover)="onHover($event)">
<svg:g id="g4746" style="display:inline" transform="matrix(0.26458333,0,0,0.26458333,-13.055467,-56.894235)">
<svg:path
id="path3892"
d="m 396.86667,292.53333 c -4.59211,0 -9.16503,0.1345 -13.7,0.36667 -4.53498,0.23217 -9.06106,0.57462 -13.53334,1.03333 -4.03511,0.41387 -8.02024,0.93941 -12,1.53334 -6.98255,23.42835 -10.71331,47.22459 -12.26666,71.16666 3.73406,-1.00536 7.41654,-2.14453 11.23333,-2.93333 6.49856,-1.34304 13.12148,-2.34492 19.83333,-3.03333 6.71185,-0.68842 13.54166,-1.06667 20.43334,-1.06667 6.89168,0 13.68815,0.37825 20.4,1.06667 6.71185,0.68841 13.33477,1.69029 19.83333,3.03333 3.82739,0.791 7.52247,1.92458 11.26667,2.93333 -1.55336,-23.94207 -5.28412,-47.73831 -12.26667,-71.16666 -3.99047,-0.59604 -7.98719,-1.11834 -12.03333,-1.53334 -4.47228,-0.45871 -8.99836,-0.80116 -13.53334,-1.03333 -4.53497,-0.23217 -9.07456,-0.36667 -13.66666,-0.36667 z"
style="color:#000000;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:medium;line-height:normal;font-family:'Bitstream Vera Sans';-inkscape-font-specification:'Bitstream Vera Sans';text-indent:0;text-align:start;text-decoration:none;text-decoration-line:none;letter-spacing:normal;word-spacing:normal;text-transform:none;writing-mode:lr-tb;direction:ltr;text-anchor:start;display:inline;overflow:visible;visibility:visible;fill:#ffffb1;fill-opacity:1;stroke:#000000;stroke-width:2.13333344;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker:none;enable-background:accumulate" />
<svg:text
id="text3897"
y="333.54636"
x="396.53268"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:12.80000019px;line-height:0%;font-family:Tahoma;-inkscape-font-specification:Tahoma;text-align:center;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1.06666672"
xml:space="preserve"><tspan
id="tspan3311"
x="396.53268"
y="333.54636"
style="font-size:14.9333334px;line-height:1.25;stroke-width:1.06666672">sereniteit</tspan></svg:text>
</svg:g>

所以我得出以下结论:

  1. 实现悬停功能OnInit(我首先这样做)不会 动画但第一层(所以layer1)。这有点道理, 因为它只发射一次。
  2. (hover) 或 (mouseover) 或任何其他事件绑定似乎没有 放置在svg标签内时工作。作为参考,我阅读和 实现了本文中的一些想法: https://teropa.info/blog/2016/12/12/graphics-in-angular-2.html#namespacing-svg-elements 这很好,但那里清楚地提到了那个事件 绑定在 Angular 2 中工作。也许有些事情发生了变化 同时和现在事件绑定不再工作。

任何想法都非常受欢迎,因为我现在很困。

后来编辑:所以 Angular 2+ 确实支持事件绑定(只要你给它正确的事件名称,显然 - "悬停"不存在)。悬停的相应事件名称是 (mouseenter) 和 (mouseleave)。如果将它们附加到 svg 标签上,它们就可以工作。

你应该能够只使用CSS做你想做的事。

以下内容适用于现代浏览器版本。 如果您需要支持较旧的浏览器,那么您可能需要做更多的工作。

.svg-layer {
transform-box: fill-box;
transform-origin: 50% 50%;
transition: 0.2s transform;
transform: scale(1,1);
}
.svg-layer:hover {
transform: scale(1.5,1.5);
}
<svg width="300" height="300" viewBox="60 10 60 60">
<g style="display:inline" class="svg-layer" id="layer1">
<g id="g4746" style="display:inline" transform="matrix(0.26458333,0,0,0.26458333,-13.055467,-56.894235)">
<path
id="path3892"
d="m 396.86667,292.53333 c -4.59211,0 -9.16503,0.1345 -13.7,0.36667 -4.53498,0.23217 -9.06106,0.57462 -13.53334,1.03333 -4.03511,0.41387 -8.02024,0.93941 -12,1.53334 -6.98255,23.42835 -10.71331,47.22459 -12.26666,71.16666 3.73406,-1.00536 7.41654,-2.14453 11.23333,-2.93333 6.49856,-1.34304 13.12148,-2.34492 19.83333,-3.03333 6.71185,-0.68842 13.54166,-1.06667 20.43334,-1.06667 6.89168,0 13.68815,0.37825 20.4,1.06667 6.71185,0.68841 13.33477,1.69029 19.83333,3.03333 3.82739,0.791 7.52247,1.92458 11.26667,2.93333 -1.55336,-23.94207 -5.28412,-47.73831 -12.26667,-71.16666 -3.99047,-0.59604 -7.98719,-1.11834 -12.03333,-1.53334 -4.47228,-0.45871 -8.99836,-0.80116 -13.53334,-1.03333 -4.53497,-0.23217 -9.07456,-0.36667 -13.66666,-0.36667 z"
style="color:#000000;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:medium;line-height:normal;font-family:'Bitstream Vera Sans';-inkscape-font-specification:'Bitstream Vera Sans';text-indent:0;text-align:start;text-decoration:none;text-decoration-line:none;letter-spacing:normal;word-spacing:normal;text-transform:none;writing-mode:lr-tb;direction:ltr;text-anchor:start;display:inline;overflow:visible;visibility:visible;fill:#ffffb1;fill-opacity:1;stroke:#000000;stroke-width:2.13333344;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker:none;enable-background:accumulate" />
<text
id="text3897"
y="333.54636"
x="396.53268"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:12.80000019px;line-height:0%;font-family:Tahoma;-inkscape-font-specification:Tahoma;text-align:center;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1.06666672"
xml:space="preserve"><tspan
id="tspan3311"
x="396.53268"
y="333.54636"
style="font-size:14.9333334px;line-height:1.25;stroke-width:1.06666672">sereniteit</tspan></text>
</g>
</g>
</svg>

最新更新