requestAnimationFrame 仅被调用一次

2024-03-28

我正在尝试在 Ionic 2 应用程序中使用 ThreeJS 实现非常基本的动画。基本上是尝试旋转一个立方体。但立方体没有旋转,因为 requestAnimationFrame 仅在渲染循环内执行一次。

I'm able to see only this. enter image description here

没有旋转动画。我在下面分享我的代码。

首页.html

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <div #webgloutput></div>
</ion-content>

home.ts

import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController } from 'ionic-angular';

import * as THREE from 'three';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  @ViewChild('webgloutput') webgloutput: ElementRef;


  private renderer: any;
  private scene: any;
  private camera: any;
  private cube: any;

  constructor(public navCtrl: NavController) {
  }

  ngOnInit() {
    this.initThree();
  }

  initThree() {
    this.scene = new THREE.Scene();
    this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

    this.renderer = new THREE.WebGLRenderer();
    this.renderer.setSize( window.innerWidth, window.innerHeight );
    this.webgloutput.nativeElement.appendChild(this.renderer.domElement);

    let geometry = new THREE.BoxGeometry(1, 1, 1);

    let material = new THREE.MeshBasicMaterial({ color: 0x00ff00});
    this.cube = new THREE.Mesh(geometry, material);
    this.scene.add(this.cube);
    this.camera.position.z = 5;

    this.render();
  }


  render() {
    console.log("render called");
    requestAnimationFrame(() => this.render);

    this.cube.rotation.x += 0.5;
    this.cube.rotation.y += 0.5;
    this.renderer.render(this.scene, this.camera);
  }

}

问题是您没有正确调用 requestAnimationFrame。您不是直接将渲染函数传递给它,而是传递一个箭头函数returns渲染函数。

换线requestAnimationFrame(() => this.render); to requestAnimationFrame(this.render);

但是,当使用类方法作为回调(作为参数)时,将不再使用相同的方法调用它this参考。在 MDN 上阅读有关此内容的更多信息 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this。在你的情况下,“这个”在render方法将变成undefined当被调用时requestAnimationFrame导致错误消息TypeError: Cannot read property 'render' of undefined。有有多种方法可以避免这种行为 https://stackoverflow.com/questions/4011793/this-is-undefined-in-javascript-class-methods,但我认为最简单的是显式绑定 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind渲染方法正确this:

requestAnimationFrame(this.render.bind(this));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

requestAnimationFrame 仅被调用一次 的相关文章

