在画布上假CRT效果

Jam*_*ger 4 javascript css html5 canvas css3

我希望能够使我的画布看起来像是在弯曲的圆形CRT屏幕上,类似于这两个图像:

图一 图二

我如何对HTML5画布执行此操作?我很乐意在画布绘图本身或者应用于画布的CSS样式中执行此操作.

谢谢!

Rud*_*ser 7

快速搜索给了我这个网站:WebGL虚假CRT效果HTML5游戏,它使用glfx.js库.

它是一个用于将实时WebGL效果应用于图像的库,但canvas标签实际上可以作为图像源进行处理...

该演示使用图像覆盖到canvas.

以及实际创建效果的代码:

// Make sure you've included the glfx.js script in your code!

// Here I load a PNG with scanlines that I overwrite onto the 2D game's canvas.
// This file happens to be customized for the demo game, so to make this a
// general solution we'll need a generic scanline image or we'll generate them
// procedurally.
// Start loading the image right away, not after the onload event.
var lines = new Image();
lines.src = 'http://i.imgur.com/TAJ0Zkw.png';

window.addEventListener('load', fakeCRT, false);

function fakeCRT() {
    var glcanvas, source, srcctx, texture, w, h, hw, hh, w75;

    // Try to create a WebGL canvas (will fail if WebGL isn't supported)
    try {
       glcanvas = fx.canvas();
    } catch (e) {return;}

    // Assumes the first canvas tag in the document is the 2D game, but
    // obviously we could supply a specific canvas element here.
    source = document.getElementsByTagName('canvas')[0];
    srcctx = source.getContext('2d');

    // This tells glfx what to use as a source image
    texture = glcanvas.texture(source);

    // Just setting up some details to tweak the bulgePinch effect
    w = source.width;
    h = source.height;
    hw = w / 2;
    hh = h / 2;
    w75 = w * 0.75;

    // Hide the source 2D canvas and put the WebGL Canvas in its place
    source.parentNode.insertBefore(glcanvas, source);
    source.style.display = 'none';
    glcanvas.className = source.className;
    glcanvas.id = source.id;
    source.id = 'old_' + source.id;

    // It is pretty silly to setup a separate animation timer loop here, but
    // this lets us avoid monkeying with the source game's code.
    // It would make way more sense to do the following directly in the source
    // game's draw function in terms of performance.
    setInterval(function () {
        // Give the source scanlines
        srcctx.drawImage(lines, 0, 0, w, h);

        // Load the latest source frame
        texture.loadContentsOf(source);

        // Apply WebGL magic
        glcanvas.draw(texture)
            .bulgePinch(hw, hh, w75, 0.12)
            .vignette(0.25, 0.74)
            .update();
    }, Math.floor(1000 / 40));
}
Run Code Online (Sandbox Code Playgroud)

通常可以工作,虽然我应该警告你,Safari 6设法冻结我的整个系统至少几分钟......但不是每次都.你可能会对Chrome更开心.