line-intersect+svg实现简单DOM光线投射鼠标跟随交互效果代码

代码语言:html

所属分类:其他

代码描述:line-intersect+svg实现简单DOM光线投射鼠标跟随交互效果代码

代码标签: line-intersect svg 简单 DOM 光线 投射 鼠标 跟随 交互

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

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

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


  
  
<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background: #13141b;
}

main section {
  width: 100vw;
  height: 100vh;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  grid-gap: 10vmin;
  padding: 10vmin;
}

main section div {
  width: 100%;
  height: 100%;
  background: #252837;
}

svg {
  position: fixed;
  width: 100vw;
  height: 100vh;
  top: 0;
  left: 0;
}
</style>


  
  
</head>

<body translate="no">
  <main>
  <section>
    <div data-observe="true"></div>
    <div data-observe="true"></div>
    <div data-observe="true"></div>
    <div data-observe="true"></div>
    <div data-observe="true"></div>
    <div data-observe="true"></div>
  </section>
</main>
  <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/svg.3.1.1.js"></script>
      <script type="module">
import { checkIntersection } from "//repo.bfw.wiki/bfwrepo/js/module/line-intersect/line-intersect.mjs";


const svg = SVG().addTo("body");

function degToRadians(degrees) {
  return degrees * (Math.PI / 180);
}

class Raycaster {
  constructor({ segments }) {
    this.uniquePoints = [];

    this.setSegments(segments);
  }

  setSegments(segments) {
    const uniquePoints = segments.reduce((arr, curr) => {
      for (const point of Object.values(curr)) {
        if (!arr.some((p) => p.x === point.x && p.y === point.y)) {
          arr.push(curr.a, curr.b);
        }
      }

      return arr;
    }, []);

    const intersections = segments.reduce((acc, curr) => {
      for (const segment of segments) {
        const intersection = checkIntersection(
          curr.a.x,
          curr.a.y,
          curr.b.x,
          curr.b.y,
          segment.a.x,
          segment.a.y,
          segment.b.x,
          segment.b.y
        );

        if (
          intersection.type === "intersecting" &&
          !acc.some(
            (i) => i.x === intersection.point.x && i.y !== intersection.point.y
          )
        ) {
          acc.push({ x: intersection.point.x, y: intersection.point.y });
        }
      }

      return acc;
    }, []);

    this.uniquePoints = [...intersections, ...uniquePoints];
    this.segments = segments;
  }

  createRay(x, y, angle) {
    const dx = Math.cos(angle);
    const dy = Math.sin(angle);

    return {
      a: { x: x, y: y },
      b: { x: x + dx, y: y + dy }
    };
  }

  getIntersection(ray, segment) {
    const r_px = ray.a.x;
    const r_py = ray.a.y;
    const r_dx = ray.b.x - ray.a.x;
    const r_dy = ray.b.y - ray.a.y;

    const s_px = segment.a.x;
    const s_py = segment.a.y;
    const s_dx = segment.b.x - segment.a.x;
    const s_dy = segment.b.y - segment.a.y;

    if (r_dx * s_dy === r_dy * .........完整代码请登录后点击上方下载按钮下载查看

网友评论0