Javascript 如何在Raphael中创建集合数组?
下面的代码将绘制48个正方形,其中包含数字0到47 我在stackoverflow上读到,使用集合是最好的方法,因为我将矩形形状与其文本编号组合在一起,这样我就可以用Javascript 如何在Raphael中创建集合数组?,javascript,jquery,arrays,raphael,Javascript,Jquery,Arrays,Raphael,下面的代码将绘制48个正方形,其中包含数字0到47 我在stackoverflow上读到,使用集合是最好的方法,因为我将矩形形状与其文本编号组合在一起,这样我就可以用location 我有很多位置,所以我想把它们放在名为位置的数组中 因此,locations[]数组是一个包含数字的矩形列表(它们本身就是集合) window.onload = function() { var paper = new Raphael(document.getElementById('canvas_contain
location
我有很多位置,所以我想把它们放在名为位置的数组中
因此,locations[]
数组是一个包含数字的矩形列表(它们本身就是集合)
window.onload = function() {
var paper = new Raphael(document.getElementById('canvas_container'), 1200, 1000);
var locations = [];
var location = paper.set();
//squares have same width and height.
var width = 12;
// draw 48 locations
for (i=0; i<48;i++) {
location.push(paper.rect(width*(i+1),10, width, width));
location.push(paper.text(width*(i+1)+(width/2),width+(width/3),i).attr({ "font-size": 8, "font-family": "Arial, Helvetica, sans-serif" }));
locations[i] = location;
location.length = 0; //clears the set
}
//locations[9].translate(Math.random() * 350, Math.random() * 380);
}
window.onload=function(){
var paper=new Raphael(document.getElementById('canvas_container'),12001000);
var位置=[];
var location=paper.set();
//正方形具有相同的宽度和高度。
var宽度=12;
//绘制48个位置
for(i=0;ifor循环中的最后一行未清除集合。您已经构建了位置数组,其中每个项包含2*48个元素(rect和text)。您可以通过console.log(位置[0]);
看到这一点,因为该转换会移动所有内容
重新设置范围,使每个数组项仅包含一对(rect,text):
window.onload=function(){
var纸张=新拉斐尔(帆布容器),1200,1000;
var位置=[];
var location=paper.set();
功能项(元素、文本){
this.elem=elem;
this.text=文本;
}
//正方形具有相同的宽度和高度。
var宽度=12;
var项目;
对于(变量i=0;i<5;i++){
项目=新项目(
纸张矩形(宽度*(i+1),10,宽度,宽度),
纸张.文本(宽度*(i+1)+(宽度/2),宽度+(宽度/3),i).attr({“字体大小”:8,“字体系列”:“Arial,Helvetica,sans serif”})
);
位置[i]=项目;
}
位置=paper.set();
位置.push(位置[3].elem);
location.push(位置[3]。文本);
location.translate(Math.random()*350,Math.random()*380);
}
使用随机选择。for
循环的中的最后一行没有太大帮助。您已经构建了位置
数组,其中每个项目包含2*48个元素(rect和text)。您可以通过控制台.log(位置[0]);
看到这一点,因为转换
可以移动所有内容。
window.onload = function() {
var paper = new Raphael('canvas_container', 1200, 1000);
var locations = [];
var location = paper.set();
function Item(elem, text) {
this.elem = elem;
this.text = text;
}
//squares have same width and height.
var width = 12;
var item;
for (var i = 0; i < 5; i++) {
item = new Item(
paper.rect(width * (i+1), 10, width, width),
paper.text(width * (i+1) + (width/2), width + (width/3), i).attr({ "font-size": 8, "font-family": "Arial, Helvetica, sans-serif" })
);
locations[i] = item;
}
location = paper.set();
location.push(locations[3].elem);
location.push(locations[3].text);
location.translate(Math.random() * 350, Math.random() * 380);
}