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、上手简单,内置详细...
据权威媒体报道!凑一桌游戏辅助... 据权威媒体报道!凑一桌游戏辅助软件,超凡辅助下载,机巧教程(有挂讲解)-哔哩哔哩凑一桌游戏辅助软件辅...
透视神器!wepoker祈福有... 透视神器!wepoker祈福有用吗,wepokerplus脚本(透视)推荐教程(有挂头条)-哔哩哔哩...
围绕透视问题!微乐家乡小程序辅... 围绕透视问题!微乐家乡小程序辅助,新道游戏辅助器免费版,指南教程(真的有挂)-哔哩哔哩1、下载好新道...
透视了解!aa poker辅助... 透视了解!aa poker辅助,德普之星透视辅助软件(透视)解迷教程(真是有挂)-哔哩哔哩1、实时德...
有消息称!四川乐易麻将辅助脚本... 有消息称!四川乐易麻将辅助脚本,新上游辅助软件,法子教程(有挂秘笈)-哔哩哔哩1、操作简单,无需四川...
现场直击!wpk是真的还是假的... 现场直击!wpk是真的还是假的,sohoo开挂辅助(透视)揭幕教程(有挂攻略)-哔哩哔哩1)wpk是...
最终!赣牌圈破解器,微信小程序... 最终!赣牌圈破解器,微信小程序嘟嘟十三张脚本,技法教程(证实有挂)-哔哩哔哩1、该软件可以轻松地帮助...
透视好牌!hhpoker透视脚... 透视好牌!hhpoker透视脚本,uupoker有透视吗(透视)总结教程(有挂神器)-哔哩哔哩运uu...
突发!牵手辅助神器下载,we ... 突发!牵手辅助神器下载,we poker辅助器下载,秘籍教程(有挂细节)-哔哩哔哩1、全新机制【牵手...