konva | HTML5 Canvas JavaScript framework | Graphics library
kandi X-RAY | konva Summary
kandi X-RAY | konva Summary
Konva is an HTML5 Canvas JavaScript framework that enables high performance animations, transitions, node nesting, layering, filtering, caching, event handling for desktop and mobile applications, and much more. You can draw things onto the stage, add event listeners to them, move them, scale them, and rotate them independently from other shapes to support high performance animations, even if your application uses thousands of shapes. Served hot with a side of awesomeness. This repository began as a GitHub fork of ericdrowell/KineticJS.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Filters a Gaussian blur .
- Returns anchor name
- Creates a smoothed edge mask .
- Generate a mask .
- Creates a background mask based on the background data .
- Creates a mask .
- Generate a number valid numbers array
- Returns a number that is valid number
- Returns the pixel ratio of the device .
- Add child nodes to the array .
konva Key Features
konva Examples and Code Snippets
// this data gives the position and size of the frame
let data = {
frameGroup: { x: 50, y: 100, width: 800, height: 300, strokeWidth: 10, stroke: 'cyan'},
fadeImage: {opacity: 0.3}
}
// add a stage
let stage = new Konva.Stage({contain
import Konva from 'konva';
Konva.showWarnings = false;
import VueKonva from "vue-konva";
import Konva from "konva";
Konva.showWarnings = false;
Vue.use(VueKonva);
Vue.use(Konva);
import React, { Component } from "react";
import Konva from "konva";
import { render } from "react-dom";
import { Stage, Layer, Rect, Text } from "react-konva";
class App extends Component {
state = {
cursor: {
x: null,
import { Stage, Layer, Rect } from "react-konva"
// linear-gradient(140deg, rgba(165, 142, 251, 1), rgb(233, 191, 248))
export default function App() {
const width = window.innerWidth / 1.25 // random width
const height = window.i
import { observer } from "mobx-react";
class KonvaComponent extends React.Component {
// ..
}
export const Konva = observer(KonvaComponent)
import { observer } from "mobx-react";
@observer
export class Konva e
export class Konva extends React.Component {
// ...
static contextType = FrameItContext;
context!: React.ContextType;
render() {
const { win } = this.context;
// ...
}
}
// This is the important call ! Cross is the rotation point as illustrated by crosshairs.
rotateAroundPoint(shape, rotateBy, {x: cross.x(), y: cross.y()});
// The label is a special case because we need to keep the text unrotated.
function getRelativePointerPosition(node) {
// the function will return pointer position relative to the passed node
var transform = node.getAbsoluteTransform().copy();
// to detect relative position we need to invert transform
tra
// Create the stage and a layer to draw on.
var stage = new Konva.Stage({
container: 'canvas-container',
width: 650,
height: 300
});
var layer = new Konva.Layer();
stage.add(layer);
stage.draw();
// listen for the file i
Community Discussions
Trending Discussions on konva
QUESTION
I have created a responsive stage using react-konva, but I cannot find a way to set a custom size to export the image. usually, right-click and saving the image is working as the size in the window at that time has. I wanted to add a button to download the image, but I couldn't set up a way to do that because I'm new to react hooks, I couldn't find a way to set up the data URL. please kindly refer to this
...ANSWER
Answered 2022-Mar-31 at 12:02You need to use a ref
to the Stage
and get the image URL and download it.
- In your Canvas component create a
ref
usingcreateRef
.
QUESTION
I am trying to create a stage using konva.js. Basically, I need to add two (at least) or more images on the stage based on some layouts.
Example:
I have a layout that splits the stage area verically into two similar groups. Each group has one image.
Example code:
...ANSWER
Answered 2022-Mar-25 at 10:32So the way to achieve this is to have a rect defining the frame in which the image will be constrained. Put this into a group and set the clip region of the group to match the position and size of the frame rect. Now add the image to the group. Only the part of the image in the group will be visible.
As a bonus, if you add a dragBoundFunc to the group you can ensure that an oversized image cannot be dragged beyond frame edges.
See snippet below (best run full-screen) and editable CodePen here.
This is, of course, only a single image frame where you have described that you will have say two in your use case. I suggest that you unravel the code then make a class, then you can use as many as required, etc.
QUESTION
I am trying to build a simple app in which the user drags a word onto the corresponding image. When the user "drops" the word on the image a collision needs to be detected. I have a console.log message which should only log when this happens. Currently my message is logging even when there is no collision. Any ideas where I'm going wrong? Here is my code:
...ANSWER
Answered 2022-Jan-24 at 17:56First issue: The second parameter you pass to haveIntersection is imgLayer, which is a Konva.Layer obect. Since a layer is the same size as the stage I assume this is always going to return true.
Second issue: Your haveIntersection() function expects object parameters with x, y, width & height. Passing in Konva objects here/like this is bad practice because they do not have those attributes. This is the case with Konva.Text which has simple neither width not height attrs.
For x you would refer to shape.x(), width would be shape.width(), etc. You can sometimes get away with passing shape.getAttrs() which provides a plain JS object containing such attrs, but even that is likely to fail as some shapes such as Konva.Image do not have the width or height attrs in the result of getAttrs(). It turns out that Konva.Rect and Konva.Image do have them, meanwhile Konva.text() does not, so you should not rely on this.
Instead, you should be explicit and pass in a defined object (see snippet code). It pays to be clear about what you intend - libs can add or remove defaults and behaviours over time and if it 'had' worked your code would still have been a possible time bomb.
As a bonus: A useful technique for case where you have multiple objects to check for collision is to use the stage,find() function. Give the drop-target objects a common name attr and you can search for such objects via stage.find('.name_to_find'). In the snippet I assign the name 'dropimage' to the rects (in replacement for image object so as to avoid image fetching issues here). Then you can iterate the found objects and run the collision detection, etc.
Lastly, you are using two layers in the code. Each layer has a processing overhead and whilst using multiple layers is a legitimate technique, you should consider whether you need two layers in this simple case. A single layer would do fine.
QUESTION
I have a very simple Konva-react app (I've taken it right out of the docs) and it doesn't work (doesn't display the image) displaying two warnings:
- Warning:
is using incorrect casing. Use PascalCase for React components, or lowercase for HTML elements.
- Warning: The tag
is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter. My Canvas component:
ANSWER
Answered 2022-Jan-22 at 21:03You can't simply render Konva components anywhere. You need to create the and a
component first, and only then add your component:
QUESTION
Using d3 for some shape calculations, then drawing with Konva.js. Creating a new shape from a svg path string. The shape is drawn, but fillStrokeShape
doesn't seem to apply any styles.
The documentions says:
We can use the renderer to access the HTML5 Canvas context, and to use special methods like context.fillStrokeShape(shape) which automatically handles filling, stroking, and applying shadows.
I am guessing this is the case because the path gets drawn using stroke()
or fill()
.
ANSWER
Answered 2021-Nov-15 at 16:25See docs of konvajs:
https://konvajs.org/docs/shapes/Path.html https://konvajs.org/docs/shapes/Custom.html
I think Path2D is not needed.
Insert your res in Konva.Path.data
QUESTION
I am using Konvajs/Vue-Konva
within my Vuejs/Nuxt
application. Using Konva
I am creating the Rect
shape dynamically at run time. I would like to know if there is a way to add a Text/Label
within each of the shapes. I want to add a name to each of Shape
so as to identify each of the Shape
separately.
I have added my code sample here in CodeSandBox.
Can someone please let me know how can I add the Text/Label
to each of the Rect/Shape
that has been created using Vue-Konva
ANSWER
Answered 2021-Nov-08 at 17:05You can use Konva.Group to organize several shapes into structures.
QUESTION
I am trying to implement the Vue-konva
into my application by following the documentation here. But I am running into the following error:
ANSWER
Answered 2021-Nov-04 at 07:20This one should work fine
QUESTION
We are a team of 5 developers working on a video rendering implementation. This implementation consists out of two parts.
- A live video preview in the browser using angular + konva.
- A node.js (node 14) serverless (AWS lambda container) implementation using konva-node that pipes frames to ffmpeg for rendering a mp4 video in higher quality for later download.
Both ways are working for us. Now we extracted the parts of the animation that are the same for frontend and backend implementation to an internal library. We imported them in BE and FE. That also works nicely for most parts.
We noticed here that konva-node is deprecated since a short time. Documentation says to use canvas
+ konva
instead on node.js. But this just doesn't work. If we don't use konva-node we cannot create a stage without a 'container'
value. Also we cannot create a raw image buffer anymore, because stage.toCanvas()
actually returns a HTMLCanvas, which does not have this functionality.
- So what does konva-node actually do to konva API?
- Is node.js still supported after deprecation of konva-node?
- How can we get
toBuffer()
andnew Stage()
functionality without konva-node in node.js?
ANSWER
Answered 2021-Sep-27 at 21:36So what does konva-node actually do to konva API?
It slightly patches Konva code to use canvas
nodejs library to use 2d canvas API. So, Konva will not use browser DOM API.
Is node.js still supported after deprecation of konva-node?
Yes. https://github.com/konvajs/konva#4-nodejs-env
How can we get toBuffer() and new Stage() functionality without konva-node in node.js?
You can try to use this:
QUESTION
I'm using Konva/Canvas to draw multiple semi-transparent rectangles z-ordered. At the moment, this looks like the right side of the following image.
Now, I want to achieve something which is called "Frosted Glass Effect" / "Background Blur" which you can see on the left. The idea is that overlapping parts of the rectangles below a semi transparent rectangle will be blurred.
How to do this in Konva or with plain Canvas?
...ANSWER
Answered 2021-Sep-20 at 18:01It may be very hard to get this effect using 2D canvas API.
If you have static shapes, you can try to "cut and copy" a part that you want to blur and apply the effect just on it.
Another possible solution is to use backdrop-filter
from CSS and draw rectangle div over canvas and apply this filter on it, it will blur part of canvas content.
On the demo, you can try to drag red square to see how "cut and paste" is working.
And blue square is working in combination of div with CSS filters.
QUESTION
I'm created a packed circle visualisation using d3 and drawing it using Konva. If you click on a circle within the packed circle visualisation, the viewport should zoom to that circle.
Each circle has an event handler attached, which gets called on a click event. There I am calculating the value by which to scale the whole visualisation by, and the values by which to reposition the visualisation by.
I can't quite seem to get the repositioning values correct. The circle on which the click event occurs should be centered in the viewport after scaling it.
...ANSWER
Answered 2021-Sep-17 at 14:14The task is one of moving the stage position (x, y) so that the center of the target circle is in the middle of the viewport - meaning the HTML canvas element visible bounds.
PosX = (viewPort.width / 2) - (circle.pos.x * scale) PosY = (viewPort.height/ 2) - (circle.pos.y * scale)
And using your code that means:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install konva
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page