flow-animation.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. * wuxing-sandbox-helpers/flow-animation.js
  3. * 五行能量流动粒子动画
  4. *
  5. * 相生循环:沿外圆路径,能量依次转化(土->金->水->木->火)
  6. * 身(土)->智(金)->富(水)->行(木)->心(火)->身(土)
  7. * 路径:outer[i] -> outer[(i+1)%5] (弧线)
  8. * 颜色随路径从源维度过渡到目标维度
  9. *
  10. * 相克循环:沿五角星边路径,能量抑制
  11. * 身(土)->富(水), 富(水)->心(火), 心(火)->智(金), 智(金)->行(木), 行(木)->身(土)
  12. * 路径:outer[i] -> inner[(i+1)%5] -> outer[(i+2)%5] (星边直线)
  13. * 粒子保持源维度颜色
  14. */
  15. /**
  16. * 粒子颜色:与 render.js 同一套原始五行亮色
  17. * 深色容器上高对比可见
  18. */
  19. var WUXING_COLORS = [
  20. { r: 141, g: 110, b: 99 }, // 0 土·身 (body) #8D6E63
  21. { r: 255, g: 107, b: 53 }, // 1 火·心 (mind) #FF6B35
  22. { r: 255, g: 215, b: 0 }, // 2 金·智 (wisdom) #FFD700
  23. { r: 76, g: 175, b: 80 }, // 3 木·行 (action) #4CAF50
  24. { r: 66, g: 165, b: 245 } // 4 水·富 (wealth) #42A5F5
  25. ]
  26. var DIM_KEYS = ['body', 'mind', 'wisdom', 'action', 'wealth']
  27. /**
  28. * 维度代码 -> 颜色
  29. */
  30. function colorForDimCode(code) {
  31. var idx = DIM_KEYS.indexOf(code)
  32. if (idx < 0) return { r: 200, g: 200, b: 200 }
  33. return WUXING_COLORS[idx]
  34. }
  35. /**
  36. * 计算两点间的弧线路径(圆心(cx,cy),顺时针方向)
  37. * @param {{x:number,y:number}} src
  38. * @param {{x:number,y:number}} dst
  39. * @param {number} cx
  40. * @param {number} cy
  41. * @param {number} numSegments - 分段数
  42. * @returns {Array<{x:number,y:number}>}
  43. */
  44. function arcWaypoints(src, dst, cx, cy, numSegments) {
  45. numSegments = numSegments || 8
  46. var angleSrc = Math.atan2(src.y - cy, src.x - cx)
  47. var angleDst = Math.atan2(dst.y - cy, dst.x - cx)
  48. var r = Math.sqrt(Math.pow(src.x - cx, 2) + Math.pow(src.y - cy, 2))
  49. // 顺时针方向角度差
  50. var diff = angleDst - angleSrc
  51. if (diff < 0) diff += Math.PI * 2
  52. var points = []
  53. for (var i = 0; i <= numSegments; i++) {
  54. var t = i / numSegments
  55. var angle = angleSrc + diff * t
  56. points.push({
  57. x: cx + r * Math.cos(angle),
  58. y: cy + r * Math.sin(angle)
  59. })
  60. }
  61. return points
  62. }
  63. /**
  64. * 粒子对象
  65. */
  66. function Particle(path, srcColor, dstColor, speed, baseSize) {
  67. this.path = path // [{x,y},...] 路径控制点
  68. this.srcColor = srcColor // {r,g,b}
  69. this.dstColor = dstColor // {r,g,b}(相克 与 srcColor 相同)
  70. this.progress = 0 // 0->1
  71. this.speed = speed
  72. this.baseSize = baseSize || 3
  73. this.x = path[0].x
  74. this.y = path[0].y
  75. this.currentColor = { r: srcColor.r, g: srcColor.g, b: srcColor.b }
  76. this.size = this.baseSize
  77. this.alive = true
  78. }
  79. Particle.prototype.update = function () {
  80. this.progress += this.speed
  81. if (this.progress >= 1) {
  82. this.alive = false
  83. return
  84. }
  85. var t = this.progress
  86. var totalSegs = this.path.length - 1
  87. var segIdx = Math.min(Math.floor(t * totalSegs), totalSegs - 1)
  88. var segFrac = (t * totalSegs) - segIdx
  89. var p0 = this.path[segIdx]
  90. var p1 = this.path[segIdx + 1]
  91. this.x = p0.x + (p1.x - p0.x) * segFrac
  92. this.y = p0.y + (p1.y - p0.y) * segFrac
  93. // 颜色插值
  94. this.currentColor = {
  95. r: Math.round(this.srcColor.r + (this.dstColor.r - this.srcColor.r) * t),
  96. g: Math.round(this.srcColor.g + (this.dstColor.g - this.srcColor.g) * t),
  97. b: Math.round(this.srcColor.b + (this.dstColor.b - this.srcColor.b) * t)
  98. }
  99. // 粒子衰减
  100. this.size = this.baseSize * (1 - t * 0.3)
  101. }
  102. /**
  103. * FlowAnimation - 五行能量粒子流动画控制器
  104. * 使用独立 Canvas 覆盖层绘制粒子
  105. *
  106. * @param {CanvasRenderingContext2D} ctx
  107. * @param {number} w - Canvas 宽度(逻辑像素)
  108. * @param {number} h - Canvas 高度(逻辑像素)
  109. * @param {Object} opts
  110. * @param {Array<{x:number,y:number}>} opts.outer - calcStarVertices 外顶点数组
  111. * @param {Array<{x:number,y:number}>} opts.inner - calcStarVertices 内顶点数组
  112. * @param {Array<string>} opts.dimOrder - sortedDimensions 的 code 数组
  113. */
  114. export function FlowAnimation(ctx, w, h, opts) {
  115. this._ctx = ctx
  116. this._w = w
  117. this._h = h
  118. this._cx = w / 2
  119. this._cy = h / 2
  120. this._outer = opts.outer
  121. this._inner = opts.inner
  122. this._dimOrder = opts.dimOrder
  123. this._particles = []
  124. this._timer = null
  125. this._running = false
  126. this._frameCount = 0
  127. this._buildShengPaths()
  128. this._buildKePaths()
  129. }
  130. /**
  131. * 构建相生路径(外圆弧线)
  132. * 顶点顺序 0->1->2->3->4->0
  133. */
  134. FlowAnimation.prototype._buildShengPaths = function () {
  135. this._shengPaths = []
  136. for (var i = 0; i < 5; i++) {
  137. var src = this._outer[i]
  138. var dst = this._outer[(i + 1) % 5]
  139. this._shengPaths.push({
  140. waypoints: arcWaypoints(src, dst, this._cx, this._cy, 6),
  141. srcColor: colorForDimCode(this._dimOrder[i]),
  142. dstColor: colorForDimCode(this._dimOrder[(i + 1) % 5])
  143. })
  144. }
  145. }
  146. /**
  147. * 构建相克路径(五角星边)
  148. * 相克索引对:0->2, 2->4, 4->1, 1->3, 3->0
  149. * 路径:outer[s] -> inner[(s+1)%5] -> outer[e]
  150. */
  151. FlowAnimation.prototype._buildKePaths = function () {
  152. this._kePaths = []
  153. var segments = [[0, 2], [2, 4], [4, 1], [1, 3], [3, 0]]
  154. for (var i = 0; i < segments.length; i++) {
  155. var s = segments[i][0]
  156. var e = segments[i][1]
  157. var srcColor = colorForDimCode(this._dimOrder[e])
  158. this._kePaths.push({
  159. waypoints: [
  160. this._outer[e],
  161. this._inner[(s + 1) % 5],
  162. this._outer[s]
  163. ],
  164. srcColor: srcColor,
  165. dstColor: srcColor // 相克粒子保持被克方颜色(能量被吸收)
  166. })
  167. }
  168. }
  169. /**
  170. * 启动连续动画循环
  171. */
  172. FlowAnimation.prototype.start = function () {
  173. if (this._running) return
  174. this._running = true
  175. this._loop()
  176. }
  177. /**
  178. * 停止动画循环
  179. */
  180. FlowAnimation.prototype.stop = function () {
  181. this._running = false
  182. if (this._timer) {
  183. clearTimeout(this._timer)
  184. this._timer = null
  185. }
  186. }
  187. /**
  188. * 刷新路径(当 Canvas 尺寸或维度排序发生变化时调用)
  189. */
  190. FlowAnimation.prototype.refreshPaths = function (opts) {
  191. if (opts) {
  192. if (opts.outer) this._outer = opts.outer
  193. if (opts.inner) this._inner = opts.inner
  194. if (opts.dimOrder) this._dimOrder = opts.dimOrder
  195. if (opts.w) {
  196. this._w = opts.w
  197. this._cx = opts.w / 2
  198. }
  199. if (opts.h) {
  200. this._h = opts.h
  201. this._cy = opts.h / 2
  202. }
  203. }
  204. this._buildShengPaths()
  205. this._buildKePaths()
  206. }
  207. FlowAnimation.prototype._loop = function () {
  208. if (!this._running) return
  209. var self = this
  210. this._frameCount++
  211. // 周期性发射粒子
  212. if (this._frameCount % 3 === 0) {
  213. this._emitSheng()
  214. }
  215. if (this._frameCount % 5 === 0) {
  216. this._emitKe()
  217. }
  218. // 更新所有粒子
  219. for (var i = this._particles.length - 1; i >= 0; i--) {
  220. this._particles[i].update()
  221. if (!this._particles[i].alive) {
  222. this._particles.splice(i, 1)
  223. }
  224. }
  225. // 绘制
  226. this._render()
  227. this._timer = setTimeout(function () {
  228. self._loop()
  229. }, 33) // ~30fps
  230. }
  231. /**
  232. * 发射一个相生粒子(随机选择段)
  233. */
  234. FlowAnimation.prototype._emitSheng = function () {
  235. if (this._shengPaths.length === 0) return
  236. var idx = Math.floor(Math.random() * this._shengPaths.length)
  237. var seg = this._shengPaths[idx]
  238. this._particles.push(new Particle(
  239. seg.waypoints,
  240. seg.srcColor,
  241. seg.dstColor,
  242. 0.012 + Math.random() * 0.01,
  243. 2.5 + Math.random() * 1.5
  244. ))
  245. }
  246. /**
  247. * 发射一个相克粒子(随机选择段)
  248. */
  249. FlowAnimation.prototype._emitKe = function () {
  250. if (this._kePaths.length === 0) return
  251. var idx = Math.floor(Math.random() * this._kePaths.length)
  252. var seg = this._kePaths[idx]
  253. this._particles.push(new Particle(
  254. seg.waypoints,
  255. seg.srcColor,
  256. seg.srcColor,
  257. 0.01 + Math.random() * 0.008,
  258. 3 + Math.random() * 1.5
  259. ))
  260. }
  261. /**
  262. * 渲染所有粒子到 Canvas
  263. */
  264. FlowAnimation.prototype._render = function () {
  265. var ctx = this._ctx
  266. ctx.clearRect(0, 0, this._w, this._h)
  267. for (var i = 0; i < this._particles.length; i++) {
  268. var p = this._particles[i]
  269. var c = p.currentColor
  270. var alpha = 1 - p.progress * 0.25
  271. var r = Math.max(p.size, 1.5)
  272. // 外层辉光
  273. ctx.beginPath()
  274. ctx.arc(p.x, p.y, r * 3.5, 0, Math.PI * 2)
  275. ctx.closePath()
  276. ctx.fillStyle = 'rgba(' + c.r + ',' + c.g + ',' + c.b + ',' + (alpha * 0.1) + ')'
  277. ctx.fill()
  278. // 内层辉光
  279. ctx.beginPath()
  280. ctx.arc(p.x, p.y, r * 1.8, 0, Math.PI * 2)
  281. ctx.closePath()
  282. ctx.fillStyle = 'rgba(' + c.r + ',' + c.g + ',' + c.b + ',' + (alpha * 0.35) + ')'
  283. ctx.fill()
  284. // 核心圆点
  285. ctx.beginPath()
  286. ctx.arc(p.x, p.y, r, 0, Math.PI * 2)
  287. ctx.closePath()
  288. ctx.fillStyle = 'rgba(' + c.r + ',' + c.g + ',' + c.b + ',' + alpha + ')'
  289. ctx.fill()
  290. }
  291. }