랭킹으로 돌아가기

konvajs/vue-konva

TypeScriptkonvajs.github.io/docs/vue/

Vue & Canvas - JavaScript library for drawing complex canvas graphics using Vue.

vuekonvacanvascanvas-animationkonvajsshapevue-styleguidistgraphics
스타 성장
스타
1.3k
포크
140
주간 성장
이슈
0
5001k
2017년 11월2020년 9월2023년 8월2026년 7월
아티팩트npmnpm install vue-konva
README

Vue Konva

Version License

ReactKonva Logo

Vue Konva is a JavaScript library for drawing complex canvas graphics using Vue.

It provides declarative and reactive bindings to the Konva Framework.

All vue-konva components correspond to Konva components of the same name with the prefix 'v-'. All the parameters available for Konva objects can add as config in the prop for corresponding vue-konva components.

Core shapes are: v-rect, v-circle, v-ellipse, v-line, v-image, v-text, v-text-path, v-star, v-label, v-path, v-regular-polygon. Also you can create custom shape.

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

Documentation / live edit

See Tutorials page

Quick Start

Vue.js version 2.4+ is required.

1 Install via npm

vue@3:

npm install vue-konva konva --save

vue@2:

npm install vue-konva@2 konva --save

2 Import and use VueKonva

vue@3:

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

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

vue@2:

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

Vue.use(VueKonva);

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

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
  </head>
  <body>
    <div id="app">
      <v-stage ref="stage" :config="configKonva">
        <v-layer ref="layer">
          <v-circle :config="configCircle"></v-circle>
        </v-layer>
      </v-stage>
    </div>
    <!--1. Link Vue Javascript & Konva-->
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/konva/konva.js"></script>
    <!--2. Link VueKonva Javascript -->
    <script src="https://unpkg.com/vue-konva/umd/vue-konva.min.js"></script>
    <script>
      // 3. Create the Vue instance
      new Vue({
        el: '#app',
        data: {
          configKonva: {
            width: 200,
            height: 200,
          },
          configCircle: {
            x: 100,
            y: 100,
            radius: 70,
            fill: 'red',
            stroke: 'black',
            strokeWidth: 4,
          },
        },
      });
    </script>
  </body>
</html>

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.

관련 저장소
vuejs/vue

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core

TypeScriptnpmMIT Licensevuejavascript
v2.vuejs.org
210.1k33.8k
justjavac/free-programming-books-zh_CN

:books: 免费的计算机编程类中文书籍,欢迎投稿

GNU General Public License v3.0pythonjavascript
weibo.com/justjavac
117.7k28.2k
storybookjs/storybook

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation

TypeScriptnpmMIT Licensestorybookstyleguide
storybook.js.org
90.6k10.2k
PanJiaChen/vue-element-admin

:tada: A magical vue admin https://panjiachen.github.io/vue-element-admin

VueMIT Licensevueadmin
90.2k30.4k
hoppscotch/hoppscotch

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

TypeScriptnpmMIT Licenseapiapi-client
hoppscotch.io
79.8k6k
nuxt/nuxt

the full-stack Vue framework

TypeScriptnpmMIT Licensecsrfull-stack
nuxt.com
60.7k5.7k
marktext/marktext

📝A simple and elegant markdown editor, available for Linux, macOS and Windows.

TypeScriptnpmMIT Licensemacostypewriter-mode
marktext.me
59k4.4k
go-gitea/gitea

Git with a cup of tea! Painless self-hosted all-in-one software development service, including Git hosting, code review, team collaboration, package registry and CI/CD

GoGo ModulesMIT Licensegiteagolang
gitea.com
57k6.9k
ElemeFE/element

A Vue.js 2.0 UI Toolkit for Web

VueMIT Licensevuecomponents
element.eleme.io
54.1k14.4k
ionic-team/ionic-framework

A powerful cross-platform UI toolkit for building native-quality iOS, Android, and Progressive Web Apps with HTML, CSS, and JavaScript.

TypeScriptnpmMIT Licenseionicmobile
ionicframework.com
52.6k13.3k
lyswhut/lx-music-desktop

一个基于 Electron 的音乐软件

TypeScriptnpmApache License 2.0music-playerjavascript
lyswhut.github.io/lx-music-doc/
52.3k6.9k
prettier/prettier

Prettier is an opinionated code formatter.

JavaScriptnpmMIT Licenseformatterprinter
prettier.io
52.1k4.9k