gpt4 book ai didi

javascript - 普通 JavaScript : How to make cells multiple different colours on a grid with user colour input

转载 作者:行者123 更新时间:2023-12-03 01:10:08 26 4
gpt4 key购买 nike

我正在构建什么

大家好!我正在构建一个 Pixel Art Maker。

它基本上是一个网格,当您单击网格上的每个单元格时,它会改变颜色。

这是一个应该允许在同一 Canvas 上使用不同颜色的应用程序。

问题:

如何使用用户选择的颜色输入在同一网格上允许多种不同的颜色?

例如,我知道如何使其全黑(目前是硬编码的),但如果我想添加更多颜色,我该怎么做?

JS Fiddle 演示

https://jsfiddle.net/JayS5/foznjb2m/

JS代码

const canvas = document.querySelector('#pixelCanvas')

const tbody = document.createElement('tbody');

canvas.addEventListener('click', function(e){
e.target.style.background = 'black';
});

canvas.addEventListener('dblclick', function(e){
e.target.style.backgroundColor = 'white';
});

// Store the value of columns
const column = document.getElementById('column');

// Store the value of rows
const row = document.getElementById('row');

// Access forms
const submitForm = document.querySelector('#submitForm');

submitForm.addEventListener('submit', function(e){
e.preventDefault(); // Prevents the submit button from refreshing the page by default
tbody.innerHTML = "";

// Color picker

const colorPicker = document.createElement('div');
colorPicker.setAttribute("id", "ActiveColor")

colorPicker.innerHTML = '<input type="color" id="head" name="color" value="#e66465"/>';

document.body.appendChild(colorPicker);

// Generate grid

for (r = 0; r < row.value; r++) {
const tr = document.createElement('tr');
tbody.appendChild(tr);
for (c = 0; c < column.value; c++) {
const td = document.createElement('td');
tr.appendChild(td);
}
canvas.appendChild(tbody);
}
});

最佳答案

最好将颜色选择器输入元素放入 HTML 中,这样您就可以在顶层选择它而无需重新分配。提交表单后,使其可见。然后,单击时,只需访问该元素的 value 并将其分配给 background 属性:

const colorInput = document.querySelector('input[type="color"]');
canvas.addEventListener('click', function(e) {
e.target.style.background = colorInput.value;
});

const canvas = document.querySelector('#pixelCanvas')
const tbody = document.createElement('tbody');
const colorInput = document.querySelector('input[type="color"]');
canvas.addEventListener('click', function(e) {
e.target.style.background = colorInput.value;
});

canvas.addEventListener('dblclick', function(e) {
e.target.style.backgroundColor = 'white';
});


const column = document.getElementById('column');
const row = document.getElementById('row');
const submitForm = document.querySelector('#submitForm');

submitForm.addEventListener('submit', function(e) {
e.preventDefault(); // Prevents the submit button from refreshing the page by default
colorInput.style.display = 'inline';
tbody.innerHTML = "";
// Generate grid

for (r = 0; r < row.value; r++) {
const tr = document.createElement('tr');
tbody.appendChild(tr);
for (c = 0; c < column.value; c++) {
const td = document.createElement('td');
tr.appendChild(td);
}
canvas.appendChild(tbody);
}
});
/**
* index.scss
* - Add any styles you want here!
*/

body {
background: #f5f5f5;
}

table,
thead,
tbody,
tfoot,
tr,
td {
border-collapse: collapse;
border: 3px solid black;
}

td {
width: 30px;
}

tr {
height: 30px;
}
input[type="color"] {
display: none;
}
<h1>
Pixel Art Maker</h1>

<fieldset>
<legend>Grid Size</legend>

<form id='submitForm'>

<label for="height">Columns:</label>
<input type="number" id="column" placeholder="Key in an integer" step="1" />

<label for="width">Rows:</label>
<input type="number" id="row" placeholder="Min: 1, max: 100" min="1" max="100" />

<div>
<input type="submit" id="submit" value="Submit" />
</div>
</form>
</fieldset>

<br>

<br>

<div>
<table id="pixelCanvas">
<!-- Dynamic pixel canvas -->
</table>
</div>
<div>
<input type="color" value="#e66465" />
</div>

关于javascript - 普通 JavaScript : How to make cells multiple different colours on a grid with user colour input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52231974/

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