在Angular 7中绘制动画SVG
我正在尝试将以下变量移植到Angular 7: (示例工作中的TL;DR-SVG,我制作的SVG不会混淆) 目前我有以下组件:在Angular 7中绘制动画SVG,angular,svg,angular7,Angular,Svg,Angular7,我正在尝试将以下变量移植到Angular 7: (示例工作中的TL;DR-SVG,我制作的SVG不会混淆) 目前我有以下组件: import { Component, OnInit, HostListener, ElementRef, Renderer2, ViewChild } from '@angular/core'; @Component({ selector: 'svg-component', templateUrl: './svg.component.html', st
import { Component, OnInit, HostListener, ElementRef, Renderer2, ViewChild } from '@angular/core';
@Component({
selector: 'svg-component',
templateUrl: './svg.component.html',
styleUrls: ['./svg.component.css'],
})
export class SvgComponent implements OnInit {
@ViewChild('svgElement') svgElement;
constructor(private el: ElementRef) {}
@HostListener('window:scroll', ['$event'])
checkScroll() {
let componentPosition = this.el.nativeElement.offsetTop;
let scrollPosition = window.pageYOffset;
let svgLength = this.svgArrow.nativeElement.getTotalLength();
this.svgArrow.nativeElement.style.strokeDasharray = arrowLength;
this.svgArrow.nativeElement.style.strokeDashoffset = arrowLength;
let scrollPercent: number;
if (scrollPosition >= componentPosition) {
// This isn't actually a percentage - in the example they're using a variable between 0 and 1.
scrollPercent = (((scrollPosition - componentPosition) / svgLength));
let draw = svgLength * scrollPercent;
this.svgArrow.nativeElement.style.strokeDashoffset = svgLength - draw;
}
}
}
它的HTML与示例中的三角形一起工作:
<svg>
<path #svgArrow fill="none" stroke="red" stroke-width="3" id="triangle" d="M150 0 L75 200 L225 200 Z" />
Sorry, your browser does not support inline SVG.
</svg>
很抱歉,您的浏览器不支持内嵌SVG。
但是当我添加以这种方式生成的自定义箭头时,它不会显示任何内容:
改为使用以下HMTL:
<svg>
<svg #svgArrow stroke="black" stroke-width="3" id="arrow" d="M 400 100 C 350 150 450 125 400 175 C 350 225 450 225 400 300 C 350 350 450 350 400 475 Q 390 450 375 450 Q 380 490 400 525 Q 420 490 425 450" />
Browser doesn't support inline SVG?
</svg>
浏览器不支持内嵌SVG?
因此,示例中的SVG起作用了,我生成的SVG根本没有显示出来。是因为它不是一个封闭的图形吗?感谢您的帮助:)天哪,我真是太蠢了。容器没有宽度和高度,因此正在显示,但窗口为0 x 0,因此无法看到。只要为SVG指定了尺寸,以上所有内容都可以工作:
<svg height="600px" width="600px">
<path #svgArrow fill="none" stroke="red" stroke-width="3" id="triangle" d="M150 0 L75 200 L225 200 Z" />
Sorry, your browser does not support inline SVG.
</svg>
很抱歉,您的浏览器不支持内嵌SVG。