【发布时间】:2014-03-28 04:14:56
【问题描述】:
我正在尝试在屏幕上实现混乱的波动,就好像您的计算机即将爆炸一样。在 Photoshop 中,这个概念是由噪声过滤器创建的,然后通过噪声推动一个滤波器以使其具有这种外观 我已经使用<canvas> 元素创建了噪声,但是有人对如何推动一个通过噪音或任何其他方式来获得这种预期的效果?
我有 4 个示例 jsfiddles 可以作为起点: 感谢Ken 提供这些示例:
really close starting point that i found
JS:
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
// a variant using fixed canvas size but strecthes the result.
// emulates interference/bad reception
// using a different "noise" algo
canvas.width = canvas.height = 256;
function resize() {
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
}
resize();
window.onresize = resize;
function noise(ctx) {
var w = ctx.canvas.width,
h = ctx.canvas.height,
idata = ctx.getImageData(0, 0, w, h),
buffer32 = new Uint32Array(idata.data.buffer),
len = buffer32.length,
i = 0,
pr = 456 * Math.random(),
prs = 716 * Math.random();;
for(; i < len;) {
buffer32[i++] = ((pr % 255)|0) << 24;
pr += prs * 1.2;
}
ctx.putImageData(idata, 0, 0);
}
var toggle = true;
// added toggle to get 30 FPS instead of 60 FPS
(function loop() {
toggle = !toggle;
if (toggle) {
requestAnimationFrame(loop);
return;
}
noise(ctx);
requestAnimationFrame(loop);
})();
HTML
<canvas id="canvas"></canvas>
CSS
html, body {
background:#0000cc;
margin:0;
}
#canvas {
position:fixed;
background:#0000dd;
opacity: .2;
}
【问题讨论】:
-
你总是可以使用这样的东西:jsfiddle.net/m1erickson/DAShs 来生成波浪,你可以将它与 globalCompositeOperation = "source-in";获得静态的波浪 (developer.mozilla.org/samples/canvas-tutorial/…)
-
我正在阅读这篇文章...当谈到画布时,我是个大菜鸟。任何答案将不胜感激。
标签: javascript html animation canvas html5-canvas