[英]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.