-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support nested elements and inputs (#55)
* feat: support nested elements in dom-to-canvas * feat: add rerender hook * fix: remove SVG wrapper to fix position bug * docs: revise log shader * Revert "docs: revise log shader" This reverts commit cae9dbc. * refactor: tidy InputSection * feat: save original opacity of the element * feat: avoid flashing the original element in text rerender * docs: improve text input example * docs: update div examples * perf: reduce DOM traversal * fix: SVG getting overdrawn and scaled incorrectly * perf: avoid rendering the same element concurrrently * feat: use OffscreenCanvas for better quality and performance * chore: remove unused css * refactor: revise hooks types * feat: use MutationObserver to watch element updates * refactor: move property dec * refactor: prefer # over private * chore: remove unused * docs: update Div section desc
- Loading branch information
Showing
11 changed files
with
414 additions
and
185 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* .DivSection { | ||
text-align: left; | ||
} */ | ||
|
||
.DivSection h2 { | ||
font-style: italic; | ||
overflow: visible; | ||
text-align: center; | ||
} | ||
|
||
.DivSections { | ||
width: 720px; | ||
max-width: 90%; | ||
margin: 0 auto; | ||
} | ||
.DivSectionField { | ||
text-align: left; | ||
display: flex; | ||
flex-direction: column; | ||
margin-bottom: 1em; | ||
} | ||
|
||
.DivSection label { | ||
font-weight: bold; | ||
} | ||
|
||
.DivSection input, | ||
.DivSection textarea { | ||
font-size: 1em; | ||
border-radius: 3px; | ||
padding: 2px 10px; | ||
width: 100%; | ||
} | ||
.DivSection textarea { | ||
height: 5em; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import React, { useState, useCallback, useRef, useEffect } from "react"; | ||
import * as VFX from "react-vfx"; | ||
import "./DivSection.css"; | ||
import { InlineCode } from "./Code"; | ||
|
||
const shader = ` | ||
precision mediump float; | ||
uniform vec2 resolution; | ||
uniform vec2 offset; | ||
uniform float time; | ||
uniform sampler2D src; | ||
uniform float dist; | ||
float noise(float y, float t) { | ||
float n = ( | ||
sin(y * .07 + t * 8. + sin(y * .5 + t * 10.)) + | ||
sin(y * .7 + t * 2. + sin(y * .3 + t * 8.)) * .7 + | ||
sin(y * 1.1 + t * 2.8) * .4 | ||
); | ||
n += sin(y * 124. + t * 100.7) * sin(y * 877. - t * 38.8) * .3; | ||
return n; | ||
} | ||
void main (void) { | ||
vec2 uv = (gl_FragCoord.xy - offset) / resolution; | ||
float t = mod(time, 30.); | ||
float amp = (3. + dist * 30.) / resolution.x; | ||
vec2 uvr = uv, uvg = uv, uvb = uv; | ||
if (abs(noise(uv.y, t)) > 1. || dist > 0.03) { | ||
uvr.x += noise(uv.y, t) * amp; | ||
uvg.x += noise(uv.y, t + 10.) * amp; | ||
uvb.x += noise(uv.y, t + 20.) * amp; | ||
} | ||
vec4 cr = texture2D(src, uvr); | ||
vec4 cg = texture2D(src, uvg); | ||
vec4 cb = texture2D(src, uvb); | ||
gl_FragColor = vec4( | ||
cr.r, | ||
cg.g, | ||
cb.b, | ||
step(.1, cr.a + cg.a + cb.a) | ||
); | ||
} | ||
`; | ||
|
||
const DivSection: React.FC = () => { | ||
const divRef = useRef<HTMLDivElement>(null); | ||
const { rerenderElement } = VFX.useVFX(); | ||
|
||
const distRef = useRef(0); | ||
|
||
useEffect(() => { | ||
let isMounted = true; | ||
const decay = () => { | ||
distRef.current *= 0.8; | ||
if (isMounted) { | ||
requestAnimationFrame(decay); | ||
} | ||
}; | ||
decay(); | ||
|
||
return () => { | ||
isMounted = false; | ||
}; | ||
}); | ||
|
||
const onChange = () => { | ||
rerenderElement(divRef.current); | ||
distRef.current = 1; | ||
}; | ||
|
||
return ( | ||
<section className="DivSection"> | ||
<h3>Div (experimental)</h3> | ||
<p> | ||
REACT-VFX also has <InlineCode>VFXDiv</InlineCode>, which allow | ||
us to wrap any elements... | ||
<br /> | ||
so you can make an interactive form with WebGL effects!! | ||
</p> | ||
<VFX.VFXDiv | ||
shader={shader} | ||
ref={divRef} | ||
uniforms={{ | ||
dist: () => distRef.current, | ||
}} | ||
> | ||
<div className="DivSections"> | ||
<div className="DivSectionField"> | ||
<label htmlFor="DivInput">Input (type="text")</label> | ||
<input | ||
id="DivInput" | ||
type="text" | ||
defaultValue="You can edit me!" | ||
onChange={onChange} | ||
/> | ||
</div> | ||
|
||
<div className="DivSectionField"> | ||
<label htmlFor="DivInputRange"> | ||
Input (type="range") | ||
</label> | ||
<input | ||
id="DivInputRange" | ||
type="range" | ||
min="0" | ||
max="100" | ||
defaultValue="0" | ||
onChange={onChange} | ||
/> | ||
</div> | ||
|
||
<div className="DivSectionField"> | ||
<label htmlFor="DivTextArea">Textarea</label> | ||
<textarea | ||
id="DivTextArea" | ||
onChange={onChange} | ||
defaultValue="You can even resize me!" | ||
/> | ||
</div> | ||
</div> | ||
</VFX.VFXDiv> | ||
</section> | ||
); | ||
}; | ||
|
||
export default DivSection; |
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
Oops, something went wrong.