canvas鼠标跟随线条绳子效果代码

代码语言:html

所属分类:其他

代码描述:canvas鼠标跟随线条绳子效果代码

代码标签: canvas 鼠标 跟随 线条 绳子

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

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

<head>

    <meta charset="UTF-8">



    <style>
        body, html {
          margin: 0;
          overflow: hidden;
        }
        
        canvas {
          display: block;
        }
    </style>



</head>

<body>
    <canvas id="canvas"></canvas>


    <script>
        /*
          Johan Karlsson, 2023
          https://twitter.com/DonKarlssonSan
          MIT License, see Details View
        */
        let canvas;
        let ctx;
        let w, h;
        let rope;
        
        function setup() {
          canvas = document.querySelector("#canvas");
          ctx = canvas.getContext("2d");
          resize();
          window.addEventListener("resize", () => {
            resize();
          });
          canvas.addEventListener("pointermove", pointermove);
        
          rope = getNewRope();
        }
        
        function pointermove(e) {
          rope[0].x = e.offsetX;
          rope[0].y = e.offsetY;
        }
        
        function resize() {
          w = canvas.width = window.innerWidth;
          h = canvas.height = window.innerHeight;
          ctx.lineWidth = 2;
        }
        
        function draw() {
          requestAnimationFrame(draw);
          ctx.fillStyle = "white";
          ctx.fillRect(0, 0, w, h);
          moveRope();
          drawRope();
        }
        
        setup();
        draw();
        
 .........完整代码请登录后点击上方下载按钮下载查看

网友评论0