ondras / primitive.js

JS port of primitive.lol
https://ondras.github.io/primitive.js/
MIT License
777 stars 81 forks source link

How to use on server-side? #19

Open ChrisRobston opened 6 years ago

ChrisRobston commented 6 years ago

Hello. Is there an option to use this package on Node backend? It is not available on npm, right? If so, why?

ondras commented 6 years ago

This project relies on the 2D Canvas API that is primarily found in browsers. There might be some nodejs module that implements Canvas, but I have never tried that. If you find a compatible server-side Canvas implementation, you might be able to run this code server-side. I would package it for NPM then.

ChrisRobston commented 6 years ago

Thanks for response @ondras I will search this topic next few days. Really hope there's the chance to use this beauty to generate previews of images on server-side. It's incredible.

For now Google show me this https://github.com/Automattic/node-canvas Seems legit.

Ataraxy commented 6 years ago

It's possible and I've done this before using node-canvas and jsdom. Essentially I created a small piece of html to mimic the html page the library uses, loaded that with jsdom, then used node-canvas in place of the browsers Canvas. I was even able to stream the results from a AWS Lambda to a browser via websockets which was kind of neat since multiple people could 'watch' the generation occur in real time.

Though I don't have any code to demonstrate this with unfortunately as I did it quite a while ago.

LoggeL commented 5 years ago

https://gist.github.com/LoggeL/ffe3204649be0aefa178dab79b5a1a1f

I've automated it with Puppeteer. Might be overkill.