【问题标题】:Water ripple effect on background of website网站背景上的水波纹效果
【发布时间】:2013-07-16 03:22:44
【问题描述】:

一位客户要求我将a similar water ripple effect 应用到他们网站上的背景图片(并且仅适用于背景图片)。

考虑到这是使用画布,这似乎是不可能的。有人知道我可以将这种效果应用于页面上的背景图像吗?

【问题讨论】:

标签: javascript css html graphics


【解决方案1】:

如需高性能、支持 WebGL 的解决方案,请查看此项目:

jQuery WebGL Ripples

here 提供演示。

不过,这项技术目前还不是每个设备上的每个人都可以使用。

【讨论】:

  • 请指导我如何实现?我正是需要这该死的好东西。请帮忙。
  • @SFDC_Learner 你可以看看main link如何调用JS函数在你的页面中启用效果。您还可以查看实时的demo page 源代码,以了解 HTML 标记是如何以及如何调用 JS 函数的。
【解决方案2】:

看看这两个演示,我认为你可以根据这些演示来创建你的。

29a.ch 和 mrdoob

希望对你有帮助

【讨论】:

    【解决方案3】:

    不确定您是否还在寻找,但这有一个关于效果的很好的演示/教程: http://code.almeros.com/water-ripple-canvas-and-javascript#.Ukpfe2QY2ok

    演示在这里: http://code.almeros.com/code-examples/water-effect-canvas/#.UkpiQmQY2ol

    【讨论】:

      【解决方案4】:

      尝试使用我发布的脚本,只需将其复制并粘贴到您的代码中即可。我已经用代码中的cmets解释了

      (function(){
          var canvas = document.getElementById('c'),
      
              /** @type {CanvasRenderingContext2D} */
              ctx = canvas.getContext('2d'),
              width = 500,
              height = 200,
      
              half_width = width >> 1,
              half_height = height >> 1,
              size = width * (height + 2) * 2,
      
              delay = 30,
              oldind = width,
              newind = width * (height + 3),
      
              riprad = 3,
              ripplemap = [],
              last_map = [],
              ripple,
              texture,
      
              line_width = 20,
              step = line_width * 2, 
              count = height / line_width;
      
          canvas.width = width;
          canvas.height = height;
      
          /*
           * Water ripple demo can work with any bitmap image
           */
          with (ctx) {
              fillStyle = '#a2ddf8';
              fillRect(0, 0, width, height);
      
              fillStyle = '#07b';
              save();
              rotate(-0.785);
              for (var i = 0; i < count; i++) {
                  fillRect(-width, i * step, width * 3, line_width);
              }
      
              restore();
          }
      
          texture = ctx.getImageData(0, 0, width, height);
          ripple = ctx.getImageData(0, 0, width, height);
      
          for (var i = 0; i < size; i++) {
              last_map[i] = ripplemap[i] = 0;
          }
      
          /**
           * Main loop
           */
          function run() {
              newframe();
              ctx.putImageData(ripple, 0, 0);
          }
      
          /**
           * Disturb water at specified point
           */
          function disturb(dx, dy) {
              dx <<= 0;
              dy <<= 0;
      
              for (var j = dy - riprad; j < dy + riprad; j++) {
                  for (var k = dx - riprad; k < dx + riprad; k++) {
                      ripplemap[oldind + (j * width) + k] += 128;
                  }
              }
          }
      
          /**
           * Generates new ripples
           */
          function newframe() {
              var a, b, data, cur_pixel, new_pixel, old_data;
      
              var t = oldind; oldind = newind; newind = t;
              var i = 0;
      
              // create local copies of variables to decrease
              // scope lookup time in Firefox
              var _width = width,
                  _height = height,
                  _ripplemap = ripplemap,
                  _last_map = last_map,
                  _rd = ripple.data,
                  _td = texture.data,
                  _half_width = half_width,
                  _half_height = half_height;
      
              for (var y = 0; y < _height; y++) {
                  for (var x = 0; x < _width; x++) {
                      var _newind = newind + i, _mapind = oldind + i;
                      data = (
                          _ripplemap[_mapind - _width] + 
                          _ripplemap[_mapind + _width] + 
                          _ripplemap[_mapind - 1] + 
                          _ripplemap[_mapind + 1]) >> 1;
      
                      data -= _ripplemap[_newind];
                      data -= data >> 5;
      
                      _ripplemap[_newind] = data;
      
                      //where data=0 then still, where data>0 then wave
                      data = 1024 - data;
      
                      old_data = _last_map[i];
                      _last_map[i] = data;
      
                      if (old_data != data) {
                          //offsets
                          a = (((x - _half_width) * data / 1024) << 0) + _half_width;
                          b = (((y - _half_height) * data / 1024) << 0) + _half_height;
      
                          //bounds check
                          if (a >= _width) a = _width - 1;
                          if (a < 0) a = 0;
                          if (b >= _height) b = _height - 1;
                          if (b < 0) b = 0;
      
                          new_pixel = (a + (b * _width)) * 4;
                          cur_pixel = i * 4;
      
                          _rd[cur_pixel] = _td[new_pixel];
                          _rd[cur_pixel + 1] = _td[new_pixel + 1];
                          _rd[cur_pixel + 2] = _td[new_pixel + 2];
                      }
      
                      ++i;
                  }
              }
          }
      
          canvas.onmousemove = function(/* Event */ evt) {
              disturb(evt.offsetX || evt.layerX, evt.offsetY || evt.layerY);
          };
      
          setInterval(run, delay);
      
          // generate random ripples
          var rnd = Math.random;
          setInterval(function() {
              disturb(rnd() * width, rnd() * height);
          }, 700);
      })();
      &lt;canvas id="c"&gt;&lt;/canvas&gt;

      【讨论】:

        【解决方案5】:
        .paperButton {
          display: block;
          text-align: center;
          text-decoration: none;
          position: relative;
          overflow: hidden;
          -webkit-transition: all 0.2s ease;
          -moz-transition: all 0.2s ease;
          -o-transition: all 0.2s ease;
          transition: all 0.2s ease;
          z-index: 0;
          cursor:pointer;
        }
        .animate {
          -webkit-animation: ripple 0.65s linear;
          -moz-animation: ripple 0.65s linear;
          -ms-animation: ripple 0.65s linear;
          -o-animation: ripple 0.65s linear;
          animation: ripple 0.65s linear;
        }
         @-webkit-keyframes 
        ripple {  100% {
        opacity: 0;
        -webkit-transform: scale(2.5);
        }
        }
        @-moz-keyframes 
        ripple {  100% {
        opacity: 0;
        -moz-transform: scale(2.5);
        }
        }
        @-o-keyframes 
        ripple {  100% {
        opacity: 0;
        -o-transform: scale(2.5);
        }
        }
        @keyframes 
        ripple {  100% {
        opacity: 0;
        transform: scale(2.5);
        }
        }
        
        
        $(function(){
            var ink, i, j, k;
            $(".paperButton").mousedown(function(e){    
                    if($(this).find(".ink").length === 0){
                        $(this).prepend("<span class='ink'></span>");
                    }
        
                    ink = $(this).find(".ink");
                    ink.removeClass("animate");
        
                    if(!ink.height() && !ink.width()){
                        i = Math.max($(this).outerWidth(), $(this).outerHeight());
                        ink.css({height: i, width: i});
                    }
        
                    j = e.pageX - $(this).offset().left - ink.width()/2;
                    k = e.pageY - $(this).offset().top - ink.height()/2;
        
                    ink.css({top: k+'px', left: j+'px'}).addClass("animate");
        
        });
        });
        

        【讨论】:

        • 这看起来是一个很好的解决方案,因为它不依赖于 OpenGL,你有没有机会把它放到一个 jsfiddle 中,看看我们可以看到这个例子在现场工作吗?
        【解决方案6】:

        如果您将照片上传到https://imgur.com/,我找到了解决方案 然后上传完成后按右键单击图像然后复制图像地址并将其放入您的代码中,如下所示: 背景图片:网址(https://i.imgur.com/yourphotolink.jpg) 注意:复制链接后,请检查您的链接是否以 .jpg 或 .png 结尾 如果它以 .jpg?1 结尾,请删除 ?1 所以你的链接以 .jpg 结尾

        我不知道为什么 jquery 波纹效果不支持普通照片,但这些步骤将解决您的问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-06
          • 2014-05-20
          相关资源
          最近更新 更多