The example provided by the original poster demonstrates how to listen for click events on an SVG element using Angular.
To simplify, here is what the main.html
file could look like:
<svg width="500" height="500" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path
(click)="click($event)"
style="fill: none; stroke: #000000; stroke-width: 1px;"
d="M 10.280374,48.130842 V 26.168224 H 52.336448 V 70.794392 H 26.869158 V 50 h -7.476635"
/>
</svg>
I have included the original poster's code for main.ts
from StackBlitz so that others can test it later:
import 'zone.js/dist/zone';
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { bootstrapApplication } from '@angular/platform-browser';
@Component({
selector: 'my-app',
standalone: true,
imports: [CommonModule],
templateUrl: `./main.html`,
})
export class App {
name = 'Angular';
click(e: MouseEvent) {
console.log('DING DONG', e.target);
}
}
bootstrapApplication(App);
This setup will ensure that e.target
points to the correct path element when clicked.
If you are encountering issues with the clickable area of the element, the problem lies elsewhere. When there is no fill present (due to fill:none
in CSS), clicking is limited to the stroke. In cases where the stroke is thin, clicking becomes difficult.
To resolve this issue without compromising performance, you can add the CSS property pointer-events: all
to the path element.
(For more information on this property, refer to https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events)
For older browser support, you can use fill: rgba(0,0,0,0)
or fill: transparent
, or apply any fill color with fill-opacity: 0
. Keep in mind that this may impact performance slightly as the browser may still attempt to paint a 'transparent color' layer over the background in those areas (even though the end result is the same – not painting over the background).