使ç¨åªå忏 餿ä½å®ç°ä¸äºå¨ç»çç®å WebGL çä¾åã
åªåå¨ç»æ¬ä¾ä¸ï¼æä»¬ä½¿ç¨scissor()
å clear()
ãæä»¬å次建ç«ä¸ä¸ªå¨ç»å¾ªç¯ä½¿ç¨è®¡æ¶å¨ã注æï¼è¿æ¬¡æ¯æ¹åçä½ç½® (åªååº) æ´æ°æ¯ä¸å¸§ (æä»¬è®¾ç½®å¸§çå¤§çº¦æ¯ 17 毫ç§ï¼çº¦ 60 fps -帧æ¯ç§)
ç¸æ¯ä¹ä¸ï¼æ¹åçé¢è² (ç¨clearColor
) ä»
å建ä¸ä¸ªæ°çæ¹åãè¿æ¯ä¸ä¸ªå¾å¥½çæ¼ç¤ºWebGLæ¯ä¸ä¸ªç¶ææºãå¯¹äºæ¯ä¸ä¸ªæ¹åï¼æä»¬è®¾ç½®å®çé¢è²ï¼ç¶ååªæ´æ°å®çä½ç½®æ¯ä¸å¸§ãWebGL çæ¸
æ°çé¢è²ç¶æç»´æå¨è®¾å®å¼ï¼ç´å°æä»¬å次æ¹åå®å建ä¸ä¸ªæ°çæ¹åã
<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 HTML5 canvas.</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;
}
"use strict";
window.addEventListener("load", setupAnimation, false);
// Variables to hold the WebGL context, and the color and
// position of animated squares.
var gl,
color = getRandomColor(),
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];
var button = document.querySelector("button");
var timer;
function startAnimation(evt) {
button.removeEventListener(evt.type, startAnimation, false);
button.addEventListener("click", stopAnimation, false);
document.querySelector("strong").innerHTML = "stop";
timer = setInterval(drawAnimation, 17);
drawAnimation();
}
function stopAnimation(evt) {
button.removeEventListener(evt.type, stopAnimation, false);
button.addEventListener("click", startAnimation, false);
document.querySelector("strong").innerHTML = "start";
clearInterval(timer);
}
stopAnimation({ type: "click" });
}
// Variables to hold the size and velocity of the square.
var size = [60, 60],
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 sqaure 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() {
var canvas = document.querySelector("canvas");
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
var gl =
canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
if (!gl) {
var paragraph = document.querySelector("p");
paragraph.innerHTML =
"Failed to get WebGL context." +
"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;
}
The source code of this example is also available on GitHub.
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