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

Scratchblocks SSR #402

Open
FunctionalMetatable opened this issue Nov 7, 2021 · 9 comments
Open

Scratchblocks SSR #402

FunctionalMetatable opened this issue Nov 7, 2021 · 9 comments

Comments

@FunctionalMetatable
Copy link

I'm making a tutorials site (using svelte and sveltekit) and it seems that scratchblocks only can run in the browser.
Is there anyway to support SSR/svg strings?

@CST1229
Copy link
Contributor

CST1229 commented Nov 7, 2021

Can't it be rendered client-side?

@FunctionalMetatable
Copy link
Author

Can't it be rendered client-side?

I want it to be rendered server-side so its less work for the client

@apple502j
Copy link
Collaborator

Syntax parsing can be done on the server (and there is a unittest for that).

Rendering, on the other hand, requires client:

  • The code assumes DOM is available. This is less of a concern when using JSDOM etc.
  • Likewise, it also requires XML parsing/serializer to be available.
  • The generated SVG depends on rendered text size - before rendering, the code calls measureText to measure the length of the strings used, in whatever font it is using.

If that one gets fixed in some way, this can be rendered server-side. Until then, it's time to use puppeteer. (Luckily, the setup is already there.)

@apple502j
Copy link
Collaborator

Usage of .width (used for rendering): scratch3/blocks.js

  • L87 This is NOT measured. Additionally, the value isn't used anyway (ignored inside SVG.symbol)
  • L178 This value, and therefore InputView.width, IS measured. the value is calculated and passed to SVG.move which uses transform: translate(dx, dy).
  • L448, L523 This .width is also measured.
  • L600 This .width is measured, and goes to <rect>.
  • L652
  • L684
  • L755 etc.

@FunctionalMetatable
Copy link
Author

Somehow s3blocks do this?

@apple502j
Copy link
Collaborator

@FunctionalMetatable This requires canvas dependency, and doesn't make sense, since text measurement and actual rendering should be performed at the same time for it to be effective.

@apple502j
Copy link
Collaborator

@tjvr There are three ways for this:

  1. Calculate text width server-side. This can potentially lead to rendering issues (e.g. font difference?), but is easier.
  2. Make it generate a JavaScript code that would output SVG. Rendering would be correct, but may not be easy to use.
  3. Make everything server-side - the result is a PNG file and has no rendering issue, although this is terrible for accessibility.

@tjvr
Copy link
Member

tjvr commented Mar 6, 2022

I'm not sure what you mean by (2) -- how would this be different to using the library currently?

I think rendering to PNG would probably be the only sensible way to achieve "server-side rendering", if you really want to.

@ghost
Copy link

ghost commented Jul 10, 2023

I have recently decided to set up something like this myself, so that people may more easily embed blocks on GitHub and Discord (and other places) which do not support the plugin!

SVG

There is also proof-of-concept support for PNG output support, but it does not have text until thx/resvg-js#217 is merged.

PNG

To use, just percent‐encode the program and put it into the URL’s query part (line breaks can be encoded as %0A): https://scratch.deno.dev/blocks.svg?say%20%5BHello%21%5D

(Hmm, now that I think about it, maybe I should set up some way to control the size of the blocks.)

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

No branches or pull requests

4 participants