Code Monkey home page Code Monkey logo

stroketext.js's Introduction

strokeText.js

Simple, pixel-perfect text stroking for the web. StrokeText.js adds strokes or "outlines" to live text. Doing this with CSS is scantly and poorly supported, so javascript to the rescue!

  • Strokes do not overlap your text like they do with -webkit-text-stroke
  • Supports nearly all browsers except IE8 and below
  • Selectable text
  • Dependency-free

strokeText.js uses the canvas API to draw stroked text in a container with your text element positioned absolutely on top of it, allowing your text element to remain intact and selectable.

Usage

Install via npm with the package name stroketext.js or simply include strokeText.min.js in your project.

For each text element that you desire to stroke, create an instance, which accepts an ID string or reference to your text element:

var strokeText = new StrokeText('targetId');

Then call stroke(), which takes 2 args:

  • stroke width
  • stroke color
strokeText.stroke(3, 'white'); 

You can also call reset() to remove the stroke.

strokeText.reset();

Calling stroke() calls reset() before it does anything, so you can use stroke() to update the stroke at any time after initialization.

Options

You can optionally pass options (totally optional) to the initializer:

// defaults shown
var options = {
	lineCap: 'round', // ['round', 'butt', 'square']
	lineJoin: 'round', // ['bevel', 'round', 'miter']
	miterLimit: 10, // control spikeyness
	lineDashArray: [0, 0], // for dashed lines: [line, gap]
	debug: false, // examine measurements and properties used
	disableForFirefox: false // some fonts don't stroke well in firefox, bc they are rendered at varying baselines
}
var strokeText = new StrokeText('targetId', options);

Making strokeText.js responsive

Respond to viewport change events:

var resizeTimeout;
function handleViewportChange() {
	// timeout to debounce
	clearTimeout(resizeTimeout);
	resizeTimeout = setTimeout(function() {
		// reset and re-init so strokeText.js can re-evaluate container size
		strokeText.reset(); 
		strokeText = new StrokeText('targetId');
		strokeText.stroke(3, 'white'); 
	}, 100);
}
window.onresize = handleViewportChange;
window.onorientationchange = handleViewportChange;

Angular

Here's an example of how to use strokeText.js in an Angular directive, and react to text changes: https://stackblitz.com/edit/stroketext-angular

jQuery

A jQuery plugin is included, but can't be installed via npm. It's dependent upon strokeText.js.

Known issues

  • In Firefox, some fonts (usually @font-face fonts) don't stroke well because they are rendered at a varying baseline. If this is your case, use the disableForFirefox option.
  • Does not support text-decoration, e.g. strike-through or underline.
  • Very thick strokes on certain fonts can leave cutouts on circle shapes like periods or dotted characters.
  • Emojis don't get stroked ๐Ÿ˜ฌ due to the limitations of canvas.

TODO

  • Better Firefox support. Due to a very old Firefox bug, canvas renders baselines of some fonts differently than html text. The issue is easily witnessed on the demo in Firefox. Some fonts have the stroke lined up perfectly, but for others it is too high, to a varying degree.

Contributing

  • Do your work on strokeText.js, please follow existing formatting conventions
  • In terminal, cd to the strokeText.js directory
  • Run npm i
  • Run npm run build to create the minified strokeText module in the dist folder

Fiddle: https://jsfiddle.net/cburoure/

stroketext.js's People

Contributors

inorganik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

stroketext.js's Issues

React wrapper?

Hey Jamie, nice library! Can we have a react wrapper component for it? I'm willing to help make it but I don't know how haha. Could you make one / give me some directions on how to make one?

Irregular baselines in Firefox

This is an issue easily seen in the demo when opened in Firefox. The issue is that certain fonts render at varying baselines (either on canvas, or rendered text).

Steps to Reproduce

  1. open the demo in FF
  2. change the font to different fonts.

Actual: Some fonts are lined up with their stroke, some are not
Expected: All fonts should line up with their stroke

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.