|
| 1 | +// "use strict"; |
| 2 | +var width, height; |
| 3 | +var canvas; |
| 4 | + |
| 5 | +if (window.ejecta) { |
| 6 | + |
| 7 | + // relative to index.js . |
| 8 | + var relativePath = "example/bunnymark/"; |
| 9 | + |
| 10 | + width = window.innerWidth // * window.devicePixelRatio; |
| 11 | + height = window.innerHeight // * window.devicePixelRatio; |
| 12 | + canvas = document.getElementById('canvas'); |
| 13 | + canvas.width = width; |
| 14 | + canvas.height = height; |
| 15 | + canvas.style.width = window.innerWidth + "px"; |
| 16 | + canvas.style.height = window.innerHeight + "px"; |
| 17 | + |
| 18 | + console.log("screen canvas : ", width, height); |
| 19 | + |
| 20 | + window.resImagePath = relativePath + ""; |
| 21 | + ejecta.include(relativePath + "pixi.min.js"); |
| 22 | + |
| 23 | + setTimeout(function() { |
| 24 | + onReady(); |
| 25 | + }, 100); |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +var useWebGL = true; |
| 31 | +var multiTexture = false; |
| 32 | + |
| 33 | +var maxBunnyCount = 1000 * 200; |
| 34 | +// var startBunnyCount = 10; |
| 35 | +var startBunnyCount = 1000 * 20; |
| 36 | + |
| 37 | + |
| 38 | +var particleMaxSize = 2500 * 1; |
| 39 | +var particleBatchSize = 2500 * 1; |
| 40 | + |
| 41 | +var bunnies = []; |
| 42 | +var bunnyTextures; |
| 43 | +var bunnyTextureIndex; |
| 44 | +var currentTexture; |
| 45 | + |
| 46 | +var gravity = 0.75 //1.5 ; |
| 47 | + |
| 48 | +var isAdding = 0; |
| 49 | +var addingGap = 35; |
| 50 | +var amount = 100; |
| 51 | + |
| 52 | +var bunnyCount = 0; |
| 53 | +var container; |
| 54 | +var stats, counter; |
| 55 | +var renderer; |
| 56 | + |
| 57 | + |
| 58 | +width = width || 640; |
| 59 | +height = height || 960; |
| 60 | +var aspectRatio = width / height; |
| 61 | +var minX, maxX; |
| 62 | +var minY, maxY; |
| 63 | +var screenWidth, screenHeight; |
| 64 | + |
| 65 | +function onReady() { |
| 66 | + console.log("ready"); |
| 67 | + |
| 68 | + doResize(); |
| 69 | + |
| 70 | + canvas = canvas || document.getElementById("canvas"); |
| 71 | + canvas.style.transform = "translatez(0)"; |
| 72 | + canvas.style.position = "absolute"; |
| 73 | + |
| 74 | + |
| 75 | + if (typeof Stats != "undefined") { |
| 76 | + stats = new Stats(); |
| 77 | + stats.domElement.style.position = "absolute"; |
| 78 | + stats.domElement.style.top = "0px"; |
| 79 | + document.body.appendChild(stats.domElement); |
| 80 | + |
| 81 | + counter = document.createElement("div"); |
| 82 | + counter.className = "counter"; |
| 83 | + document.body.appendChild(counter); |
| 84 | + } |
| 85 | + |
| 86 | + doReLocation(); |
| 87 | + |
| 88 | + |
| 89 | + if ("ontouchstart" in window) { |
| 90 | + window.addEventListener("touchstart", onTouchStart, true); |
| 91 | + window.addEventListener("touchend", onTouchEnd, true); |
| 92 | + } else { |
| 93 | + window.addEventListener("mousedown", onTouchStart, true); |
| 94 | + window.addEventListener("mouseup", onTouchEnd, true); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + // return; |
| 99 | + var options = { view: canvas, backgroundColor: 0xFFFFFF }; |
| 100 | + |
| 101 | + if (useWebGL) { |
| 102 | + renderer = new PIXI.WebGLRenderer(width, height, options); |
| 103 | + } else if (useWebGL === false) { |
| 104 | + renderer = new PIXI.CanvasRenderer(width, height, options); |
| 105 | + } else { |
| 106 | + renderer = new PIXI.autoDetectRenderer(width, height, options); |
| 107 | + } |
| 108 | + |
| 109 | + createContainer(); |
| 110 | + |
| 111 | + initBunnyTextures(); |
| 112 | + |
| 113 | + bunnyTextureIndex = -1; |
| 114 | + |
| 115 | + nextTexture(); |
| 116 | + |
| 117 | + addMoreBunnies(startBunnyCount); |
| 118 | + |
| 119 | + requestAnimationFrame(update); |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +function initBunnyTextures() { |
| 124 | + |
| 125 | + bunnyTextures = []; |
| 126 | + |
| 127 | + var textures = [ |
| 128 | + new PIXI.Texture.fromImage(window.resImagePath + "bunnies-1.png"), |
| 129 | + new PIXI.Texture.fromImage(window.resImagePath + "bunnies-2.png"), |
| 130 | + new PIXI.Texture.fromImage(window.resImagePath + "bunnies-3.png"), |
| 131 | + ]; |
| 132 | + |
| 133 | + var textureCount = 1; |
| 134 | + if (multiTexture) { |
| 135 | + textureCount = textures.length; |
| 136 | + } |
| 137 | + |
| 138 | + var rects = []; |
| 139 | + for (var i = 0; i < textureCount; i++) { |
| 140 | + var baseTexture = textures[i].baseTexture; |
| 141 | + // console.log(baseTexture.imageUrl); |
| 142 | + rects.push(new PIXI.Texture(baseTexture, new PIXI.Rectangle(0, 0, 30, 46))); |
| 143 | + rects.push(new PIXI.Texture(baseTexture, new PIXI.Rectangle(0, 46 + 39 * 0, 30, 39))); |
| 144 | + rects.push(new PIXI.Texture(baseTexture, new PIXI.Rectangle(0, 46 + 39 * 1, 30, 39))); |
| 145 | + rects.push(new PIXI.Texture(baseTexture, new PIXI.Rectangle(0, 46 + 39 * 2, 30, 39))); |
| 146 | + rects.push(new PIXI.Texture(baseTexture, new PIXI.Rectangle(0, 46 + 39 * 3, 30, 39))); |
| 147 | + } |
| 148 | + |
| 149 | + var count = rects.length * 3; |
| 150 | + for (var i = 0; i < count; i++) { |
| 151 | + bunnyTextures.push(rects[i % rects.length]); |
| 152 | + } |
| 153 | + |
| 154 | +} |
| 155 | + |
| 156 | +var pressed = false; |
| 157 | + |
| 158 | +function onTouchStart(event) { |
| 159 | + isAdding = 0; |
| 160 | + pressed = true; |
| 161 | +} |
| 162 | + |
| 163 | +function onTouchEnd(event) { |
| 164 | + |
| 165 | + if (isAdding !== -1) { |
| 166 | + // nextTexture(); |
| 167 | + addMoreBunnies(amount); |
| 168 | + } |
| 169 | + pressed = false; |
| 170 | +} |
| 171 | + |
| 172 | +function resize() { |
| 173 | + setTimeout(function() { |
| 174 | + doResize(); |
| 175 | + doReLocation(); |
| 176 | + |
| 177 | + renderer.resize(width, height); |
| 178 | + }, 10); |
| 179 | +} |
| 180 | + |
| 181 | +function doResize() { |
| 182 | + |
| 183 | + screenWidth = window.innerWidth; |
| 184 | + screenHeight = window.innerHeight; |
| 185 | + |
| 186 | + if (screenWidth / screenHeight >= aspectRatio) { |
| 187 | + height = screenHeight; |
| 188 | + width = height * aspectRatio; |
| 189 | + } else if (screenWidth / screenHeight < aspectRatio) { |
| 190 | + width = screenWidth; |
| 191 | + height = width / aspectRatio; |
| 192 | + } |
| 193 | + |
| 194 | + maxX = width; |
| 195 | + minX = 0; |
| 196 | + maxY = height - 10; |
| 197 | + minY = 0; |
| 198 | + |
| 199 | +} |
| 200 | + |
| 201 | +function doReLocation() { |
| 202 | + |
| 203 | + var w = screenWidth / 2 - width / 2; |
| 204 | + var h = screenHeight / 2 - height / 2; |
| 205 | + |
| 206 | + canvas.style.left = w + "px" |
| 207 | + canvas.style.top = h + "px" |
| 208 | + |
| 209 | + if (stats) { |
| 210 | + stats.domElement.style.left = w + "px"; |
| 211 | + stats.domElement.style.top = h + "px"; |
| 212 | + |
| 213 | + counter.style.left = w + "px"; |
| 214 | + counter.style.top = h + 40 + "px"; |
| 215 | + } |
| 216 | + |
| 217 | +} |
| 218 | + |
| 219 | + |
| 220 | +function nextTexture() { |
| 221 | + bunnyTextureIndex = (bunnyTextureIndex + 1) % bunnyTextures.length; |
| 222 | + currentTexture = bunnyTextures[bunnyTextureIndex]; |
| 223 | +} |
| 224 | + |
| 225 | +function update() { |
| 226 | + stats && stats.begin(); |
| 227 | + |
| 228 | + if (pressed) { |
| 229 | + isAdding++; |
| 230 | + } |
| 231 | + if (isAdding >= addingGap) { |
| 232 | + addMoreBunnies(amount); |
| 233 | + isAdding = -1; |
| 234 | + } |
| 235 | + |
| 236 | + for (var i = 0, len = bunnies.length; i < len; i++) { |
| 237 | + var bunny = bunnies[i]; |
| 238 | + bunny.rotation += bunny.speedR; |
| 239 | + var x = bunny.position.x += bunny.speedX; |
| 240 | + var y = bunny.position.y += bunny.speedY; |
| 241 | + bunny.speedY += gravity; |
| 242 | + |
| 243 | + if (x > maxX) { |
| 244 | + bunny.speedX *= -1; |
| 245 | + bunny.position.x = maxX; |
| 246 | + } else if (x < minX) { |
| 247 | + bunny.speedX *= -1; |
| 248 | + bunny.position.x = minX; |
| 249 | + } |
| 250 | + |
| 251 | + if (y > maxY) { |
| 252 | + bunny.speedY *= -0.85; |
| 253 | + bunny.position.y = maxY; |
| 254 | + bunny.spin = (Math.random() - 0.5) * 0.2 |
| 255 | + if (Math.random() > 0.5) { |
| 256 | + bunny.speedY -= Math.random() * 6; |
| 257 | + } |
| 258 | + } else if (y < minY) { |
| 259 | + bunny.speedY = 0; |
| 260 | + bunny.position.y = minY; |
| 261 | + } |
| 262 | + |
| 263 | + } |
| 264 | + |
| 265 | + renderer.render(container); |
| 266 | + requestAnimationFrame(update); |
| 267 | + |
| 268 | + stats && stats.end(); |
| 269 | +} |
| 270 | + |
| 271 | + |
| 272 | +function addMoreBunnies(count) { |
| 273 | + var i = 0; |
| 274 | + while (bunnyCount < maxBunnyCount && i < count) { |
| 275 | + var bunny = createBunny(currentTexture); |
| 276 | + |
| 277 | + bunnies.push(bunny); |
| 278 | + //bunny.rotation = Math.random() - 0.5; |
| 279 | + container.addChild(bunny); |
| 280 | + // var random = randomInt(0, container.children.length - 2); |
| 281 | + // container.addChildAt(bunny, random); |
| 282 | + |
| 283 | + nextTexture(); |
| 284 | + |
| 285 | + i++; |
| 286 | + bunnyCount++; |
| 287 | + } |
| 288 | + if (counter) { |
| 289 | + counter.innerHTML = bunnyCount + " BUNNIES"; |
| 290 | + } |
| 291 | +} |
| 292 | + |
| 293 | + |
| 294 | +function createBunny(currentTexture) { |
| 295 | + var bunny = new PIXI.Sprite(currentTexture); |
| 296 | + |
| 297 | + bunny.anchor.x = 0.5; |
| 298 | + bunny.anchor.y = 1; |
| 299 | + bunny.scale.set(0.5 + Math.random() * 0.5); |
| 300 | + bunny.rotation = (Math.random() - 0.5); |
| 301 | + //bunny.alpha = 0.3 + Math.random() * 0.7; |
| 302 | + bunny.speedX = Math.random() * 5; |
| 303 | + bunny.speedY = (Math.random() * 6) - 4; |
| 304 | + bunny.speedR = ((1 + Math.random() * 10) >> 0) / 50; |
| 305 | + |
| 306 | + return bunny; |
| 307 | +} |
| 308 | + |
| 309 | + |
| 310 | +function createContainer() { |
| 311 | + |
| 312 | + var options = { |
| 313 | + position: true, |
| 314 | + rotation: true, |
| 315 | + scale: true, |
| 316 | + |
| 317 | + alpha: true, |
| 318 | + uvs: true, |
| 319 | + }; |
| 320 | + |
| 321 | + container = new PIXI.particles.ParticleContainer(particleMaxSize, options, particleBatchSize); |
| 322 | + // container = new PIXI.Container(); |
| 323 | + |
| 324 | + if (!useWebGL) { |
| 325 | + container = new PIXI.Container(); |
| 326 | + } |
| 327 | + |
| 328 | +} |
| 329 | + |
| 330 | + |
| 331 | + |
| 332 | +///////////////////////////////////////////////////////// |
| 333 | +///////////////////////////////////////////////////////// |
| 334 | +///////////////////////////////////////////////////////// |
| 335 | +///////////////////////////////////////////////////////// |
| 336 | +///////////////////////////////////////////////////////// |
| 337 | + |
| 338 | + |
| 339 | +function randomInt(from, to) { |
| 340 | + return Math.floor(Math.random() * (to - from + 1) + from); |
| 341 | +} |
| 342 | + |
| 343 | +window.resImagePath = window.resImagePath || ""; |
| 344 | +window.onload = function() { |
| 345 | + onReady(); |
| 346 | +}; |
| 347 | +window.onresize = function() { |
| 348 | + resize(); |
| 349 | +}; |
| 350 | +window.onorientationchange = function() { |
| 351 | + resize(); |
| 352 | +}; |
0 commit comments