Jump to content

CodePen Integration

Recommended Posts

Posted

Hi! I hope to add this codepen url: https://codepen.io/tomwitkovsky/pen/zezQLe to the homepage of my website. I also want to have text in the center of the homepage with a subtitle. How would I do this? Thanks!

  • Replies 12
  • Views 1.6k
  • Created
  • Last Reply
Posted
27 minutes ago, az19 said:

Hi! I hope to add this codepen url: https://codepen.io/tomwitkovsky/pen/zezQLe to the homepage of my website. I also want to have text in the center of the homepage with a subtitle. How would I do this? Thanks!

This topic seems a good start

Add CSS animated background from codepen to squarespace site - Coding and Customization - Squarespace Forum

BeyondSpace - Squarespace Website Developer

🖼️ Lightbox Studio (Pinch/Zoom images, videos - PDFs Lightbox - ...) </>  🗓️ Delivery Date Picker (Date picker form field)
Gallery block 7.1 workaround </> 🤖 Ask me anything

Posted
Just now, bangank36 said:

You should place them in code block for better organized

For some reason, nothing is showing up. I also added the <style> and <script> for CSS and JavaScript. 

Posted
1 minute ago, az19 said:

For some reason, nothing is showing up. I also added the <style> and <script> for CSS and JavaScript. 

I believe there is one dependencies

<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>

image.png.2a88e6248a7095608464312f7508a971.png

BeyondSpace - Squarespace Website Developer

🖼️ Lightbox Studio (Pinch/Zoom images, videos - PDFs Lightbox - ...) </>  🗓️ Delivery Date Picker (Date picker form field)
Gallery block 7.1 workaround </> 🤖 Ask me anything

Posted
1 minute ago, bangank36 said:

I believe there is one dependencies


<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>

image.png.2a88e6248a7095608464312f7508a971.png

Sorry what does one dependencies mean?

Posted
On 4/8/2021 at 1:04 PM, az19 said:

Hi! I hope to add this codepen url: https://codepen.io/tomwitkovsky/pen/zezQLe to the homepage of my website. I also want to have text in the center of the homepage with a subtitle. How would I do this? Thanks!

Try adding a Code BLock > Paste this Codepen code

<div class="particles-wrapper">
<div id="particles-js"></div>
</div>
<style>
  .particles-wrapper {
  background-color: #222;
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  overflow: hidden;
}
</style>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script>
  /* ---- particles.js config ---- */
particlesJS("particles-js", {
  "particles": {
    "number": {
      "value": 300,
      "density": {
        "enable": true,
        "value_area": 1000
      }
    },
    "color": {
      "value": ["#356797", "#356797"]
    },
    "shape": {
      "type": "polygon",
      "stroke": {
        "width": 0,
        "color": "#000000"
      },
      "polygon": {
        "nb_sides": 4
      }
    },
    "size": {
      "value": 5,
      "random": true,
      "anim": {
        "enable": true,
        "speed": 4,
        "size_min": 1,
        "sync": false
      }
    },
    "line_linked": {
      "enable": true,
      "distance": 150,
      "color": "#58636d",
      "opacity": 0.4,
      "width": 1
    },
    "move": {
      "enable": true,
      "speed": 5,
      "direction": "left",
      "random": true,
      "straight": true,
      "out_mode": "out",
      "bounce": false,
      "attract": {
        "enable": true,
        "rotateX": 600,
        "rotateY": 1200
      }
    }
  },
  "interactivity": {
    "detect_on": "canvas",
    "events": {
      "onhover": {
        "enable": false,
        "mode": "grab"
      },
      "onclick": {
        "enable": true,
        "mode": "repulse"
      },
      "resize": true
    },
    "modes": {
      "grab": {
        "distance": 200,
        "line_linked": {
          "opacity": 1
        }
      },
      "bubble": {
        "distance": 400,
        "size": 40,
        "duration": 2,
        "opacity": 8,
        "speed": 3
      },
      "repulse": {
        "distance": 200,
        "duration": 0.4
      },
      "push": {
        "particles_nb": 4
      },
      "remove": {
        "particles_nb": 2
      }
    }
  },
  "retina_detect": true
});
</script>

 

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

  • 2 weeks later...
