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

195
Visualizações
Vue - wrong size of canvas with refs

I have a problem with accessing canvas with vue refs. The ref to the canvas doesn't properly respond to applied css height and width (it has still the old property). This leads to wrong canvas context (ctx) and drawing. The partial solution is shown in function "getPropperSize" in the code, but there has to be a better way. Has someone any idea what's going on and why the myCanvas variable behaves strangely.

<template>
<h1>Canvas</h1>
<button @click="getPropperSize">getPropperSize</button>
<button @click="drawRect">drawRect</button>
  <canvas ref="myCanvas" id="canv"> </canvas>
</template>

<script>
import { onMounted, onUpdated, ref } from 'vue'

export default {
setup() {
const myCanvas = ref(null);
console.log(myCanvas)
let ctx = null

onMounted(() => {
    //This will output 150 even it shuld be 500
    console.log(myCanvas.value.height)
    ctx = myCanvas.value.getContext('2d')
})


const getPropperSize = function(){
    console.log(myCanvas.value.clientHeight)
    console.log(myCanvas.value.clientWidth)
    myCanvas.value.height = myCanvas.value.clientHeight
    myCanvas.value.width = myCanvas.value.clientWidth
}

const drawRect = function(){
    ctx.fillStyle = "#FF0000";
    ctx.fillRect(0, 0, 150, 75);
}

return {myCanvas, getPropperSize, drawRect}
}
}
</script>

<style>
#canv{
    height: 50vh;
    width: 50vw;
    border: 3px solid #73AD21;
}
</style>
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