欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

WebGL源码:HTML5 模拟现实物理效果

介绍 评论 失效链接反馈

一个交互式的WebGL代码,点击生成球体from clipboard
from clipboard
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;

}

}

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复