当前位置:网站首页>Konva series tutorial 3: Customizing drawings
Konva series tutorial 3: Customizing drawings
2022-06-28 18:26:00 【Front end watermelon brother】
Hello everyone , I'm brother watermelon .
Let's see how it works kanva Draw custom graphics .
Now we're going to draw a diamond . So called diamond , Is a quadrilateral with equal sides .
We need to bring in (x, y) Set the upper left corner of the diamond , And use width and height Set the width and height of the diamond .

Konva.Shape class
We need to new One Konva.Shape object , And write senceFunc function , To describe graphics .
const diamond = new Konva.Shape({
sceneFunc(ctx, shape) {
// Draw your own custom shapes
},
width: 60,
height: 100
});
senceFunc Function receive konva Incoming canva Context ctx, And a shape object .
ctx It's right canvas Of 2d context The enhancement of , In addition to the original properties and methods , It also provides konva Some special methods . We need to use this ctx To draw something .
shape Is a shape instance , You can get some attribute values through it ( For example, width and height values ), It is used to calculate the coordinate position of each point of the graph we want to draw .
Drawing of diamond
function sceneFunc(ctx, shape) {
const w = shape.getAttr('width');
const h = shape.getAttr('height');
ctx.beginPath();
ctx.moveTo(w / 2, 0);
ctx.lineTo(w, h / 2);
ctx.lineTo(w / 2, h);
ctx.lineTo(0, h / 2);
ctx.closePath();
ctx.fillStrokeShape(shape);
}
First, we calculate the end points of the diamond , use canvas Native ctx.moveTo and ctx.lineTo Connect in turn , Then call konva Enhanced ctx.fillStrokeShape(shape) Method , Fill the path first , Stroke again .
It should be noted that ,x and y Can't participate in the calculation , because konva Will do additional position offset operations . If your calculations here take into account x and y, Will cause the actual position to become twice as expected . Again ,scale The scaling value should not be involved in the calculation .
The full way to write it is :
const diamond = new Konva.Shape({
x: 200,
y: 20,
width: 60,
height: 100,
fill: '#0288d1',
sceneFunc(ctx, shape) {
const w = shape.getAttr('width');
const h = shape.getAttr('height');
ctx.beginPath();
ctx.moveTo(w / 2, 0);
ctx.lineTo(w, h / 2);
ctx.lineTo(w / 2, h);
ctx.lineTo(0, h / 2);
ctx.closePath();
ctx.fillStrokeShape(shape);
}
});
layer.add(diamond);
We can simply encapsulate , Write a createDiamond Method , Convenient for multiple calls .
const createDiamond = (props) => {
return new Konva.Shape({
...props,
sceneFunc(ctx, shape) {
const w = shape.getAttr('width');
const h = shape.getAttr('height');
ctx.beginPath();
ctx.moveTo(w / 2, 0);
ctx.lineTo(w, h / 2);
ctx.lineTo(w / 2, h);
ctx.lineTo(0, h / 2);
ctx.closePath();
ctx.fillStrokeShape(shape);
}
});
};
// usage
const diamond2 = createDiamond({
x: 30,
y: 30,
width: 120,
height: 100,
fill: '#fff'
});
layer.add(diamond2);
sceneFunc It may be called more than once in a second , It is better not to do some operations that will degrade performance , For example, create a large image object .
Besides ,scenceFunc You should only do operations related to drawing , It is not recommended to do other operations in it , For example, binding events .
I'm brother watermelon , Welcome to follow me , Learn front-end knowledge together .
边栏推荐
- 架构自治服务:构建数据驱动的架构洞察
- Mycat+ sub database and sub table
- Can data sources only be connected to Alibaba cloud cloud databases? Can't you connect the databases installed in Alibaba cloud servers?
- Database Experiment 7 integrity constraints
- Small program graduation project based on wechat recruitment small program graduation project opening report function reference
- Spark Tuning (submit job resource parameter tuning)
- Small program graduation project based on wechat chess and card room small program graduation project opening report function reference
- CORBA 架构体系指南(通用对象请求代理体系架构)
- HTNL introduction
- 请教大佬们,oracle cdc的NUMBER类型,打印出来为什么变成字符串了呢,怎么转换回去?
猜你喜欢

nuc980心跳灯
![[algorithm] I brushed two big factory interview questions and learned array again with tears in my eyes“](/img/ab/69c79690bf42c58c39b2e85fb3690c.png)
[algorithm] I brushed two big factory interview questions and learned array again with tears in my eyes“

Small program graduation design based on wechat driving school examination small program graduation design opening report function reference

324. 摆动排序 II

How to design a business high performance and high availability computing architecture - job

Applet graduation project is based on wechat property maintenance application property applet graduation project opening report function reference

Small program graduation project based on wechat chess and card room small program graduation project opening report function reference

Small program graduation project based on wechat agricultural and sideline products agricultural products mall small program graduation project opening report function reference

第四个专栏,Kubernetes云原生实战,它来了~

Kubernetes visual interface dashboard
随机推荐
Le test de reconnaissance faciale comporte - t - il des préoccupations en matière de protection de la vie privée? Une entreprise étrangère a été arrêtée en cas d'urgence
GCC getting started manual
使用Pega进行一个简单的RPA程序开发
Go all out to unreal
Three communication skills in software testing
Does the dataworks SQL script support if else judgment of statement blocks
Database Experiment 7 integrity constraints
June 27, 2022 to July 3, 2022 (UE4 video tutorial)
About the solution of "modulenotfounderror: no module named 'flask.\u compat'"
国内怎么就做不出 IDEA 那样的产品?
2022年7月计划(全力unreal)
Does face recognition test involve privacy and security issues? A foreign company was urgently stopped
From getting started to mastering the application of | yalmip+cplex in power system (excellent, I will lose if I can't understand it, and I will lose if I don't gain)
2022 review questions and answers for safety production management personnel of hazardous chemical production units
From Mogao Grottoes to the Pacific Ocean, massive data have found new homes
JQ plug-in analysis
记一次Emotet木马处理案例
图形系统——1. 布局加载
【译】clickhouse 22.4和22.5核心特性一览
Unity about oculus quest2 basic development based on XR interaction toolkit 003- capture function - making a VR bowling game