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

相关内容

热门资讯

透视教学!德普之星有透视辅助吗... 透视教学!德普之星有透视辅助吗,德普之星有辅助软件吗,德州论坛(有挂揭秘);1.德普之星有辅助软件吗...
透视黑科技“德州圈脚本”透视辅... 透视黑科技“德州圈脚本”透视辅助挂(好像真的是有挂)1、点击下载安装,德州圈脚本插件透视分类一目了然...
透视科技!德普之星透视辅助软件... 透视科技!德普之星透视辅助软件激活码,德普之星透视辅助,玩家教你(有挂揭秘)1、完成德普之星透视辅助...
透视app!pokemmo手机... 透视app!pokemmo手机辅助软件,pokemmo脚本辅助器下载,2025版教程(有挂教程)1、...
透视免费!德扑圈透视挂,德普之... 透视免费!德扑圈透视挂,德普之星辅助工具如何打开,新版2025教程(有挂解说);德普之星辅助工具如何...
透视ai“拱趴大菠萝万能辅助器... 透视ai“拱趴大菠萝万能辅助器”透视辅助机制(总是是有挂)1、完成拱趴大菠萝万能辅助器透视辅助安装,...
透视系统!德普之星透视,(德扑... 透视系统!德普之星透视,(德扑之心)切实真的有挂,系统教程(有挂揭秘)1、德普之星透视系统规律教程、...
透视教学“拱趴大菠萝自动计算机... 透视教学“拱趴大菠萝自动计算机器人”透视辅助下载(总是真的是有挂)1、让任何用户在无需拱趴大菠萝自动...
透视脚本“约局吧app有挂吗”... 透视脚本“约局吧app有挂吗”透视辅助器(本来是真的有挂);1、玩家可以在约局吧app有挂吗软件透明...
透视计算!德普辅助器辅助器怎么... 透视计算!德普辅助器辅助器怎么用,(德扑之心)切实存在有挂,德州教程(有挂教程)1、任何德普辅助器辅...