Posted
On 4/12/2021 at 1:56 AM, tuanphan said:

Try adding a Code BLock > Paste this Codepen code



<div class="particles-wrapper">
<div id="particles-js"></div>
</div>
<style>
  .particles-wrapper {
  background-color: #222;
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  overflow: hidden;
}
</style>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script>
  /* ---- particles.js config ---- */
particlesJS("particles-js", {
  "particles": {
    "number": {
      "value": 300,
      "density": {
        "enable": true,
        "value_area": 1000
      }
    },
    "color": {
      "value": ["#356797", "#356797"]
    },
    "shape": {
      "type": "polygon",
      "stroke": {
        "width": 0,
        "color": "#000000"
      },
      "polygon": {
        "nb_sides": 4
      }
    },
    "size": {
      "value": 5,
      "random": true,
      "anim": {
        "enable": true,
        "speed": 4,
        "size_min": 1,
        "sync": false
      }
    },
    "line_linked": {
      "enable": true,
      "distance": 150,
      "color": "#58636d",
      "opacity": 0.4,
      "width": 1
    },
    "move": {
      "enable": true,
      "speed": 5,
      "direction": "left",
      "random": true,
      "straight": true,
      "out_mode": "out",
      "bounce": false,
      "attract": {
        "enable": true,
        "rotateX": 600,
        "rotateY": 1200
      }
    }
  },
  "interactivity": {
    "detect_on": "canvas",
    "events": {
      "onhover": {
        "enable": false,
        "mode": "grab"
      },
      "onclick": {
        "enable": true,
        "mode": "repulse"
      },
      "resize": true
    },
    "modes": {
      "grab": {
        "distance": 200,
        "line_linked": {
          "opacity": 1
        }
      },
      "bubble": {
        "distance": 400,
        "size": 40,
        "duration": 2,
        "opacity": 8,
        "speed": 3
      },
      "repulse": {
        "distance": 200,
        "duration": 0.4
      },
      "push": {
        "particles_nb": 4
      },
      "remove": {
        "particles_nb": 2
      }
    }
  },
  "retina_detect": true
});
</script>

 

can you do that for this one: https://codepen.io/vincebrown/pen/BNazqL

I've no idea how to use codepen with squarespace 

thank you so much!

Posted
On 4/24/2021 at 4:03 AM, bitterbluess said:

can you do that for this one: https://codepen.io/vincebrown/pen/BNazqL

I've no idea how to use codepen with squarespace 

thank you so much!

I think this Codepen code will create some problems for your site...

Try adding a Code Block > Paste this code

