Skip to content

Commit

Permalink
Placing rocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
DeboAtWinterlight committed Aug 25, 2024
1 parent 4946f5c commit 97cce00
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
5 changes: 1 addition & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@
</head>

<body>
<div id="scrollable-area">
<div id="center-point"></div>
<div id="rocks-container"></div>
</div>
<div id="scrollable-area"></div>

<template id="rock-template">
<div class="rock">
Expand Down
44 changes: 36 additions & 8 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
const numRocks = 1000;
const gridSize = 51200 / 256;

document.addEventListener("DOMContentLoaded", () => {
const rocksContainer = document.getElementById("rocks-container");
const scrollableArea = document.getElementById("scrollable-area");
const rockTemplate = document.getElementById("rock-template");
const numRocks = 20; // Number of rocks to create

function createRock() {
function createRockComponent(location) {
const rock = rockTemplate.content.cloneNode(true).querySelector(".rock");
rock.style.left = `${Math.random() * 100}%`;
rock.style.top = `${Math.random() * 100}%`;
rock.style.gridRow = location.x;
rock.style.gridColumn = location.y;
rock.addEventListener("click", flingRock);
rocksContainer.appendChild(rock);
scrollableArea.appendChild(rock);
}

function createRocks() {
const rockLocations = [];
while (rockLocations.length < numRocks) {
const newRockLocation = {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize),
};

if (
rockLocations.find(
(l) => l.x === newRockLocation.x && l.y === newRockLocation.y
)
) {
continue;
}

rockLocations.push(newRockLocation);
}

for (const location of rockLocations) {
createRockComponent(location);
}
}

function flingRock(event) {
Expand All @@ -28,7 +54,9 @@ document.addEventListener("DOMContentLoaded", () => {
}, duration);
}

for (let i = 0; i < numRocks; i++) {
createRock();
function initialize() {
createRocks();
}

initialize();
});
8 changes: 5 additions & 3 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ body {
}

#scrollable-area {
width: 50000px;
height: 50000px;
width: 51200px;
height: 51200px;
background-image: url("./img/forest-tile-512x512.jpg");
background-repeat: repeat;
display: grid;
grid-template-columns: repeat(auto-fill, 256px);
grid-template-rows: repeat(auto-fill, 256px);
}

.rock {
position: absolute;
width: 256px;
height: 256px;
cursor: pointer;
Expand Down

0 comments on commit 97cce00

Please # to comment.