forked from framer/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c863aa8
commit 1bc3da6
Showing
5 changed files
with
35 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
import { useEffect, useState } from "react" | ||
import { OGLRenderingContext, Texture, Color } from "ogl" | ||
import { OGLRenderingContext, Texture } from "ogl" | ||
import Color from "colorjs.io" | ||
|
||
export function useGradientTexture(gl: OGLRenderingContext, colors: string[]) { | ||
export function useGradientTexture(gl: OGLRenderingContext, colors: string[], quantization: number) { | ||
const [texture] = useState(() => new Texture(gl, { minFilter: gl.NEAREST, magFilter: gl.NEAREST })) | ||
const [canvas] = useState(() => document.createElement("canvas")) | ||
|
||
useEffect(() => { | ||
if (!colors.length) return | ||
|
||
const pixels = new Uint8ClampedArray(colors.length * 4) | ||
quantization = Math.max(2, quantization) | ||
|
||
const list = new Color(colors[0]).steps(colors[1], { steps: quantization, space: "hsl", outputSpace: "srgb" }) | ||
|
||
const pixels = new Uint8ClampedArray(list.length * 4) | ||
for (let i = 0; i < pixels.length; i += 4) { | ||
const color = new Color(colors[i / 4]) | ||
const color = list[i / 4] | ||
pixels[i] = color.r * 255 | ||
pixels[i + 1] = color.g * 255 | ||
pixels[i + 2] = color.b * 255 | ||
pixels[i + 3] = 255 | ||
} | ||
|
||
canvas.width = colors.length | ||
canvas.width = list.length | ||
canvas.height = 1 | ||
const ctx = canvas.getContext("2d") | ||
// ctx.putImageData(pixels, 0, 0) | ||
ctx?.putImageData(new ImageData(pixels, colors.length, 1), 0, 0) | ||
ctx?.putImageData(new ImageData(pixels, list.length, 1), 0, 0) | ||
|
||
texture.image = pixels | ||
texture.width = colors.length | ||
texture.width = list.length | ||
texture.height = 1 | ||
texture.update() | ||
}, [texture, canvas, colors]) | ||
}, [texture, canvas, colors, quantization]) | ||
|
||
return { texture } | ||
} |