gpt4 book ai didi

javascript - 如何修复我的 Pixel Art Maker 中黑色的彩色单元格?

转载 作者:行者123 更新时间:2023-11-27 23:21:12 26 4
gpt4 key购买 nike

我正在为我的研究开发一个像素艺术制作工具,但我还有一个问题。

我无法弄清楚我的代码有什么问题,即使使用颜色选择器选择了一种颜色,单元格也会变成黑色。我错过了什么 ?我几乎是 JS 的新手。

// Select color
var chooseColor = document.getElementById('colorPicker').value;
// Select size
var size = document.getElementById('sizePicker');
var height = document.getElementById('inputHeight').value;
var width = document.getElementById('inputWidth').value;



var canvas = document.getElementById("pixelCanvas");

size.addEventListener('submit', function(event) {
event.preventDefault()
var height = document.getElementById('inputHeight').value;
var width = document.getElementById('inputWidth').value;
makeGrid(height, width);
});



function makeGrid(height, width) {
var height = document.getElementById('inputHeight').value;
var width = document.getElementById('inputWidth').value;
canvas.innerHTML = null;
for (var m = 0; m < height; m++) {
var row = canvas.insertRow(m);
for (var n = 0; n < width; n++) {
var column = row.insertCell(n);
column.addEventListener('click', function(e) {
var color = chooseColor;
e.target.style.backgroundColor = color;
})
}
}
};
body {
text-align: center;
background-color: rgba(203, 224, 236, 0.2);
font-family: Baskerville;
}

h1 {
font-size: 60px;
margin: 0.2em;
border-style: outset;
border-color: rgb(42, 42, 143);
}

h2 {
margin: 1em 0 0.25em;
}

h2:first-of-type {
margin-top: 0.5em;
padding: 35px;
}

table,
tr,
td {
border: 1px solid black;
}

table {
border-collapse: collapse;
margin: 0 auto;
cursor: pointer;
}

tr {
height: 25px;
}

td {
width: 25px;
}

input[type=number] {
width: 6em;
}

input[type=submit]:active {
background-color: rgba(41, 28, 185, 0.5);
color: #000;
}
<link href="https://fonts.googleapis.com/css?family=Baskerville" rel="stylesheet" />

<body>
<h1>Pixel Art Maker</h1>

<h2>Choose Grid Size</h2>
<form id="sizePicker" onsubmit="makeGrid(event)">
Grid Height:
<input type="number" id="inputHeight" name="height" min="1" value="1"> Grid Width:
<input type="number" id="inputWidth" name="width" min="1" value="1">
<input type="submit" value="submit/erase">
</form>

<h2>Pick Your Color</h2>
<input type="color" id="colorPicker">

<h2>Design Canvas</h2>
<table id="pixelCanvas"></table>
</body>

我没有收到任何错误消息,只有黑色的单元格。预先感谢您的帮助/建议:)

最佳答案

您“在运行时”在代码之上获取输入的值,因此仅使用初始值。

var chooseColor = document.getElementById('colorPicker').value;

您只需将它移动到您的函数 makeGrid 中。

jsfiddle

关于javascript - 如何修复我的 Pixel Art Maker 中黑色的彩色单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57982998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com