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

相关内容

热门资讯

截至发稿!科乐第三方辅助工具(... 截至发稿!科乐第三方辅助工具(辅助挂)果然真的有挂(有挂窍要)-哔哩哔哩;无需打开直接搜索薇:136...
第十分钟开挂!789大菠萝可以... 第十分钟开挂!789大菠萝可以控制吗,途游辅助软件网站,力荐教程-2026最新版本1、进入游戏-大厅...
刚刚!兴动游戏辅助器(辅助挂)... 刚刚!兴动游戏辅助器(辅助挂)一直是有挂(有挂讲义)-哔哩哔哩您好:兴动游戏辅助器这款游戏可以开挂,...
八分钟开挂!家乡大二辅助工具,... 八分钟开挂!家乡大二辅助工具,四川游戏家园破解版,大神讲解-2026最新版本1、这是跨平台的四川游戏...
最终!山西扣点有没有辅助器(辅... 山西扣点有没有辅助器是一款专注玩家量身打造的游戏记牌类型软件,在山西扣点有没有辅助器这款游戏中我们可...
第3分钟开挂!佛手在线大菠萝可... 第3分钟开挂!佛手在线大菠萝可以辅助码,同城游辅助工具,微扑克教程-2026最新版本佛手在线大菠萝可...
随着!上品游戏辅助器(辅助挂)... 随着!上品游戏辅助器(辅助挂)果然确实有挂(有挂机巧)-哔哩哔哩;打开点击测试直接进入微信(1367...
第2分钟开挂!微信小程序外辅助... 第2分钟开挂!微信小程序外辅助工具,中至余干小程序辅助安装,解密教程-2026最新版本1、中至余干小...
针对!约战破解(辅助挂)果然是... 针对!约战破解(辅助挂)果然是有挂的(有挂法门)-哔哩哔哩1、下载安装好约战破解,进入游戏主界面,点...
第六分钟开挂!新九哥破解版,蜀... 第六分钟开挂!新九哥破解版,蜀山四川智能辅助插件下载,总结教程-2026最新版本1、蜀山四川智能辅助...