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 poker游戏下,好像真的有挂(的确有挂)1、进入游戏-大厅左...
技术分享!wepoker有没有... 技术分享!wepoker有没有透视方法,wepoker一直输的号能继续打吗,确实是真的有挂(有挂方法...
攻略讲解!哈糖大菠萝免费辅助器... 攻略讲解!哈糖大菠萝免费辅助器,wepoker辅助脚本,总是真的是有挂(有挂秘籍)小薇(辅助器软件下...
重大通报!wpk辅助是什么,a... 重大通报!wpk辅助是什么,aapoker如何设置胜率,一贯存在有挂(有挂方法)1、下载好正确养号方...
玩家必看攻略!wepoker可... 玩家必看攻略!wepoker可以免费玩吗,hhpoker买挂,总是存在有挂(有挂解密)1、在插件功能...
最新通报!德普之星透视辅助软件... 最新通报!德普之星透视辅助软件是真的吗,德州局HHpoker透视脚本,真是真的是有挂(详细教程)1、...
最新技巧!wepoker辅助软... 最新技巧!wepoker辅助软件价格,fishpoker透视,一直是有挂(有挂技术)1、首先打开we...
今日重大通报!hhpoker怎... 今日重大通报!hhpoker怎么开透视,wejoker免费脚本,总是真的是有挂(揭秘有挂)1、点击下...
推荐一款!aa poker辅助... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
热门推荐!微扑克辅助,wepo... 热门推荐!微扑克辅助,wepoker辅助器最新版本更新内容,真是有挂(发现有挂)1、玩家可以在wep...