gpt4 book ai didi

javascript - 如何将表格用作绘图网格

转载 作者:行者123 更新时间:2023-11-30 16:36:07 25 4
gpt4 key购买 nike

我正在尝试创建一个包含约 10,000 个单元格的表格,并将其变成一个 Canvas ,用户可以在该 Canvas 上使用鼠标移动进行绘制。

我希望能够在按住某些键(蓝色 = ctrl、红色 = shift 等)时突出显示鼠标移过的单元格的背景来在 Canvas 上绘图。

我已经生成了我的 HTML 代码,但我在处理表格时遇到了问题。它似乎试图选择表格单元格而不是在单元格中着色。

这是我正在谈论的内容的屏幕截图:

HTML:

<html>
<head>
<meta charset="utf-8">
<title>Drawing Program</title>
<h1>Drawing Demonstration</h1>
<link rel = "stylesheet" type = "text/css" href = "style.css">
<script src = "draw.js"></script>
</head>
<body>
<table id = "canvas">
<caption>Hold Ctrl (or Control) to draw in blue.
Hold Shift to draw in red.</caption>
<tbody id = "tablebody"></tbody>
</table>
</body>
</html>

CSS:

table{
width: 400px;
height: 400px;
border-collapse: collapse;
}

td {
width: 4px;
height: 4px;
border-collapse: collapse;
}

JavaScript:

function createCanvas()
{
var side = 100;
var tbody = document.getElementById( "tablebody" );

for( var i = 0; i < side; i++)
{
var row = document.createElement( "tr" );

for( var j = 0; j < side; j++)
{
var cell = document.createElement( "td" );
row.appendChild( cell );
}
tbody.appendChild( row );
}
document.getElementById( "canvas" ).addEventListener( "mousemove", processMouseMove, false );
}

function processMouseMove( e )
{
if( e.target.tagName.toLowerCase() == "td" )
{
if( e.ctrlKey )
{
e.target.setAttribute( "class", "blue" );
}
if ( e.shiftKey )
{
e.target.setAttribute( "class", "red" );
}
}
}
window.addEventListener( "load", createCanvas, false );

最佳答案

与其制作一万个表格单元格,我建议您制作一个 canvas 元素并用表格单元格大小的像素绘制它。您可以使用模运算将鼠标坐标转换为像素位置。

例如,如果鼠标位于 (x, y) 并且每个像素的大小为 4,则鼠标位于像素上方时:

  • 行 = x - x % 4

  • 列 = y - y % 4

以下代码段演示了这种方法。运行该代码段时,您必须在包含 Canvas 的框架内单击才能将鼠标焦点置于框架上。

var Paint = {
pixel: { size: 4 },
grid: { numRows: 100, numCols: 100 }
};

window.onload = function () {
var canvas = document.getElementById('paintCanvas'),
context = canvas.getContext('2d'),
offset = getOffset(canvas, document.body),
pixelSize = Paint.pixel.size,
numRows = Paint.grid.numRows,
numCols = Paint.grid.numCols,
painting = false;

canvas.width = numCols * pixelSize;
canvas.height = numRows * pixelSize;

window.onkeydown = function (event) {
var code = event.which;
if (code == 17 || code == 16) {
painting = true;
context.fillStyle = (code == 17 ? '#1b6bb5' : '#b53a31');
}
};

window.onkeyup = function (event) {
var code = event.which;
if (code == 17 || code == 16) {
painting = false;
}
};

canvas.onmousemove = function (event) {
if (!painting) {
return;
}
event = event || window.event;
var mouse = getMousePosition(event),
x = mouse.x - offset.left,
y = mouse.y - offset.top;
x -= x % pixelSize;
y -= y % pixelSize;
context.fillRect(x, y, pixelSize, pixelSize);
};
};

function getOffset(element, ancestor) {
var left = 0,
top = 0;
while (element != ancestor) {
left += element.offsetLeft;
top += element.offsetTop;
element = element.parentNode;
}
return { left: left, top: top };
}

function getMousePosition(event) {
if (event.pageX !== undefined) {
return { x: event.pageX, y: event.pageY };
}
return {
x: event.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft,
y: event.clientY + document.body.scrollTop +
document.documentElement.scrollTop
};
}
body {
font-family: sans-serif;
}

canvas {
border: 2px solid #ccc;
}
<p> <b>Click here to start.</b> Hold Ctrl to draw in blue, Shift to draw in red. </p>

<canvas id="paintCanvas"></canvas>

关于javascript - 如何将表格用作绘图网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32684648/

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