【xpj网址】jquery制作 随机弹跳的小球特效,jquery小球

HTML5 随机弹跳的小球, &lt,html&gt,以下是源码,这款动画可以让你甩动页面中的小球,这些小球就在canvas中运动

HTML5 随机弹跳的小球

jquery制作 随机弹跳的小球特效,jquery小球

以下是源码:

复制代码 代码如下:

 <!doctype html>
 <html>
 <head>
 <title>HTML5 随机弹跳的小球</title>
 <style>
 body{
 font-family: 微软雅黑;   
 }
 body,h1{
 margin:0;
 }
 canvas{
 display:block;margin-left: auto;margin-right: auto;
 border:1px solid #DDD;   
 background: -webkit-linear-gradient(top, #222,#111);
 }   
 </style>
 </head>
 <body>
 <h1>HTML5神效 随机弹跳的小球</h1>
 <div>请使用匡助HTML5的浏览器开打本页。 <button
id=”stop-keleyi-com”>暂停</button>
 <button id=”run-keleyi-com”>继续</button>
 <button id=”addBall-keleyi-com”>增添小球</button>
<button
onclick=”javascript:window.location.reload(卡塔尔(قطر‎;”>刷新</button>
 <br
/>每回刷新页面包车型地铁小球大小,颜色,运动路径,都是新的,能够点击下面种种按键看看效果。
 </div>
 <canvas id=”canvas-keleyi-com” >
 </canvas>
 <script type=”text/javascript”
src=”;
 <script type=”text/javascript”>
 var nimo = {
 aniamted: null,
 content: null,
 data: {
 radiusRange: [5, 20],
 speedRange: [-5, 5],
 scrollHeight: null,
 scrollWdith: null
 },
 balls: [],
 ele: {
 canvas: null
 },
 fn: {
 creatRandom: function (startInt, endInt卡塔尔国 {//临盆随机数
 var iResult;
 iResult = startInt + (Math.floor(Math.random() * (endInt – startInt +
1)));
 return iResult
 },
 init: function () {
 nimo.data.scrollWdith = document.body.scrollWidth;
 nimo.data.scrollHeight = document.body.scrollHeight;
 nimo.ele.canvas = document.getElementById(‘canvas-ke’+’leyi-com’);
 nimo.content = nimo.ele.canvas.getContext(‘2d’);
 nimo.ele.canvas.width = nimo.data.scrollWdith – 50;
 nimo.ele.canvas.height = nimo.data.scrollHeight – 100;
 },
 addBall: function () {
 var aRandomColor = [];
 aRandomColor.push(nimo.fn.creatRandom(0, 255));
 aRandomColor.push(nimo.fn.creatRandom(0, 255));
 aRandomColor.push(nimo.fn.creatRandom(0, 255));
 var iRandomRadius = nimo.fn.creatRandom(nimo.data.radiusRange[0],
nimo.data.radiusRange[1]);
 var oTempBall = {
 coordsX: nimo.fn.creatRandom(iRandomRadius, nimo.ele.canvas.width –
iRandomRadius),
 coordsY: nimo.fn.creatRandom(iRandomRadius, nimo.ele.canvas.height –
iRandomRadius),
 radius: iRandomRadius,
 bgColor: ‘rgba(‘ + aRandomColor[0] + ‘,’ + aRandomColor[1] + ‘,’ +
aRandomColor[2] + ‘,0.5)’
 };
 oTempBall.speedX = nimo.fn.creatRandom(nimo.data.speedRange[0],
nimo.data.speedRange[1]);
 if (oTempBall.speedX === 0) {
 oTempBall.speedX = 1
 }
 if (oTempBall.speedY === 0) {
 oTempBall.speedY = 1
 }
 oTempBall.speedY = nimo.fn.creatRandom(nimo.data.speedRange[0],
nimo.data.speedRange[1]);
 nimo.balls.push(oTempBall)
 },
 drawBall: function (bStatic) {
 var i, iSize;
 nimo.content.clearRect(0, 0, nimo.ele.canvas.width,
nimo.ele.canvas.height)
 for (var i = 0, iSize = nimo.balls.length; i < iSize; i++) {
 var oTarger = nimo.balls[i];
 nimo.content.beginPath();
 nimo.content.arc(oTarger.coordsX, oTarger.coordsY, oTarger.radius, 0,
10);
 nimo.content.fillStyle = oTarger.bgColor;
 nimo.content.fill();
 if (!bStatic) {
 if (oTarger.coordsX + oTarger.radius >= nimo.ele.canvas.width) {
 oTarger.speedX = -(Math.abs(oTarger.speedX))
 }
 if (oTarger.coordsX – oTarger.radius <= 0) {
 oTarger.speedX = Math.abs(oTarger.speedX)
 }
 if (oTarger.coordsY – oTarger.radius <= 0) {
 oTarger.speedY = Math.abs(oTarger.speedY)
 }
 if (oTarger.coordsY + oTarger.radius >= nimo.ele.canvas.height) {
 oTarger.speedY = -(Math.abs(oTarger.speedY))
 }
 oTarger.coordsX = oTarger.coordsX + oTarger.speedX;
 oTarger.coordsY = oTarger.coordsY + oTarger.speedY;
 }
 }
 },
 run: function () {
 nimo.fn.drawBall();
 nimo.aniamted = setTimeout(function () {
 nimo.fn.drawBall();
 nimo.aniamted = setTimeout(arguments.callee, 10)
 }, 10)
 },
 stop: function () {
 clearTimeout(nimo.aniamted)
 }
 }
 }
 window.onload = function () {
 nimo.fn.init();
 var i;
 for (var i = 0; i < 10; i++) {
 nimo.fn.addBall();
 }
 nimo.fn.run();
 document.getElementById(‘stop-kel’+’eyi-com’).onclick = function () {
 nimo.fn.stop()
 }
 document.getElementById(‘run-keley’+’i-com’).onclick = function () {
 nimo.fn.stop()
 nimo.fn.run()
 }
 document.getElementById(‘addBall-k’+’eleyi-com’).onclick = function ()
{
 var i;
 for (var i = 0; i < 10; i++) {
 nimo.fn.addBall();
 }
 nimo.fn.drawBall(true);
 }
 }
 </script>
 </body>
 </html>

随机弹跳的小球特效,jquery小球
以下是源码: 复制代码 代码如下: !doctype html html head titleHTML5
随机弹跳的小球/title style body{ fon…

web前端:

明日我们来分享后生可畏款太帅的HTML5重力影响动漫教程,那款动漫可以令你甩动页面中的小球,小球的大…

HTML5特效 随机弹跳的小球

请使用援助HTML5的浏览器开打本页。

暂停

继续

日增小球

刷新

老是刷新页面包车型大巴小球大小,颜色,运动路径,都以新的,可以点击上面各类按键看看效果。

Chrome效果图:
xpj网址 1

Javascript代码:

var canvas;

var delta = [ 0, 0 ];
var stage = [ window.screenX, window.screenY, window.innerWidth, window.innerHeight ];
getBrowserDimensions();

var themes = [ [ "#10222B", "#95AB63", "#BDD684", "#E2F0D6", "#F6FFE0" ],
        [ "#362C2A", "#732420", "#BF734C", "#FAD9A0", "#736859" ],
        [ "#0D1114", "#102C2E", "#695F4C", "#EBBC5E", "#FFFBB8" ],
        [ "#2E2F38", "#FFD63E", "#FFB54B", "#E88638", "#8A221C" ],
        [ "#121212", "#E6F2DA", "#C9F24B", "#4D7B85", "#23383D" ],
        [ "#343F40", "#736751", "#F2D7B6", "#BFAC95", "#8C3F3F" ],
        [ "#000000", "#2D2B2A", "#561812", "#B81111", "#FFFFFF" ],
        [ "#333B3A", "#B4BD51", "#543B38", "#61594D", "#B8925A" ] ];
var theme;

var worldAABB, world, iterations = 1, timeStep = 1 / 15;

var walls = [];
var wall_thickness = 200;
var wallsSetted = false;

var bodies, elements, text;

var createMode = false;
var destroyMode = false;

var isMouseDown = false;
var mouseJoint;
var mouse = { x: 0, y: 0 };
var gravity = { x: 0, y: 1 };

var PI2 = Math.PI * 2;

var timeOfLastTouch = 0;

init();
play();

function init() {

    canvas = document.getElementById( 'canvas' );

    document.onmousedown = onDocumentMouseDown;
    document.onmouseup = onDocumentMouseUp;
    document.onmousemove = onDocumentMouseMove;
    document.ondblclick = onDocumentDoubleClick;

    document.addEventListener( 'touchstart', onDocumentTouchStart, false );
    document.addEventListener( 'touchmove', onDocumentTouchMove, false );
    document.addEventListener( 'touchend', onDocumentTouchEnd, false );

    window.addEventListener( 'deviceorientation', onWindowDeviceOrientation, false );

    // init box2d

    worldAABB = new b2AABB();
    worldAABB.minVertex.Set( -200, -200 );
    worldAABB.maxVertex.Set( window.innerWidth + 200, window.innerHeight + 200 );

    world = new b2World( worldAABB, new b2Vec2( 0, 0 ), true );

    setWalls();
    reset();
}


function play() {

    setInterval( loop, 1000 / 40 );
}

function reset() {

    var i;

    if ( bodies ) {

        for ( i = 0; i < bodies.length; i++ ) {

            var body = bodies[ i ]
            canvas.removeChild( body.GetUserData().element );
            world.DestroyBody( body );
            body = null;
        }
    }

    // color theme
    theme = themes[ Math.random() * themes.length >> 0 ];
    document.body.style[ 'backgroundColor' ] = theme[ 0 ];

    bodies = [];
    elements = [];

    createInstructions();

    for( i = 0; i < 10; i++ ) {

        createBall();

    }

}

//

function onDocumentMouseDown() {

    isMouseDown = true;
    return false;
}

function onDocumentMouseUp() {

    isMouseDown = false;
    return false;
}

function onDocumentMouseMove( event ) {

    mouse.x = event.clientX;
    mouse.y = event.clientY;
}

function onDocumentDoubleClick() {

    reset();
}

function onDocumentTouchStart( event ) {

    if( event.touches.length == 1 ) {

        event.preventDefault();

        // Faking double click for touch devices

        var now = new Date().getTime();

        if ( now - timeOfLastTouch  < 250 ) {

            reset();
            return;
        }

        timeOfLastTouch = now;

        mouse.x = event.touches[ 0 ].pageX;
        mouse.y = event.touches[ 0 ].pageY;
        isMouseDown = true;
    }
}

function onDocumentTouchMove( event ) {

    if ( event.touches.length == 1 ) {

        event.preventDefault();

        mouse.x = event.touches[ 0 ].pageX;
        mouse.y = event.touches[ 0 ].pageY;

    }

}

function onDocumentTouchEnd( event ) {

    if ( event.touches.length == 0 ) {

        event.preventDefault();
        isMouseDown = false;

    }

}

function onWindowDeviceOrientation( event ) {

    if ( event.beta ) {

        gravity.x = Math.sin( event.gamma * Math.PI / 180 );
        gravity.y = Math.sin( ( Math.PI / 4 ) + event.beta * Math.PI / 180 );

    }

}

//

function createInstructions() {

    var size = 250;

    var element = document.createElement( 'div' );
    element.width = size;
    element.height = size;    
    element.style.position = 'absolute';
    element.style.left = -200 + 'px';
    element.style.top = -200 + 'px';
    element.style.cursor = "default";

    canvas.appendChild(element);
    elements.push( element );

    var circle = document.createElement( 'canvas' );
    circle.width = size;
    circle.height = size;

    var graphics = circle.getContext( '2d' );

    graphics.fillStyle = theme[ 3 ];
    graphics.beginPath();
    graphics.arc( size * .5, size * .5, size * .5, 0, PI2, true );
    graphics.closePath();
    graphics.fill();

    element.appendChild( circle );

    text = document.createElement( 'div' );
    text.onSelectStart = null;
    text.innerHTML = ';
    text.style.color = theme[1];
    text.style.position = 'absolute';
    text.style.left = '0px';
    text.style.top = '0px';
    text.style.fontFamily = 'Georgia';
    text.style.textAlign = 'center';
    element.appendChild(text);

    text.style.left = ((250 - text.clientWidth) / 2) +'px';
    text.style.top = ((250 - text.clientHeight) / 2) +'px';    

    var b2body = new b2BodyDef();

    var circle = new b2CircleDef();
    circle.radius = size / 2;
    circle.density = 1;
    circle.friction = 0.3;
    circle.restitution = 0.3;
    b2body.AddShape(circle);
    b2body.userData = {element: element};

    b2body.position.Set( Math.random() * stage[2], Math.random() * -200 );
    b2body.linearVelocity.Set( Math.random() * 400 - 200, Math.random() * 400 - 200 );
    bodies.push( world.CreateBody(b2body) );    
}

function createBall( x, y ) {

    var x = x || Math.random() * stage[2];
    var y = y || Math.random() * -200;

    var size = (Math.random() * 100 >> 0) + 20;

    var element = document.createElement("canvas");
    element.width = size;
    element.height = size;
    element.style.position = 'absolute';
    element.style.left = -200 + 'px';
    element.style.top = -200 + 'px';
    element.style.WebkitTransform = 'translateZ(0)';
    element.style.MozTransform = 'translateZ(0)';
    element.style.OTransform = 'translateZ(0)';
    element.style.msTransform = 'translateZ(0)';
    element.style.transform = 'translateZ(0)';

    var graphics = element.getContext("2d");

    var num_circles = Math.random() * 10 >> 0;

    for (var i = size; i > 0; i-= (size/num_circles)) {

        graphics.fillStyle = theme[ (Math.random() * 4 >> 0) + 1];
        graphics.beginPath();
        graphics.arc(size * .5, size * .5, i * .5, 0, PI2, true); 
        graphics.closePath();
        graphics.fill();
    }

    canvas.appendChild(element);

    elements.push( element );

    var b2body = new b2BodyDef();

    var circle = new b2CircleDef();
    circle.radius = size >> 1;
    circle.density = 1;
    circle.friction = 0.3;
    circle.restitution = 0.3;
    b2body.AddShape(circle);
    b2body.userData = {element: element};

    b2body.position.Set( x, y );
    b2body.linearVelocity.Set( Math.random() * 400 - 200, Math.random() * 400 - 200 );
    bodies.push( world.CreateBody(b2body) );
}

//

function loop() {

    if (getBrowserDimensions()) {

        setWalls();

    }

    delta[0] += (0 - delta[0]) * .5;
    delta[1] += (0 - delta[1]) * .5;

    world.m_gravity.x = gravity.x * 350 + delta[0];
    world.m_gravity.y = gravity.y * 350 + delta[1];

    mouseDrag();
    world.Step(timeStep, iterations);

    for (i = 0; i < bodies.length; i++) {

        var body = bodies[i];
        var element = elements[i];

        element.style.left = (body.m_position0.x - (element.width >> 1)) + 'px';
        element.style.top = (body.m_position0.y - (element.height >> 1)) + 'px';

        if (element.tagName == 'DIV') {

            var style = 'rotate(' + (body.m_rotation0 * 57.2957795) + 'deg) translateZ(0)';
            text.style.WebkitTransform = style;
            text.style.MozTransform = style;
            text.style.OTransform = style;
            text.style.msTransform = style;
            text.style.transform = style;

        }

    }

}


// .. BOX2D UTILS

function createBox(world, x, y, width, height, fixed) {

    if (typeof(fixed) == 'undefined') {

        fixed = true;

    }

    var boxSd = new b2BoxDef();

    if (!fixed) {

        boxSd.density = 1.0;

    }

    boxSd.extents.Set(width, height);

    var boxBd = new b2BodyDef();
    boxBd.AddShape(boxSd);
    boxBd.position.Set(x,y);

    return world.CreateBody(boxBd);

}

function mouseDrag()
{
    // mouse press
    if (createMode) {

        createBall( mouse.x, mouse.y );

    } else if (isMouseDown && !mouseJoint) {

        var body = getBodyAtMouse();

        if (body) {

            var md = new b2MouseJointDef();
            md.body1 = world.m_groundBody;
            md.body2 = body;
            md.target.Set(mouse.x, mouse.y);
            md.maxForce = 30000 * body.m_mass;
            // md.timeStep = timeStep;
            mouseJoint = world.CreateJoint(md);
            body.WakeUp();

        } else {

            createMode = true;

        }

    }

    // mouse release
    if (!isMouseDown) {

        createMode = false;
        destroyMode = false;

        if (mouseJoint) {

            world.DestroyJoint(mouseJoint);
            mouseJoint = null;

        }

    }

    // mouse move
    if (mouseJoint) {

        var p2 = new b2Vec2(mouse.x, mouse.y);
        mouseJoint.SetTarget(p2);
    }
}

function getBodyAtMouse() {

    // Make a small box.
    var mousePVec = new b2Vec2();
    mousePVec.Set(mouse.x, mouse.y);

    var aabb = new b2AABB();
    aabb.minVertex.Set(mouse.x - 1, mouse.y - 1);
    aabb.maxVertex.Set(mouse.x + 1, mouse.y + 1);

    // Query the world for overlapping shapes.
    var k_maxCount = 10;
    var shapes = new Array();
    var count = world.Query(aabb, shapes, k_maxCount);
    var body = null;

    for (var i = 0; i < count; ++i) {

        if (shapes[i].m_body.IsStatic() == false) {

            if ( shapes[i].TestPoint(mousePVec) ) {

                body = shapes[i].m_body;
                break;

            }

        }

    }

    return body;

}

function setWalls() {

    if (wallsSetted) {

        world.DestroyBody(walls[0]);
        world.DestroyBody(walls[1]);
        world.DestroyBody(walls[2]);
        world.DestroyBody(walls[3]);

        walls[0] = null; 
        walls[1] = null;
        walls[2] = null;
        walls[3] = null;
    }

    walls[0] = createBox(world, stage[2] / 2, - wall_thickness, stage[2], wall_thickness);
    walls[1] = createBox(world, stage[2] / 2, stage[3] + wall_thickness, stage[2], wall_thickness);
    walls[2] = createBox(world, - wall_thickness, stage[3] / 2, wall_thickness, stage[3]);
    walls[3] = createBox(world, stage[2] + wall_thickness, stage[3] / 2, wall_thickness, stage[3]);    

    wallsSetted = true;

}

// BROWSER DIMENSIONS

function getBrowserDimensions() {

    var changed = false;

    if (stage[0] != window.screenX) {

        delta[0] = (window.screenX - stage[0]) * 50;
        stage[0] = window.screenX;
        changed = true;

    }

    if (stage[1] != window.screenY) {

        delta[1] = (window.screenY - stage[1]) * 50;
        stage[1] = window.screenY;
        changed = true;

    }

    if (stage[2] != window.innerWidth) {

        stage[2] = window.innerWidth;
        changed = true;

    }

    if (stage[3] != window.innerHeight) {

        stage[3] = window.innerHeight;
        changed = true;

    }

    return changed;

}

地点mouseDrag方法就完毕了鼠标拖拽甩动小球的功效,这也是该卡通最要紧的章程。全部代码能够下载源码来斟酌。源码下载>>