Tag Archives: WebGL

WebGL – sound test example with Three.js .

This is the result of my example : webgl test sound .

And this it’s the source code :

<!DOCTYPE html>
<html lang=”en”>
<head>
<title>three.js misc – sound</title>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0″>
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
font-family:Monospace;
font-size:13px;
text-align:center;
font-weight: bold;
text-align:center;
}
</style>
</head>
<body>
<div id=”container”></div>
<script src=”../js/three.min.js”></script>
<script src=”../js/controls/FirstPersonControls.js”></script>
<script src=”../js/Detector.js”></script>
<script>

if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

var container;
var camera, controls, scene, renderer;
var light, pointLight;

var mesh;
var material_sphere1, material_sphere2;

var clock = new THREE.Clock();

init();
animate();

function init() {

container = document.getElementById( ‘container’ );

camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 0, 25, 0 );

var listener = new THREE.AudioListener();
camera.add( listener );

controls = new THREE.FirstPersonControls( camera );

controls.movementSpeed = 70;
controls.lookSpeed = 0.05;
controls.noFly = true;
controls.lookVertical = false;

scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x000000, 0.0035 );

light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 0, 0.5, 1 ).normalize();
scene.add( light );

var sound1 = new THREE.Audio( listener );
sound1.load( ‘../sounds/sound.wav’ );
sound1.setRefDistance( 20 );
sound1.autoplay = true;
scene.add( sound1 );

var helper = new THREE.GridHelper( 500, 10 );
helper.color1.setHex( 0x444444 );
helper.color2.setHex( 0x444444 );
helper.position.y = 0.1
scene.add( helper );

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.innerHTML = “”;
container.appendChild( renderer.domElement );
window.addEventListener( ‘resize’, onWindowResize, false );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
controls.handleResize();
}

function animate() {
requestAnimationFrame( animate );
render();
}

function render() {

var delta = clock.getDelta(),
time = clock.getElapsedTime() * 5;
controls.update( delta );
renderer.render( scene, camera );
}
</script>
</body>
</html>

Posted in All, HTML 5, javascript, Programming, web, WebGL. Tagged with , , , , , , .

Introduction about online jsFiddle website .

This website named jsFiddle it’s a great web tool.
You can allow you to test your javascript scripts, CSS, HTML … over internet.
The site is made by Piotr Zalewa.
One great article about Piotr Zalewa can be found here.
You can read the documentation for jsFiddle.
What I used this website tool?
First, I can used to write javascript, CSS, HTML and test all over internet.
Also this website come with some features like:

  • I can used the Codereview / kata;
  • Debugging my scripts;
  • Collaboration with users can sign up and work on the same fiddle simultaneously;
  • Unit testing with for example QUnit and Jasmine;
  • clean up the code – “tidy it” and run jslint;
  • Working on that CSS easy and provide examples for forums, blogs;
  • I can testing new frameworks like WebGL…

Now let’s see how to do it. Create your account and take a first look of the website.

jsfiddle_net

You will see four areas / panels:

  • HTML  – will type your html code;
  • JavaScript – your javascript testing example;
  • CSS – the css area of source code;
  • … and tha result / output.

In the top of the webpage you will have:

  • Run – this button will run your example;
  • Save – this will save your example and will come after with few buttons:
    • Update – will update your run example and you will see it into result /output area;
    • Fork – this button is used to create a new fiddle from the existing one;
    • Base – user who created the first version may change the base version;
  • TidyUp – his button will help format your code and cleaning up JavaScript formatting;
  • JSHint – this will help verify that your JavaScript is valid;
  • Collaboration – is a service for your website that makes it easy to collaborate in real-time on – need to : Edit fiddle – JSFiddle to use mail, chat, microphone ;
  • Share – will share your work.

The left side of this website tool will come with one menu.
Most used is the External Resources where you can put the link of the javascript and used with your source code. for example add this: https://cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.js.
The Languages let you to set the JavaScript / CoffeeScript and also the CSS / SCSS.
I will come with some tutorials about WebGL and some usefull javascript scripts.
Thank’s .

Posted in All, Programming, web, WebGL. Tagged with , , , , , .

Test your javascript online with many libs.

If you want to test some java script then use jsbin.com.

This website allow you to script html, css, javascript with many libs.

This libs include JQuery, Bootstrap, Angular, Dojo, Enyo, CoffeeScript and more…

After you edit your script just press button Run with JS.

See the next image where I test if WebGL is supported by my browser.

javascript programming online

Posted in All, HTML 5, Programming, web, WebGL. Tagged with , , , , .

How to Enable WebGL in Firefox

Type about:config in Firefox’s address bar

Now will need to make the following changes:

Search webgl.force-enabled and set to true.

See images:

firefox-webgl-001
firefox-webgl-002

Posted in WebGL. Tagged with , .

Simple WebGL example

WebGL is a software library and cross-platform API that extends the capability of the JavaScript programming language.
That brings OpenGL to the web as a 3D drawing context within HTML
Today we present a simple example of using WebGL.
Here, there is an html content, java script and content of WebGL content.
The three main sections are combined.
In section WebGL enter and the fragment shader and vertex shader.
For those who have worked with OpenGL will be easy to recognize specific functions.
Also the source code written for OpenGL format file.

<script type=”text/javascript”>
function shaderProgram(gl, vs, fs) ;
{

var prog = gl.createProgram(); var addshader = function(type, source) ;
{
var s = gl.createShader((type == ‘vertex’) ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER); gl.shaderSource(s, source); gl.compileShader(s); if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) ;
{
throw “Could not compile “+type+ ” shader:\n\n”+gl.getShaderInfoLog(s); } gl.attachShader(prog, s); }; addshader(‘vertex’, vs); addshader(‘fragment’, fs); gl.linkProgram(prog); if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) ;
{
throw “Could not link the shader program!”; } return prog; } function attributeSetFloats(gl, prog, attr_name, rsize, arr) ;
{
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(arr),gl.STATIC_DRAW); var attr = gl.getAttribLocation(prog, attr_name); gl.enableVertexAttribArray(attr); gl.vertexAttribPointer(attr, rsize, gl.FLOAT, false, 0, 0); } function draw() ;
{
try ;
{
var gl = document.getElementById(“webgl”) .getContext(“experimental-webgl”); if (!gl) ;
{
throw “x”; } } catch (err) ;
{
throw “Your web browser does not support WebGL!”; } gl.clearColor(0.0, 1.0, 0.0, 1); gl.clear(gl.COLOR_BUFFER_BIT); var prog = shaderProgram(gl, “attribute vec3 pos; “+ “void main() ;
{
“+” gl_Position = vec4(pos, 2.0);”+ “}”, “void main() ;
{
“+”gl_FragColor = vec4(0.0, 0.5, 0.1, 1.0);”+ “}” ); gl.useProgram(prog); attributeSetFloats(gl, prog, “pos”, 3, [ -1, 1, 0, 0, 1, 0, 0, -1, 0,1, 0, 0 ]); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); } function init() ;
{
try ;
{
draw();} catch (e) ;
{
alert(“Error: “+e); } } setTimeout(init, 100);

</script>

<canvas id=”webgl” width=”340″ height=”280″></canvas>

 

The result is this:

Posted in 3D, Programming, WebGL. Tagged with , , .