CSS Shape-outside using Three.js HTML, CSS And Javascript

 

HTML:


<!-- using three.js -->
<section class="section">
  <h1>L<span class="hide">o</span><svg viewBox="0 0 512 512" class="animRotate" title="dharmachakra">
      <path d="M495 225.06l-17.22 1.08c-5.27-39.49-20.79-75.64-43.86-105.84l12.95-11.43c6.92-6.11 7.25-16.79.73-23.31L426.44 64.4c-6.53-6.53-17.21-6.19-23.31.73L391.7 78.07c-30.2-23.06-66.35-38.58-105.83-43.86L286.94 17c.58-9.21-6.74-17-15.97-17h-29.94c-9.23 0-16.54 7.79-15.97 17l1.08 17.22c-39.49 5.27-75.64 20.79-105.83 43.86l-11.43-12.95c-6.11-6.92-16.79-7.25-23.31-.73L64.4 85.56c-6.53 6.53-6.19 17.21.73 23.31l12.95 11.43c-23.06 30.2-38.58 66.35-43.86 105.84L17 225.06c-9.21-.58-17 6.74-17 15.97v29.94c0 9.23 7.79 16.54 17 15.97l17.22-1.08c5.27 39.49 20.79 75.64 43.86 105.83l-12.95 11.43c-6.92 6.11-7.25 16.79-.73 23.31l21.17 21.17c6.53 6.53 17.21 6.19 23.31-.73l11.43-12.95c30.2 23.06 66.35 38.58 105.84 43.86L225.06 495c-.58 9.21 6.74 17 15.97 17h29.94c9.23 0 16.54-7.79 15.97-17l-1.08-17.22c39.49-5.27 75.64-20.79 105.84-43.86l11.43 12.95c6.11 6.92 16.79 7.25 23.31.73l21.17-21.17c6.53-6.53 6.19-17.21-.73-23.31l-12.95-11.43c23.06-30.2 38.58-66.35 43.86-105.83l17.22 1.08c9.21.58 17-6.74 17-15.97v-29.94c-.01-9.23-7.8-16.54-17.01-15.97zM281.84 98.61c24.81 4.07 47.63 13.66 67.23 27.78l-42.62 48.29c-8.73-5.44-18.32-9.54-28.62-11.95l4.01-64.12zm-51.68 0l4.01 64.12c-10.29 2.41-19.89 6.52-28.62 11.95l-42.62-48.29c19.6-14.12 42.42-23.71 67.23-27.78zm-103.77 64.33l48.3 42.61c-5.44 8.73-9.54 18.33-11.96 28.62l-64.12-4.01c4.07-24.81 13.66-47.62 27.78-67.22zm-27.78 118.9l64.12-4.01c2.41 10.29 6.52 19.89 11.95 28.62l-48.29 42.62c-14.12-19.6-23.71-42.42-27.78-67.23zm131.55 131.55c-24.81-4.07-47.63-13.66-67.23-27.78l42.61-48.3c8.73 5.44 18.33 9.54 28.62 11.96l-4 64.12zM256 288c-17.67 0-32-14.33-32-32s14.33-32 32-32 32 14.33 32 32-14.33 32-32 32zm25.84 125.39l-4.01-64.12c10.29-2.41 19.89-6.52 28.62-11.96l42.61 48.3c-19.6 14.12-42.41 23.71-67.22 27.78zm103.77-64.33l-48.29-42.62c5.44-8.73 9.54-18.32 11.95-28.62l64.12 4.01c-4.07 24.82-13.66 47.64-27.78 67.23zm-36.34-114.89c-2.41-10.29-6.52-19.89-11.96-28.62l48.3-42.61c14.12 19.6 23.71 42.42 27.78 67.23l-64.12 4z" />
    </svg>rem Ipsum</h1>
  <canvas id="canvas"></canvas>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
  <p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.</p>
  <p>Cras a nisi est. Nulla vitae finibus purus, vel malesuada nibh. Nullam pellentesque massa ipsum, at luctus nisi semper vulputate. Aliquam feugiat leo quis dui tempus, et porta nisl hendrerit. Etiam ac ex tortor. Donec sapien massa, interdum et fringilla a, lacinia eu lacus. Nunc nulla nibh, semper vehicula velit et, auctor hendrerit neque. Sed dolor lectus, laoreet aliquam venenatis at, sagittis quis ante.</p>
  <footer>
    <div class="separate"><span class="symbol animRotate">&#10059;</span></div>
    <div>Lorem Ipsum</div>
  </footer>
</section>

