1

I'm working on canvas and was handling gesture events with hammerjs. But there I was facing an issue which I already asked here and other places but nobody responded. So now I'm working with ionic gestures but there also I can't find rotation angle property. Can anybody help me with that?

Here's the code:

ngAfterViewInit() {
      const gesture: Gesture = this.gestureCtrl.create({
      el: this.transCanvasElement,
      gestureName: 'rotate',
      onStart: ev => this.onStartHandler(ev)
    }, true);
    gesture.enable();
}
onStartHandler(ev) {
    console.log('ionic gesture ev angle: ', ev.rotation); // ev.rotation is undefined
}

1 Answer 1

0

Meaning of this in JS depends on the scope. See here for more info. You could either bind this to the callback or make it an arrow function.

Try the following

Option 1: bind this

ngAfterViewInit() {
  const gesture: Gesture = this.gestureCtrl.create({
    el: this.transCanvasElement,
    gestureName: 'rotate',
    onStart: this.onStartHandler.bind(this)  // <-- bind `this` to the callback
  }, true);
  gesture.enable();
}

onStartHandler(ev) {
  console.log('ionic gesture ev angle: ', ev.rotation);
}

Option 2: arrow function

ngAfterViewInit() {
  const gesture: Gesture = this.gestureCtrl.create({
    el: this.transCanvasElement,
    gestureName: 'rotate',
    onStart: ev => this.onStartHandler(ev)
  }, true);
  gesture.enable();
}

onStartHandler = (ev) => {                           // <-- arrow function
  console.log('ionic gesture ev angle: ', ev.rotation);
}
3
  • Option 1 is giving an error bind() method doesn't exist on type void and Option 2 giving the same result as before. ev.rotation is still undefined. Commented Nov 18, 2020 at 10:16
  • Yes the arrow notation isn't required in option. I've updated the answer. Regarding error in option 2, please check if rotation property is actually available in ev variable. Try to print only ev to verify.
    – Michael D
    Commented Nov 18, 2020 at 10:43
  • No there's no rotation property. That's why I asked this question if anybody could help me find it in nested objects of ev. Its strange to not have rotation property in gesture API. Commented Nov 18, 2020 at 12:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.