我做 Etch-a-Sketch - https://www.theodinproject.com/lessons/foundations-etch-a-sketch 。但我想做的不仅仅是作业。
我只需要使用 Flexbox 和 JavaScript(没有 CSS 网格)来完成此操作
当我执行 gridContainer.style.height = "100vh" 时,它比屏幕高度稍高一些。所以我做97vh。还有其他解决办法吗?
我想仅针对移动屏幕添加宽度=100vw(替换高度=“97vh”)。但我不能用媒体查询 javasript 来做到这一点。 (结果是网格容器的高度大于宽度。)也许我不知道什么?我该怎么做?
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="./styles.css" />
<script src="./script.js" defer></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Etch-a-Sketch</title>
</head>
<body>
<div id="gridUser">
<h1>Etch-a-Sketch</h1>
<form id="gridForm">
<p><label for="gridInput">Enter a number of squares per side: 1...100</label></p>
<p><input id="gridInput" type="text"></p>
<p><legend>Choose mouse hover color</legend></p>
<p>
<input id="gridRadioYellow" name="gridRadio" value ="yellow" type="radio" checked>
<label for="gridRadioYellow"> Yellow</label>
<input id="gridRadioRandomize" name="gridRadio" value ="randomize" type="radio">
<label for="gridRadioRandomize"> Randomize</label>
<input id="gridRadioRandomizeEverySquare" name="gridRadio" value ="randomizeEverySquare" type="radio">
<label for="gridRadioRandomizeEverySquare"> Randomize every square</label>
</p>
<p><button type="submit">Submit</button></p>
</form>
<div id="gridText"></div><br/>
</div>
<div id="gridContainer"></div>
</body>
</html>
* {
box-sizing: border-box;
font-family: Arial, sans-serif;
text-align: center;
}
body {
display: flex;
flex-flow: row wrap;
justify-content: flex-start;
}
#gridUser, #gridContainer {
flex: 1 1 0%;
}
#gridInput {
text-align: left;
}
// also add functional for yellow color (not randomize) and for randomize every square
let gridContainer = document.querySelector("#gridContainer");
let gridSize = 16*16;
let gridSquares = "";
let mouseSquares = [];
let mouseHoveringColor = "";
getGridPerSide();
function getGridPerSide() {
let gridForm = document.querySelector("#gridForm");
gridForm.addEventListener("submit", (e) => {
e.preventDefault();
let gridInput = document.querySelector("#gridInput");
let gridRadio = document.querySelectorAll(`input[name="gridRadio"]`);
let gridRadioChecked = "";
let gridText = document.querySelector("#gridText");
mouseHoveringColor = "";
for (const gridRadioOne of gridRadio) {
if (gridRadioOne.checked) {
gridRadioChecked = gridRadioOne.value;
break;
}
}
if (isNaN(gridInput.value) || gridInput.value < 1 || gridInput.value > 100) {
gridText.innerHTML = `Enter a <em>right</em> number <strong>from 1 to 100</strong>`;
} else if (gridRadioChecked === "yellow") {
gridText.innerHTML = `
The grid has <em>${gridInput.value}*${gridInput.value}</em> squares<br/>
The mouse hover color is <strong>yellow</strong><br/>
`;
mouseHoveringColor = "yellow"; // value for doMouseHovering()
} else if (gridRadioChecked === "randomize") {
gridText.innerHTML = `
The grid has <em>${gridInput.value}*${gridInput.value}</em> squares<br/>
The mouse hover color is <strong>randomize</strong><br/>
`;
mouseHoveringColor = "randomize"; // value for doMouseHovering()
} else {
gridText.innerHTML = `
The grid has <em>${gridInput.value}*${gridInput.value}</em> squares<br/>
The mouse hover color is <strong>randomize every square</strong><br/>
`;
mouseHoveringColor = "randomizeEverySquare"; // value for doMouseHovering()
}
gridSize = gridInput.value * gridInput.value;
createGrid();
});
}
function createGrid() {
gridContainer.innerHTML = ""; // grid clean for new submit
for (let i=0; i < (gridSize); i++) {
gridSquares = document.createElement("div");
gridSquares.classList.add("gridSquares");
gridContainer.appendChild(gridSquares);
gridSquares.style.cssText = `
width: calc(100% / ${gridInput.value});
height: calc(100% / ${gridInput.value});
background-color: #eee;
border: 1px solid #ccc;
`;
gridContainer.style.cssText = `
height: 97vh;
width: auto;
display: flex;
flex-wrap: wrap;
aspect-ratio: 1/1;
`;
mouseSquares.push(gridSquares); // create array for doMouseHovering()
}
doMouseHovering();
}
function doMouseHovering() {
// need mouseBackgroundColor for calculate "randomize" (NOT for "randomizeEverySquare")
let mouseBackgroundColor = "";
if (mouseHoveringColor === "yellow") {
mouseBackgroundColor = "yellow";
} else if (mouseHoveringColor === "randomize") {
let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
mouseBackgroundColor = `rgb(${r}, ${g}, ${b})`;
}
mouseSquares.forEach(gridSquares => {
gridSquares.addEventListener('mouseover', () => {
if (mouseHoveringColor === "yellow") {
gridSquares.style.backgroundColor = mouseBackgroundColor;
} else if (mouseHoveringColor === "randomize") {
gridSquares.style.backgroundColor = mouseBackgroundColor;
} else {
let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
gridSquares.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}
});
});
}
我尝试使用 CSS 媒体查询和 JavaScript 媒体查询来做到这一点。结果是网格容器的高度大于宽度。但网格容器必须是正方形的。
您只能将 CSS 和 Javascript 与 Canvas Paint API 结合使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Etch-A-Sketch</title>
</head>
<body>
<canvas id="etch-a-sketch"></canvas>
<script>
const canvas = document.getElementById('etch-a-sketch');
const ctx = canvas.getContext('2d');
// Set starting position in the middle of the canvas
let posX = canvas.width / 2;
let posY = canvas.height / 2;
ctx.lineWidth = 2; // Line thickness
ctx.lineCap = 'round'; // Round line ends for smoother appearance
// Draw initial point
ctx.beginPath();
ctx.moveTo(posX, posY);
document.addEventListener('keydown', function(event) {
const step = 10; // Distance moved with each key press
switch (event.key) {
case 'w': // Move up
posY -= step;
break;
case 's': // Move down
posY += step;
break;
case 'a': // Move left
posX -= step;
break;
case 'd': // Move right
posX += step;
break;
default:
return; // Ignore other keys
}
draw(posX, posY);
});
function draw(x, y) {
ctx.lineTo(x, y); // Draw to the new position
ctx.stroke(); // Render the path
}
</script>
</body>
<style>
#etch-a-sketch {
width: 500px;
height: 500px;
border: 1px solid black;
position: relative;
}
</style>
</html>
您甚至可以在开始时使用 document.createElement("canvas") 从 javascript 创建画布。