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

相关内容

热门资讯

一秒答解(WPK原理)外挂透明... 一秒答解(WPK原理)外挂透明挂辅助挂(黑科技)软件透明挂(真是有挂)-哔哩哔哩;WPK原理是一项W...
实测教程(智星德州菠萝安全)外... 实测教程(智星德州菠萝安全)外挂透明挂辅助器(黑科技)德扑之星(证实有挂)-哔哩哔哩;智星德州菠萝安...
盘点十款(WPK辨别)外挂透明... 盘点十款(WPK辨别)外挂透明挂辅助工具(黑科技)透视辅助(有挂攻略)-哔哩哔哩是一款可以让一直输的...
重要通知(新版Wepoke)外... 重要通知(新版Wepoke)外挂透明挂辅助器(黑科技)插件(有挂总结)-哔哩哔哩;是一款可以让一直输...
分享一款(微扑克智能ai)外挂... 分享一款(微扑克智能ai)外挂透明挂辅助插件(黑科技)保险规则(有挂工具)-哔哩哔哩;大家肯定在之前...
玩家必看攻略(德州实战)外挂透... 玩家必看攻略(德州实战)外挂透明挂辅助安装(黑科技)德州扑克系统规律(有挂教程)-哔哩哔哩是一款可以...
一秒答解(WePoKe辅助插件... 您好:WePoKe辅助插件这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
总算了解(WPK透明)外挂透明... 总算了解(WPK透明)外挂透明挂辅助黑科技(黑科技)有正规(确实有挂)-哔哩哔哩;AI辅助机器人普及...
教程辅助(德州ai代理)外挂透... 教程辅助(德州ai代理)外挂透明挂辅助下载(黑科技)辅助技巧(有挂秘笈)-哔哩哔哩;AI辅助机器人普...
推荐攻略(德州手机)外挂透明挂... 推荐攻略(德州手机)外挂透明挂辅助下载(黑科技)ai机器人(了解有挂)-哔哩哔哩;超受欢迎的德州手机...