0

Say, I want to increase the radius of an SVG circle on mousewheelup and decrease on mousewheeldown, how do I achieve it?

<svg width="800" height="400">
  <circle
    [attr.r]          ="cRadius" 
    (mousewheelup)    ="cRadius = cRadius + 5"
    (mousewheeldown)  ="cRadius = cRadius - 5"
  />
</svg>

1 Answer 1

3
@Directive({ selector: '[circle]' })
export class MouseWheelDirective {
  @Output() mouseWheelUp = new EventEmitter();
  @Output() mouseWheelDown = new EventEmitter();

  @HostListener('mousewheel', ['$event']) onMouseWheelChrome(event: any) {
    this.mouseWheelFunc(event);
  }

  @HostListener('DOMMouseScroll', ['$event']) onMouseWheelFirefox(event: any) {
    this.mouseWheelFunc(event);
  }

  @HostListener('onmousewheel', ['$event']) onMouseWheelIE(event: any) {
    this.mouseWheelFunc(event);
  }

  mouseWheelFunc(event: any) {
    var event = window.event ;
    var delta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail)));
    if(delta > 0) {
        this.mouseWheelUp.emit(event);
    } else if(delta < 0) {
        this.mouseWheelDown.emit(event);
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Though, you can remove those old IE support parts, since relevant versions do not support svg at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.