随机推荐

  • Typescript 编译器或 ES2015 - 是否可以优化代码?

    在其他语言的编译器领域中 当编译为从循环中拉出时 类似这样的东西会被优化 这样就不会每次都创建一个新对象 const arr 1 2 3 4 5 arr map num gt const one time 5 this never chan
  • 正则表达式精确单词匹配

    我需要匹配行中的单词 例如 The blue bird is dancing Yellow card is drawn The day is perfect rainy blue bird is eating 这四行位于文本文件中l2 我想
  • 无需外部 SD 卡即可缓存位图

    图像不会缓存在内部存储中 并且在没有外部 SD 卡的设备上会崩溃 我已经尝试了我所知道的 但没有任何效果 这是ImageCache java package com minecraftpix android bitmapfun util i
  • Azure API 应用程序 (.NET Core) 截断了大型 JSON 响应

    我有一个返回 JSON 的标准 NET Core API 控制器 一切都按设计进行 但对于较大的数据集 响应会被截断 我无法弄清楚 或在 Stackoverflow 或其他地方找到 如何增加限制 假设这就是原因 有人可以指出我正确的方向吗
  • web.xml 中的安全约束未应用于具有文件扩展名的 URL 模式

    我在 web xml 中输入了以下安全约束 我的目标是 XML 文件位于公共区域 这适用于 images 文件夹 然而 url 模式 xml似乎不起作用 有任何想法吗
  • 如何使用内联汇编在 C/C++ 程序中调用 DOS 中断?

    我需要从 C C 程序调用一些 DOS 中断 服务 我尝试了以下内联 asm 代码 读一个字符 int main asm movb 0x01 ah int 0x21 system PAUSE 但这没有用 我想知道我在这里做错了什么 另外如果
  • 每个 id 单行到每个 id 多行

    我想根据给定的时间间隔将观察结果从每个 id 单行扩展到每个 id 多行 gt dput df structure list id c 123 456 789 gender c 0 1 1 yr start c 2005 2010 2000
  • 使用 Ruby on Rails 进行 Postgres 公共表表达式查询

    我试图找到在 Rails 应用程序中使用通用表表达式进行 Postgres 查询的最佳方法 因为我知道 ActiveRecord 显然不支持 CTE 我有一张桌子叫user activity transitions其中包含一系列正在启动和停
  • 如何将数据帧转换为R中的ID列表? [复制]

    这个问题在这里已经有答案了 我整个晚上都在努力弄清楚如何在 R 中做到这一点 基本上我有一个如下的数据集 id lt c 1 1 1 2 2 3 3 3 3 label lt c a b c b d a c d e mydata lt as
  • 在固定超时内缓存单个对象的最佳方法是什么?

    Google Guava 有 CacheBuilder 允许使用过期密钥创建 ConcurrentHash 从而允许在固定 tiemout 后删除条目 但是我只需要缓存某种类型的一个实例 使用 Google Guava 在固定超时内缓存单个
  • Rxjs 主题下一个或 onNext

    我对 rxjs 很陌生 请耐心等待 例如在本教程中http blog angular university io how to build angular2 apps using rxjs observable data services
  • 无法在 Ubuntu 12.10 上安装 pg gem

    我使用的是 Ubuntu 12 10 64 位 并且安装了以下软件包 dpkg get selections grep postgre output postgresql postgresql 9 1 postgresql client p
  • 在 Java 中对月份和年份进行排序的最有效方法

    我有一个列表 其中包含字符串格式的日期 MON YYYY 我需要对此列表进行排序 到目前为止我遵循的方法是读取列表并以日期格式转换字符串并使用比较选项 但是我我没有得到想要的结果 代码片段 List
  • 检测受密码保护的 PPT 和 XLS 文档

    我找到了这个答案https stackoverflow com a 14336292 1537195 https stackoverflow com a 14336292 1537195这提供了检测 DOC 和 XLS 文件密码保护的好方法
  • 追加在 for 循环中生成的 pandas 数据帧

    我正在 for 循环中访问一系列 Excel 文件 然后我将 Excel 文件中的数据读取到 pandas 数据框中 我不知道如何将这些数据框附加在一起 然后将数据框 现在包含所有文件中的数据 保存为新的 Excel 文件 这是我尝试过的
  • Python Reddis 队列 ValueError:worker 无法处理 __main__ 模块中的函数

    我正在尝试使用 python rq 在 redis 中排队一项基本作业 但它会抛出此错误 ValueError 函数来自main模块无法被工作人员处理 这是我的程序 import requests def count words at ur
  • Oracle:DDL 和事务回滚

    Oracle DDL 创建 更改 是否可以像 MS SQL 中那样具有事务性 从 2005 年开始 DDL 在 Oracle 中不是事务性的 来自11 2 doc http docs oracle com cd E25054 01 serv
  • 使用 javascript regexp 查找第一个和最长的匹配

    我有一个像下面的简化示例一样的正则表达式 var exp he hell 当我在字符串上运行它时 它会给我第一个匹配项 fx var str hello world var match exp exec str match contains
  • 将 CNN 的输出传递给 BILSTM

    我正在开发一个项目 其中我必须将 CNN 的输出传递给双向 LSTM 我创建了如下模型 但它抛出 不兼容 错误 请让我知道哪里出了问题以及如何解决这个问题 model Sequential model add Conv2D filters
  • requestAnimationFrame 仅被调用一次

    我正在尝试在 Ionic 2 应用程序中使用 ThreeJS 实现非常基本的动画 基本上是尝试旋转一个立方体 但立方体没有旋转 因为 requestAnimationFrame 仅在渲染循环内执行一次 I m able to see onl