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

Implement Node support #5

Merged
merged 1 commit into from
Nov 16, 2021
Merged

Conversation

pento
Copy link
Contributor

@pento pento commented Nov 8, 2021

Demo: https://codepen.io/pento/pen/OJjZPgE

Fixes #4.

Uses the canvas package, which provides a Node implementation of <canvas>, as well as some basic Image support (sufficient for these purposes, at least).

When running in the browser, it provides a small shim that maps to document.createElement('canvas') and document.createElement('img').

Where Stickerify currently takes a HTMLImageElement and returns a HTMLCanvasElement, running it in Node.js will cause it to take an Image object, and return a Canvas object, both defined by the canvas package.

Here's an example of Node usage (I've also added this to the README):

const { stickerify } = require("stickerify");
const { loadImage } = require( "canvas" );
const { writeFile } = require( "fs" );

loadImage("https://raw.githubusercontent.com/markus-wa/stickerify/main/example/input.png")
	.then(image => stickerify(image, 3, "white"))
	.then(image => stickerify(image, 1, "grey"))
	.then(canvas => writeFile("output.png", canvas.toBuffer(), err => console.log(err || "done")));

@markus-wa
Copy link
Owner

Great work!

I'll have a closer look later this week, but one thing that bothers me is that we have to turn the images into data-URL strings, which has a high overhead (~33% data size increase and time to encode/decode).

Ideally the API would take and return one of the following:

  • byte array
  • reader / writer Streams
  • abstract cross-environment "image" type

the least one would be my preference.

some things to look at - maybe that will help solve this issue:

we could also use the above libraries instead of canvas - not sure.

@markus-wa
Copy link
Owner

markus-wa commented Nov 8, 2021

also a tiny minor request - could you make sure all the indentation is consistent across the code? 😇

but yeah I know auto-format from editors can be a pain 😅

@markus-wa markus-wa added the enhancement New feature or request label Nov 8, 2021
@markus-wa
Copy link
Owner

markus-wa commented Nov 8, 2021

ah - I just realised now that the only reason we'd need a promise-based API is if we're loading remote images.

so yeah sorry, I got myself a bit confused!

but basically - can we split this into multiple functions? It would be good to have a func that doesn't do promises, but rather just the pure transformation and then we provide a wrapper that adds image loading via promises as a utility.

sorry for causing confusion 😄

@pento pento changed the title Implement Node support, and switch to a Promise-based interface. Implement Node support Nov 13, 2021
@pento pento force-pushed the promisify-and-nodify branch 5 times, most recently from e9836e4 to 3bfdabf Compare November 13, 2021 09:13
@pento
Copy link
Contributor Author

pento commented Nov 13, 2021

Thanks for the feedback, @markus-wa. 🙂 I've re-thought it a bit, and realised it's just as straightforward for the image loading to be done before passing the image to Stickerify: this removes the API breakage that I'd originally proposed.

also a tiny minor request - could you make sure all the indentation is consistent across the code?

No worries, I think I've fixed it all up, but please double check it. If you happen to have a linting config you could add to the project, that'd be super helpful. 🙂

@markus-wa
Copy link
Owner

Thanks @pento - that looks and sounds all good to me!

I will add a linting config later this week and make sure all the code is compliant with that 🙂

But for now I'll merge it as is!

@markus-wa markus-wa merged commit 93f9e78 into markus-wa:main Nov 16, 2021
@pento pento deleted the promisify-and-nodify branch November 17, 2021 00:35
@markus-wa
Copy link
Owner

just published v0.2.0 with this change 🚀 - thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggestion: Provide a Promise-based interface
2 participants