Skip to content

Instantly share code, notes, and snippets.

@petervavro
Last active June 16, 2023 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petervavro/6fa3f2fbe14e5245c08600565144db62 to your computer and use it in GitHub Desktop.
Save petervavro/6fa3f2fbe14e5245c08600565144db62 to your computer and use it in GitHub Desktop.
I would like to share my Konva Node.js wrapper for server-side generation in Next.js.
let Konva = require('konva');
let canvas = require('canvas');
// mock window
Konva.window = {
Image: canvas.Image,
devicePixelRatio: 1,
};
// mock document
Konva.document = {
createElement: function () { },
documentElement: {
addEventListener: function () { },
},
};
// make some global injections
global.requestAnimationFrame = (callback: FrameRequestCallback): number => {
setTimeout(() => {
callback(performance.now());
}, 1000 / 60);
return 1
};
// create canvas in Node env
Konva.Util.createCanvasElement = () => {
const node = new canvas.Canvas();
node.style = {};
return node;
};
// create image in Node env
Konva.Util.createImageElement = () => {
const node = new canvas.Image();
node.style = {};
return node;
};
// _checkVisibility use dom element, in node we can skip it
Konva.Stage.prototype._checkVisibility = () => { };
export default Konva;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment