您当前位置:首页 > 文章中心 > SCSCMS

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)
共有0 条评论 发言请遵守【相关规定

网友评论

会员头像
发 表同步腾讯微博    验证码:  点击更新请先登陆
  • 暂无评论
关闭模块文章图片 article Pictrue
  • 我的妈妈爸爸
  • 基于koa2+mysql+vue2.0+Element阳光内容管理系统
  • 代码覆盖率工具 Istanbul 入门教程
  • 全栈工程师的武器——MEAN
  • 9款超炫的 CSS3 复选框(Checkbox)
  • 微信开发在线翻译功能
  • CSS3那些不为人知的高级属性
  • 给easyui的datebox添加清空事件
  • flash写字效果
  • kendoUI系列教程之DropDownList下拉菜单
  • kendoUI系列教程之datetimepicker日期时间选择
  • kendoUI系列教程之datepicker日期选择
  • kendoUI系列教程之combobox下拉列表框
  • kendoUI系列教程之colorpicker
  • kendoUI系列教程之calendar日历表
  • kendoUI系列教程之autocomplete自动补齐