Motion of a molecule
How do I make an motion of a molecule?
Another experiment with glsl shaders.. What is a motion of a molecule? How do you make a motion of a molecule? This script and codes were developed by Alex Permyakov on 27 August 2022, Saturday.
Motion of a molecule - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Motion of a molecule</title> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ * { margin: 0; padding: 0;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <script id="sf" type="x-shader/x-fragment">
precision highp float;
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
float ball(vec2 p, float fx, float fy, float ax, float ay) { vec2 r = vec2(p.x + cos(time * fx) * ax, p.y + sin(time * fy) * ay); return 0.09 / length(r);
}
void main(void) { vec2 q = gl_FragCoord.xy / resolution.xy; vec2 p = -1.0 + 2.0 * q; p.x *= resolution.x / resolution.y; float col = 0.0; col += ball(p, 1.0, 2.0, 0.1, 0.2); col += ball(p, 1.5, 2.5, 0.2, 0.3); col += ball(p, 2.0, 3.0, 0.3, 0.4); col += ball(p, 2.5, 3.5, 0.4, 0.5); col += ball(p, 3.0, 4.0, 0.5, 0.6); col += ball(p, 1.5, 0.5, 0.6, 0.7); gl_FragColor = vec4(col, col * 0.3, col, 1.0);
}
</script>
<script id="sv" type="x-shader/x-vertex"> attribute vec4 vPosition; void main (void) { gl_Position = vPosition; }
</script>
<canvas></canvas> <script src="js/index.js"></script>
</body>
</html>
Motion of a molecule - Script Codes CSS Codes
* { margin: 0; padding: 0;
}
Motion of a molecule - Script Codes JS Codes
var canvas = document.querySelector('canvas'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; var ut, st = Date.now(), um, mouseX = 0, mouseY = 0; function initShaders (gl, vertexShaderId, fragmentShaderId) { var vertexEl = document.querySelector(vertexShaderId); var vertexShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vertexShader, vertexEl.text); gl.compileShader(vertexShader); var fragmentEl = document.querySelector(fragmentShaderId); var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fragmentShader, fragmentEl.text); gl.compileShader(fragmentShader); var program = gl.createProgram(); gl.attachShader(program, vertexShader); gl.attachShader(program, fragmentShader); gl.linkProgram(program); gl.useProgram(program); return program; } function initGraphics () { gl = canvas.getContext('webgl'); var width = canvas.width; var height = canvas.height; gl.viewport(0, 0, width, height); canvas.addEventListener('mousemove', function (e) { mouseX = e.pageX / canvas.width; mouseY = e.pageY / canvas.height; }, false); var program = initShaders(gl, '#sv', '#sf'); var buffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buffer); gl.bufferData( gl.ARRAY_BUFFER, new Float32Array([-1, 1, -1, -1, 1, -1, 1, 1]), gl.STATIC_DRAW ); var vPosition = gl.getAttribLocation(program, 'vPosition'); gl.vertexAttribPointer(vPosition, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(vPosition); ut = gl.getUniformLocation(program, 'time'); um = gl.getUniformLocation(program, 'mouse'); var resolution = new Float32Array([canvas.width, canvas.height]); gl.uniform2fv(gl.getUniformLocation(program, 'resolution'), resolution); } function render () { gl.uniform1f(ut, (Date.now() - st) / 1000); gl.uniform2fv(um, new Float32Array([mouseX, mouseY])); gl.drawArrays(gl.TRIANGLE_FAN, 0, 4); requestAnimationFrame(render); } initGraphics(); render();
Developer | Alex Permyakov |
Username | alexdevp |
Uploaded | August 27, 2022 |
Rating | 4.5 |
Size | 2,776 Kb |
Views | 40,480 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Apple WWDC 2015 logo | 2,293 Kb |
Pure CSS3 Tetris preview | 2,451 Kb |
Plazma circle | 2,470 Kb |
3D Particle Heart | 2,860 Kb |
SVG UI Navigation Concept | 13,397 Kb |
Smooth curves | 2,606 Kb |
Audio Player | 17,720 Kb |
Google Particle Logo | 17,309 Kb |
WebGL Unicolorous Loader | 2,816 Kb |
SVG Fluid Menu | 2,777 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Twitch API | Coderpilot | 3,412 Kb |
Mega Menu by Joni | Asakasinsky | 3,171 Kb |
CSS Parent Selector | Tomhodgins | 2,143 Kb |
Airbnb Homepage | SindhujaD | 2,480 Kb |
Reading Grid | Tappily | 4,306 Kb |
Myprofile | SoufianeAbid | 2,451 Kb |
Mega menu | DimaZubkov | 5,066 Kb |
Toggle menu | Seyedi | 2,279 Kb |
Client-side Email Validation | Collizo4sky | 1,538 Kb |
CSS Chat Bubbles | Boylett | 2,094 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!