Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
make HTML5 Canvas text fontSize based on text size in the page instead of Canvas resolution

In the project i have the following Textarea:

<textarea ref={textAreaRef} className='relative z-30 w-11/12 h-full font-sans text-xs bg-transparent outline-none resize-none ' />

and canvas:

 <canvas
                    id={'canvas'}
                    ref={boardRef}
                    })}
                    onMouseDown={onMouseDown}
                    onMouseUp={onMouseUp}
                    onMouseMove={throttle(onMouseMove, 15)}
                    onTouchStart={onMouseDown}
                    onTouchEnd={onMouseUp}
                    onMouseLeave={onPaletteLeave}
                    onTouchCancel={onMouseUp}
                    onTouchMove={throttle(onMouseMove, 15)}
                    width={1600}
                    height={900}
                    style={isLayer ? canvasStyle : null}
                  />

The textarea can contain text with a specific font size in pixels that changes based on user input.

The problem is that if we compare the same text with the same properties in the text area and canvas, the text imprinted in the canvas is clearly smaller than those found in the textarea.

I tried to use the page dimension ratio to try to scale up the resulting text of the canvas, but it did not give good results:

 function ratioSize(size) {
      const canvasRatio = currentCanvas.current.clientWidth / currentCanvas.current.clientHeight;
      const pageRatio = window.screen.width / window.screen.height;
      return (pageRatio * size) / canvasRatio;
    }
  }
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda