2017-03-14 46 views
0

我正在試驗用戶Alex(https://www.openprocessing.org/user/74843)在openprocessing.org上找到的代碼。保存p5.js草圖後僅出現語法錯誤

如果我在處理中使用p5.js模式在未保存的草圖中運行它,代碼就會執行得很好,但只要我嘗試保存項目以在稍後打開它時會出現語法錯誤並拒絕運行。我很難過,因爲代碼在保存項目之前似乎運行得很好。

有誰知道這是什麼原因造成的?

編譯器給了我以下錯誤:

SyntaxError: Expected ; but found poly

的代碼如下:

// 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(); 
} 

回答

0

我崇高的文本編輯器,而不是打開了該項目,並運行它使用WAMP本地服務器上/ MAMP和JavaScript運行得很好。

我相信這個錯誤是由處理客戶端試圖建立必要的文件(例如index.html)引起的。

仍然不確定是什麼原因導致了錯誤,但僅僅使用Processing客戶端之外的p5js解決了這種情況下的問題。