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

相关内容

热门资讯

第五俱乐部(wepoke真的)... 第五俱乐部(wepoke真的)外挂透明挂辅助器(辅助挂)可靠技巧(有挂细节)-哔哩哔哩;实战中需综合...
五个检测!wpk怎么下载ios... 五个检测!wpk怎么下载ios(外挂被实锤)原来真的有挂(有挂实锤)-哔哩哔哩;支持2-10人实时对...
2个挂!德扑之星电脑版(数据解... 2个挂!德扑之星电脑版(数据解析)透视辅助(有挂分析)-哔哩哔哩关于德扑之星电脑的基本介绍、功能特点...
4修改器!白金岛放炮罚有挂吗(... 4修改器!白金岛放炮罚有挂吗(辅助挂)太坑了其实真的有挂(确实有挂)-哔哩哔哩是一款可以让一直输的玩...
3个规律!用ai外挂打德州有用... 3个规律!用ai外挂打德州有用(辅助机器人)透视(真的有挂)-哔哩哔哩这是由厦门游乐互动科技有限公司...
第3新版(德扑手机上)外挂透明... 第3新版(德扑手机上)外挂透明挂辅助APP(辅助挂)安装教程(有挂规律)-哔哩哔哩;wpk透视辅助官...
第4个助手!德扑ai软件(牌力... 第4个助手!德扑ai软件(牌力分析软件)辅助透视(有挂讲解)-哔哩哔哩1、玩家可以在德扑ai软件软件...
第九计算器!网易棋牌一直输为什... 第九计算器!网易棋牌一直输为什么(辅助挂)太坑了其实真的有挂(有挂解密)-哔哩哔哩1、任何德州ai辅...
第四个技巧!微扑克app(伙牌... 第四个技巧!微扑克app(伙牌技巧)透视辅助(的确有挂)-哔哩哔哩;wpk透视辅助官方版是专为公司和...
第6专用(德州免费)外挂透明挂... 1、第6专用(德州免费)外挂透明挂辅助神器(透视)德州论坛(有挂详情)-哔哩哔哩;详细教程。2、德州...