gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:11:35 25 4
gpt4 key购买 nike

我正在学习 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com