Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rendering raindrops layer only #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bundle/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bundle/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/renderer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export interface RenderOptions {
width: number;
height: number;
background: TextureData | string;
/**
* Render only raindrops layer on transparent background.
* Useful for custom background blending with HTML elements.
*/
raindropLayerOnly: boolean;
/**
* Background blur steps used for background & raindrop refract image.
* Value should be integer from 0 to log2(backgroundSize).
Expand Down
2 changes: 1 addition & 1 deletion dist/renderer.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/detached-background/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Raindrop Effect</title>
<link rel="stylesheet" href="./style.css">
<script src="/bundle/index.js"></script>
</head>

<body>
<div id="root">
<canvas id="canvas"></canvas>
<div id="background"></div>
</div>
<script src="./script.js"></script>
</body>

</html>
30 changes: 30 additions & 0 deletions examples/detached-background/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @typedef {import("../../bundle/index")}
*/

const canvas = document.querySelector("#canvas");
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width;
canvas.height = rect.height;

const raindropFx = new RaindropFX({
canvas: canvas,
background: "/assets/img/84765992_p0.jpg",
raindropLayerOnly: true,
});

window.onresize = () =>
{
const rect = canvas.getBoundingClientRect();
raindropFx.resize(rect.width, rect.height);
}
window.addEventListener("mousemove", (ev) =>
{
const offsetX = ev.clientX - window.innerWidth / 2;
const offsetY = ev.clientY - window.innerHeight / 2;
const background = document.querySelector("#background");
background.style.transform = `translate(${offsetX * 0.02}px, ${offsetY * 0.02}px)`;

})

raindropFx.start();
50 changes: 50 additions & 0 deletions examples/detached-background/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
body {
margin: 0;

background-position: 0px 0px,
16px 16px;
background-size: 32px 32px;
background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee 100%),
linear-gradient(45deg, #eee 25%, white 25%, white 75%, #eee 75%, #eee 100%);
}

#root {
position: absolute;
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: grey;
overflow: hidden;
}

#canvas {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 10;
}

#background {
background-image: url("/assets/img/84765992_p0.jpg");
background-size: cover;
background-position: center;
width: 110vw;
height: 110vh;
position: absolute;
left: -5vw;
top: -5vh;
z-index: 0;
filter: blur(16px);
}

#loading {
position: absolute;
z-index: -1;
font-family: 'Segoe UI', Roboto, Tahoma, Geneva, Verdana, sans-serif;
font-size: 2em;
color: gray;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class RaindropFX
xShifting: [0, 0.1],

// Rendering options
raindropLayerOnly: false,
backgroundBlurSteps: 3,
mist: true,
mistColor: [0.01, 0.01, 0.01, 1],
Expand Down
21 changes: 19 additions & 2 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ export interface RenderOptions
width: number;
height: number;
background: TextureData | string;
/**
* Render only raindrops layer on transparent background.
* Useful for custom background blending with HTML elements.
*/
raindropLayerOnly: boolean;
/**
* Background blur steps used for background & raindrop refract image.
* Value should be integer from 0 to log2(backgroundSize).
Expand Down Expand Up @@ -349,9 +354,21 @@ export class RaindropRenderer
this.drawRaindrops(raindrops);

this.renderer.setRenderTarget(RenderTarget.CanvasTarget);
this.renderer.clear(Color.black);
this.renderer.clear(Color.black.transparent());

this.drawBackground();
if (this.options.raindropLayerOnly)
{
if (this.options.mist)
{
this.matrlMistCompose.mistTex = this.mistTexture;
this.matrlMistCompose.texture = null;
this.matrlMistCompose.mistColor = new Color(...this.options.mistColor);
this.matrlMistCompose.mistColor.a = 0.1;
this.renderer.blit(this.mistBackground, RenderTarget.CanvasTarget, this.matrlMistCompose);
}
}
else
this.drawBackground();

this.matrlCompose.background = this.blurryBackground;
this.matrlCompose.backgroundSize = vec4(this.options.width, this.options.height, 1 / this.options.width, 1 / this.options.height);
Expand Down