Randomly generated bubble collisions (native JS)

Source: Internet
Author: User

<! DOCTYPE html>

<meta charset= "UTF-8" >
<title> randomly generated bubble collisions </title>
<style>
HTML {
height:100%;
}

Body {
width:100%;
margin:0;
padding:0;
height:100%;
}

Div.wrap {
height:100%;
position:relative;
Overflow:hidden;
margin:0;
padding:0;
}

Div:not (. Wrap) {
border-radius:50%;
/*-webkit-animation:tanslateani 5000ms Infinite Linear alternate;*/
/*transition:all 10000ms linear;*/
}

Div:nth-of-type (2) {
-webkit-animation-delay:2000ms;
}

Div:nth-of-type (3) {
-webkit-animation-delay:4000ms;
}

Div:nth-of-type (4) {
-webkit-animation-delay: -1000ms;
}

@-webkit-keyframes Tanslateani {
0% {
-webkit-transform:rotate (10deg) translate3d (0px, 0px, 0)
}
35% {
-webkit-transform:rotate (0deg) translate3d (20px, 0px, 0)
}
70% {
-webkit-transform:rotate ( -10deg) translate3d (0px, 0px, 0)
}
100% {
-webkit-transform:rotate (0deg) translate3d ( -20px, 0px, 0)
}
}

