文章分类 Classification
js实现图片水波倒影效果代码
稿件来源: 互联网 撰稿作者: 太阳光 发表日期: 2018-07-03 阅读次数: 120 查看权限: 游客查看
js实现图片水波倒影效果代码
function waterWave(img, options) { var settings = Object.assign({ 'speed': 1, 'scale': 1, 'waves': 10, 'image': true }, options) var waves = settings.waves var speed = settings.speed / 4 var scale = settings.scale / 2 var ca = document.createElement('canvas') var c = ca.getContext('2d') var img_loaded = false img.parentNode.insertBefore(ca, img) var w, h, dh var offset = 0 var frame = 0 var max_frames = 0 var frames = [] img.onload = function () { this.style.display = 'none' c.save() w = this.width h = this.height * 2 c.canvas.width = w c.canvas.height = h c.drawImage(this, 0, 0) c.scale(1, -1) c.drawImage(this, 0, -this.height * 2) img_loaded = true c.restore() dh = h / 2 var id = c.getImageData(0, h / 2, w, h).data var end = false c.save() while (!end) { var odd = c.getImageData(0, h / 2, w, h) var od = odd.data var pixel = 0 for (var y = 0; y < dh; y++) { for (var x = 0; x < w; x++) { var displacement = (scale * 10 * (Math.sin((dh / (y / waves)) + (-offset)))) | 0 var j = ((displacement + y) * w + x + displacement) * 4 if (j < 0) { pixel += 4 continue } var m = j % (w * 4) var n = scale * 10 * (y / waves) if (m < n || m > (w * 4) - n) { var sign = y < w / 2 ? 1 : -1 od[pixel] = od[pixel + 4 * sign] od[++pixel] = od[pixel + 4 * sign] od[++pixel] = od[pixel + 4 * sign] od[++pixel] = od[pixel + 4 * sign] ++pixel continue } if (id[j + 3] !== 0) { od[pixel] = id[j] od[++pixel] = id[++j] od[++pixel] = id[++j] od[++pixel] = id[++j] ++pixel } else { od[pixel] = od[pixel - w * 4] od[++pixel] = od[pixel - w * 4] od[++pixel] = od[pixel - w * 4] od[++pixel] = od[pixel - w * 4] ++pixel } } } if (offset > speed * (6 / speed)) { offset = 0 max_frames = frame - 1 frame = 0 end = true } else { offset += speed frame++ } frames.push(odd) } c.restore() if (!settings.image) { c.height = c.height / 2 } } setInterval(function () { if (img_loaded) { if (!settings.image) { c.putImageData(frames[frame], 0, 0) } else { c.putImageData(frames[frame], 0, h / 2) } if (frame < max_frames) { frame++ } else { frame = 0 } } }, 33) }
关键词: canvas 编辑时间: 2018-07-03 15:34:18
0
高兴0
支持0
搞笑0
不解0
谎言0
枪稿0
震惊0
无奈0
无聊0
反对0
愤怒
0%(0)
0%(0)
- 中搜索:js实现图片水波倒影效果代码
- 中搜索:js实现图片水波倒影效果代码
- 暂无评论
文章图片 article Pictrue
网友评论