Skip to main content

How to Drag and drop canvas shapes with vue-konva | Interchange locations of two shapes by drag and drop

Drag and drop canvas shapes with vue-konva

 

Konva is a JavaScript library that supports animations, tweens, drawing many different shapes and designs, ready-to-use filters, node nesting, grouping, and bubbling on both desktop and mobile devices. It also supports native integration with web frameworks like React and Vue by exporting high-quality image data, image objects, and data URLs. In this blog, we will learn to draw different shapes using Konva and Nuxt.js and will make those shapes draggable and droppable.

Installing VueKonva with npm:

npm install vue-konva konva --save

Import and use VueKonva:

import Vue from 'vue';
import VueKonva from 'vue-konva'
Vue.use(VueKonva)

Create shapes with drag and drop:

In the example below I tried to create a few shapes using Vue-Konva on konva stage. So initially we need to define the main template that will contain all HTML elements including the Konva Stage container.

<template>
<div>
<button
type="button"
class="success"
@click="saveFile"
>
Download File
<i class="icon-arrow rotate-90"></i>
</button>

<button
type="button"
id="saveImage"
class="info"
>
Download Image
<i class="icon-arrow rotate-90"></i>
</button>

<button
type="button"
@click="resetTables"
class="reset"
>
Reset Tables
<i class="icon-arrow rotate-90"></i>
</button>

<div id="stage-parent">
<div id="container">
</div>
</div>
</div>
</template>

 

When the script runs it loads all the predefined shapes like squares, circles, lines, and ellipses. Then it creates a new stage and a layer object and then adds that layer to the stage. Now it draws various shapes to this layer. 


  // load previous data
   this.load()

   //create new stage
   var stage = new Konva.Stage({
        container: 'container',
        width: width,
        height: height,
      });
    
    //create new layer
    var layer = new Konva.Layer();
    stage.add(layer);

    //draw defined elements
    Object.keys(this.elements).forEach(key => {
      layer.add(this.createShape(this.elements[key]));       
    });
    layer.draw();
Finally bind the prepared layer with dragend event assign this stage to the container '#stage-parent' specified in the template. 

Demo:


Interchange locations of two shapes :

Now if you want to interchange the locations of two shapes like when you drag and move the first shape over another shape and drop it there, the second shape will automatically move to the location of the first shape. So for this, you will need to find out the intersection point or we can say the collision point of both shapes. If both the shapes are intersecting each other then replace the coordinates of both shapes at the time of the drag end event.

Demo:



Comments

Popular posts from this blog

Secure Rest APIs with SHA-256 Cryptographic Hash Algorithm

A cryptographic hash is an algorithm that takes required data to be encrypted and produces enciphered text called "hash". There are different algorithms available in which SHA-256 is way better because it produces a different hash every time even if the input is the same. SHA-256 is more secure and fast enough to be used to secure HTTP calls. So as an API owners, we always want to secure our API endpoints from being attacked. In this blog we will see how can a user securely call an API endpoint via adding a hash to the request header and how can an API owner verify that hash at his end. So let's start. # Steps to secure an API request: Generate your public and private key pair:  Obtain your public/private key pair  as an API user. Here we will use a private key to generate the hash value and sign the API request. A private key is shared only with the key creator and never be shared with anyone whereas the public key is sharable. Share this public key with th...

How OAuth 2.0 authorize mini-programs to access resources of native app

OAuth 2.0 is widely used in authentication and authorization that allows third-party applications to access resources that are hosted on other applications. OAuth 2.0 provides access to resources with some limitations to the client applications without sharing the actual credentials of the user. Here I will specify my own experience that how I implemented Oauth2 in our App. We implemented a few mini-programs in our app using a third-party mini-app container. Let's understand more about mini-programs and mini-app containers. Mini Programs: Mini programs are mini-apps or we can say independent child apps with limited features that run inside a parent app or bigger app. Mini programs can perform a variety of functions that can enhance native app features like food ordering, ticket booking, banking, chat, E-commerce, and promo codes and can take your small business app to the next level of growth. It allows 3rd parties to implement new features and services on your native app...