D3.drag():初始化"start"然后用它来拖拽和“;以何种;
d3.drag(): initialize a variable on "start" and use it on "drag" and "end"
我有一个类似下面的代码:
var dragme = d3.drag()
.on("start", function (d) {
var variable1 = // complex calucaltion result;
})
.on("drag", function (d) {
//do something with variable1 without repeating the complex calculation
})
.on("end", function (d) {
//access variable1 again without repeat calculation
});
如果不将variable1
扩展到drag()的上下文中,如何实现这一点?
UPDATE:函数调用如下:
d3.select("#container").call(dragme); //#container is a svg group
你可以这样做:
var dragme = d3.drag()
.on("start", function (d) {
var variable1 = d3.select(this).attr("x");
//do some heavy calculation and store in variable
var calculation = 12354;
//store it in the data model.
d.calculation = calculation;
})
.on("drag", function (d) {
//do something with d.calculation
})
.on("end", function (d) {
//do something with d.calculation
});
由于你没有数据,这是不太好的方法:
var dragme = d3.drag()
.on("start", function (d) {
var variable1 = d3.select(this).attr("x");
//do some heavy calculation and store in variable
var calculation = 12354;
//store it in the data model.
d3.select(this)[0][0].my_data = 1234;
})
.on("drag", function (d) {
var calculation = d3.select(this)[0][0].my_data;
//do something with calculation
})
.on("end", function (d) {
var calculation = d3.select(this)[0][0].my_data;
//do something with calculation
});
相关文章:
- 铬:“;未捕获的语法错误:意外的标记:"
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 使用“+="操作人员
- //而不是在src=“”上使用http://"属性
- "未捕获的语法错误:意外的标记}"
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- "实例范围”;TypeScript类的getter/setter
- Javascript复选框函数:;缺少:在属性id之后"
- "“;变量未引用正确的对象
- "日期“;AJAX请求返回的类型值未定义
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- "工具提示"jQuery插件坏了
- "锻造;React中的表达式
- 图像可以从源<img src=""/>.TEXT可以在没有javascript的情况下从外部
- 如何提取“;href"最近列表项中的属性值
- CKEditor如何允许href="javascript:void(0)"在小部件中
- 插入“;img src"在javascript中