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

相关内容

热门资讯

教程辅助开挂!大大娱乐开挂多功... 教程辅助开挂!大大娱乐开挂多功能AI方法,大大娱乐其实是真的有开挂方法亲,关键说明,大大娱乐透视脚本...
玩家必备教程开挂!葫芦娃大厅开... 玩家必备教程开挂!葫芦娃大厅开挂多功能AIapp,葫芦娃大厅真是真的有开挂app1、完成葫芦娃大厅有...
推荐十款辅助!豪发娱乐辅助多功... 推荐十款辅助!豪发娱乐辅助多功能AI插件,豪发娱乐其实真的有辅助插件1、首先打开豪发娱乐辅助器下载最...
解密关于透视挂!辣椒大厅透视挂... 解密关于透视挂!辣椒大厅透视挂多功能AI软件,辣椒大厅都是真的有透视挂软件1)辣椒大厅有没有挂:进一...
揭秘几款辅助器!天天娱乐辅助器... 揭秘几款辅助器!天天娱乐辅助器多功能AI教程,天天娱乐好像是真的有辅助器教程小薇(辅助器软件下载)致...
一分钟揭秘辅助器!辣椒大厅辅助... 一分钟揭秘辅助器!辣椒大厅辅助器多功能AI软件,辣椒大厅原来真的有辅助器软件1、许多玩家不知道辣椒大...
今日百科开挂!新九五开挂多功能... 今日百科开挂!新九五开挂多功能AI脚本,新九五一贯存在有开挂脚本1、新九五破解器简单,新九五机器人操...
揭秘一下透视挂!星狮娱乐透视挂... 揭秘一下透视挂!星狮娱乐透视挂多功能AIapp,星狮娱乐总是真的是有透视挂app亲,关键说明,星狮娱...
总算了解辅助!凤凰大厅辅助多功... 总算了解辅助!凤凰大厅辅助多功能AI工具,凤凰大厅好像真的是有辅助工具1、这是跨平台的凤凰大厅轻量版...
重大消息开挂!新518互游开挂... 重大消息开挂!新518互游开挂多功能AI软件,新518互游原来有开挂软件1、新518互游辅助软件下载...