From 9e234f63edc630f767e571acfc662a539432e137 Mon Sep 17 00:00:00 2001 From: Sherry1978 Date: Thu, 9 Jan 2025 03:41:56 +0000 Subject: [PATCH] Add code --- code | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 code diff --git a/code b/code new file mode 100644 index 0000000..5bade14 --- /dev/null +++ b/code @@ -0,0 +1,67 @@ +import React, { useRef, useEffect } from 'react'; + +const DistortedGrid = () => { + const canvasRef = useRef(null); + + useEffect(() => { + const canvas = canvasRef.current; + const ctx = canvas.getContext('2d'); + let t = 0; + const w = 400; + const s = 3; + + const mag = (x, y) => Math.sqrt(x * x + y * y); + + const a = (x, y, t) => { + const k = x / 8 - 25; + const e = y / 8 - 25; + const d = 2 * Math.cos(mag(k, e) / 3 - t); + return [x + d * k, y + d * e]; + }; + + const shuffle = (array) => { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + return array; + }; + + const draw = () => { + ctx.fillStyle = 'rgba(6, 6, 6, 0.1)'; + ctx.fillRect(0, 0, w, w); + ctx.strokeStyle = 'rgba(255, 255, 255, 0.7)'; + ctx.lineWidth = 0.5; + + for (let y = 100; y < 300; y += s) { + for (let x = 100; x < 300; x += s) { + const points = shuffle([ + a(x, y, t), + a(x, y + s, t), + a(x + s, y, t) + ]); + ctx.beginPath(); + ctx.moveTo(points[0][0], points[0][1]); + ctx.lineTo(points[1][0], points[1][1]); + ctx.stroke(); + } + } + + t += 0.02; + requestAnimationFrame(draw); + }; + + draw(); + }, []); + + return ( +
+
+

Distorted Grid Visualization

+ +
+
+ ); +}; + +export default DistortedGrid;