<nav id="menu_icon">
  <svg viewBox="0 0 192 512" class="animRotate open" title="ellipsis-v">
    <path d="M96 184c39.8 0 72 32.2 72 72s-32.2 72-72 72-72-32.2-72-72 32.2-72 72-72zM24 80c0 39.8 32.2 72 72 72s72-32.2 72-72S135.8 8 96 8 24 40.2 24 80zm0 352c0 39.8 32.2 72 72 72s72-32.2 72-72-32.2-72-72-72-72 32.2-72 72z" />
  </svg>
  <svg viewBox="0 0 352 512" class="close" title="times">
    <path d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z" />
  </svg>
</nav>
<nav id="menu_content">
  <div>
    <h2>Menu</h2>
    <ul>
      <li>Paragraph 1</li>
      <li>Paragraph 2</li>
      <li>Paragraph 3</li>
      <li>Paragraph 4</li>
      <li>Paragraph 5</li>
    </ul>
  </div>
</nav>

CSS:


@import url("https://fonts.googleapis.com/css2?family=Spectral:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;0,800;1,200;1,300;1,400;1,500;1,600;1,700;1,800&display=swap");

:root {
  --animRotate-variable: 0deg;
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html,
body {
  overscroll-behavior-x: none;
  overscroll-behavior-y: none;
}
body {
  background: mediumaquamarine;
  color: #fff;
  position: relative;
  font-family: "Spectral", serif;
  font-size: 20px;
}
body::-webkit-scrollbar {
  width: 0.5rem;
}
body::-webkit-scrollbar-thumb {
  border: 0.25rem solid #000;
}
section {
  display: block;
  min-height: 100vh;
  padding: 2vw;
  fill: white;
  clear: both;
  z-index: 1;
}
footer {
  border-top: 1px solid white;
  margin: 10vw 20vw;
  padding-top: 5vw;
  text-align: center;
}
.animRotate,
.animRotateY {
  position: relative;
  /*
  will-change: transform;
*/
}
.animRotate {
  transform: rotate(var(--animRotate-variable));
}
.animRotateY {
  transform: rotateY(var(--animRotate-variable));
}
h1 {
  font-size: 11vw;
  font-weight: 600;
  margin: 1vw 0;
  fill: slategray;
}
h1 svg {
  width: 7vw;
  height: 7vw;
}
.hide {
  font-size: 0;
}
.separate {
  display: block;
  text-align: center;
  margin: 0;
  color: white;
}

.symbol {
  font-family: serif;
  position: relative;
  font-size: 35px;
  width: 35px;
  height: 35px;
  display: inline-block;
  line-height: 1;
}
p {
  margin: 2rem 0;
  line-height: 1.5rem;
  z-index: 1;
}
p:first-of-type:first-letter {
  font-size: 2.2rem;
  font-weight: 600;
  color: slategray;
  /*
  font-family: "Fleur De Leah", serif;
  margin: 0 1rem 0.3rem 0;
  -webkit-initial-letter: 3;
  initial-letter: 3;
*/
}
#canvas {
  margin: 3rem 0 0;
  float: left;
  /*
  width: auto !important;
  height: auto !important;
  max-width: 200px;
  max-height: 200px;
  
  margin: 5rem 0 0;
  shape-outside: url(var(--imageData-variable));
  shape-outside: url();
  will-change: shape-outside;
*/

  shape-image-threshold: 0;
  shape-margin: 0.2rem;
}
canvas {
  z-index: 0;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

#menu_icon {
  position: fixed;
  top: 10px;
  right: 10px;
  width: 25px;
  height: 25px;
  background-color: none;
  z-index: 3;
}
#menu_icon:hover {
  cursor: pointer;
}
#menu_icon svg {
  width: 25px;
  height: 25px;
  position: absolute;
  top: 0;
  left: 0;
  fill: white;
  transition: visibility 0.5s ease, opacity 0.5s ease;
}
.open {
  visibility: visible;
  opacity: 1;
}
.close {
  visibility: hidden;
  opacity: 0;
}
.active .open {
  visibility: hidden;
  opacity: 0;
}
.active .close {
  visibility: visible;
  opacity: 1;
}
#menu_content {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  overflow-y: auto;
  overscroll-behavior-y: none;
  background-color: moccasin;
  color: slategray;
  text-align: center;
  transition: 1s all;
  visibility: hidden;
  opacity: 0;
  z-index: 2;
}
.active + #menu_content {
  visibility: visible;
  opacity: 1;
}
#menu_content > div {
  min-height: calc(100vh + 1px);
  display: inline-block;
  text-align: left;
}
ul li {
  line-height: 1;
}

