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

相关内容

热门资讯

必备辅助推荐!欢乐情怀怎么开挂... 必备辅助推荐!欢乐情怀怎么开挂(透视)德普之星辅助器(透牌教程有挂技术);无需打开直接搜索加薇136...
透视黑科技!熊猫辅助官网下载安... 熊猫辅助官网下载安装是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加...
分享一款!宝宝浙江辅助工具(透... 分享一款!宝宝浙江辅助工具(透视)we poker辅助器v3.3(可靠技巧有人有挂);亲,宝宝浙江辅...
透视能赢!方片十三张透视脚本(... 透视能赢!方片十三张透视脚本(透视)德州hhpoker是真的吗(存在挂教程真是有挂)这是一款可以让一...
必看攻略!龙岩闲游安卓脚本(透... 必看攻略!龙岩闲游安卓脚本(透视)aapoker真的假的(黑科技教程有挂工具)龙岩闲游安卓脚本ai黑...
玩家爆料!创思维激k透视免费下... 玩家爆料!创思维激k透视免费下载(透视)wepoker轻量版透视系统(技巧教程讲解有挂) 【无需打开...
透视好友房!优乐江西辅助器(透... 您好:hhpoker辅助器视频这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
透视线上!新九哥脚本(透视)德... 透视线上!新九哥脚本(透视)德州透视脚本(必备教程发现有挂) 了解更多开挂安装加(136704302...
玩家必看!老友广西玩有破解视频... 老友广西玩有破解视频是一款专注玩家量身打造的游戏记牌类型软件,在老友广西玩有破解视频这款游戏中我们可...
教程攻略!新道游辅助器免费版(... 哈糖大菠萝可以开挂吗是一款专注玩家量身打造的游戏记牌类型软件,在哈糖大菠萝可以开挂吗这款游戏中我们可...