Skip to content

Commit e55c8f2

Browse files
AlexBTurchynomohokcoj
authored andcommitted
automatically lower signature font size if text doesn't fit
1 parent f1d6bbd commit e55c8f2

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

app/javascript/submission_form/initials_step.vue

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,28 @@ export default {
251251
const context = canvas.getContext('2d')
252252
253253
const fontFamily = 'Arial'
254-
const fontSize = '44px'
254+
const initialFontSize = 50
255255
const fontStyle = 'italic'
256256
const fontWeight = ''
257257
258-
context.font = fontStyle + ' ' + fontWeight + ' ' + fontSize + ' ' + fontFamily
259-
context.textAlign = 'center'
258+
const setFontSize = (size) => {
259+
context.font = `${fontStyle} ${fontWeight} ${size}px ${fontFamily}`
260+
}
261+
262+
const adjustFontSizeToFit = (text, maxWidth, initialSize) => {
263+
let size = initialSize
260264
265+
setFontSize(size)
266+
267+
while (context.measureText(text).width > maxWidth && size > 1) {
268+
size -= 1
269+
setFontSize(size)
270+
}
271+
}
272+
273+
adjustFontSizeToFit(e.target.value, canvas.width / scale, initialFontSize)
274+
275+
context.textAlign = 'center'
261276
context.clearRect(0, 0, canvas.width / scale, canvas.height / scale)
262277
context.fillText(e.target.value, canvas.width / 2 / scale, canvas.height / 2 / scale + 11)
263278
},

app/javascript/submission_form/signature_step.vue

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,28 @@ export default {
455455
const context = canvas.getContext('2d')
456456
457457
const fontFamily = 'Dancing Script'
458-
const fontSize = '38px'
458+
const initialFontSize = 44
459459
const fontStyle = 'italic'
460460
const fontWeight = ''
461461
462-
context.font = fontStyle + ' ' + fontWeight + ' ' + fontSize + ' ' + fontFamily
463-
context.textAlign = 'center'
462+
const setFontSize = (size) => {
463+
context.font = `${fontStyle} ${fontWeight} ${size}px ${fontFamily}`
464+
}
465+
466+
const adjustFontSizeToFit = (text, maxWidth, initialSize) => {
467+
let size = initialSize
464468
469+
setFontSize(size)
470+
471+
while (context.measureText(text).width > maxWidth && size > 1) {
472+
size -= 1
473+
setFontSize(size)
474+
}
475+
}
476+
477+
adjustFontSizeToFit(e.target.value, canvas.width / scale, initialFontSize)
478+
479+
context.textAlign = 'center'
465480
context.clearRect(0, 0, canvas.width / scale, canvas.height / scale)
466481
context.fillText(e.target.value, canvas.width / 2 / scale, canvas.height / 2 / scale + 11)
467482
},

0 commit comments

Comments
 (0)