Skip to content

Commit

Permalink
Use texture from Apollo 11 for moon surface
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmorris committed Mar 10, 2023
1 parent 8d365f6 commit 4a6ba4c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Binary file added images/surfacerepeat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 25 additions & 4 deletions terrain.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { randomBetween } from "./helpers/helpers.js";
import { randomBetween, randomBool } from "./helpers/helpers.js";

export const makeTerrain = (state) => {
const CTX = state.get("CTX");
Expand All @@ -7,23 +7,44 @@ export const makeTerrain = (state) => {
const maxHeight = 10;
const minHeight = 5;
const points = Math.max(canvasWidth / 60, 20);
const backgroundImage = new Image();
backgroundImage.src = "./images/surfacerepeat.jpg";

let backgroundPattern;
let terrain = [];

const reGenerate = () => {
const getBackgroundPattern = () =>
new Promise((resolve) => {
backgroundImage.complete
? resolve(backgroundPattern)
: (backgroundImage.onload = () => {
backgroundPattern = CTX.createPattern(backgroundImage, "repeat");
resolve(backgroundPattern);
});
});

async function reGenerate() {
terrain = [];
const pattern = await getBackgroundPattern();

pattern.setTransform(
new DOMMatrix()
.scale(randomBetween(0.7, 0.9))
.rotate(randomBool() ? 0 : 180)
);

for (let index = 1; index < points; index++) {
terrain.push({
x: index * (canvasWidth / points),
y: randomBetween(canvasHeight - minHeight, canvasHeight - maxHeight),
});
}
};
}
reGenerate();

const draw = () => {
CTX.save();
CTX.fillStyle = "#A2A4A1";
CTX.fillStyle = backgroundPattern;
CTX.beginPath();
CTX.moveTo(0, canvasHeight);
CTX.lineTo(0, canvasHeight - maxHeight / 2);
Expand Down

0 comments on commit 4a6ba4c

Please sign in to comment.