【问题标题】:Fake CRT effect on Canvas [closed]Canvas 上的假 CRT 效果 [关闭]
【发布时间】:2016-07-01 15:31:20
【问题描述】:

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

如何对 HTML5 画布执行此操作?我很乐意在画布绘图本身内完成此操作,或者将 CSS 样式应用于画布。

谢谢!

【问题讨论】:

标签: javascript html css canvas


【解决方案1】:

快速搜索给了我这个网站:WebGL Fake CRT Effect for HTML5 Games, 它使用glfx.js 库。

它是一个旨在将实时 WebGL 效果应用于图像的库,但 canvas 标签实际上可以作为图像源来寻址...

演示使用this 图像覆盖到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));
}

通常有效,但我应该警告您,Safari 6 至少有几次成功地将我的整个系统冻结了一分钟......但不是每次都如此。在 Chrome 中您可能会更快乐。

【讨论】:

  • 伟大的收获!如果您可以直接在答案中添加一些相关代码,那就太好了。
  • @morgul 添加了一个示例。
猜你喜欢
  • 1970-01-01
  • 2016-11-15
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多