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)拱趴大菠萝有什么挂免费钻石...
了解透视!约局吧能不能开挂!真... 了解透视!约局吧能不能开挂!真是真的是有辅助app(有挂分析)-哔哩哔哩1、全新机制【约局吧能不能开...
解迷透视!德州透视是真的吗!一... 解迷透视!德州透视是真的吗!一直是真的有辅助插件(讲解有挂)-哔哩哔哩德州透视是真的吗透视方法中分为...
分享透视!xpoker辅助!本... 分享透视!xpoker辅助!本来是真的有辅助神器(有挂规律)-哔哩哔哩xpoker辅助是不是有人用挂...
推荐透视!xpoker辅助!其... 推荐透视!xpoker辅助!其实有辅助软件(有挂详细)-哔哩哔哩1、操作简单,无需xpoker辅助手...
关于透视!xpoker辅助神器... 关于透视!xpoker辅助神器!总是是真的有辅助攻略(有挂技巧)-哔哩哔哩1)xpoker辅助神器免...
分享透视!cloudpoker... 分享透视!cloudpoker作必弊!确实有辅助脚本(有挂秘笈)-哔哩哔哩1、许多玩家不知道clou...
揭露透视!竞技联盟透视!切实是... 揭露透视!竞技联盟透视!切实是真的有辅助软件(有挂技巧)-哔哩哔哩1、该软件可以轻松地帮助玩家将竞技...
关于透视!红龙poker有辅助... 关于透视!红龙poker有辅助吗!切实一直都是有辅助攻略(有挂秘籍)-哔哩哔哩1、下载好红龙poke...
详情透视!智星菠萝透视!本来一... 详情透视!智星菠萝透视!本来一直都是有辅助app(今日头条)-哔哩哔哩1、下载好智星菠萝透视脚本下载...