Skip to content

Commit

Permalink
Renderer config is now an object instead of params, allow clearColor …
Browse files Browse the repository at this point in the history
…in config

To hide flash of black canvas
  • Loading branch information
jonikorpi committed Aug 26, 2021
1 parent bfcb063 commit e6f7b8c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
6 changes: 4 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ const requestRendering = () => {
}
};

export const createRenderer = (canvas, attributes = blankObject, pixelRatio = 1, debug = false) => {
export const createRenderer = (
canvas,
{ attributes = blankObject, pixelRatio = 1, debug = false, clearColor = [0, 0, 0, 1] }
) => {
let gl = canvas.getContext("webgl2", { ...defaultAttributes, ...attributes });

// Caches and setters
Expand Down Expand Up @@ -172,7 +175,6 @@ export const createRenderer = (canvas, attributes = blankObject, pixelRatio = 1,
};

// Clearing
let clearColor = [0, 0, 0, 1];
let lastDepth = 1;
const setClear = (color = clearColor, depth = lastDepth) => {
color.forEach((value, index) => {
Expand Down
12 changes: 5 additions & 7 deletions main.preact.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,24 @@ import {
} from "./main.js";
export * from "./main.js";

const blankObject = {};

export const Canvas = ({ attributes, pixelRatio, debug = false, ...rest }) => {
export const Canvas = ({ attributes, pixelRatio, debug, clearColor, ...rest }) => {
const ref = useRef();
useCanvas(ref, attributes, pixelRatio, debug);
useCanvas(ref, { attributes, pixelRatio, debug, clearColor });
return h("canvas", {
ref,
...rest,
});
};

export const useCanvas = (elementOrRef, attributes = blankObject, pixelRatio, debug = false) => {
export const useCanvas = (elementOrRef, config) => {
useEffect(() => {
const canvas = elementOrRef.current || elementOrRef;
const renderer = createRenderer(canvas, attributes, pixelRatio, debug);
const renderer = createRenderer(canvas, config);

return () => {
renderer.destroy();
};
}, [elementOrRef, pixelRatio, debug, ...Object.keys(attributes), ...Object.values(attributes)]);
}, [elementOrRef, ...Object.keys(config), ...Object.values(config)]);
};

export const component = (...args) => {
Expand Down
20 changes: 9 additions & 11 deletions main.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@ import {
} from "./main.js";
export * from "./main.js";

const blankObject = {};

export const Canvas = ({ attributes, pixelRatio, debug = false, ...rest }) => {
export const Canvas = ({ attributes, pixelRatio, debug, clearColor, ...rest }) => {
const ref = useRef();
useCanvas(ref, attributes, pixelRatio, debug);
useCanvas(ref, { attributes, pixelRatio, debug, clearColor });
return createElement("canvas", {
ref,
...rest,
});
};

export const useCanvas = (elementOrRef, attributes = blankObject, pixelRatio, debug = false) => {
export const useCanvas = (elementOrRef, config) => {
useEffect(() => {
const canvas = elementOrRef.current || elementOrRef;
const renderer = createRenderer(canvas, attributes, pixelRatio, debug);
const renderer = createRenderer(canvas, config);

return () => {
renderer.destroy();
};
}, [elementOrRef, pixelRatio, debug, ...Object.keys(attributes), ...Object.values(attributes)]);
}, [elementOrRef, ...Object.keys(config), ...Object.values(config)]);
};

export const component = (...args) => {
Expand Down Expand Up @@ -59,10 +57,10 @@ export const useComponent = (component, props, enabled = true) => {

const instance = ref.current;

const update = useCallback((name, value) => updateInstance(instance, name, value), [
instance,
updateInstance,
]);
const update = useCallback(
(name, value) => updateInstance(instance, name, value),
[instance, updateInstance]
);

// Update data from props
for (const key in props) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vuoro/sahti",
"version": "0.9.0",
"version": "0.10.0",
"description": "Write a WebGL 2 command, use it like a component.",
"type": "module",
"main": "main.js",
Expand Down

0 comments on commit e6f7b8c

Please sign in to comment.