简体   繁体   English

网格 JavaScript 的颜色方块

[英]Color squares of a grid JavaScript

I am trying to implement the Gauss Formula Visually so I am having a little bit of problems with JavaScript since I never worked with it before.我正在尝试以可视方式实现高斯公式,所以我在使用 JavaScript 时遇到了一些问题,因为我以前从未使用过它。 My question would be how do I color certain squares of my grid我的问题是如何为网格的某些正方形着色

在此处输入图像描述

    {
  const w = width, h = w * .6, mx = w/2 * margin, my = h/2 * margin;
  const s = DOM.svg(w, h);
  const path = `M${mx},${my}${gridPath(count+1, count, w - mx * 2, h - my * 2, false)}`;
  s.appendChild(svg`<path stroke=#555 d="${path}">`);
  s[0].sty
  return s;
}

and this is the function that computes the grid这是计算网格的函数

    function gridPath(cols, rows, width = 1, height = 1, initPosition = true) {
  // Line distances.
  const sx = width / cols, sy = height / rows;
  // Horizontal and vertical path segments, joined by relative move commands.
  const px = Array(rows+1).fill(`h${width}`).join(`m${-width},${sy}`);
  const py = Array(cols+1).fill(`v${height}`).join(`m${sx},${-height}`);
  
  // Paths require an initial move command. It can be set either by this function
  // or appended to the returned path.
  return `${initPosition ? 'M0,0' : ''}${px}m${-width}${-height}${py}`;

}

You need to create solid rectangles that could be filled.您需要创建可以填充的实心矩形。
Your script generates an array of lines – so you don't get any surface area.您的脚本会生成一系列线条——因此您不会得到任何表面积。
Besides, all lines are currently defined as concatenated commands in a single <path> element.此外,当前所有行都定义为单个<path>元素中的串联命令。

That's great for optimizing filesize – but you won't be able to select individual sub paths (grid lines).这对于优化文件大小非常有用——但您将无法选择单独的子路径(网格线)。

Example 1: Svg markup for a 3x3 grid changing fill color on hover示例 1:3x3 网格的 Svg 标记在悬停时更改填充颜色

 svg { display: block; overflow: visible; width: 75vmin } path { fill: #fff; stroke-width: 1; stroke: #000 } path:hover { fill: red }
 <svg viewBox="0 0 90 90"> <!-- 1. row --> <path d="M 0 0 h30 v30 h-30z" /> <path d="M 30 0 h30 v30 h-30z" /> <path d="M 60 0 h30 v30 h-30z" /> <!-- 2. row --> <path d="M 0 30 h30 v30 h-30z" /> <path d="M 30 30 h30 v30 h-30z" /> <path d="M 60 30 h30 v30 h-30z" /> <!-- 3. row --> <path d="M 0 60 h30 v30 h-30z" /> <path d="M 30 60 h30 v30 h-30z" /> <path d="M 60 60 h30 v30 h-30z" /> </svg>

Example 2: Create grid svg dynamically.示例 2:动态创建网格 svg。 Set fill colors by class按类设置填充颜色

 let svg = document.querySelector('svg'); // generate grid svg let gridSVG1 = gridPaths(4, 4, 100, 100, 0.25, '#ccc', true); // highlight columns highlightColumns(gridSVG1, ['1-2', '2-3', '3-4']); function highlightColumns(svg, col_array) { col_array.forEach(function(colIndex, i) { // select let currentCol = svg.querySelector('.col-' + colIndex); currentCol.classList.add('highlight'); }) } function gridPaths(cols, rows, width = 1, height = 1, strokeWidth = 1, strokeColor = '#000', render = false) { let gridSvg = `<g fill="#fff" stroke-width="${strokeWidth}" stroke="${strokeColor}">\n`; let offsetX = width / cols; let offsetY = height / rows; let currentRow = 1; let currentCol = 1; // add initial y/x offset according to stroke width to avoid cropped outer strokes let shiftX = 0; let shiftY = 0; let cellCount = cols * rows; for (let i = 0; i < cellCount; i++) { // if current index is divisible by columns – move to next row if (i > 0 && i % (cols) === 0) { shiftX = 0; shiftY += offsetY; currentCol = 1; currentRow++; } // add classnames for rows and columns to make each cell selectable let className = `col row-${currentRow} col-${currentCol} col-${currentRow}-${currentCol}`; // add new cell to output gridSvg += `<path class="${className}" d="M ${shiftX} ${shiftY} h${offsetX} v${offsetY} h${-offsetX}z"/>\n`; // increment x offset for next column shiftX += offsetX; currentCol++; } //close group gridSvg += '\n</g>'; // create new svg let nameSpace = 'http://www.w3.org/2000/svg'; let xlink = 'http://www.w3.org/1999/xlink'; let gridSvgEl = document.createElementNS(nameSpace, 'svg'); gridSvgEl.setAttribute('xmlns', nameSpace); gridSvgEl.setAttribute('xmlns:xlink', xlink); gridSvgEl.setAttribute('overflow', 'visible'); // add stroke width to viewBox to avoid cropped outer strokes gridSvgEl.setAttribute('viewBox', [0, 0, width, height].join(' ')); gridSvgEl.innerHTML = gridSvg; // optional: render grid if (render) { document.body.appendChild(gridSvgEl); } return gridSvgEl; }
 svg { display: block; overflow: visible; width: 75vmin } .row-3 { fill: #eee } .col-2 { fill: #fee } .col:hover, .highlight { fill: lime }

The gridPaths() function loops through the total amount of columns/cells. gridPaths()函数循环遍历列/单元格的总数。 It also adds class names according to the current row and column index.它还根据当前的行和列索引添加类名。
This way you can select individual columns and rows in css or js.这样,您可以在 css 或 js 中选择单个列和行。
All paths are grouped in a <g> element for styling – you could also style each column in css alone.所有路径都分组在一个<g>元素中以进行样式设置——您也可以单独在 css 中设置每一列的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM