Last active
August 5, 2021 09:17
-
-
Save alanbsmith/f51c7adc4d8fd1341a569247cc78d28b to your computer and use it in GitHub Desktop.
A JS function for adding a signature to an HTML canvas element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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