let game = newPIXI.Application({ width: 640, height: 320, backgroundColor: 0x333333 }) document.getElementById('preview-box').appendChild(game.view) game.view.style.width = "480px" game.view.style.maxWidth = "100%"
let width = game.renderer.width let height = game.renderer.height
// 舞台分割粒度 let cellSize = 20 // 二维数组 y做为第1维度,x作为第2维度 let team = [] // 列数 let colNum = Math.ceil(width / cellSize) // 行数 let rowNum = Math.ceil(height / cellSize) // 格子数 let boxNum = 0 // 总粒子数 let pNum = 120 // 总运行帧数 可以在达到一定值之后置0 let frame = 0 // 引力影响格子数 R=1 则影响包括自身格子的 9格 (1 + 2R)^2 // 也就是说 认为这些距离的粒子才会对当前粒子产生引力效果 虽然不严谨 但是考虑到计算性能 只能折中考虑 let R = 1
let container = newPIXI.ParticleContainer(pNum) game.stage.addChild(container)
// 预先准备格子 for (let x = 0; x < colNum; x++) { let col = [] for (let y = 0; y < rowNum; y++) { let box = {} boxNum++ col.push(box) } team.push(col) }
// 创建粒子 for (let n = 0; n < pNum; n++) { let px = Math.random() * width let py = Math.random() * height let col = ~~(px / cellSize) let row = ~~(py / cellSize)
// 每个粒子检查周边 (1+2R)^2个 格子 functiongravityCalc(p, range) { let { col: centerCol, row: centerRow } = p; let g = 0.9;
for (let x = 0; x < range * 2 + 1; x++) { for (let y = 0; y < range * 2 + 1; y++) { let testCol = centerCol - range + x, testRow = centerRow - range + y;