Vanilla Javascript:如何通过用户颜色输入使网格上的单元格具有多种不同的颜色

时间:2018-09-08 04:20:33

标签: javascript colors color-picker

我在做什么

大家好!我正在建立Pixel Art Maker。

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

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

问题:

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

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

JS小提琴演示

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);  
  }
});

2 个答案:

答案 0 :(得分:1)

最好在开始时将颜色选择器输入元素放入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>

答案 1 :(得分:0)

您可以添加事件onchange并将颜色写入全局可变颜色

  colorPicker.onchange = function (e) {
    color = e.target.value
  }

Full code