Search code examples
vue.jskonvavue-konva

Draw shapes by konva to be displayed in canvas


I want to use konva in vue.js to draw shapes to be displayed in a canvas, so i have some rectangles stored in rectangles array which need to be drawn, but no thing is drawn.

<template>
<div>
  <Button id="b1" @click="promp()" style="background:url(/squ.png)"></Button>
  <canvas id="myCanvas" width="500" height="600" style="border:1px solid #000000;" @click="detectclick($event)">
      <v-rect
          v-for="item in rectangles"
          :key="item.id"
          :config="item"
          @transformend="handleTransformEnd($e,rectangles)"
        />
        <v-transformer ref="transformer" />
  </canvas>
 
  </div>
</template>

Solution

  • You can't put vue-konva components inside <canvas> element.

    All vue-konva components are designed to work only inside <v-layer component, which should be placed inside <v-stage>. For understanding, Konva nodes structure take a look into Konva Overview.

    <v-stage :config="{width: 500, height: 500}">
      <v-layer>
        <v-rect
          v-for="item in rectangles"
          :key="item.id"
          :config="item"
          @transformend="handleTransformEnd($e,rectangles)"
        />
      </v-layer>
    </v-stage>