D3 动态效果

transition()

.attr("fill","red")         //初始颜色为红色
.transition()               //启动过渡
.attr("fill","steelblue")   //终止颜色为铁蓝色

duration()
指定过度时间,单位毫秒

delay()
延迟

 duration(2000)

ease()
过度方式,”linear”,”circle”,”elastic”,”bounce”

实例

<html>
    <body>
        <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
        <script>
        var svg =d3.select("body").append("svg")
            .attr("width",400)
            .attr("height",400)
        var circle = svg.append("circle")
        .attr("cx", 50)  //初始位置
        .attr("cy", 50)
        .attr("r", 50)  //初始半径
        .style("fill","green");

        //将颜色从绿色变为红色
        //将半径从45变成25
        //过渡方式采用bounce(在终点处弹跳几次)
        circle.transition()
            .duration(2000)  //变化时间是2s
            .ease("bounce")  //变换之后跳动
            .attr("cx", 300) //变换之后移动到x坐标为300
            .style("fill","red") //变换之后为红色
            .attr("r", 20);  //变换之后的半径
        </script>
    </body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue和D3是两个不同的库,Vue主要用于构建用户界面,而D3是用于数据可视化的库。所以,我们可以利用Vue来构建一个界面,然后使用D3来实现动态时间的效果。 以下是一个简单的示例代码: ``` <template> <div ref="clock"></div> </template> <script> import * as d3 from 'd3' export default { mounted() { const svg = d3.select(this.$refs.clock) .append('svg') .attr('width', 200) .attr('height', 200) const circle = svg.append('circle') .attr('cx', 100) .attr('cy', 100) .attr('r', 50) .attr('fill', 'none') .attr('stroke', '#000000') const hourHand = svg.append('line') .attr('x1', 100) .attr('y1', 100) .attr('x2', 100) .attr('y2', 50) .attr('stroke', '#000000') .attr('stroke-width', 5) const minuteHand = svg.append('line') .attr('x1', 100) .attr('y1', 100) .attr('x2', 100) .attr('y2', 30) .attr('stroke', '#000000') .attr('stroke-width', 3) const secondHand = svg.append('line') .attr('x1', 100) .attr('y1', 100) .attr('x2', 100) .attr('y2', 20) .attr('stroke', '#ff0000') .attr('stroke-width', 1) const updateClock = () => { const now = new Date() const hour = now.getHours() const minute = now.getMinutes() const second = now.getSeconds() const hourAngle = (hour % 12 + minute / 60) * 30 const minuteAngle = minute * 6 const secondAngle = second * 6 hourHand.attr('transform', `rotate(${hourAngle}, 100, 100)`) minuteHand.attr('transform', `rotate(${minuteAngle}, 100, 100)`) secondHand.attr('transform', `rotate(${secondAngle}, 100, 100)`) setTimeout(updateClock, 1000) } updateClock() } } </script> <style scoped> svg { border: 1px solid #000000; } </style> ``` 在这个示例中,我们使用D3来创建一个SVG画布,并添加一个圆形和三个线条,分别代表时针、分针和秒针。然后,在`updateClock`函数中,我们使用`Date`对象来获取当前时间,并计算出每个针的角度,然后使用D3的`transform`方法来旋转每个针的位置。最后,我们使用`setTimeout`函数来每秒钟更新一次时间。 在Vue中,我们可以将这段代码放在一个组件中,并在`mounted`钩子函数中调用`updateClock`函数来启动动态时间的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值