javascript - 在 JS 中创建网格艺术
问题描述
我正在尝试创建一个可点击的网格。
When the user clicks a grid
Then fill that box with a chosen color
And continue filling cells
But if user clicks mouse
Then stop filling cells
When the user inputs grid dimensions
Then create the grid cells
我有基本的网格结构,但它出现然后消失。
function makeGrid() {
var heights = document.getElementById("inputHeight").value;
var widths = document.getElementById("inputWidth").value;
for (var i=0; i<heights; i++){
for (var j=0; j<heights; j++){
var x = document.createElement("CANVAS");
var ctx = x.getContext("2d");
//stroke(0);
ctx.fillStyle = "white";
ctx.fillRect(63*heights, 63*widths, 63, 63);
document.body.appendChild(x);
}
}
}
body {
text-align: center;
}
h1 {
font-family: Monoton;
font-size: 70px;
margin: 0.2em;
}
h2 {
margin: 1em 0 0.25em;
}
h2:first-of-type {
margin-top: 0.5em;
}
table,
tr,
td {
border: 1px solid black;
}
table {
border-collapse: collapse;
margin: 0 auto;
}
tr {
height: 20px;
}
td {
width: 20px;
}
input[type=number] {
width: 6em;
}
<!DOCTYPE html>
<html>
<head>
<style>
canvas {
border: 1px solid black;
}
</style>
<title>Clickable Grid!</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Monoton">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Clickable Grid</h1>
<h2>Choose Grid Size</h2>
<form id="sizePicker">
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" onClick = "makeGrid()">
</form>
<h2>Pick A Color</h2>
<input type="color" id="colorPicker">
<h2>Design Canvas</h2>
<table id="pixelCanvas"></table>
<script src="designs.js"></script>
</body>
</html>
解决方案
提交事件正在触发刷新。您需要利用该submit
事件并阻止其默认操作。
此外,对于网格宽度 in ,您有一个错误,makeGrid()
因为您对两个循环都使用height
了 value。网格未按您的意愿显示,因为逻辑需要修改(超出此问题的范围):
function makeGrid(ev) {
ev.preventDefault();
var heights = document.getElementById("inputHeight").value;
var widths = document.getElementById("inputWidth").value;
for (var i=0; i<heights; i++){
for (var j=0; j<widths; j++){
var x = document.createElement("CANVAS");
var ctx = x.getContext("2d");
//stroke(0);
ctx.fillStyle = "white";
ctx.fillRect(63*heights, 63*widths, 63, 63);
document.body.appendChild(x);
}
}
}
body {
text-align: center;
}
h1 {
font-family: Monoton;
font-size: 70px;
margin: 0.2em;
}
h2 {
margin: 1em 0 0.25em;
}
h2:first-of-type {
margin-top: 0.5em;
}
table,
tr,
td {
border: 1px solid black;
}
table {
border-collapse: collapse;
margin: 0 auto;
}
tr {
height: 20px;
}
td {
width: 20px;
}
input[type=number] {
width: 6em;
}
<!DOCTYPE html>
<html>
<head>
<style>
canvas {
border: 1px solid black;
}
</style>
<title>Clickable Grid!</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Monoton">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Clickable Grid</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">
</form>
<h2>Pick A Color</h2>
<input type="color" id="colorPicker">
<h2>Design Canvas</h2>
<table id="pixelCanvas"></table>
<script src="designs.js"></script>
</body>
</html>
推荐阅读
- vb.net - VB.NET 计时器在第一次计时后停止工作
- c# - 无法启动多个 Kafka 消费者
- css - 为什么宽度为百分比的 div 在 localhost 和 ElasticBeanstalk 环境中显示为不同的长度?
- c++ - 是否可以创建一个 C++ 库,该库可以部署在使用 Core Location(或等效项)的 iOS/Android/HoloLens 上?
- python - Python 3.9 - 尝试通过 PIP 安装模块 -> 无法为使用 PEP 517 且无法直接安装的标记器构建轮子
- schema - 是的嵌套对象枚举验证
- python - Python3 代码在 Jupyter 和 iPython 中工作,但当我从命令行将其作为脚本运行时失败
- python - 我需要选择以大写字母开头并可能以小写字母结尾的字符串
- python-3.x - az keyvault 命令突然失败,并显示“错误:APIVersion 2020-04-01-preview 不可用 Traceback”
- delphi - Delphi 可以从 .NET Standard 类库中调用函数吗?