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、构建自己的边锋斗地主底...
微扑克ai辅助!微扑克使用方法... 微扑克ai辅助!微扑克使用方法(透视辅助)都是有挂是一款可以让一直输的玩家,快速成为一个“必胜”的a...
WePoKe透视挂!wepok... 1、WePoKe透视挂!wepokewopoker透明挂(透视)we poker(原来真的有挂);详...
查到实测辅助!随意玩软件黑科技... 查到实测辅助!随意玩软件黑科技(透明挂)一贯真的有挂(2026已更新)(哔哩哔哩)亲,关键说明,随意...
三分钟了解!雀神开外挂专用辅助... 三分钟了解!雀神开外挂专用辅助器下载安装,妙趣斗地主有挂吗,黑科技教程(有挂方法)1、每一步都需要思...
微扑克辅助软件!微扑克ai机器... 微扑克辅助软件!微扑克ai机器人打德州,微扑克ai辅助器苹果版(果然是有挂)1、许多玩家不知道微扑克...
wepokeai机器人!wep... wepokeai机器人!wepoke有正规,德州wepower总是真的有挂,力荐教程(有挂揭秘);德...
查到实测!阳光岛牌乐汇怎么开挂... 查到实测!阳光岛牌乐汇怎么开挂(透视)好像是有挂(2020已更新)(哔哩哔哩)1、不需要AI权限,帮...
微扑克有辅助挂!微扑克发牌有问... 微扑克有辅助挂!微扑克发牌有问题,微扑克好像真的有挂,黑科技教程(有挂机密)1)微扑克辅助挂:进一步...
七分钟了解!大众互娱有办法开挂... 七分钟了解!大众互娱有办法开挂吗,哈局十三张为什么老是输,靠谱教程(有挂普及)1、哈局十三张为什么老...