[英]Syntax error only occuring after saving p5.js sketch
I am experimenting with code found on openprocessing.org by the user Alex ( https://www.openprocessing.org/user/74843 ). 我正在试验用户Alex( https://www.openprocessing.org/user/74843 )在openprocessing.org上找到的代码。
The code executes just fine if I run it in an unsaved sketch in Processing using p5.js mode, but as soon as I try to save the project to open it later it gives syntax errors and refuses to run. 如果我使用p5.js模式在“处理”中未保存的草图中运行代码,则代码执行得很好,但是一旦我尝试保存项目以在以后打开它,它就会出现语法错误并拒绝运行。 I'm stumped as the code seemed to run just fine prior to saving the project.
我很沮丧,因为在保存项目之前代码似乎可以正常运行。
Does anyone know what is causing this? 有谁知道是什么原因造成的?
The compiler gives me the following error: 编译器给我以下错误:
SyntaxError: Expected ;
语法错误:预期; but found poly
但发现聚
The code is below: 代码如下:
// polygon array and number of verts
let poly = [];
let n = 100;
// canvas size variables
let w = 500;
let h = 500;
// setup and draw functions ---
function setup() {
createCanvas(w, h);
strokeWeight(12);
noFill();
cursor(HAND);
noStroke();
n++; // add extra point for closing the polygon
for (let i = 0; i < n; i++) {
// populate regular polygon vertices given number of points n
let a = {
x: (w/2) + 100*sin(map(i, 0, n-1, 0, TAU)),
y: (h/2) + 100*cos(map(i, 0, n-1, 0, TAU))
};
poly.push(a);
}
}
function draw() {
// use default blend mode for background
blendMode(BLEND);
background(0, 0, 0);
// use additive blend mode to separate color channels
blendMode(ADD);
stroke(255, 0, 0);
drawPoly(1000, 1000);
stroke(0, 255, 0);
drawPoly(1200, 1500);
stroke(0, 0, 255);
drawPoly(2000, 1700);
}
// helper function implementations ---
function logMap(value, start1, stop1, start2, stop2) {
// based off of linear regression + existing p5.map function
start2 = log(start2);
stop2 = log(stop2);
return exp(start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1)));
}
function drawPoly(dx, dy) {
// draws polygon given vertices in the poly[] array, adds mouse bias using params
let g = 0;
if (mouseIsPressed)
g = random(-2, 2);
beginShape();
for (let i = 0; i < n; i++) {
let bias = dist(mouseX, mouseY, poly[i].x, poly[i].y);
vertex(poly[i].x + dx / logMap(bias, w, 0, dx, 45) + g, poly[i].y + dy / logMap(bias, h, 0, dy, 45) + g);
}
endShape();
}
I opened up the project in Sublime Text Editor instead and ran it on a local server using WAMP/MAMP and the javascript runs just fine. 我改为在Sublime Text Editor中打开了该项目,并使用WAMP / MAMP在本地服务器上运行了该项目,而javascript运行得很好。
I believe this error was caused by the Processing client trying to build the necessary files ( index.html for example ). 我认为此错误是由处理客户端尝试构建必要的文件(例如index.html)引起的。
Still not sure what caused the error but simply working with p5js outside of the Processing client solved the issues in this case. 仍然不确定是什么导致了错误,但是在此情况下,仅在Processing客户端之外使用p5js即可解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.