Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!