Skip to content

Commit

Permalink
automatically lower signature font size if text doesn't fit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrToorchyn authored and omohokcoj committed Jul 6, 2024
1 parent f1d6bbd commit e55c8f2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
21 changes: 18 additions & 3 deletions app/javascript/submission_form/initials_step.vue
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,28 @@ export default {
const context = canvas.getContext('2d')
const fontFamily = 'Arial'
const fontSize = '44px'
const initialFontSize = 50
const fontStyle = 'italic'
const fontWeight = ''
context.font = fontStyle + ' ' + fontWeight + ' ' + fontSize + ' ' + fontFamily
context.textAlign = 'center'
const setFontSize = (size) => {
context.font = `${fontStyle} ${fontWeight} ${size}px ${fontFamily}`
}
const adjustFontSizeToFit = (text, maxWidth, initialSize) => {
let size = initialSize
setFontSize(size)
while (context.measureText(text).width > maxWidth && size > 1) {
size -= 1
setFontSize(size)
}
}
adjustFontSizeToFit(e.target.value, canvas.width / scale, initialFontSize)
context.textAlign = 'center'
context.clearRect(0, 0, canvas.width / scale, canvas.height / scale)
context.fillText(e.target.value, canvas.width / 2 / scale, canvas.height / 2 / scale + 11)
},
Expand Down
21 changes: 18 additions & 3 deletions app/javascript/submission_form/signature_step.vue
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,28 @@ export default {
const context = canvas.getContext('2d')
const fontFamily = 'Dancing Script'
const fontSize = '38px'
const initialFontSize = 44
const fontStyle = 'italic'
const fontWeight = ''
context.font = fontStyle + ' ' + fontWeight + ' ' + fontSize + ' ' + fontFamily
context.textAlign = 'center'
const setFontSize = (size) => {
context.font = `${fontStyle} ${fontWeight} ${size}px ${fontFamily}`
}
const adjustFontSizeToFit = (text, maxWidth, initialSize) => {
let size = initialSize
setFontSize(size)
while (context.measureText(text).width > maxWidth && size > 1) {
size -= 1
setFontSize(size)
}
}
adjustFontSizeToFit(e.target.value, canvas.width / scale, initialFontSize)
context.textAlign = 'center'
context.clearRect(0, 0, canvas.width / scale, canvas.height / scale)
context.fillText(e.target.value, canvas.width / 2 / scale, canvas.height / 2 / scale + 11)
},
Expand Down

0 comments on commit e55c8f2

Please sign in to comment.