canvas+webgl实现图片像素扭曲动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas+webgl实现图片像素扭曲动画效果代码,可通过lil-gui来调节参数。

代码标签: canvas webgl 图片 像素 扭曲 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">

  
  
  
<style>
html, body {
    margin: 0;
    overflow: hidden;
}

.container {
    width: 100%;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

canvas {
    display: block;
    height: 55%;
}

.lil-gui {
    --width: 450px;
    max-width: 90%;
    --widget-height: 20px;
    font-size: 15px;
    --input-font-size: 15px;
    --padding: 10px;
    --spacing: 10px;
    --slider-knob-width: 5px;
    --background-color: rgba(5, 0, 15, .8);
    --widget-color: rgba(255, 255, 255, .3);
    --focus-color: rgba(255, 255, 255, .4);
    --hover-color: rgba(255, 255, 255, .5);

    --font-family: monospace;
}
</style>

  
  
  
</head>

<body translate="no">
  <input id="image-selector-input" style="visibility:hidden;" type="file">

<div class="container">
    <canvas></canvas>
</div>


<script type="x-shader/x-fragment" id="vertShader">
    precision highp float;

    varying vec2 vUv;
    attribute vec2 a_position;

    void main () {
        vUv = .5 * (a_position + 1.);
        gl_Position = vec4(a_position, 0., 1.);
    }
</script>

<script type="x-shader/x-fragment" id="fragShader">
    precision highp float;
    precision highp sampler2D;

    varying vec2 vUv;
    uniform sampler2D u_input_txr;
    uniform sampler2D u_image_texture;
    uniform vec2 u_pointer;
    uniform vec3 u_dot_color;
    uniform float u_time;
    uniform float u_tile_scale;
    uniform float u_offset;
    
    vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
    vec2 mod289(vec2 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
    vec3 permute(vec3 x) { return mod289(((x*34.0)+1.0)*x); }
    float snoise(vec2 v) {
        const vec4 C = vec4(0.211324865405187, 0.366025403784439, -0.577350269189626, 0.024390243902439);
        vec2 i = floor(v + dot(v, C.yy));
        vec2 x0 = v - i + dot(i, C.xx);
        vec2 i1;
        i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
        vec4 x12 = x0.xyxy + C.xxzz;
        x12.xy -= i1;
        i = mod289(i);
        vec3 p = permute(permute(i.y + vec3(0.0, i1.y, 1.0)) + i.x + vec3(0.0, i1.x, 1.0));
        vec3 m = max(0.5 - vec3(dot(x0, x0), dot(x12.xy, x12.xy), dot(x12.zw, x12.zw)), 0.0);
        m = m*m;
        m = m*m;
        vec3 x = 2.0 * fract(p * C.www) - 1.0;
        vec3 h = abs(x) - 0.5;
        vec3 ox = floor(x + 0.5);
        vec3 a0 = x - ox;
        m *= 1.79284291400159 - 0.85373472095314 * (a0*a0 + h*h);
        vec3 g;
        g.x = a0.x * x0.x + h.x * x0.y;
        g.yz = a0.yz * x12.xz + h.yz * x12.yw;
        return 130.0 * dot(m, g);
    }
    
    void main () {

        vec2 sampling_uv = vUv;
        sampling_uv.y = 1. - sampling_uv.y;
        sampling_uv -= .5;
        sampling_uv /= u_tile_scale;

        vec2 fract_uv = fract(sampling_uv);
        vec2 floor_uv = floor(sampling_uv);

        fract_uv.x += u_offset * snoise(floor_uv + .001 * u_time);

        sampling_uv = (floor_uv + fract_uv);
        sampling_uv *= u_tile_scale;
        sampling_uv += .5;

        vec4 img_shifted = texture2D(u_image_texture, sampling_uv);
        img_shifted.a = step(.1, img_shifted.a);
        img_shifted.a *= step(0., sampling_uv.x);
        img_shifted.a *= (1. - step(1., sampling_uv.x));

        gl_FragColor = img_shifted;
    }
</script>
  
      <script  type="module">
import GUI from "//repo.bfw.wiki/bfwrepo/js/lil-gui.esm.js";

const containerEl = document.querySelector(".container")[0];
const canvasEl = document.querySelector("canvas");
const imgInput = document.querySelector("#image-selector-input");
canvasEl.width = 0;

const devicePixelRatio = Math.min(window.devicePixelRatio, 2);

const params = {
  tileSize: 33,
  offset: .5,
  loadMyImage: () => {
    imgInput.click();
  } };


imgInput.onchange = () => {
  const [file] = imgInput.files;
  if (file) {
    const reader = new FileReader();
    reader.onload = e => {
      loadImage(e.target.result);
    };
    reader.readAsDataURL(file);
.........完整代码请登录后点击上方下载按钮下载查看

网友评论0