JavaScript:


window.onload = function () {
  "use strict";
  // scroll rotation
  const ratio = 3; // set integer
  let height = 0;
  document.addEventListener("scroll", function () {
    height = document.body.clientHeight;
    height -= window.innerHeight;
    let scrollAmount = window.pageYOffset;
    scrollAmount = (scrollAmount / height) * 360 * ratio;
    document.documentElement.style.setProperty(
      "--animRotate-variable",
      scrollAmount + "deg"
    );
  });
  window.addEventListener("resize", function () {
    height = document.body.clientHeight;
    height -= window.innerHeight;
  });
  // menu
  document.querySelector(".open").addEventListener("click", toggle, false);
  document.querySelector(".close").addEventListener("click", toggle, false);
  function toggle() {
    document.querySelector("#menu_icon").classList.toggle("active");
  }
};

//////// three.js
const canvas = document.getElementById("canvas");
let camera, scene, renderer, controls;
let geometry, material, mesh;
let ww, hh;
let timeoutID = 0;
const size = 3;
const color = new THREE.Color("#FF7F50");

init();
animate();
setShapeOutside();

function init() {
  //ww = window.innerWidth / 2;
  ww = document.body.clientWidth / 2 / window.devicePixelRatio;
  hh = ww;

  scene = new THREE.Scene();
  renderer = new THREE.WebGLRenderer({
    canvas: canvas,
    antialias: true,
    alpha: true
  });
  renderer.setPixelRatio(window.devicePixelRatio);
  //renderer.setSize(ww, hh);
  renderer.setSize(ww, hh, false);
  renderer.outputEncoding = THREE.sRGBEncoding;
  //
  camera = new THREE.PerspectiveCamera(40, ww / hh, 0.01, size * 30);
  camera.position.set(0, size, size * 6);
  camera.lookAt(0, 0, 0);

  // AmbientLight
  const light = new THREE.AmbientLight(0xffffff, 0.5);
  scene.add(light);

  // DirectionalLight
  const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
  directionalLight.position.set(0, size * 6, size * 5);
  scene.add(directionalLight);

  // Geometry
  geometry = new THREE.TorusKnotGeometry(size, size / 3, 100, 16);
  material = new THREE.MeshStandardMaterial({
    color: color
  });
  mesh = new THREE.Mesh(geometry, material);
  scene.add(mesh);

  //
  controls = new THREE.OrbitControls(camera, renderer.domElement);
  controls.autoRotate = false;
  //controls.autoRotateSpeed = 2;
  controls.enableDamping = true;
  controls.enablePan = false;
  controls.minDistance = size * 5.5;
  controls.maxDistance = size * 10;
  //controls.minPolarAngle = 0;
  //controls.maxPolarAngle = Math.PI / 2;
  controls.target.set(0, 0, 0);
  controls.update();
  //
  window.addEventListener("resize", onWindowResize, false);
  //
  //controls.addEventListener("change", setShapeOutside, false);
  controls.addEventListener("end", setShapeOutside, false);
}
function onWindowResize(event) {
  //ww = window.innerWidth / 2;
  ww = document.body.clientWidth / 2 / window.devicePixelRatio;
  hh = ww;
  camera.aspect = ww / hh;
  camera.updateProjectionMatrix();
  //renderer.setSize(ww, hh);
  renderer.setSize(ww, hh, false);
  //
  setShapeOutside(event);
}
function animate() {
  requestAnimationFrame(animate);
  controls.update();
  render();
}
function render() {
  renderer.render(scene, camera);
}
function setShapeOutside(e = null, type = null) {
  clearTimeout(timeoutID);
  let timerDelay = 0;
  if (e) {
    if (e.type == "resize") {
      timerDelay = 1000;
    } else if (e.type == "end") {
      //timerDelay = 1500;
      timerDelay = 0;
    } else if (e.type == "change") {
      timerDelay = 0;
    }
  }
  if (type == "afterEnd1" || type == "afterEnd2") {
    timerDelay = 1500;
  }
  timeoutID = setTimeout(function () {
    render();
    // Data URIに変換
    const dataURI = canvas.toDataURL("image/png", 0.1);
    canvas.style.shapeOutside = "url(" + dataURI + ")";
    if (e && e.type == "end") {
      setShapeOutside(null, "afterEnd1");
    }
    if (type && type == "afterEnd1") {
      setShapeOutside(null, "afterEnd2");
    }
  }, timerDelay);
}

Comments