当前位置:网站首页>konva系列教程3:自定义图形
konva系列教程3:自定义图形
2022-06-28 17:45:00 【前端西瓜哥】
大家好,我是前端西瓜哥。
我们来看下怎么用 kanva 绘制自定义图形。
现在我们要绘制一个菱形。所谓菱形,就是所有边相等的四边形。
我们需要传入 (x, y) 设置菱形的左上角,并用 width 和 height 设置菱形的宽高。

Konva.Shape 类
我们需要 new 一个 Konva.Shape 对象,并编写 senceFunc 函数,来描述图形。
const diamond = new Konva.Shape({
sceneFunc(ctx, shape) {
// 绘制你自己自定义的形状
},
width: 60,
height: 100
});
senceFunc 函数接收 konva 传入的 canva 上下文 ctx,和一个 shape 对象。
ctx 是对 canvas 的 2d context 的增强,除了原本的属性和方法,还提供了 konva 专用的一些方法。我们需要用这个 ctx 来绘制一些东西。
shape 则是一个形状实例,可以通过它获取一些属性值(比如宽高值),用来计算我们要绘制的图形的各点的坐标位置。
菱形的绘制
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);
}
首先我们计算出菱形的各个端点位置,用 canvas 原生的 ctx.moveTo 和 ctx.lineTo 依次相连,然后再调用 konva 增强的 ctx.fillStrokeShape(shape) 方法,对路径先进行填充,再描边。
需要注意的是,x 和 y 不能参与计算,因为 konva 会做额外的位置偏移操作。如果你在这里的计算考虑了 x 和 y,就会导致实际的位置变成了预期的两倍。同样,scale 缩放值也不要参与计算。
完整的写法为:
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);
我们可以简单封装一下,写一个 createDiamond 方法,方便多次调用。
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);
}
});
};
// 用法
const diamond2 = createDiamond({
x: 30,
y: 30,
width: 120,
height: 100,
fill: '#fff'
});
layer.add(diamond2);
sceneFunc 可能会在一秒内调用多次,最好不要做一些损耗性能的操作,比如创建一个很大的 image 对象。
此外,scenceFunc 应该只做和绘制相关的操作,不建议在里面做一些其他的操作,比如绑定事件。
我是前端西瓜哥,欢迎关注我,一起学习前端知识。
边栏推荐
- 2022年山东省安全员C证考试练习题及模拟考试
- 58 Technology Salon issue 31 flutter dynamic special Salon
- Introduction to kubernetes resource object and common commands
- EasyExcel 学习笔记
- Home based efficient remote office | community essay solicitation
- Learning notes: how to time 10ms for 51 single chip microcomputer (STC89C52)
- 如何从RHEL 8升级到RHEL 9
- Small program graduation project based on wechat campus lost and found graduation project opening report function reference
- 324. 摆动排序 II
- Use PEGA to develop a simple RPA program
猜你喜欢

强化学习在黄页商家智能聊天助手中的探索实践

Redis principle - hash
![[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](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[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

Kubeadm create kubernetes cluster

DNSLog注入

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

Redis6笔记04 主从复制,集群,应用问题,Redis6新功能

单片机修改网络硬件驱动(phy芯片更换)

CSDN Blogger

Common DOS commands
随机推荐
软件测试的三个沟通技巧
9个优秀的占位图服务
Does face recognition test involve privacy and security issues? A foreign company was urgently stopped
Visio use
架构自治服务:构建数据驱动的架构洞察
Redis6笔记04 主从复制,集群,应用问题,Redis6新功能
Small program graduation project based on wechat subscription water supply mall small program graduation project opening report function reference
It's said that software testing for career change can only be self-taught. Is the training institution a pit?
Finally quit, full-time UE
使用Pega进行一个简单的RPA程序开发
强化学习在黄页商家智能聊天助手中的探索实践
How to open a futures account? Where is it safer to open an account?
数据库MySQL语句期末复习 CTGU
Go 降序排序 取 Top N
Win10 compiles curl library source code with cmake3.22 and vs2019 and calls
kubeadm创建kubernetes集群
HackTheBox-baby CachedView
How to upgrade from RHEL 8 to RHEL 9
工业数字化与新一代数字化系统设计平台----讲座
Would you like to ask for advice on how to open a stock account? Is it safe to open an account online?