UNPKG

pixijs-remote-helper

Version:
93 lines (71 loc) 3.41 kB
<!DOCTYPE html> <html> <head> <title>Pixijs remote helper example mixin</title> </head> <body style="display: flex"> <canvas id="example" style="width: 480;height: 360px;"></canvas> <div id="devpanel" style="flex: 1"> <pixi-panel></pixi-panel> <!-- this is where the PixiPanel will get rendered --> </div> <script type="text/javascript" src="mixin_build/main5v.bundle.js"></script> <!-- <script src="mixin_build/libs.bundle.js"></script> --> <script> const app = new PIXI.Application(); document.body.appendChild(app.view); // holder to store the aliens const aliens = []; const totalDudes = 20; for (let i = 0; i < totalDudes; i++) { // create a new Sprite that uses the image name that we just generated as its source const dude = PIXI.Sprite.from('assets/eggHead.png'); // set the anchor point so the texture is centerd on the sprite dude.anchor.set(0.5); // set a random scale for the dude - no point them all being the same size! dude.scale.set(0.8 + Math.random() * 0.3); // finally lets set the dude to be at a random position.. dude.x = Math.random() * app.screen.width; dude.y = Math.random() * app.screen.height; dude.tint = Math.random() * 0xFFFFFF; // create some extra properties that will control movement : // create a random direction in radians. This is a number between 0 and PI*2 which is the equivalent of 0 - 360 degrees dude.direction = Math.random() * Math.PI * 2; // this number will be used to modify the direction of the dude over time dude.turningSpeed = Math.random() - 0.8; // create a random speed for the dude between 0 - 2 dude.speed = 2 + Math.random() * 2; // finally we push the dude into the aliens array so it it can be easily accessed later aliens.push(dude); app.stage.addChild(dude); } // create a bounding box for the little dudes const dudeBoundsPadding = 100; const dudeBounds = new PIXI.Rectangle(-dudeBoundsPadding, -dudeBoundsPadding, app.screen.width + dudeBoundsPadding * 2, app.screen.height + dudeBoundsPadding * 2); app.ticker.add(() => { // iterate through the dudes and update their position for (let i = 0; i < aliens.length; i++) { const dude = aliens[i]; dude.direction += dude.turningSpeed * 0.01; dude.x += Math.sin(dude.direction) * dude.speed; dude.y += Math.cos(dude.direction) * dude.speed; dude.rotation = -dude.direction - Math.PI / 2; // wrap the dudes by testing their bounds... if (dude.x < dudeBounds.x) { dude.x += dudeBounds.width; } else if (dude.x > dudeBounds.x + dudeBounds.width) { dude.x -= dudeBounds.width; } if (dude.y < dudeBounds.y) { dude.y += dudeBounds.height; } else if (dude.y > dudeBounds.y + dudeBounds.height) { dude.y -= dudeBounds.height; } } }); </script> </body> </html>