A web component called tiny-ps
Now let's make something useful. What if you could add PostScript code just into your HTML code and forget about all the JavasScript? What if you could configure it just with the properties of the tag, having your PostScript graphics at the size as you want and at the format you want? Like an canvas tag, but without JavaScript. Like an SVG tag, but programmable.
In this article, we will configure a HTML tag that can do exactly that. The HTML standard allows us to define custom tags, called web components, that are freely programmable. The only condition is that the tag has a hyphen in it, to separate it from the native tags. The web component API allows you to maintain a shadow DOM where you can render the node as you wish.
We add the configuration code to our codebase. The tinyPStag class extends HTML-element and the attributeChangedCallback method reacts to all changes of the attributes and the innerHTML which will be the PostScript code.
We put all device elements in the attributes
- width
- height
- format (canvas, canvasurl, pdf, pdfurl, raw, rawurl, svg, svgurl)
- oversampling (1, 2 only used by raw)
- transparent
- interval (msec, for multiple pages)
A minimal installation is on
https://www.belle-nuit.com/site/files/minimal11.html
You can see what happens with the inspector. Try to change the PostScript code and the attributes, the node browser rerenders the element.
An installation with sample code
https://www.belle-nuit.com/site/files/minimal12.html
The next step is to publish the web component on Github.
ps20250211.js (4049 lines, 147 KB)
And properly named
tinyps100.js
https://www.belle-nuit.com/site/files/minimal13.html
https://www.belle-nuit.com/site/files/minimal14.html
Ready to go GitHub
https://github.com/bellenuit/tiny-ps