<header class="theader">
    <div class="poster">
      <h1 class="poster__heading">The North Pole 2015 </h1>
          <span class="poster__subheading">The Ultimate Artic Adventure</span>
         <a href="#" class="btn--primary">Book Request</a>
      </div>
  </header>
  <main class="timeline-wrapper">
    <div class="svg-timeline">
      <svg width="8px" height="1490.7px" viewBox="0 0 8 1490.7">
        <line id="timeline" fill="none" stroke="#FFFFFF" stroke-width="8" stroke-miterlimit="10" x1="4" y1="0" x2="4" y2="1490.7"/>
      </svg>
    </div>
    <ol class="timeline clearfix">
      <li class="timeline__item">
        <h3 class="timeline__day">Day 1: <span>Helsinki, Finland</span></h3>
        <p class="timeline__day-sum">Your adventure begins with a one-night stay in Helsinki, Finland's capital.</p>
        <a href="#" class="btn--comments">19 Comments</a>
      </li>
      <li class="timeline__item right">
        <h3 class="timeline__day dayTwo">Day 2: <span>Embarkation Day in Murmansk</span></h3>
        <p class="timeline__day-sum">From Helsinki, your charter flight will take you to Murmansk, Russia, where you'll embark on your voyage to the North Pole and get acquainted with 50 Years of Victory, the world's largest and most powerful icebreaker.</p>
        <a href="#" class="btn--comments">35 Comments</a>
      </li>
      <li class="timeline__item">
        <h3 class="timeline__day dayThree">Day 3-6: <span>Northbound in the Arctic Ocean</span></h3>
        <p class="timeline__day-sum">Watching the ship crush through the Arctic ice pack is a sight you'll never forget, made even more memorable by taking a helicopter flight for a thrilling aerial view of the ship and expansive Arctic Ocean.</p>
        <p class="timeline__day-sum">You can expect days, and hours,to present variable sailing conditions this far north. The crossing from Murmansk to the North Pole can take us anywhere from 5-8 days. This means you'll have plenty of time to get to know your shipmates, stare out at the horizon, grab some drinks at the bar or do some laps in our indoor pool.</p>
        <a href="#" class="btn--comments">72 Comments</a>
      </li>
      <li class="timeline__item right">
        <h3 class="timeline__day dayFour">Day 7: <span>90&deg; North</span></h3>
        <p class="timeline__day-sum">All of the anticipation of the past few days reaches a climax as you reach the North Pole! Many travelers find themselves overcome with emotion, while others are in a festive mood. Take photos, wave a flag, enjoy your moment! Later, everyone enjoys a champagne toast and a BBQ on the ice. Add an exclamation to your success by rising high in one of our hot air balloons to commemerate this great moment.</p>
        <a href="#" class="btn--comments">132 Comments</a>
      </li>
      <li class="timeline__item">
        <h3 class="timeline__day dayFive">Day 8: <span>Southbound in the Arctic Ocean</span></h3>
        <p class="timeline__day-sum">Now you can sit back and relax, everything from here on out is a bonus! Expect more great memories as you head south, you may get lucky and spot a Polar Bear hunting a Seal.</p>
        <a href="#" class="btn--comments">2 Comments</a>
      </li>
      <li class="timeline__item right">
        <h3 class="timeline__day daySix">Day 9-10: <span>Franz Josef Land</span></h3>
        <p class="timeline__day-sum">Now you can sit back and relax, everything from here on out is a bonus! Expect more great memories as you head south, you may get lucky and spot a Polar Bear hunting a Seal.</p>
        <a href="#" class="btn--comments">12 Comments</a>
      </li>
    </ol>
  </main>
<style>
  .right {
  float: right;
}

/* ====== MIXINS ===== */
.poster__subheading, .poster__heading {
  text-align: center;
}

.poster__heading {
  margin: 0 auto;
}

html {
  font-family: "Open Sans";
  font-size: 100%;
}
@media screen and (min-width: 31.25em) {
  html {
    font-size: 105%;
  }
}
@media screen and (min-width: 50em) {
  html {
    font-size: 115%;
  }
}
@media screen and (min-width: 75em) {
  html {
    font-size: 125%;
  }
}

.btn--comments, .btn--primary {
  display: inline;
  text-decoration: none;
  padding: 0.45em 1.25em;
  text-transform: uppercase;
  font-weight: 400;
  letter-spacing: 1px;
  font-size: 0.75rem;
  border-radius: 25px;
  color: white;
  font-family: "Open Sans";
  transition: all 250ms ease;
}

.btn--primary {
  border: 1px solid white;
}
.btn--primary:hover {
  background-color: white;
  color: black;
}

.poster {
  height: 100vh;
  width: 100%;
  background-size: cover;
  background-position: center center;
  background-image: url("http://codepen.vincebrown.me/assets/images/snow-mountains.jpg");
}
.poster__heading {
  width: 95%;
  color: white;
  padding-top: 5.5rem;
  font-weight: 600;
  font-size: 2em;
  transition: all 400ms ease;
}
@media screen and (min-width: 31.25em) {
  .poster__heading {
    font-size: 2.25em;
  }
}
@media screen and (min-width: 50em) {
  .poster__heading {
    font-size: 2.75em;
  }
}
.poster__subheading {
  display: block;
  font-size: 0.3em;
  letter-spacing: 5px;
  font-weight: 400;
  text-transform: uppercase;
  color: #FF9639;
  padding-top: 0.8em;
  margin-bottom: 0.6em;
}

