我正在用d3.js做一些关于缩放的测试.目前,我已经在我的测试中成功实现了几何缩放,但它有一个缺点:缩放下的元素g
正在缩放.据我了解,这可以通过使用语义缩放来解决.
问题是我需要scale
在我的测试中,因为我正在将它与jQuery.UI滑块同步value
.
另一方面,我希望text
调整元素大小以在缩放操作后保持其大小.
我有我目前尝试的例子在这里.
我无法更改代码以适应此目的.任何人都可以分享一些见解/想法吗?
对于您的解决方案,我合并了两个示例:
语义缩放
程序化缩放
代码片段:
function zoom() { text.attr("transform", transform); var scale = zoombehavior.scale(); //to make the scale rounded to 2 decimal digits scale = Math.round(scale * 100) / 100; //setting the slider to the new value $("#slider").slider( "option", "value", scale ); //setting the slider text to the new value $("#scale").val(scale); } //note here we are not handling the scale as its Semantic Zoom function transform(d) { //translate string return "translate(" + x(d[0]) + "," + y(d[1]) + ")"; } function interpolateZoom(translate, scale) { zoombehavior .scale(scale)//we are setting this zoom only for detecting the scale for slider..we are not zoooming using scale. .translate(translate); zoom(); } var slider = $(function() { $("#slider").slider({ value: zoombehavior.scaleExtent()[0],//setting the value min: zoombehavior.scaleExtent()[0],//setting the min value max: zoombehavior.scaleExtent()[1],//settinng the ax value step: 0.01, slide: function(event, ui) { var newValue = ui.value; var center = [centerX, centerY], extent = zoombehavior.scaleExtent(), translate = zoombehavior.translate(), l = [], view = { x: translate[0], y: translate[1], k: zoombehavior.scale() }; //translate w.r.t the center translate0 = [(center[0] - view.x) / view.k, (center[1] - view.y) / view.k]; view.k = newValue;//the scale as per the slider //the translate after the scale(so we are multiplying the translate) l = [translate0[0] * view.k + view.x, translate0[1] * view.k + view.y]; view.x += center[0] - l[0]; view.y += center[1] - l[1]; interpolateZoom([view.x, view.y], view.k); } }); });
我正在缩放250,250,这是剪辑圈的中心.
这里的工作代码(添加了必要的评论)
希望这可以帮助!