span {
Display:inline-block;
width:100px;
height:100px;
border-radius:50%;
background-color:red;
Background:-webkit-repeating-radial-gradient (red, yellow 10%, green 15%);
/* Safari 5.1-6.0 */
Background:-o-repeating-radial-gradient (red, yellow 10%, green 15%);
/* Opera 11.6-12.0 */
Background:-moz-repeating-radial-gradient (red, yellow 10%, green 15%);
/* Firefox 3.6-15 */
Background:repeating-radial-gradient (red, yellow 10%, green 15%);
/* Standard syntax (must be placed at the end) */
</style>

<body>
<div class= "wrap" id= ' parent ' ></div>
<span></span>
</body>
<script>
var elwidth = 110;
var containerwidth;
var color = ['-webkit-repeating-radial-gradient (red, yellow 10%, green 15%) ', ' green ', ' blue ', ' black ', ' yellow '];
Random Color
var ballnum = 20;
var pageheight=window.screen.height;
var pageheight = document.body.clientHeight;
var pagewidth=window.screen.width;
var pagewidth = document.body.clientWidth;
Containerwidth = PageWidth;
var sizerange = 60;
var balls = [];
Window.onload = function () {
Createballs (Elwidth + sizerange, PageWidth, PageHeight, ballnum);
SetInterval (function () {Ballhit (balls, PageWidth, PageHeight)}, 30)
}
Generate a virtual mesh
function CreateTable (ballwidth, PageWidth, PageHeight) {
var col = Math.floor (pagewidth/ballwidth);
var Marginh = pagewidth% Ballwidth;
var MARGINV = pageheight% Ballwidth;
var row = Math.floor (pageheight/ballwidth);
var total = col * row;
var ballmap = [];
for (var i = 0; i < total; i++) {
Grid map deposit into one-dimensional array//top, left value has a large deviation reason:/, the% operation is a decimal, and Java is different
Ballmap[i] = {LEFT:MARGINH/2 + math.floor (i% col) * Ballwidth, TOP:MARGINV/2 + math.floor (i/col) * Ballwidth}
}
return ballmap;
}
Generate pellets
var spaceindex = 25;

function Createballs (ballwidth, PageWidth, PageHeight, Ballnum) {
var ballmap = createtable (Ballwidth, PageWidth, PageHeight);
var range = Ballmap.length
if (ballnum >= range-spaceindex) {
Ballnum = Range-spaceindex;
}
var ballsdata = [];
var Ballindex;
Ballindex = getrandom (0, Range, ballnum);
for (var i = 0; i < ballindex.length; i++) {ballsdata[i] = Ballmap[ballindex[i]];} renderballs (Ballsdata, ballnum);
}//Render small balls
function Renderballs (Balldata, Ballnum) {
for (var i = 0; i < Ballnum; i++) {
var divball = createel (' div ')
(Pending improvement)
Balls.push ({
El:divball,
Left:balldata[i].left,
Top:balldata[i].top,
Vx:Math.random () * 6-3,
Vy:Math.random () * 6-3,
Raduis:parseint (divBall.style.width)/2
});
}
for (var j = 0; J < Ballnum; J + +) {
Balls[j].el.style.left = balls[j].left + ' px ';
Balls[j].el.style.top = balls[j].top + ' px ';
}
}//random number de-weight
function Norepeat (arr, newnum, range) {
var isrepeat = false;
for (var i = 0; i < arr.length; i++) {
if (Newnum = = Arr[i]) {
Isrepeat = true;
Break
}
}
if (isrepeat) {
Newnum = Math.floor (Math.random () * range);
Norepeat (arr, newnum, range);
} else {
return newnum;
}
}//Random number de-tool function 2
function Getrandom (min, max, n) {
if (n > (max-min + 1) | | Max < min) {
return null;
}
var result = [];
var count = 0;
while (Count < N) {
var num = Math.floor (Math.random () * (max-min)) + min;
var flag = true;
for (var j = 0; J < N; j + +) {
if (num = = Result[j]) {
Flag = false;
Break
}
}
if (flag) {
Result[count] = num;
count++;
}
}
return result;
}//randomly generate a DOM element
function Createel (el, text) {
var elment = document.createelement (EL);
document.getElementById (' parent '). AppendChild (elment);
var len = Math.ceil (Math.random () * sizerange);
Elment.style.width = len + elwidth + ' px ';
Elment.style.height = len + elwidth + ' px ';
Elment.style.background = Color[math.floor (Math.random () * 5)];
elment.style.position = ' absolute ';
elment.innerhtml = text | | "";
return elment;
}

function Moveball (balls) {
for (var i = 0; i < balls.length; i++) {
Balls[i].left + = BALLS[I].VX;
Balls[i].top + = Balls[i].vy;
Balls[i].el.style.left = balls[i].left + ' px ';
Balls[i].el.style.top = balls[i].top + ' px ';
}
}
var spring = 1; Elasticity coefficient between ball and ball
var bounce =-1; Elasticity coefficient between ball and edge
function Ballhit (balls, PageWidth, PageHeight) {
Detecting edges
for (var i = 0; i < balls.length; i++) {
if (balls[i].left <= 0 | | balls[i].left >= pageWidth-2 * balls[i].raduis) {
BALLS[I].VX *= Bounce;
}
if (balls[i].top <= 0 | | balls[i].top >= pageHeight-2 * balls[i].raduis) {
Balls[i].vy *= Bounce;
}
}
Detecting small balls and small balls
for (i = 0; i < balls.length; i++) {
var ball1 = balls[i];
ball1.x = Ball1.left + Ball1.raduis;
BALL1.Y = Ball1.top + Ball1.raduis;
for (j = i + 1; j < Balls.length; J + +) {
var ball2 = balls[j];
ball2.x = Ball2.left + Ball2.raduis;
BALL2.Y = Ball2.top + Ball2.raduis;
DX = ball2.x-ball1.x;
dy = ball2.y-ball1.y;
var dist = math.sqrt (dx * dx + dy * dy);
var misdist = Ball1.raduis + Ball2.raduis;
if (Dist < misdist) {
var angle = math.atan2 (dy, dx);
tx = ball1.x + math.cos (angle) * misdist;
ty = ball1.y + math.sin (angle) * misdist;
Ax = (tx-ball2.x) * SPRING;
ay = (ty-ball2.y) * SPRING;
BALL1.VX-= ax;
Ball1.vy-= ay;
BALL2.VX + = ax;
Ball2.vy + = ay;
}
}
}
Moveball (balls);
}
</script>

Randomly generated bubble collisions (native JS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.