.btn--comments {
  background-color: rgba(255, 255, 255, 0.4);
  border: none;
  font-size: 0.6em;
}
.btn--comments:hover {
  background-color: rgba(255, 255, 255, 0.6);
}

.timeline-wrapper {
  position: relative;
  padding-bottom: 25em;
  background: linear-gradient(#000 10%, #283048 60%, #55679a 70%, rgba(255, 255, 255, 0));
}
.timeline-wrapper:after {
  content: "";
  display: block;
  background-image: url("http://codepen.vincebrown.me/assets/images/white-mountains.jpg");
  height: 500px;
  width: 100%;
  background-size: cover;
  position: absolute;
  background-position: 25% center;
  z-index: -10;
  bottom: 0;
}

.svg-timeline {
  position: absolute;
  margin-left: 50%;
  transform: translateX(-50%);
  display: none;
}
@media screen and (min-width: 50em) {
  .svg-timeline {
    display: block;
  }
}

.timeline {
  transition: all 250ms ease;
  width: 90%;
  margin: 0 auto;
}
@media screen and (min-width: 31.25em) {
  .timeline {
    width: 80%;
  }
}
@media screen and (min-width: 50em) {
  .timeline {
    width: 95%;
  }
}
.timeline__item {
  margin-bottom: 4em;
  clear: both;
}
@media screen and (min-width: 50em) {
  .timeline__item {
    width: 40%;
  }
}
.timeline__day {
  font-weight: 600;
  font-size: 0.9em;
  color: white;
  text-transform: uppercase;
  padding-bottom: 0.75em;
}
.timeline__day span {
  font-weight: 300;
  color: orange;
  text-transform: none;
}
.timeline__day-sum {
  color: white;
  font-weight: 300;
  font-size: 0.8em;
  display: block;
  margin-bottom: 1.5em;
  line-height: 1.5;
}

.clearfix:after {
  visibility: hidden;
  display: block;
  font-size: 0;
  content: " ";
  clear: both;
  height: 0;
}

.clearfix {
  display: inline-block;
}

/* start commented backslash hack \*/
* html .clearfix {
  height: 1%;
}

.clearfix {
  display: block;
}

/* close commented backslash hack */
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.14.2/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.3/ScrollMagic.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.3/plugins/animation.gsap.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/DrawSVGPlugin.js?r=11"></script>
<script>
  var controller = new ScrollMagic.Controller();
TweenMax.set('#timeline',{visibility:0});

var tweenOne =
new TweenMax.fromTo('#timeline',1,{drawSVG:"0%"}, {drawSVG:"10%"});  
 
var scene1 = new ScrollMagic.Scene({
  triggerElement: '#timeline',
}).setTween(tweenOne).addTo(controller); 
    
var tweenTwo =
new TweenMax.fromTo('#timeline',1,{drawSVG:"10%"}, {drawSVG:"20%"});

var scene2 = new ScrollMagic.Scene({
  triggerElement: '.dayTwo',
}).setTween(tweenTwo).addTo(controller); 


var tweenThree =
new TweenMax.fromTo('#timeline',1,{drawSVG:"20%"}, {drawSVG:"40%"});

var scene3 = new ScrollMagic.Scene({
  triggerElement: '.dayThree',
}).setTween(tweenThree).addTo(controller); 


var tweenFour =
new TweenMax.fromTo('#timeline',1,{drawSVG:"40%"}, {drawSVG:"65%"});

var scene3 = new ScrollMagic.Scene({
  triggerElement: '.dayFour',
}).setTween(tweenFour).addTo(controller); 
 
var tweenFive =
new TweenMax.fromTo('#timeline',1,{drawSVG:"65%"}, {drawSVG:"85%"});

var scene3 = new ScrollMagic.Scene({
  triggerElement: '.dayFive',
}).setTween(tweenFive).addTo(controller); 

var tweenSix =
new TweenMax.fromTo('#timeline',1,{drawSVG:"85%"}, {drawSVG:"102%"});

var scene3 = new ScrollMagic.Scene({
  triggerElement: '.daySix',
}).setTween(tweenSix).addTo(controller); 
</script>

 

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

  • 1 month later...
Posted
1 hour ago, anna6 said:

Can you please do this one... https://codepen.io/annanz/pen/poeaZWK

I have tried and tried, but I see you work magic that I cannot. Do you know if it can be added as a section background?

Cheers

This look complex. Try this

<script id="vertexShader" type="x-shader/x-vertex">
  #include <fog_pars_vertex>

uniform float uTime;

uniform float uBigWavesElevation;
uniform vec2 uBigWavesFrequency;
uniform float uBigWaveSpeed;

uniform  float uSmallWavesElevation;
uniform  float uSmallWavesFrequency;
uniform  float uSmallWavesSpeed;
uniform float uSmallWavesIterations;

varying float vElevation;

//	Classic Perlin 3D Noise 
//	by Stefan Gustavson
//
vec4 permute(vec4 x){return mod(((x*34.0)+1.0)*x, 289.0);}
vec4 taylorInvSqrt(vec4 r){return 1.79284291400159 - 0.85373472095314 * r;}
vec3 fade(vec3 t) {return t*t*t*(t*(t*6.0-15.0)+10.0);}

float cnoise(vec3 P){
  vec3 Pi0 = floor(P); // Integer part for indexing
  vec3 Pi1 = Pi0 + vec3(1.0); // Integer part + 1
  Pi0 = mod(Pi0, 289.0);
  Pi1 = mod(Pi1, 289.0);
  vec3 Pf0 = fract(P); // Fractional part for interpolation
  vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0
  vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  vec4 iy = vec4(Pi0.yy, Pi1.yy);
  vec4 iz0 = Pi0.zzzz;
  vec4 iz1 = Pi1.zzzz;

  vec4 ixy = permute(permute(ix) + iy);
  vec4 ixy0 = permute(ixy + iz0);
  vec4 ixy1 = permute(ixy + iz1);

  vec4 gx0 = ixy0 / 7.0;
  vec4 gy0 = fract(floor(gx0) / 7.0) - 0.5;
  gx0 = fract(gx0);
  vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
  vec4 sz0 = step(gz0, vec4(0.0));
  gx0 -= sz0 * (step(0.0, gx0) - 0.5);
  gy0 -= sz0 * (step(0.0, gy0) - 0.5);

  vec4 gx1 = ixy1 / 7.0;
  vec4 gy1 = fract(floor(gx1) / 7.0) - 0.5;
  gx1 = fract(gx1);
  vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
  vec4 sz1 = step(gz1, vec4(0.0));
  gx1 -= sz1 * (step(0.0, gx1) - 0.5);
  gy1 -= sz1 * (step(0.0, gy1) - 0.5);

  vec3 g000 = vec3(gx0.x,gy0.x,gz0.x);
  vec3 g100 = vec3(gx0.y,gy0.y,gz0.y);
  vec3 g010 = vec3(gx0.z,gy0.z,gz0.z);
  vec3 g110 = vec3(gx0.w,gy0.w,gz0.w);
  vec3 g001 = vec3(gx1.x,gy1.x,gz1.x);
  vec3 g101 = vec3(gx1.y,gy1.y,gz1.y);
  vec3 g011 = vec3(gx1.z,gy1.z,gz1.z);
  vec3 g111 = vec3(gx1.w,gy1.w,gz1.w);

  vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  g000 *= norm0.x;
  g010 *= norm0.y;
  g100 *= norm0.z;
  g110 *= norm0.w;
  vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  g001 *= norm1.x;
  g011 *= norm1.y;
  g101 *= norm1.z;
  g111 *= norm1.w;

  float n000 = dot(g000, Pf0);
  float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));
  float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));
  float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));
  float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));
  float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));
  float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));
  float n111 = dot(g111, Pf1);

  vec3 fade_xyz = fade(Pf0);
  vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);
  vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);
  float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x); 
  return 2.2 * n_xyz;
}

