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元素的平移值。根据需要选择适合的生命周期钩子函数。

相关内容

热门资讯

据目击者称!新道游app辅助器... 据目击者称!新道游app辅助器!原来是有辅助修改器(真的有挂)-哔哩哔哩新道游app辅助器是不是有人...
现有关情况通报如下!开心网辅助... 现有关情况通报如下!开心网辅助工具!真是存在有辅助平台(有挂工具)-哔哩哔哩;暗藏猫腻,小编详细说明...
出乎意料的是!上饶辅助!好像是... 出乎意料的是!上饶辅助!好像是有辅助修改器(有挂方法)-哔哩哔哩1、上饶辅助破解器简单,上饶辅助机器...
据相关数据显示!广西友乐app... 据相关数据显示!广西友乐app辅助工具!确实存在有辅助器(有挂攻略)-哔哩哔哩1、游戏颠覆性的策略玩...
今天上午!新上游通用挂!原来真... 今天上午!新上游通用挂!原来真的有辅助平台(有挂总结)-哔哩哔哩;一、新上游通用挂游戏安装教程牌型概...
有了最新消息!大唐山西辅助软件... 有了最新消息!大唐山西辅助软件!其实存在有辅助工具(有挂详情)-哔哩哔哩1、大唐山西辅助软件免费脚本...
值得注意的是!微乐贵阳捉鸡麻将... 值得注意的是!微乐贵阳捉鸡麻将开挂!其实真的是有辅助神器(有挂技巧)-哔哩哔哩1、玩家可以在微乐贵阳...
据报道!广东雀神智能插件安装软... 据报道!广东雀神智能插件安装软件!总是是真的辅助工具(有挂猫腻)-哔哩哔哩1、游戏颠覆性的策略玩法,...
今天下午!小程序牵手跑得快辅助... 今天下午!小程序牵手跑得快辅助器!一直是有辅助下载(有挂规律)-哔哩哔哩1、小程序牵手跑得快辅助器公...
今天上午!钱塘十三水透视脚本!... 今天上午!钱塘十三水透视脚本!好像真的有辅助平台(有挂教学)-哔哩哔哩该软件可以轻松地帮助玩家将钱塘...