标签:apc gcj lam anti jci title ons round auc
ballPool
用javaScript实现实现一个球池的效果,实现效果如下所示:
动图:
截图:
HTML代码:
<!DOCTYPE html> <html > <head> <meta charset="UTF-8"> <title>ball pool</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <div class="instructions"> <p id="title">触摸你的屏幕/或移动你的鼠标</p> </div> <script src=‘http://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.5.1/pixi.min.js‘></script> <script src=‘https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js‘></script> <script src="js/index.js"></script> </body> </html>
CSS代码:
body { margin:0; overflow:hidden; color:rgba(255,255,255,.5); font-family:arial; font-size: 3em; font-family: ‘幼圆‘; color: #fff; text-shadow: 1px 1px 10px #000; position: relative; } canvas { background: #dbdbdb; } .instructions { position: absolute; width: 100%; } #title{ display: inline-block;; border: 2px solid transparent; box-shadow: 1px 1px 20px #000; width: 100%; margin: auto; padding: 3px 0 3px 0; text-align: center; background: inherit; opacity: 0.8; position: fixed; cursor: none; border-radius: 0 0 20px 20px; }
JAVASCRIPT代码:
// ******************* // 初始化 // ******************* const userDefined = { gravity: 10, friction: .99, ballSize: 65, wallFriction: .5, distribution: 10, spread: 10 } const colours = [ ‘0xf20053‘, ‘0xfffc00‘, ‘0x0070ff‘, ‘0x00ffff‘, ‘0x00ff00‘, ‘0xffb2aa‘ ] const coloursLength = colours.length; const halfSpread = userDefined.spread/2; var mouse = {x:0,y:0}; var oldMouse = {x:0,y:0}; var balls = []; // ******************* // 图像 // ******************* const pixelTexture = PIXI.Texture.fromImage(‘data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4Onhtc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s1xotDBlmlGp0jMSjEoWx3SbJpg00LeTuos/pFSTHBLBZpF9DKI/Qn4MRoNqZnbL5++9/xN/ND29Kb6lp7qnXnxh7vC/feNL3W73U1vmtxrXKeVxGBlWO0c+dsuk8ck8h4hOnpv4oMO0DPtzyY+gfX9UtiLtak8f03dZWi09kNAUQD5uYC13aXXl8tzC4mfuvu8TD09NTV/dc/bTL79QHv2fpx5avnLudxYWdm3nc0GW5k7mVjoEkBRqg8xv+cxdcp09jEDMSk/iY5h8OSCmzo/0WiPhmdhsGFo5Bk4cDfnQ5zvvuPG237jxpvf+19T0zNvzzQcu4N//9Z/vunD2lc9vmpm5e3bLdnlElhf5pMxg7uTsJKLmjvTTMoOSKRMWW78mkaOU5FJTtYxII6rfepbv1cq2ucW/uP5d7/nygYM3X/6xfKnm/NlTnW8++k8Pov7/1S3b5+/odqctRVHEMdhUH45HInRAMxlQ0BApWThKIq1CteVBDw1gLgz6vSzPH9m157o/+5kPf/yJH+WbQm/5a07PPf3ETN3UHzt/8cwnIYR3IQHmu6ikO6gA9HslIc2cQhrIaS1FCElpiTxY7/ckH9DGPr99bseji7uv/btdew88Mb+w6yf9fS1DL5rjx753LfrnO5tqcHtvbfXWPPcLiMxWeH4WC+2iDsvg9Tq01QCC12sqfwVBeWXXnmsfGw4GR/YfuOE7+/YfXCvK8i2v438FGACEYR+8Q5rpeQAAAABJRU5ErkJggg==‘); // ******************* //窗口尺寸 // ******************* var windowDimensions = { width: window.innerWidth, height: window.innerHeight } // ******************* // 设置 pixi // ******************* const app = new PIXI.Application(windowDimensions.width, windowDimensions.height, { antialias: true, transparent: true }); document.body.appendChild(app.view); // ******************* // DOM // ******************* $(window).on(‘resize‘, function () { windowDimensions = { width: window.innerWidth, height: window.innerHeight } app.renderer.resize(windowDimensions.width,windowDimensions.height); }); $(window).on(‘mousemove touchmove‘, function (e) { mouse = { x: e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX, y: e.originalEvent.touches ? e.originalEvent.touches[0].pageY : e.pageY, } for (let i = 0, length = Math.random()*userDefined.distribution; i < length; i++) { createBall( mouse.x, mouse.y, (mouse.x - oldMouse.x)*Math.random(), (mouse.y - oldMouse.y)*Math.random()); } oldMouse = { x: mouse.x, y: mouse.y } }); // ******************* // 创建小球 // ******************* function createBall (x, y, xv, yv) { var ball = new PIXI.Sprite(pixelTexture); ball.x = x; ball.y = y; ball.width = ball.height = userDefined.ballSize; ball.xv = xv + (-halfSpread+Math.random()*userDefined.spread); ball.yv = yv + (-halfSpread+Math.random()*userDefined.spread); ball.tint = colours[Math.floor(Math.random()*coloursLength)]; balls.push(ball); app.stage.addChild(ball); } // ******************* // 开始窗口 // ******************* setTimeout(function () { for (let i = 0, length = userDefined.distribution*100; i < length; i++) { createBall( Math.random()*windowDimensions.width, Math.random()*(-windowDimensions.height), -5+Math.random()*10, -5+Math.random()*10) } }, 2500); // ****************** // 小球运动周期 // ****************** (function loop() { for (let i = 0, length = balls.length; i < length; i++) { //移动 balls[i].x += balls[i].xv; balls[i].y += balls[i].yv+userDefined.gravity; //通过摩擦力减速 balls[i].xv *= userDefined.friction; balls[i].yv *= userDefined.friction; //禁止出界 if (balls[i].x <= 0) { balls[i].x = 0; } else if (balls[i].x >= windowDimensions.width-userDefined.ballSize) { balls[i].x = windowDimensions.width-userDefined.ballSize; } //弹离 if (balls[i].x <= 0 || balls[i].x >= windowDimensions.width-userDefined.ballSize) { //反转 balls[i].xv*=-1; //减速 balls[i].xv *= userDefined.wallFriction; } } // 清除 for (let i = 0, length = balls.length; i < length; i++) { if (balls[i].y >= windowDimensions.height) { app.stage.removeChild(balls[i]); balls.splice(i, 1); length--; } } //再次运行 requestAnimationFrame(loop); })();
标签:apc gcj lam anti jci title ons round auc
原文地址:http://www.cnblogs.com/dragonir/p/7152409.html