在使用繪圖功能添加幾何資訊后,我試圖在 p5js 中回圈瀏覽我當前的畫布。
所以我宣告了一個函式 getCanvas() 來嘗試回圈它,并將像素復制到一個變數中,但它似乎不起作用。我收到此錯誤:
無法讀取未定義的屬性。檢查錯誤的行號并確保正在操作的變數不是未定義的。
有人可以闡明這個問題嗎?這個想法是繪制幾件東西并通過按“p”保存畫布,將其系結到一個變數,以便它可以插入到一個陣列中。這是我的代碼:
let canvas;
let img;
function setup() {
let img = createImage(300, 300);
img.loadPixels();
canvas = createCanvas(windowWidth, windowHeight);
background(220);
...
}
function draw() {
//code that draws what I need successfully
...
}
function getCanvas(image){
let h, k;
// fill with random colors
for (h = 0; h < canvas.height; h ) {
for (k = 0; k < canvas.width; k ) {
image.pixels[h][k] = get(h, k);
}
}
}
function keyPressed() {
if (key === 'p'){
getCanvas(img);
img.updatePixels();
image(img, 0, 0);
}
uj5u.com熱心網友回復:
現在,img在函式的范圍內定義setup,所以你不能在外面訪問它。
您需要將它的宣告setup移到全域范圍之外,以便能夠在其他函式中使用它:
let img;
function setup() {
img = createImage(300, 300);
img.loadPixels();
canvas = createCanvas(windowWidth, windowHeight);
background(220);
// ...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/517595.html
標籤:哎呀帆布p5.js
