网格JavaScript的颜色正方形
我正在尝试以视觉上实现高斯公式,因此我在JavaScript上遇到了一些问题,因为我以前从未使用过它。我的问题是如何为网格的某些平方涂上
{
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;
}
,这是计算网格的函数
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}`;
}
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. 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}`;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要创建可以填充的固体矩形。
您的脚本会生成一系列线条 - 因此您没有任何表面积。
此外,当前所有行被定义为单个
&lt; path&gt;
元素中的串联命令。这对于优化文件非常好 - 但是您将无法选择各个子路径(网格线)。
示例1:3x3网格更换填充颜色的SVG标记
示例2:动态创建网格SVG。按课程设置填充颜色
gridpaths()
函数通过列/单元格的总量循环。它还根据当前行和列索引添加类名。这样,您可以在CSS或JS中选择单个列和行。
所有路径均以用于造型的
&lt; g&gt;
元素中的分组 - 您还可以单独使用CSS中的每一列样式。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.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
Example 2: Create grid svg dynamically. Set fill colors by class
The
gridPaths()
function loops through the total amount of columns/cells. 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.
All paths are grouped in a
<g>
element for styling – you could also style each column in css alone.