Ein einfaches WebGL-Beispiel, in dem wir etwas Animationsspaà mit Scherenschnitt- und Löschoperationen haben.
Animation mit ScherenschnittIn diesem Beispiel animieren wir Quadrate mit scissor()
und clear()
. Wir etablieren erneut eine Animationsschleife mit Timern. Beachten Sie, dass diesmal die Position des Quadrats (der Scherenschnitt-Bereich) jedes Frame aktualisiert wird (wir haben die Bildrate auf ungefähr alle 17ms oder ungefähr 60fps â Frames pro Sekunde â eingestellt).
Im Gegensatz dazu wird die Farbe des Quadrats (festgelegt mit clearColor
) nur dann aktualisiert, wenn ein neues Quadrat erstellt wird. Dies ist eine schöne Demonstration von WebGL als Zustandsmaschine. Für jedes Quadrat legen wir einmal seine Farbe fest und aktualisieren dann nur jeden Frame seine Position. Der Löschfarbzustand von WebGL bleibt beim festgelegten Wert, bis wir ihn ändern, wenn ein neues Quadrat erstellt wird.
<p>
WebGL animation by clearing the drawing buffer with solid color and applying
scissor test.
</p>
<button id="animation-onoff">
Press here to <strong>[verb goes here]</strong> the animation.
</button>
<canvas>Your browser does not seem to support canvases.</canvas>
body {
text-align: center;
}
canvas {
display: block;
width: 280px;
height: 210px;
margin: auto;
padding: 0;
border: none;
background-color: black;
}
button {
display: block;
font-size: inherit;
margin: auto;
padding: 0.6em;
}
window.addEventListener("load", setupAnimation, false);
// Variables to hold the WebGL context, and the color and
// position of animated squares.
let gl;
let color = getRandomColor();
let position;
function setupAnimation(evt) {
window.removeEventListener(evt.type, setupAnimation, false);
if (!(gl = getRenderingContext())) return;
gl.enable(gl.SCISSOR_TEST);
gl.clearColor(color[0], color[1], color[2], 1.0);
// Unlike the browser window, vertical position in WebGL is
// measured from bottom to top. In here we set the initial
// position of the square to be at the top left corner of the
// drawing buffer.
position = [0, gl.drawingBufferHeight];
const button = document.querySelector("button");
let timer;
function startAnimation(evt) {
button.removeEventListener(evt.type, startAnimation, false);
button.addEventListener("click", stopAnimation, false);
document.querySelector("strong").textContent = "stop";
timer = setInterval(drawAnimation, 17);
drawAnimation();
}
function stopAnimation(evt) {
button.removeEventListener(evt.type, stopAnimation, false);
button.addEventListener("click", startAnimation, false);
document.querySelector("strong").textContent = "start";
clearInterval(timer);
}
stopAnimation({ type: "click" });
}
// Variables to hold the size and velocity of the square.
const size = [60, 60];
let velocity = 3.0;
function drawAnimation() {
gl.scissor(position[0], position[1], size[0], size[1]);
gl.clear(gl.COLOR_BUFFER_BIT);
// Every frame the vertical position of the square is
// decreased, to create the illusion of movement.
position[1] -= velocity;
// When the square hits the bottom of the drawing buffer,
// we override it with new square of different color and
// velocity.
if (position[1] < 0) {
// Horizontal position chosen randomly, and vertical
// position at the top of the drawing buffer.
position = [
Math.random() * (gl.drawingBufferWidth - size[0]),
gl.drawingBufferHeight,
];
// Random velocity between 1.0 and 7.0
velocity = 1.0 + 6.0 * Math.random();
color = getRandomColor();
gl.clearColor(color[0], color[1], color[2], 1.0);
}
}
function getRandomColor() {
return [Math.random(), Math.random(), Math.random()];
}
function getRenderingContext() {
const canvas = document.querySelector("canvas");
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
const gl =
canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
if (!gl) {
const paragraph = document.querySelector("p");
paragraph.textContent =
"Failed. Your browser or device may not support WebGL.";
return null;
}
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
return gl;
}
Der Quellcode dieses Beispiels ist auch auf GitHub verfügbar.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4