简体   繁体   English

如何基于某些功能为SVG中的每个像素分配颜色?

[英]How can I assign every pixel in an svg a color based on some function?

What I'd like to do is color each pixel based on a function of its x and y coordinates, based on relations to other objects on the screen. 我想做的就是根据与屏幕上其他对象的关系,根据像素的x和y坐标为每个像素着色。 I'm using d3 to do all of my other svg work but I can't figure out how to bind each pixel as the data then work off of that. 我正在使用d3来完成所有其他的svg工作,但是我无法弄清楚如何在数据绑定后再处理每个像素。 Not posting code because I am looking for a very general way to do this: for instance, color each point such that (x+y)%73==0 green. 不发布代码,因为我正在寻找一种非常通用的方式来执行此操作:例如,为每个点着色,使(x + y)%73 == 0绿色。

For those interested: looking back I'd recommend canvas over SVG for a task like this, but here's a kind of hacked-together solution using d3: 对于那些感兴趣的人:回想一下,我建议在SVG上使用canvas来完成这样的任务,但这是使用d3的一种共同解决方案:

WIDTH = $(window).width();
HEIGHT = $(window).height();

$(document).ready( function() {
    var svg = d3.select("#svg")
        .attr("width", WIDTH)
        .attr("height", HEIGHT);
    points = []
    for (x=0;x<WIDTH/10;x++) {
        for (y=0;y<HEIGHT/10;y++) {
            points.push([x,y]);
        }
    }
    svg.selectAll("rect")
        .data(points)
      .enter().append("rect")
        .attr("x", function(d) {return d[0];})
        .attr("y", function(d) {return d[1];})
        .attr("width",1)
        .attr("height",1)
        .style("fill",function(d) {
            if ((d[0] + d[1])%73 == 0)
                return "green";
            return "black";
        });
}

This assumes you have an svg element with id=svg in your html body. 假设您的html正文中有一个id = svg的svg元素。 I've only gone through a 1/10 of the way in each dimensino for testing purposes, full size would load 100 times slower, which is pretty slow considering you really shouldn't be using an SVG for this. 为了测试目的,我在每个dimensino中只经历了1/10的方式,完整尺寸的加载速度要慢100倍,考虑到您确实不应该为此使用SVG,这相当慢。

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

相关问题 如何将 SVG 的宽度从百分比转换为像素? - How can I convert the width of SVG from percent to pixel? 我可以根据jquery中的条件分配功能吗? - Can I assign function based on the condition in jquery? 每次我将变量分配给 javascript 中的不同值时,如何将变量传递给 function - How can I pass a variable to a function every time I assign it to a different value in javascript 是否可以根据网页中的背景像素颜色更改图像像素的颜色? - Can I change color of image pixels based on background pixel color in web page? 如何获取javascript中图像的像素颜色值? - How can I get the pixel color value of an image in javascript? 如何在React Native中从屏幕上的像素获取颜色? - How can I get the color from a pixel on the screen in React Native? 如何使用WebGL在画布上设置像素的颜色? - How can I set the color of a pixel on a canvas using WebGL? 如何为 map 函数内的每次迭代分配一个新的 ref? - How can I assign a new ref to every iteration inside a map function? 我如何将id分配给svg元素? (对raphaeljs酒吧) - how i can assign ids to svg elements? (to raphaeljs bars) 如何从逐个像素的颜色阵列中绘制图像? - How can I draw an image from a pixel-by-pixel color array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM