fix: fixed grad and grain
This commit is contained in:
parent
30f5c4b39d
commit
c21304fa67
|
@ -8,16 +8,16 @@ import { motion } from "framer-motion";
|
||||||
|
|
||||||
export function GradientProvider({ children }: { children: React.ReactNode }) {
|
export function GradientProvider({ children }: { children: React.ReactNode }) {
|
||||||
return (
|
return (
|
||||||
<motion.div
|
<div className="relative w-fit">
|
||||||
initial={{ opacity: 0 }}
|
<motion.div
|
||||||
animate={{ opacity: 1 }}
|
initial={{ opacity: 0 }}
|
||||||
className="relative w-fit"
|
animate={{ opacity: 1 }}
|
||||||
>
|
className="absolute rotate-180 -z-[5] w-[600px] h-[800px] opacity-60 top-1/2 -translate-x-1/2 -translate-y-1/2 left-1/2"
|
||||||
<div className="absolute rotate-180 -z-[5] w-[600px] h-[800px] opacity-60 top-1/2 -translate-x-1/2 -translate-y-1/2 left-1/2">
|
>
|
||||||
<Image src={gradient} alt="gradient" />
|
<Image src={gradient} alt="gradient" />
|
||||||
</div>
|
</motion.div>
|
||||||
{children}
|
{children}
|
||||||
</motion.div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,19 +31,17 @@ export function SymetricGradProvider({
|
||||||
gradient_class?: string;
|
gradient_class?: string;
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<motion.div
|
<motion.div className={cn("relative w-fit", className)}>
|
||||||
initial={{ opacity: 0 }}
|
<motion.div
|
||||||
animate={{ opacity: 1 }}
|
initial={{ opacity: 0 }}
|
||||||
className={cn("relative w-fit", className)}
|
animate={{ opacity: 1 }}
|
||||||
>
|
|
||||||
<div
|
|
||||||
className={cn(
|
className={cn(
|
||||||
"absolute w-full -z-10 h-[800px] opacity-70 top-1/4 rotate-180 -translate-x-1/2 -translate-y-1/2 left-1/2",
|
"absolute w-full -z-10 h-[800px] opacity-70 top-1/4 rotate-180 -translate-x-1/2 -translate-y-1/2 left-1/2",
|
||||||
gradient_class,
|
gradient_class,
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<Image src={symetric_gradient} alt="gradient" />
|
<Image src={symetric_gradient} alt="gradient" />
|
||||||
</div>
|
</motion.div>
|
||||||
{children}
|
{children}
|
||||||
</motion.div>
|
</motion.div>
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { motion } from "framer-motion";
|
import React, { useEffect, useRef, useCallback } from "react";
|
||||||
// Grain.tsx
|
|
||||||
import React, { useEffect, useRef } from "react";
|
|
||||||
|
|
||||||
type GrainOptions = {
|
type GrainOptions = {
|
||||||
patternWidth?: number;
|
patternWidth?: number;
|
||||||
|
@ -18,68 +16,76 @@ export const GrainProvider = ({
|
||||||
}: {
|
}: {
|
||||||
grain_options?: GrainOptions;
|
grain_options?: GrainOptions;
|
||||||
}) => {
|
}) => {
|
||||||
const providerRef = useRef<HTMLDivElement>(null);
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||||
|
const animationFrameId = useRef<number>();
|
||||||
|
|
||||||
// Function to generate and apply grain effect
|
const createGrainPattern = (
|
||||||
const applyGrain = (ele: HTMLElement, opt: GrainOptions) => {
|
ctx: CanvasRenderingContext2D,
|
||||||
const canvas = document.createElement("canvas");
|
opt: GrainOptions,
|
||||||
const ctx = canvas.getContext("2d");
|
) => {
|
||||||
if (!ctx) return;
|
const patternCanvas = document.createElement("canvas");
|
||||||
|
const pctx = patternCanvas.getContext("2d");
|
||||||
|
|
||||||
canvas.width = opt.patternWidth ?? 100;
|
patternCanvas.width = opt.patternWidth ?? 200;
|
||||||
canvas.height = opt.patternHeight ?? 100;
|
patternCanvas.height = opt.patternHeight ?? 200;
|
||||||
|
|
||||||
for (let w = 0; w < canvas.width; w += opt.grainDensity ?? 1) {
|
if (pctx) {
|
||||||
for (let h = 0; h < canvas.height; h += opt.grainDensity ?? 1) {
|
for (let w = 0; w < patternCanvas.width; w += opt.grainDensity ?? 1) {
|
||||||
const rgb = Math.floor(Math.random() * 256);
|
for (let h = 0; h < patternCanvas.height; h += opt.grainDensity ?? 1) {
|
||||||
ctx.fillStyle = `rgba(${rgb / 2}, ${rgb / 2}, ${rgb}, ${opt.grainOpacity ?? 0.1
|
const rgb = Math.floor(Math.random() * 256);
|
||||||
})`;
|
pctx.fillStyle = `rgba(${rgb / 2}, ${rgb / 2}, ${rgb}, ${opt.grainOpacity ?? 0.1
|
||||||
ctx.fillRect(w, h, opt.grainWidth ?? 1, opt.grainHeight ?? 1);
|
})`;
|
||||||
|
pctx.fillRect(w, h, opt.grainWidth ?? 1, opt.grainHeight ?? 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ele.style.backgroundImage = `url(${canvas.toDataURL("image/png")})`;
|
return ctx.createPattern(patternCanvas, "repeat");
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
const animate = useCallback(() => {
|
||||||
const element = providerRef.current;
|
const canvas = canvasRef.current;
|
||||||
if (!element) return;
|
if (canvas) {
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
if (ctx) {
|
||||||
|
const options: GrainOptions = {
|
||||||
|
patternWidth: 200,
|
||||||
|
patternHeight: 200,
|
||||||
|
grainOpacity: 0.05,
|
||||||
|
grainDensity: 1,
|
||||||
|
grainWidth: 1,
|
||||||
|
grainHeight: 1,
|
||||||
|
};
|
||||||
|
|
||||||
// Define options here
|
canvas.width = window.innerWidth;
|
||||||
const options: GrainOptions = {
|
canvas.height = window.innerHeight;
|
||||||
patternWidth: 240,
|
|
||||||
patternHeight: 300,
|
|
||||||
grainOpacity: 0.05,
|
|
||||||
grainDensity: 1,
|
|
||||||
grainWidth: 2,
|
|
||||||
grainHeight: 2,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Apply grain initially
|
const pattern = createGrainPattern(ctx, grain_options ?? options);
|
||||||
applyGrain(element, grain_options ?? options);
|
if (pattern) {
|
||||||
|
ctx.fillStyle = pattern;
|
||||||
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||||
|
}
|
||||||
|
|
||||||
// Set interval to update grain
|
animationFrameId.current = requestAnimationFrame(animate);
|
||||||
const intervalId = setInterval(() => {
|
}
|
||||||
applyGrain(element, grain_options ?? options);
|
}
|
||||||
}, 100);
|
|
||||||
|
|
||||||
// Cleanup function
|
|
||||||
return () => clearInterval(intervalId);
|
|
||||||
}, [grain_options]);
|
}, [grain_options]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
animationFrameId.current = requestAnimationFrame(animate);
|
||||||
|
return () => {
|
||||||
|
if (animationFrameId.current) {
|
||||||
|
cancelAnimationFrame(animationFrameId.current);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [animate]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<motion.div
|
<canvas
|
||||||
initial={{
|
ref={canvasRef}
|
||||||
opacity: 0,
|
id="grained-canvas"
|
||||||
}}
|
|
||||||
animate={{
|
|
||||||
opacity: 1,
|
|
||||||
}}
|
|
||||||
ref={providerRef}
|
|
||||||
id="grained-bg"
|
|
||||||
className="fixed left-0 top-0 inset-0 z-50 pointer-events-none w-full h-full"
|
className="fixed left-0 top-0 inset-0 z-50 pointer-events-none w-full h-full"
|
||||||
></motion.div>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default GrainProvider;
|
export default GrainProvider;
|
||||||
|
|
Loading…
Reference in New Issue