Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pixelairport's avatar

Resize canvas with components from 100% to 120%

I want to build somthing that has a feature like in figma, photoshop, sketch,... where you have a scale option to show your workarea. To make the question more easy: I have a Div layer where user can drag an image to. The div layer is 400pxx400px. But the user should be able to make it larger at the preview with a button. For example scale it to 120% that it is still 400pxx400px but the preview shows it as 480pxx480px. Is there a solution for this? Maybe a ready to use option for vue? Thx

0 likes
2 replies
LaryAI's avatar
Level 58

One possible solution to resize the canvas with components from 100% to 120% is by using CSS transforms. Here's an example using Vue.js:

  1. First, create a Vue component for the canvas:
<template>
  <div class="canvas" :style="{ transform: `scale(${scale})` }">
    <!-- Your canvas content here -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      scale: 1, // Initial scale is 100%
    };
  },
  methods: {
    increaseScale() {
      this.scale += 0.2; // Increase scale by 20%
    },
  },
};
</script>

<style>
.canvas {
  width: 400px;
  height: 400px;
  transform-origin: top left; /* Set the origin point for scaling */
}
</style>
  1. In your parent component, import and use the canvas component:
<template>
  <div>
    <canvas></canvas>
    <button @click="increaseScale">Increase Scale</button>
  </div>
</template>

<script>
import Canvas from './Canvas.vue';

export default {
  components: {
    Canvas,
  },
};
</script>

Now, when the user clicks the "Increase Scale" button, the canvas will be scaled up by 20% each time. The transform-origin property ensures that the scaling is applied from the top left corner of the canvas.

Note: This solution assumes you are using Vue.js and have a basic understanding of Vue components. Adjust the code accordingly if you are using a different framework or plain JavaScript.

Please or to participate in this conversation.