window.requestAnimationFrame() 在 Ionic2 应用程序中不起作用

尼尔

我正在尝试在我的 Ionic 2 应用程序中使用 HTML5 画布制作动画。为此,我必须使用window.requestAnimationFrame()动画我的画布。这是我的代码:

import { Component, ViewChild, Renderer } from '@angular/core';
import { Platform } from 'ionic-angular';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

...

export class CubePage {
  @ViewChild('glCanvas') canvas: any;
  canvasElement: any;
  ctx: any;
  radius: number;
  leftBallX: number;
  leftBallY: number;
  rightBallX: number;
  rightBallY: number;

  constructor(public navCtrl: NavController, public navParams: NavParams, public renderer: Renderer, public platform: Platform) {
  }

  ngAfterViewInit() {
    console.log(this.canvas);
    this.radius = 25;
    this.canvasElement = this.canvas.nativeElement;

    this.renderer.setElementAttribute(this.canvasElement, 'width', this.platform.width() + "");
    this.renderer.setElementAttribute(this.canvasElement, 'height', this.platform.height() + "");
    this.ctx = this.canvasElement.getContext('2d');

    this.leftBallX = 5;
    this.leftBallY = 5;

    window.requestAnimationFrame(this.cycle);
  }


  cycle() {
    if (this.leftBallX < this.platform.width() / 2) {
      this.ctx.clearRect(0, 0, this.platform.width(), this.platform.height());
      this.drawCenterLine();
      this.updateLeftBall(this.leftBallX + 5, this.leftBallY);
      this.drawLeftBall();
    }
    window.requestAnimationFrame(this.cycle);
  }
...
    }

Cannot read property 'leftBallX' of undefined当我在 Web 浏览器中加载应用程序时,它会出现运行时错误但是,当我消除这些window.requestAnimationFrame(this.cycle)行,用 替换第一个时this.cycle(),没有错误。window在 Ionic/Angular 中使用有问题吗?

Duannx

要解决您的问题,首先,您需要了解this调用 javascript 函数时的上下文。让我们看看例子:

foo = 0;
ngAfterViewInit(){
  let self = this;
  //way #1
  setTimeout(function(){
    console.log(this.foo); //undefinded; because this != self;
  },1000);
  //way #2
  setTimeout(()=>{
    console.log(this.foo); //0; because this == self;
  },1000)
}

当您通过#1 方式调用函数时,javascript 会重新绑定this对象,因此您无法foothis.
当您通过#2(箭头函数)调用函数时,javascript 不会重新绑定this对象,因此您可以this按预期使用

现在您可以使用箭头函数解决您的问题:

requestAnimationFrame(()=>{this.cycle()})

查看更多关于箭头函数

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

videojs在Angular 2应用程序中不起作用

Ajax在JSF 2应用程序中不起作用

RequestAnimationFrame在简单的画布中不起作用

Ionic 1本地通知在Ionic View应用程序中不起作用

Windows ionic 应用程序的 Weinre 调试不起作用?

为什么ionic 4中的主应用程序scss文件不起作用?

PHP $ _SESSION变量在Ionic / AngularJS应用程序中不起作用

Ionic 3:应用程序组件中的事件不起作用

sourceLanguage中的翻译在Yii2应用程序中不起作用

cakephp 2应用程序食谱编辑不起作用

Ionic2 proxyUrl在设备上不起作用

Ionic2 标签向后滑动不起作用 swipeBackEnabled

ionic2 通过 facebook 登录不起作用

Angular 2 应用程序在 android 4.4.2 默认浏览器中不起作用

Angular 2应用程序在ASP.NET 5环境中不起作用

在watchOS 2中读取NSUserDefaults(我知道应用程序组不起作用)

Kendo-ui-angular2 控件在 .NET 4.5 应用程序中不起作用

ng2-toastr 在 angular 4 应用程序中不起作用

依赖项注入在Angular 2入门应用程序中不起作用

构建ionic2应用程序失败

在angular2中使用requestAnimationFrame

Ionic2:处理提供程序

ionic2 中的搜索栏、按钮和滑动选项不起作用

POST请求在Ionic2 App中的IOS上不起作用

在Ionic2中设置警报超时

Ionic2中的多个$ http请求

ionic2中的动态表单验证

无法在ionic2中打开菜单

google auth 在生产 apk 中工作,但在 ionic 2 中的 Play 商店中启动应用程序后不起作用