javascript
Posted in
398
4:48 am, May 21, 2023
Transform Text into Images using SnapText JS
Note: It seems this only works on node as far as i can tell from testing and its not a scipt you can just embed into a html page.
SnapText
📸 Snap a picture of HTML text
Converts HTML text to an SVG, Image, Blob, or a Canvas element.
For a demo see the "snap picture" buttons at the top of code examples on PQINA.nl
<script type="module">
// Import the module
import snapText from './snapText.js';
// Snap a picture of some text
const canvas = await snapText('pre.language-html');
// Add picture to body for viewing
document.body.append(canvas);
</script>
I've only tested this on desktop Chrome, Firefox, and Safari with text inside a <pre>
element. At this time I'm not sure if it'll work with non preformatted text.
Usage
Pass either an Element
or querySelector string
as the first argument, the second argument is an optional options object:
// Use default options
const svg = await snapText('.my-element');
// Customize options (shows defaults)
const svg = await snapText('.my-element', {
// The output format, set to 'svg', 'canvas', 'img', or 'blob'
format: 'svg',
// The output width in pixels (can set `width`, or `height`, or both)
width: undefined,
// The output height in pixels (can set `width`, or `height`, or both)
height: undefined,
// Padding in pixels around the output, added to the inside when `width` or `height` is set
padding: 0,
// Size multiplier in pixels
scalar: 1,
});
Example:
const img = await snapText('.my-element', {
// We want an <img> element
format: 'img',
// Image will be 1024 wide
width: 1024,
// Add 20 pixels padding on the inside
padding: 20,
});
// Add to body for viewing
document.body.append(img);
Transform Text into Images using SnapText JS Demo
View Demo Full Screen View Demo New Tab
Transform Text into Images using SnapText JS Code
Add Comment
Other Items in javascript
Related Search Terms
Other Categories in Code
404 pages animation animations background effects boxes buttons css css & javascript css animation css animations css backgrounds css cursors css framework css frameworks css modal css positions css reset css shadows css slider css text effects css text formatting css tips css tools css z-index content slider cookie alerts design html css sections html canvas html sections icons image zoom interactive images lightbox link effects loading animation modals mouse effects navigation notifications parallax profiles quick css seo sections text ui web developer checklists animations backgrounds bootstrap buttons css css animated css animations css framework css grid footers forms html image effects images javascript scroll effects sections show your work svg text animation timeline
css animations
HTML Canvas
javascript