Selaa lähdekoodia

Add logic for scaling things. Add peppers.

tags/v0.0.1
Fen Dweller 5 vuotta sitten
vanhempi
commit
0be0927ad1
5 muutettua tiedostoa jossa 109 lisäystä ja 16 poistoa
  1. +2
    -3
      macrovision.css
  2. +5
    -2
      macrovision.html
  3. +59
    -11
      macrovision.js
  4. +43
    -0
      math.min.js
  5. BIN
      pepper.png

+ 2
- 3
macrovision.css Näytä tiedosto

@@ -18,14 +18,12 @@ body {
.entity {
position: absolute;
--height: 100px;
width: 50px;
background-color: red;
height: var(--height);
text-align: center;
}

.entity.selected {
box-shadow: 10px 10px 5px grey;
filter: drop-shadow(0px 0px 10px gold);
}

#world {
@@ -46,6 +44,7 @@ body {
.menu-item {
font-size: 24px;
color: #ccc;
margin: 20px;
}

#display {


+ 5
- 2
macrovision.html Näytä tiedosto

@@ -6,6 +6,7 @@
<title>Macrovision</title>
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="macrovision.css">
<script src="math.min.js"></script>
<script src="macrovision.js"></script>
<meta name="theme-color" content="#000000" />
<meta name="description" content="How big are they anyway?" />
@@ -17,11 +18,13 @@

<body>
<div id="menubar">
<div class="menu-item">This will be a menu at some point</div>
<div class="menu-item" id="entity-name"></div>
<div class="menu-item" id="entity-author"></div>
<div class="menu-item" id="entity-height"></div>
</div>
<div id="world">
<div id="entities">
</div>
<canvas id="display">
</canvas>


+ 59
- 11
macrovision.js Näytä tiedosto

@@ -1,9 +1,28 @@
let selected = null;
let selectedEntity = null;

let entityIndex = 0;

const config = {
height: math.unit(10, "meters")
}

const entities = {

}

function updateSizes() {
drawScale();
Object.entries(entities).forEach(([key, entity]) => {
const element = document.querySelector("#entity-" + key);
const canvasHeight = document.querySelector("#display").clientHeight;
const pixels = math.divide(entity.height, config.height).value * canvasHeight;
element.style.setProperty("--height", pixels + "px");
});
}
function drawScale() {
function drawTicks(/** @type {CanvasRenderingContext2D} */ ctx) {
for (let y = ctx.canvas.clientHeight - 50; y >= 50; y -= 50) {
function drawTicks(/** @type {CanvasRenderingContext2D} */ ctx, pixelsPer) {
for (let y = ctx.canvas.clientHeight - 50; y >= 50; y -= pixelsPer) {
drawTick(ctx, 50, y);
}
}
@@ -38,6 +57,10 @@ function drawScale() {
/** @type {CanvasRenderingContext2D} */

const ctx = canvas.getContext("2d");

const pixelsPer = (ctx.canvas.clientHeight - 100) / config.height.value;

ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.scale(1,1);
console.log(canvas.clientWidth);
ctx.canvas.width = canvas.clientWidth;
@@ -53,7 +76,7 @@ function drawScale() {
ctx.lineTo(ctx.canvas.clientWidth - 50, ctx.canvas.clientHeight - 50);
ctx.stroke();

drawTicks(ctx);
drawTicks(ctx, pixelsPer);
}

function makeEntity() {
@@ -63,7 +86,8 @@ function makeEntity() {
location: {
x: 0,
y: 0
}
},
height: math.unit(Math.random() * 2 + 1, "meters")
}
return entityTemplate;
@@ -75,32 +99,56 @@ function select(target) {
}

selected = target;
selectedEntity = entities[target.dataset.key];

console.log(selectedEntity)
selected.classList.add("selected");

entityInfo(selectedEntity);
}

function entityInfo(entity) {
document.querySelector("#entity-name").innerText = "Name: " + entity.name;
document.querySelector("#entity-author").innerText = "Author: " + entity.author;
document.querySelector("#entity-height").innerText = "Height: " + entity.height.format({precision: 3});
}

function displayEntity(entity) {
const location = entity.location;

const div = document.createElement("div");
div.classList.add("entity");
const img = document.createElement("img");
img.src = "./pepper.png"
img.classList.add("entity");

div.style.left = location.x + "px";
div.style.top = location.y + "px";
img.style.left = location.x + "px";
img.style.top = location.y + "px";

div.addEventListener("click", e => select(e.target));
img.addEventListener("click", e => select(e.target));

img.id = "entity-" + entityIndex;
img.dataset.key = entityIndex;

entities[entityIndex] = entity;

entityIndex += 1;

const world = document.querySelector("#entities");
world.appendChild(div);
world.appendChild(img);
}

document.addEventListener("DOMContentLoaded", () => {
for (let x = 0; x < 5; x++) {
const entity = makeEntity();
entity.name = "Green is my pepper";
entity.author = "Fen"
entity.location.x = 300 + Math.random() * 600;
entity.location.y = 300 + Math.random() * 400;
displayEntity(entity);
}

drawScale();
updateSizes();
});

window.addEventListener("resize", () => {
updateSizes();
})

+ 43
- 0
math.min.js
File diff suppressed because it is too large
Näytä tiedosto


BIN
pepper.png Näytä tiedosto

Before After
Width: 178  |  Height: 218  |  Size: 30 KiB

Loading…
Peruuta
Tallenna