You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一事件触发不同动画及d3.ease('cubic')使用问题咨询

解决你的D3.js炸弹动画问题

Got it, let's break down your two issues and fix them step by step—both are common animation pitfalls, so you'll have your bomber game running smoothly in no time!

问题1:多枚炸弹爆炸动画同步触发

问题原因

Right now, all your explosion animations are probably tied to a shared timeline or starting point (like the moment the first bomb was dropped), instead of being triggered individually when each bomb finishes falling. That's why they all blow up at the same time.

解决方案:每枚炸弹单独触发爆炸动画

The simplest fix is to trigger the explosion only after each bomb finishes its fall animation. You can do this using D3's on("end") callback on the transition. This way, each bomb's explosion starts exactly when it hits the ground, no overlap or sync issues.

Here's how to adjust your code:

// 假设planeX和planeY是飞机当前的坐标,width/height是SVG的尺寸
let svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height"),
    planeX = width / 2, // 示例飞机X坐标
    planeY = 50; // 示例飞机Y坐标

// 点击投放炸弹
svg.on("click", function() {
  // 创建新炸弹元素
  const bomb = svg.append("circle")
    .attr("cx", planeX)
    .attr("cy", planeY)
    .attr("r", 6)
    .style("fill", "#333");

  // 下落动画:完成后触发爆炸
  bomb.transition()
    .duration(1800) // 下落时长,可调整
    .ease(d3.easeCubicOut) // 先快后慢,模拟重力
    .attr("cy", height - 10) // 落到接近底部的位置
    .on("end", function() {
      // 调用爆炸动画函数,传入当前炸弹
      triggerExplosion(d3.select(this));
    });
});

// 单独的爆炸动画函数
function triggerExplosion(bomb) {
  const bombX = +bomb.attr("cx"),
        bombY = +bomb.attr("cy");

  // 创建爆炸效果(这里用一个扩散的半透明圆示例)
  const explosion = svg.append("circle")
    .attr("cx", bombX)
    .attr("cy", bombY)
    .attr("r", 0)
    .style("fill", "#ff6b35")
    .style("opacity", 0.8);

  // 爆炸动画:扩散+淡出,结束后移除元素
  explosion.transition()
    .duration(600)
    .ease(d3.easeCubicIn) // 先慢后快,模拟爆炸冲击力
    .attr("r", 40)
    .style("opacity", 0)
    .on("end", function() {
      d3.select(this).remove();
    });

  // 移除原炸弹元素
  bomb.remove();
}

This approach ensures each explosion is tied directly to its own bomb's fall completion—no more synchronized blasts!

问题2:ease('cubic') 报错

问题原因

This error happens because D3 v4+ changed how ease functions work. Older versions (v3 and below) allowed string values like 'cubic', but modern D3 requires you to pass a reference to the actual ease function instead.

正确使用技巧

For D3 v4 and later, use the built-in d3.ease* functions directly in the ease() method:

  • Basic cubic ease: d3.easeCubic
  • Cubic ease-in (starts slow, speeds up): d3.easeCubicIn
  • Cubic ease-out (starts fast, slows down): d3.easeCubicOut
  • Cubic ease-in-out (slow start/end, fast middle): d3.easeCubicInOut

Example of correct usage in your transition:

// 下落动画用easeCubicOut模拟重力
bomb.transition()
  .duration(1800)
  .ease(d3.easeCubicOut) // ✅ 正确写法
  .attr("cy", height - 10);

If you're stuck on an older D3 version (not recommended, since it's unmaintained), the string syntax would work, but upgrading to the latest version is better for security and feature support.


内容的提问来源于stack exchange,提问作者claudio gugliotta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:27:06