webgl照片粒子化动画效果代码

代码语言:html

所属分类:动画

代码描述:webgl照片粒子化动画效果代码

代码标签: 动画 效果

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


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

<head>

 
<meta charset="UTF-8">
 

 
 
 
<style>
body
{
 
margin: 0;
 
background-color: black;
}

.loading {
 
color: white;
}

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

 
font-family: sans-serif;

 
background-color: black;
}

canvas
{
 
max-height: 100vh;
 
max-width: 100vw;
}

.toggle-help-link {
 
position: absolute;
 
bottom: 10px;
 
right: 10px;

 
font-size: 12px;
 
text-decoration: none;

 
color: lightgrey;
}

.status {
 
position: absolute;
 
bottom: 10px;
 
left: 10px;

 
font-size: 12px;

 
color: lightgrey;
}

.help-container {
 
position: fixed;
 
left: 0;
 
top: 0;
 
right: 0;
 
bottom: 0;

 
display: flex;
 
align-items: center;
 
justify-content: center;

 
background-color: rgba(0, 0, 0, 0.4);
 
color: white;
 
backdrop-filter: blur(3px);
}

.help-container .help {
 
width: 90%;
 
max-width: 500px;
}

.help li {
 
margin-bottom: 1em;
}

.help a {
 
color: inherit;
}
</style>


</head>

<body  >
 
<!-- this pen was built and maintained in https://github.com/callumacrae/sketchbook/blob/master/src/views/ParticlePhotoWebgl.vue -->

<p class="loading">loading…</p>

<div class="canvas-container" style="display: none">
 
<canvas
         
ref="canvas"
          @
click="status = status === 'playing' ? 'paused' : 'playing'"
          @
dragenter.prevent
          @
dragleave.prevent
          @
dragover.prevent
          @
drop.prevent="handleDrop"
         
></canvas>
 
<GlobalEvents target="window" @resize="setSize" />

 
<div class="help-container" v-show="showHelp || showIosWarning">
   
<div class="help" v-show="showIosWarning">
     
<h2 style="color: red">WARNING</h2>
     
<p>
        Due to a bug in how iOS handles webgl instancing, this animation
        performs incredibly poorly on iOS—to the point where it doesn't really
        work at all. On my phone, it runs at 0.0075 fps and makes my phone
        heat up to the temperature of the sun.
     
</p>

     
<p>
        I'd recommend trying this on desktop (it also just looks better), but
        if you really want to see what happens on iOS, you can
       
<a
           
href
           @
click.prevent="
                           showIosWarning = false;
                           status = 'playing';
                           "

           
>
          proceed anyway
</a
         
>.
     
</p>
   
</div>
   
<div class="help" v-show="showHelp">
     
<h2>Instructions</h2>

     
<p>
        This experiment visualises images as collections of animated coloured
        particles.
     
</p>

     
<p>
        In addition to providing a few images (selectable on the configuration
        in the top right of the screen), it also allows you to upload your own
        by dragging and dropping them onto the canvas.
     
</p>

     
<p>
        Through a bit of experimentation, I've found the following makes the
        best images:
     
</p>

     
<ul>
       
<li>
          Lower resolution images or blurred images work better as it prevents
          the particles from changing size and colour too quickly as they move
          about. Most of the demo images are lower than 500px wide.
       
</li>

       
<li>
          Images with lots of contrast between lighter and darker areas make
          more effective graphics. Similarly, images where the details are on
          the lighter parts of the images and the backgrounds are dark also
          look better.
       
</li>

       
<li>
          Some images, especially images with finer details, look better with
          a high number of smaller points (adjust pointSizeMultiplier), while
          others look better with a low number of larger points.
       
</li>
     
</ul>

     
<p>
        To export high quality videos from this you'll need to
       
<a href="https://github.com/callumacrae/sketchbook">clone the repo</a>
        and look for the
<code>this.record</code> call in
        ParticlePhotoWebgl.vue (or ask me for help on Twitter!)
     
</p>
   
</div>
 
</div>

 
<p class="status" v-show="status === 'paused' && !showIosWarning" @click="status = 'playing'">
    paused, click to resume
 
</p>

 
<a
     
v-if="!showIosWarning"
     
class="toggle-help-link"
     
href
     @
click.prevent="showHelp = !showHelp"
     
>
    toggle help
 
</a>
</div>

<script type="nojs" id="vs">
precision mediump float;

//
// Description : Array and textureless GLSL 2D simplex noise function.
//      Author : Ian McEwan, Ashima Arts.
//  Maintainer : stegu
//     Lastmod : 20110822 (ijm)
//     License : Copyright (C) 2011 Ashima Arts. All rights reserved.
//               Distributed under the MIT License. See LICENSE file.
//               https://github.com/ashima/webgl-noise
//               https://github.com/stegu/webgl-noise
//

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)+10.0)*x);
}

float noise2d(vec2 v)
  {
  const vec4 C = vec4(0.211324865405187,  // (3.0-sqrt(3.0))/6.0
                      0.366025403784439,  // 0.5*(sqrt(3.0)-1.0)
                     -0.577350269189626,  // -1.0 + 2.0 * C.x
                      0.024390243902439); // 1.0 / 41.0
// First corner
  vec2 i  = floor(v + dot(v, C.yy) );
  vec2 x0 = v -   i + dot(i, C.xx);

// Other corners
  vec2 i1;
  //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
  //i1.y = 1.0 - i1.x;
  i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
  // x0 = x0 - 0.0 + 0.0 * C.xx ;
  // x1 = x0 - i1 + 1.0 * C.xx ;
  // x2 = x0 - 1.0 + 2.0 * C.xx ;
  vec4 x12 = x0.xyxy + C.xxzz;
  x12.xy -= i1;

// Permutations
  i = mod289(i); // Avoid truncation effects in permutation
  vec3 p = permute( .........完整代码请登录后点击上方下载按钮下载查看

网友评论0