Skip to content

Instantly share code, notes, and snippets.

@alanbsmith
Last active August 5, 2021 09:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanbsmith/f51c7adc4d8fd1341a569247cc78d28b to your computer and use it in GitHub Desktop.
Save alanbsmith/f51c7adc4d8fd1341a569247cc78d28b to your computer and use it in GitHub Desktop.
A JS function for adding a signature to an HTML canvas element
function sign(name, fontSize = 96) {
// assumes the id of the canvas element is 'canvas'
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d');
ctx.font = `italic ${fontSize}px Snell Roundhand`;
const signatureWidth = ctx.measureText(name).width;
const x = canvas.width/2 - signatureWidth/2;
const y = canvas.height/2 + fontSize/2;
ctx.fillStyle = ctx.strokeStyle;
ctx.fillText(name, x, y);
}
sign('Alan B Smith')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment