Angular页面渲染或生命周期问题:当重新进入页面时,SVGGraphicsElement.getScreenCTM返回错误的平移值。
创始人
2024-10-30 11:30:33
0

问题描述: 当重新进入Angular页面时,使用SVGGraphicsElement.getScreenCTM方法获取SVG元素的平移值时,返回错误的值。

解决方法:

  1. 在Angular组件中,使用ngAfterViewInit生命周期钩子来处理SVG元素的渲染和获取平移值的逻辑。
import { Component, AfterViewInit, ViewChild, ElementRef } from '@angular/core';

@Component({
  selector: 'app-svg-component',
  template: `
    
      
    
  `,
})
export class SvgComponent implements AfterViewInit {
  @ViewChild('svgElement') svgElementRef: ElementRef;

  ngAfterViewInit(): void {
    this.getSVGElementTranslation();
  }

  getSVGElementTranslation(): void {
    const svgElement = this.svgElementRef.nativeElement;
    const gElement = svgElement.querySelector('g'); // Assuming the SVG element is inside a  element

    if (gElement) {
      const ctm = gElement.getScreenCTM();
      const translationX = ctm.e;
      const translationY = ctm.f;

      console.log('Translation X:', translationX);
      console.log('Translation Y:', translationY);
    }
  }
}
  1. 使用ngAfterViewChecked生命周期钩子来处理SVG元素的渲染和获取平移值的逻辑。
import { Component, AfterViewChecked, ViewChild, ElementRef } from '@angular/core';

@Component({
  selector: 'app-svg-component',
  template: `
    
      
    
  `,
})
export class SvgComponent implements AfterViewChecked {
  @ViewChild('svgElement') svgElementRef: ElementRef;

  ngAfterViewChecked(): void {
    this.getSVGElementTranslation();
  }

  getSVGElementTranslation(): void {
    const svgElement = this.svgElementRef.nativeElement;
    const gElement = svgElement.querySelector('g'); // Assuming the SVG element is inside a  element

    if (gElement) {
      const ctm = gElement.getScreenCTM();
      const translationX = ctm.e;
      const translationY = ctm.f;

      console.log('Translation X:', translationX);
      console.log('Translation Y:', translationY);
    }
  }
}

这两种方法都会在Angular组件初始化完成后,或在Angular渲染周期内检查并获取SVG元素的平移值。根据需要选择适合的生命周期钩子函数。

相关内容

热门资讯

6分钟实锤!智星德州菠萝(智星... 6分钟实锤!智星德州菠萝(智星德州)都是是有挂(详细辅助玩家教你)1、下载好智星德州菠萝辅助软件之后...
第9分钟实锤!德州之星辅助(n... 第9分钟实锤!德州之星辅助(nzt德州)一贯真的是有挂(详细辅助靠谱教程);1、德州之星辅助系统规律...
第9分钟实锤!德扑之星辅助器购... 第9分钟实锤!德扑之星辅助器购买(德州app)本来真的是有挂(详细辅助2025新版总结)1、许多玩家...
4分钟实锤!德州之星外挂(线上... 4分钟实锤!德州之星外挂(线上德州)其实是有挂(详细辅助必胜教程);1、构建自己的德州之星外挂辅助插...
第5分钟实锤!智星德州菠萝辅助... 第5分钟实锤!智星德州菠萝辅助器(德州扑克)切实真的是有挂(详细辅助切实教程)1、第5分钟实锤!智星...
7分钟实锤!智星德州菠萝有挂吗... 7分钟实锤!智星德州菠萝有挂吗(手机德州)切实有挂(详细辅助透明挂教程)1、打开软件启动之后找到中间...
九分钟实锤!德扑数据软件(云扑... 九分钟实锤!德扑数据软件(云扑克德州)其实真的有挂(详细辅助透明挂教程)1、德扑数据软件系统规律教程...
第三分钟实锤!德州之星插件(线... 第三分钟实锤!德州之星插件(线上wpk德州)一贯存在有挂(详细辅助攻略方法);该软件可以轻松地帮助玩...
第十分钟实锤!德州ai辅助有用... 第十分钟实锤!德州ai辅助有用(德州俱乐部)总是有挂(详细辅助玩家教程)德州ai辅助有用辅助器中分为...
七分钟实锤!智星德州菠萝外挂(... 七分钟实锤!智星德州菠萝外挂(德州俱乐部)果然存在有挂(详细辅助教你攻略);1、智星德州菠萝外挂ai...