javascript - 与 Angular 一起使用时,p5.js 中的变量未定义?

标签 javascript angular typescript p5.js

我正在学习 WebGL 并开始使用 p5.js 和 Angular 开发 Sketch 应用程序。我在组件文件中定义了 bool 变量,以便基于此我想在绘图函数中触发特定函数,如椭圆、矩形、直线等。这些 bool 变量由另一个组件中的按钮管理。

我收到错误 core.js:6014 ERROR TypeError: Cannot read property 'isRectangleMode' of undefined

组件文件:

import { Component, OnInit } from '@angular/core';

import * as p5 from 'p5';

import { Subscription } from 'rxjs';

import { HomeService } from '../home.service';

@Component({
  selector: 'app-doodle-area',
  templateUrl: './doodle-area.component.html',
  styleUrls: ['./doodle-area.component.css']
})
export class DoodleAreaComponent implements OnInit {

  private p5Init : any;

  modeSubs : Subscription;
  modeSelected : string = null;
  isCircleMode : boolean = false;
  isEllipseMode : boolean = false;
  isRectangleMode : boolean = false;
  isLineMode : boolean = false;
  isPointMode : boolean = false;
  isBrushMode : boolean = false;
  isPenMode : boolean = false;

  constructor(private homeService : HomeService) { }

  ngOnInit() {
    this.createCanvas();

    this.homeService.modeSelected
      .subscribe(modeSelected => {
        this.modeSelected = modeSelected;
        console.log(this.modeSelected);
        if(this.modeSelected) {
          this.modeReset();
          if(this.modeSelected === "circle") {
            this.isCircleMode = true;
          } else if(this.modeSelected === 'ellipse') {
            this.isEllipseMode = true;
          } else if(this.modeSelected === 'rectangle') {
            this.isRectangleMode = true;
          } else if(this.modeSelected === 'line') {
            this.isLineMode = true;
          } else if(this.modeSelected === 'point') {
            this.isPointMode = true;
          } else if(this.modeSelected === 'brush') {
            this.isBrushMode = true;
          } else if(this.modeSelected === 'pen') {
            this.isPenMode = true;
          }
        }
      });
  }

  private modeReset() {
    this.isCircleMode = false;
    this.isEllipseMode = false;
    this.isRectangleMode = false;
    this.isLineMode = false;
    this.isPointMode = false;
    this.isBrushMode = false;
    this.isPenMode = false;
  }

  private createCanvas() {
    this.p5Init = new p5(this.doodleArea);
  }

  private doodleArea(p : any) {

    p.setup = () => {
      p.createCanvas(p.windowWidth - 440, p.windowHeight - 200).parent('doodle-area');
      p.background(206,214,224);
      p.createP("Hello");
    }

    p.draw = () => {

      if(this.isRectangleMode) {
        console.log("Rectangle");
      }

      p.stroke(0);
      if(p.mouseIsPressed === true) { 
        p.line(p.mouseX, p.mouseY, p.pmouseX, p.pmouseY); 
      }
    }
  }

}

控制台截图:

enter image description here

最佳答案

这个问题是由于范围造成的。在回调函数 doodleArea 中,作用域不是组件作用域 (this)。这里这是未定义的,我们无法访问未定义的isRectangleMode。可能的解决方案是:

在.ts中修改createCanvas(),代码如下:

private createCanvas() {
  const doodleArea = s => {
  s.setup = () => {
    let canvas = s.createCanvas(s.windowWidth - 440, s.windowHeight - 200);
    canvas.parent("doodle-area");
    s.draw = () => {
      if (this.isRectangleMode) {
        console.log("Rectangle");
      }

      s.stroke(0);
      if (s.mouseIsPressed === true) {
        s.line(s.mouseX, s.mouseY, s.pmouseX, s.pmouseY);
      }
    };
    s.keyPressed = () => {
    if (s.key === 'c') {
      window.location.reload();
    }};};
  };
  this.p5Init = new p5(doodleArea);
}

下面是相同的示例代码:

https://stackblitz.com/edit/angular-s-p5-angular?file=src%2Fapp%2Fapp.component.ts

https://angular-s-p5-angular.stackblitz.io

希望这会有所帮助。

关于javascript - 与 Angular 一起使用时,p5.js 中的变量未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60910050/

相关文章:

javascript - 如何为 React 函数保留状态文本

javascript - 从键和值是动态的数组中搜索字符串

node.js - Angular 5 使用 CORS 调用 api

javascript - 使用 typescript react useTable 钩子(Hook)

TypeScript 重新定义局部变量

javascript - 将 socket.io 加载到 JavaScriptCore for iOS

javascript - 为什么当我运行 For 循环时它不起作用?

javascript - Typescript 声明文件不是模块

spring - 使用 Angular2 将 MultipartFile 作为请求参数发送到 REST 服务器

php - Laravel 无法读取表单数据