如何在拉斐尔中创建一组集合
How to create an array of sets in Raphael?
下面的代码将绘制48个正方形,其中包含数字0到47我在stackoverflow上读到,使用集合是最好的方法,因为我将矩形及其文本编号分组在一起,这样我就可以用location
引用它们我有很多位置,所以我想把它们放在名为locations
的数组中
因此,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);
}
问题出在最后一行。如果我取消注释,所有48个框都将被翻译并一起移动。我只想移动第10个广场
我显然对我的数组以及如何填充它们做了一些错误,但我不知道。
循环中的最后一行未清除集合。您已经构建了位置数组,其中每个项包含2*48个元素(rect和text)。你可以在console.log(locations[0]);
中看到,因为变换会移动所有的东西。
重新排列,使每个数组项只包含一对(rect,text):
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);
}
随机选择演示和更新。
相关文章:
- fluxxor向一个flux实例添加一组以上的操作
- 我们怎样才能将内含子J用于一组特定的元素
- 使用linq.js按当前日期订购一组momentjs日期
- 正则表达式,从一组关键字中获取奇数
- 创建一组元素JointJS
- 如何在javascript中获取一组复选框值
- 一组(共组..)
- 使用JS将页面中的每个字母设置为随机颜色.为什么在页面的每个打开标记之前总是有一组额外的span标记
- 在一组复选框中,一次只允许选择一个
- 为什么在fabricjs中可选择的不适用于一组圆
- 如何在不刷新页面的情况下每次单击一组选项卡时刷新视图内容窗格
- 从一组具有相同起始和结束的所有段落的段落中提取包含特定字符串的段落
- 简单的Javascript文本编辑器或一组函数
- 如何在拉斐尔中创建一组集合
- 捕捉svg拖动一组集合
- d3如何将一组数字转换为代表第一组的较大集合
- 如何仅查找集合中具有一组ID的最新文档
- 主干:如何只对集合的一组模型进行字符串化
- 删除现有集合中的一组值
- 在键值对数组中添加一组集合