百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
V

vue-konva

> 前端框架
开源

Vue & Canvas - 使用 Vue 绘制复杂 Canvas 图形的 JavaScript 库。

1.3K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Vue & Canvas - 使用 Vue 绘制复杂 Canvas 图形的 JavaScript 库。

Vue Konva

Vue Konva provides declarative Vue components for the Konva 2D canvas scene graph. Use it to build design editors, whiteboards, diagrams, annotations, and other interactive graphics.

Vue Konva is MIT licensed. Each component uses the name of its Konva node with a v- prefix.

  • Vue tutorial
  • Live demos
  • Konva API
  • Star the project Each vue-konva component corresponds to a Konva node and uses the v- prefix. Pass Konva properties through the component config prop.

Core shapes include v-rect, v-circle, v-line, v-image, v-text, and v-path. You can also create a custom shape.

To get more info about Konva you can read Konva Overview.

Quick Start

Vue 3 is required for the current package.

1 Install via npm

npm install vue-konva konva

For Vue 2, use vue-konva@2.

2 Import and use VueKonva

import { createApp } from 'vue';
import App from './App.vue';
import VueKonva from 'vue-konva';

const app = createApp(App);
app.use(VueKonva);
app.mount('#app');

3 Reference in your component templates

<template>
  <v-stage :config="configKonva">
    <v-layer>
      <v-circle :config="configCircle"></v-circle>
    </v-layer>
  </v-stage>
</template>
<script>
export default {
  data() {
    return {
      configKonva: {
        width: 200,
        height: 200
      },
      configCircle: {
        x: 100,
        y: 100,
        radius: 70,
        fill: "red",
        stroke: "black",
        strokeWidth: 4
      }
    };
  }
};

</script>

Or use a CDN

The UMD build registers itself as the global VueKonva.


<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="https://unpkg.com/konva@10/konva.min.js"></script>
<script src="https://unpkg.com/vue-konva@4/dist/vue-konva.umd.js"></script>
<script>
  const app = Vue.createApp({
    data() {
      return {
        configKonva: { width: 200, height: 200 },
        configCircle: { x: 100, y: 100, radius: 70, fill: 'red', stroke: 'black' },
      };
    },
  });
  app.use(VueKonva);
  app.mount('#app');
</script>

Building a full design editor? Polotno is a commercial design editor SDK built on Konva by the Konva maintainers. It ships templates, text editing, and export, so you integrate an editor instead of building one: npm install polotno. Polotno has a Vue integration guide.

Core API

Getting reference to Konva objects

You can use ref feature from vue.

<template>
  <v-stage ref="stage">
    <v-layer ref="layer">
      <v-rect ref="rect" />
    </v-layer>
  </v-stage>
</template>

<script>
  const width = window.innerWidth;
  const height = window.innerHeight;

  export default {
    mounted() {
      const stage = this.$refs.stage.getNode();
      const layer = this.$refs.layer.getNode();
      const rect = this.$refs.rect.getNode();
    },
  };
</script>

Strict mode

By default vue-konva works in "non-strict" mode. If you changed a property manually (or by user action like drag&drop) properties of the node will be not matched with properties passed as config. vue-konva updates ONLY changed properties.

In strict mode vue-konva will update all properties of the nodes to the values that you provided in config, no matter changed they or not.

You should decide what mode is better in your actual use case.

To enable strict mode pass __useStrictMode attribute:

<v-rect :config="{}" __useStrictMode></v-rect>

Configurable prefix

By default vue-konva is using v- prefix for all components.

You can use your own prefix if default one conflicts with some other libs or your components.

import Vue from 'vue';
import VueKonva from 'vue-konva'

Vue.use(VueKonva, { prefix: 'Konva'});

// in template:
<konva-stage ref="stage" :config="stage">

Custom Konva Nodes

By passing a Record<string, new (...args: any) => Node<any>> object to customNodes in options, you can use your own konva node classes in Vue Konva.

import Vue from 'vue';
import VueKonva from 'vue-konva'

class MyRect extends Konva.Rect {
  constructor() {
    super()
    console.log('MyRect')
  }
}

Vue.use(VueKonva, {
    // The keys are used as component names.
    customNodes: { MyRect }
})

// in template:
<v-my-rect />

Minimal Bundle (Tree-Shaking)

By default, vue-konva imports the full konva package with all shapes included. If you want to minimize your bundle size, you can use the core build that only includes what you explicitly import.

// Use core build instead of full build
import VueKonva from 'vue-konva/core';

// Import only the shapes you need
import 'konva/lib/shapes/Rect';
import 'konva/lib/shapes/Circle';
import 'konva/lib/shapes/Text';

const app = createApp(App);
app.use(VueKonva);
app.mount('#app');

Or if you prefer individual component imports:

import { Stage, Layer, Rect, Circle } from 'vue-konva/core';

// You still need to import the Konva shapes you use
import 'konva/lib/shapes/Rect';
import 'konva/lib/shapes/Circle';

This approach only includes the Konva shapes you actually use, significantly reducing bundle size.

Note: Layer, Group, FastLayer, and Label are always available in the core build since they are part of Konva's core module. You only need to import individual shapes like Rect, Circle, Text, etc.

Change log

The change log can be found on the Releases page.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptcanvascanvas-animationgraphicskonva

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架