void main() {
  #include <begin_vertex>
  #include <project_vertex>
  #include <fog_vertex>
  vec4 modelPosition = modelMatrix * vec4(position, 1.0);
  float elevation = 
    sin(modelPosition.x * uBigWavesFrequency.x + uTime * uBigWaveSpeed) 
    * sin(modelPosition.z * uBigWavesFrequency.y + uTime * uBigWaveSpeed) 
    * uBigWavesElevation;
  
  for(float i = 1.0; i <= 10.0; i++) {
   
    elevation -= abs(
      cnoise(
        vec3(modelPosition.xz * uSmallWavesFrequency * i, uTime * uSmallWavesSpeed)
        ) 
        * uSmallWavesElevation / i
      );
     if(i >= uSmallWavesIterations ) {
      break;
    }
  }
  
  modelPosition.y += elevation;
  vec4 viewPosition = viewMatrix * modelPosition;
  vec4 projectedPosition = projectionMatrix * viewPosition;
  gl_Position = projectedPosition;

  vElevation = elevation;
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
  #include <fog_pars_fragment>
precision mediump float;
uniform vec3 uDepthColor;
uniform vec3 uSurfaceColor;

uniform float uColorOffset;
uniform float uColorMultiplier;

varying float vElevation;

void main() {
  float mixStrength = (vElevation + uColorOffset) * uColorMultiplier;
  vec3 color = mix(uDepthColor, uSurfaceColor, mixStrength);
  gl_FragColor = vec4(color, 1.0);
   #include <fog_fragment>
}
</script>
<canvas class="webgl"></canvas>
<style>
  .webgl {
  position: fixed;
  top: 0;
  left: 0;
  outline: none;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r125/three.min.js"></script>
<script src="https://unpkg.com/three@0.125.2/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.min.js"></script>
<script>
  /**
 * Debug
 */
const gui = new dat.GUI({ closed: false, width: 340 });
const bigWavesFolder = gui.addFolder("Large Waves");
const smallWavesFolder = gui.addFolder("Small Waves");
const colorFolder = gui.addFolder("Colors");
const debugObject = {
  waveDepthColor: "#181fbb",
  waveSurfaceColor: "#1dc7eb",
  fogNear: 1,
  fogFar: 3,
  fogColor: "#128df2"
};

/**
 * Base
 */
// Canvas
const canvas = document.querySelector("canvas.webgl");

// Scene
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(
  debugObject.fogColor,
  debugObject.fogNear,
  debugObject.fogFar
);
scene.background = new THREE.Color(debugObject.fogColor);

/**
 * Object
 */
const waterGeometry = new THREE.PlaneGeometry(12, 12, 512, 512);

// Material
const waterMaterial = new THREE.ShaderMaterial({
  vertexShader: document.getElementById("vertexShader").textContent,
  fragmentShader: document.getElementById("fragmentShader").textContent,
  transparent: true,
  fog: true,
  uniforms: {
    uTime: { value: 0 },
    uMouse: { value: new THREE.Vector2() },
    uBigWavesElevation: { value: 0.2 },
    uBigWavesFrequency: { value: new THREE.Vector2(4, 2) },
    uBigWaveSpeed: { value: 0.75 },
    // Small Waves
    uSmallWavesElevation: { value: 0.15 },
    uSmallWavesFrequency: { value: 3 },
    uSmallWavesSpeed: { value: 0.2 },
    uSmallWavesIterations: { value: 4 },
    // Color
    uDepthColor: { value: new THREE.Color(debugObject.waveDepthColor) },
    uSurfaceColor: { value: new THREE.Color(debugObject.waveSurfaceColor) },
    uColorOffset: { value: 0.08 },
    uColorMultiplier: { value: 5 },

    // Fog, contains fogColor, fogDensity, fogFar and fogNear
    ...THREE.UniformsLib["fog"]
  }
});

const water = new THREE.Mesh(waterGeometry, waterMaterial);
water.rotation.x = -Math.PI * 0.5;
scene.add(water);

/**
 * Sizes
 */
const sizes = {
  width: window.innerWidth,
  height: window.innerHeight
};

window.addEventListener("resize", () => {
  // Update sizes
  sizes.width = window.innerWidth;
  sizes.height = window.innerHeight;

  // Update camera
  camera.aspect = sizes.width / sizes.height;
  camera.updateProjectionMatrix();

  // Update renderer
  renderer.setSize(sizes.width, sizes.height);
  renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});

/**
 * Camera
 */

// Base camera
const camera = new THREE.PerspectiveCamera(
  75,
  sizes.width / sizes.height,
  0.1,
  100
);
camera.position.set(1, 1, 1);
scene.add(camera);

// Controls
const controls = new THREE.OrbitControls(camera, canvas);
controls.enableDamping = true;

/**
 * Renderer
 */
const renderer = new THREE.WebGLRenderer({
  canvas: canvas
});
renderer.setSize(sizes.width, sizes.height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));

/**
 * Add GUI
 */
// Big Waves
bigWavesFolder
  .add(waterMaterial.uniforms.uBigWavesElevation, "value")
  .min(0)
  .max(1)
  .step(0.001)
  .name("Elevation");
bigWavesFolder
  .add(waterMaterial.uniforms.uBigWavesFrequency.value, "x")
  .min(0)
  .max(10)
  .step(0.001)
  .name("Frequency X");
bigWavesFolder
  .add(waterMaterial.uniforms.uBigWavesFrequency.value, "y")
  .min(0)
  .max(10)
  .step(0.001)
  .name("Frequency Y");
bigWavesFolder
  .add(waterMaterial.uniforms.uBigWaveSpeed, "value")
  .min(0.25)
  .max(5)
  .step(0.001)
  .name("Speed");

// Small Waves
smallWavesFolder
  .add(waterMaterial.uniforms.uSmallWavesElevation, "value")
  .min(0.0)
  .max(0.3)
  .step(0.001)
  .name("Elevation");
smallWavesFolder
  .add(waterMaterial.uniforms.uSmallWavesFrequency, "value")
  .min(0)
  .max(30)
  .step(0.001)
  .name("Frequency");
smallWavesFolder
  .add(waterMaterial.uniforms.uSmallWavesSpeed, "value")
  .min(0.0)
  .max(1)
  .step(0.001)
  .name("Speed");
smallWavesFolder
  .add(waterMaterial.uniforms.uSmallWavesIterations, "value")
  .min(0)
  .max(10)
  .step(1)
  .name("Iterations");

// Colors
colorFolder
  .add(waterMaterial.uniforms.uColorOffset, "value")
  .min(0)
  .max(0.15)
  .step(0.0001)
  .name("Color Offset");
colorFolder
  .add(waterMaterial.uniforms.uColorMultiplier, "value")
  .min(0.0)
  .max(10.0)
  .step(0.001)
  .name("Color multiplier");
colorFolder
  .addColor(debugObject, "waveDepthColor")
  .name("Wave depth color")
  .onChange(() => {
    waterMaterial.uniforms.uDepthColor.value.set(debugObject.waveDepthColor);
  });
colorFolder
  .addColor(debugObject, "waveSurfaceColor")
  .name("Wave surface color")
  .onChange(() => {
    waterMaterial.uniforms.uSurfaceColor.value.set(
      debugObject.waveSurfaceColor
    );
  });
colorFolder
  .addColor(debugObject, "fogColor")
  .name("Fog Color")
  .onChange(() => {
    waterMaterial.uniforms.fogColor.value.set(debugObject.fogColor);
    scene.background.set(debugObject.fogColor);
    scene.fog = new THREE.Fog(
      debugObject.fogColor,
      debugObject.fogNear,
      debugObject.fogFar
    );
  });

/**
 * Animate
 */
const clock = new THREE.Clock();

const tick = () => {
  const elapsedTime = clock.getElapsedTime();

  // Update controls
  controls.update();

  // Update time
  waterMaterial.uniforms.uTime.value = elapsedTime;

  // Render
  renderer.render(scene, camera);

  // Call tick again on the next frame
  window.requestAnimationFrame(tick);
};

tick();

</script>

 

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.