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

相关内容

热门资讯

透视脚本!哈糖大菠萝开挂,we... 透视脚本!哈糖大菠萝开挂,wepoker私人局规律(透视)固有有挂(微扑克教程)1、下载好wepok...
透视代打!哈糖大菠萝有没有挂,... 透视代打!哈糖大菠萝有没有挂,pokemmo手机版脚本免费,竟然存在有挂(辅助教程)1、用户打开应用...
透视新版!wepoker透视底... 透视新版!wepoker透视底牌,aapoker辅助怎么用(透视)原来存在有挂(可靠教程)aapok...
透视脚本!pokerworld... 透视脚本!pokerworld辅助器,佛手在线大菠萝智能辅助器,果然存在有挂(教你教程);暗藏猫腻,...
透视肯定!wepoker透视挂... 透视肯定!wepoker透视挂底牌,德州hhpoker是真的吗(透视)原来是真的有挂(2025新版总...
透视ai!智星德州插件最新版本... 透视ai!智星德州插件最新版本更新内容详解,steampokermaster辅助,一直有挂(规律教程...
透视免费!wejoker开挂,... 透视免费!wejoker开挂,竞技联盟辅助插件(透视)原来真的是有挂(透明教程)1、任何竞技联盟辅助...
透视黑科技!智星德州辅助译码插... 透视黑科技!智星德州辅助译码插件靠谱吗,cloudpoker作弊,一贯有挂(存在挂教程)1、这是跨平...
透视代打!hhpoker必备开... 透视代打!hhpoker必备开挂,wepoker免费脚本咨询(透视)从前是有挂(介绍教程)1、wep...
透视脚本!wepoker安装教... 透视脚本!wepoker安装教程,约局吧怎么看有没有挂,好像真的有挂(教你攻略);1、很好的工具软件...