All Projects → palantir → Typesettable

palantir / Typesettable

Licence: mit
📐 A typesetting library for SVG and Canvas

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typesettable

Zfont
💬 Text plugin for Zdog - works with any .ttf font!
Stars: ✭ 126 (-5.97%)
Mutual labels:  svg, text, canvas
Vega
A visualization grammar.
Stars: ✭ 9,554 (+7029.85%)
Mutual labels:  svg, canvas
Waveforms
An interactive, explorable explanation about the peculiar magic of sound waves.
Stars: ✭ 1,218 (+808.96%)
Mutual labels:  svg, canvas
Anime
JavaScript animation engine
Stars: ✭ 41,064 (+30544.78%)
Mutual labels:  svg, canvas
Zdog
Flat, round, designer-friendly pseudo-3D engine for canvas & SVG
Stars: ✭ 8,904 (+6544.78%)
Mutual labels:  svg, canvas
Pasition
Path Transition with little JS code, render to anywhere - 轻量级 Path 过渡库,渲染到任何地方
Stars: ✭ 1,149 (+757.46%)
Mutual labels:  svg, canvas
Reimg
reimg - A javascript library for converting image formats
Stars: ✭ 106 (-20.9%)
Mutual labels:  svg, canvas
Html To Image
✂️ Generates an image from a DOM node using HTML5 canvas and SVG.
Stars: ✭ 595 (+344.03%)
Mutual labels:  svg, canvas
Vue Graph
⚡️ Vue components based on the JUI chart available in Vue.js
Stars: ✭ 114 (-14.93%)
Mutual labels:  svg, canvas
Pixelfarm
From Vectors to (sub) Pixels, C# 2D Rendering Library
Stars: ✭ 120 (-10.45%)
Mutual labels:  svg, canvas
Xsound
Web Audio API Library for Synthesizer, Effects, Visualization, Multi-Track Recording, Audio Streaming, Visual Audio Sprite ...
Stars: ✭ 123 (-8.21%)
Mutual labels:  svg, canvas
Calendar Graph
Calendar graph like github using jsx support SVG, Canvas and SSR
Stars: ✭ 58 (-56.72%)
Mutual labels:  svg, canvas
Deep Viz
A React component library, providing concise and beautiful diversity charts with Canvas, SVG, E-map, WebGL, Dom, based on data visualization experience and commercial data display practice.
Stars: ✭ 55 (-58.96%)
Mutual labels:  svg, canvas
Nivo
nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
Stars: ✭ 9,550 (+7026.87%)
Mutual labels:  svg, canvas
Mylittlecanvas
🎨Need to create a custom view ? You don't know how to use Canvas, use MyLittleCanvas instead !
Stars: ✭ 870 (+549.25%)
Mutual labels:  text, canvas
Fe Daily Record
📚前端书籍汇集点 + 每日好文推荐 + 公开课学习资料 + 各种大会资料
Stars: ✭ 94 (-29.85%)
Mutual labels:  svg, canvas
Zrender
A lightweight graphic library providing 2d draw for Apache ECharts
Stars: ✭ 5,122 (+3722.39%)
Mutual labels:  svg, canvas
People You Should Follow On Codepen
People You Should Follow on CodePen
Stars: ✭ 542 (+304.48%)
Mutual labels:  svg, canvas
Echarts
Apache ECharts is a powerful, interactive charting and data visualization library for browser
Stars: ✭ 49,119 (+36555.97%)
Mutual labels:  svg, canvas
G2
📊 A highly interactive data-driven visualization grammar for statistical charts.
Stars: ✭ 11,020 (+8123.88%)
Mutual labels:  svg, canvas

Typesettable

Circle CI

Overview

Typesettable is a library for measuring, wrapping, and writing text on Canvas, SVG, and HTML.

Canvas and HTML support some rudimentary wrapping, but SVG does not support any. Furthermore, developers often want wrapped text to auto-hyphenate and truncate with ellipses when overflowing the bounding box. Typesettable aims to make this entire process easier.

Typesettable works with native browser APIs and has no external dependencies.

Features

  • Measurers efficiently measure the size of a piece of text, taking into account the font styles affecting that text.
  • Wrappers calculate how best to fit text into a given space, based on results from the Measurer.
  • Writers layout and write text based on specified options such as wrapping, alignment, rotation, and shearing.
  • SvgContext, CanvasContext, and HtmlContext implement factories for the IRuler and IPen objects, which encapsulate the functionality for measuring and writing text in SVG, Canvas, and HTML elements.

Installation

npm install --save typesettable

Usage

Example Two Liner

import { Typesetter } from "typesettable";

const typesetter = Typesetter.svg(document.querySelector("svg"));
typesetter.write("Hello World!", 200, 200);

Example SVG, Canvas, & HTML

Use typesetters with both SVG and Canvas elements:

import { Typesetter } from "typesettable";

// A typesetter for SVG elements
const svgTypesetter = Typesetter.svg(document.querySelector("svg"));

// A typesetter for Canvas elements
const canvasTypesetter = Typesetter.canvas(document.querySelector("canvas").getContext("2d"));

// A typesetter for HTML elements
const htmlTypesetter = Typesetter.html(document.querySelector("div.text-container"));

// The dimensions into which the text will be wrapped and truncated
const width = 300;
const height = 50;

// Options for alignment, etc.
const writeOptions = {
  xAlign: "left",
  yAlign: "top",
  textRotation: 0,
  textShear: 0,
};

// Write the text to the elements
svgTypesetter.write("Hello SVG!", width, height, writeOptions);
canvasTypesetter.write("Hello Canvas!", width, height, writeOptions);
htmlTypesetter.write("Hello HTML!", width, height, writeOptions);

Example Shared Cache

If you are typesetting multiple strings of text with the same font style, maintain a cache of Measurer results to improve performance.

Your HTML might look like:

<svg>
  <g class="section-one"></g>
  <g class="section-two" transform="translate(120 0)"></g>
</svg>

To share text measurements between writer, you can use the simple Typesetter interface, which already uses a shared CacheMeasurer. Or, you can compose the components manually like so:

import { CacheMeasurer, SvgContext, Wrapper, Writer } from "typesettable";

const svg = document.querySelector("svg");
const context = new SvgContext(svg);
const measurer = new CacheMeasurer(context);
const wrapper = new Wrapper();
const writer = new Writer(measurer, context, wrapper);
const writeOptions = { xAlign: "center" };

writer.write(
  "This text is in the first section",
  100, 400, writeOptions,
  svg.querySelector("g.section-one")
);

writer.write(
  "This text is in the second section",
  100, 200, writeOptions,
  svg.querySelector("g.section-two")
);

API Docs

See the docs for more detailed examples.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].