1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| function random(arr) { return arr[Math.floor(Math.random() * arr.length)] }
let scale = 4, num = 500 * scale, strArr = ['Hello', 'World', 'I\'am','pixi.js'], rate = 180, frame = 0, times = 0, joinTimes = 0, color = '#ffffff', shake = { x: 1.4, y: 0.2 }, game = new PIXI.Application({ width: 120 * scale, height: 28 * scale, antialias: true, backgroundColor: 0x333333 });
game.view.style.maxWidth = '100%' document.getElementById('preview-box').appendChild(game.view)
let shape = strArr.map(str => { let s = new PIXI.Text(str, { fontSize: 24 * scale, fontStyle: 'italic', fontWeight: 'bold', letterSpacing: 2 }) s.name = str s.anchor.set(0.5) s.position.set(game.renderer.width / 2, game.renderer.height / 2) return s })
let paticles = new PIXI.ParticleContainer(num); for (let n = 0; n < num; n++) { let d = new PIXI.Sprite(PIXI.Texture.WHITE) d.name = n d.width = 7 d.height = 1 d.anchor.set(0) d.position.set(game.renderer.width * Math.random(), game.renderer.height * Math.random()) d.dx = d.x d.dy = d.y d.alpha = Math.random() if (typeof color === 'string') d.tint = PIXI.utils.string2hex(color) if (Array.isArray(color)) { let c = random(color) d.tint = PIXI.utils.string2hex(c) } paticles.addChild(d) } game.stage.addChild(paticles)
join(game.renderer, shape[joinTimes % shape.length], paticles) PIXI.Ticker.shared.add(() => {
frame++ paticles.children.forEach(d => { if (d.dx !== d.x) { d.x += (d.dx - d.x) / 20 } if (d.dy !== d.y) { d.y += (d.dy - d.y) / 20 } d.x += (Math.random() - 0.5) * shake.x * scale / 2 d.y += (Math.random() - 0.5) * shake.y * scale / 2 })
if (frame % rate === 0) { joinTimes++ times++; join(game.renderer, shape[joinTimes % shape.length], paticles) }
})
function join(renderer, sprite, paticles) { let { x: ox, y: oy, width, height } = sprite.getBounds(); game.stage.removeChild(game.stage.getChildByName('shape'))
let shadow = new PIXI.Container() shadow.addChild(sprite) let dotData = renderer.extract.pixels(shadow); shadow.destroy()
let colorPos = [] for (let y = 0; y < height; y += 1) { for (let x = 0; x < width; x += 1) { let alpha = dotData[y * width * 4 + x * 4 + 3]; if (alpha > 1) { colorPos.push({ x: ox + x, y: oy + y }) } } }
paticles.children.forEach(d => { let pos = random(colorPos) d.dx = pos.x d.dy = pos.y })
dotData = null colorPos = null }
function leave(renderer, paticles) { paticles.children.forEach(d => { d.dx = renderer.width * Math.random() d.dy = renderer.height * Math.random() }) }
|