我正在制作一個自動生成行星的腳本,例如,請參見codepen。但我遇到的問題是我想讓它不那么像素化,如果我將圖塊設為 70 * 70 并將圖塊大小設為 10 * 10 像素,我會遇到一些問題,它可以正常工作。但我想把它設置為像瓷磚 360 * 360 和大小為 1 或 2 像素的東西。但是當我嘗試這樣做時,我得到了最大調用堆棧錯誤。所以我嘗試使用requestAnimationFrame但加載需要很長時間有沒有辦法加快這個過程?var tileNum = 0; var tiles; var colorsLand; var colorsWater; var size = 360; var tileSize = 2; var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); window.onload = function () { generatePlanet(); } function generatePlanet() { tileNum = 0; tiles = [{ x: 0, y: 0, land: false }]; //Retrive colors colorsLand = interpolateColors("rgb(" + getColor(true) + ")", "rgb(" + getColor(true) + ")", 6000); colorsWater = interpolateColors("rgb(" + getColor(false) + ")", "rgb(" + getColor(false) + ")", 6000); //Creates a array of my tiles and sets either water or land to them and calculates the % of being water/land for (var i = 0; i < (size * size); i++) { var currentTile = tiles[tiles.length - 1]; if (currentTile.x <= (size - 1)) { var isLand = false; if (currentTile.land == true || tiles.length > size && tiles[tiles.length - size].land == true) { isLand = (Math.floor(Math.random() * 100) + 1) > 35; } else if (currentTile.land == true || tiles.length > size && (tiles[tiles.length - 1].land == true || tiles[tiles.length - size].land == true)) { isLand = (Math.floor(Math.random() * 100) + 1) > size; } else { isLand = (Math.floor(Math.random() * 100) + 1) > 99; } tiles.push({ x: currentTile.x + 1, y: currentTile.y, land: isLand }); } else { tiles.push({ x: 0, y: currentTile.y + 1, land: isLand }); } } drawPlanet() }
畫布生成最大調用堆棧的問題
Helenr
2021-06-08 13:08:26