当前位置:网站首页>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 .
边栏推荐
- 你们采集oracle数据,数据延迟大约有多少啊?我这边就维持在3秒了,降不下去了。有没有个业内参考啊
- Common DOS commands
- [algorithm] I brushed two big factory interview questions and learned array again with tears in my eyes“
- moco挡板制作及运行成功
- Ding! Techo day Tencent technology open day arrived as scheduled!
- 如何高效优雅地管理接口文档
- 原生实现.NET 5.0+ 自定义日志
- Detailed explanation of select in golang (forward)
- [cloud resident co creation] shengteng heterogeneous computing architecture cann helps release the surging computing power of hardware
- 华为云AOM发布2.0版本,3大特性亮相
猜你喜欢

IDM certification process log embedding point description

使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据

杂记:数据库go,begin,end,for,after,instead of

2022 recurrent training question bank and online simulation examination for main principals of hazardous chemicals business units

Small program graduation project based on wechat recruitment small program graduation project opening report function reference

Applet graduation design based on wechat gym private education appointment applet graduation design opening report function reference

数据库对比工具

EasyExcel 学习笔记

2022 practice questions and mock examination of Shandong Province safety officer C certificate examination

PCB线路板布局和布线都有哪些设计要求?
随机推荐
2022A特种设备相关管理(电梯)特种作业证考试题库及在线模拟考试
From Mogao Grottoes to the Pacific Ocean, massive data have found new homes
How to create a CSR (certificate signing request) file?
Unity about oculus quest2 basic development based on XR interaction toolkit 003- capture function - making a VR bowling game
[dark horse morning post] Tencent responded that a large number of users' QQ numbers were stolen; Weiya's husband company was fined 190000 yuan; China Evergrande is applied for liquidation; Guanxiaoto
如何设计业务高性能高可用计算架构 - 作业
DNSLog注入
启牛学堂的vip证券账户是真的安全正规吗?怎么说
EasyExcel 学习笔记
Google推出Advanced API Security 保护API免受安全威胁
Ten MySQL locks, one article will give you full analysis
Database Experiment 7 integrity constraints
Go 降序排序 取 Top N
Small program graduation project based on wechat recruitment small program graduation project opening report function reference
February 15, 2022 learning summary
Sharing-JDBC分布式事务之Seata实现
halcon知识:矩阵专题【01】
请问大智慧上开户安全吗
Small program graduation project based on wechat mobile mall small program graduation project opening report function reference
2022年6月27日-2022年7月3日(ue4视频教程)