当前位置:网站首页>konva系列教程4:图形属性
konva系列教程4:图形属性
2022-06-29 14:36:00 【前端西瓜哥】
大家好,我是前端西瓜哥,我们今天来看看图形的一些常见属性。
几何属性
{
x: 10,
y: 10,
width: 50,
height: 50
}
x 和 y 代表的是图形的 位置,对于矩形来说,是左上角。对于圆形、圆环这些来说,则是在圆心处。
这个位置也是图形进行变形的参考点,或者说是旋转时的中心。
offsetX 和 offsetY 可以改变图形的位置。offset 其实是将画布坐标系的原点移动一定距离,再绘制图形。
比如对矩形,我们将 offsetX 和 offsetY 设置为 width/2 和 height/2,我们就能让矩形的位置跑到矩形的中点。
width 和 height 为图形的宽高。但对于圆形类图形,则是通过半径 radius 来设置大小。
填充(fill)
用法:fill: #f04
修改图形颜色:
rect.fill('green');
fill 方法不仅可以设置颜色,还可以获取颜色,只要不传入参数即可:
const fill = rect.fill();
当然我们还可以使用高级的填充图片、渐变色的功能,需要用到 fillPatternImage 、fillLinearGradientStartPoint 、fillLinearGradientEndPoint 、fillLinearGradientColorStops 等方法,内容有点多,这里就不展开讲了。
描边(stroke)
描边相关的样式很多,这里只介绍最常用的 stroke 和 strokeWidth。
用法:stroke: '#000'strokeWidth: 4
修改图形的描边颜色、线宽:
rect.stroke('blue').strokeWidth(8);
konva 支持链式写法。
链式写法,其实就是一个对象,执行了方法后,又返回了这个对象。
这样的话我们就可以将多个方法的调用链接起来,提高代码可读性,常见于修改对象属性值。
获取描边颜色、线宽
const stroke = rect.stroke();
const strokeWidth = rect.strokeWidth();
不透明度(opacity)
用法:opcity: 0.5
设置透明度:
rect.opcity(0.3)
获取不透明度的值:
const opcity = rect.opcity();
显示/隐藏(visible)
用法:visible: true
显示图形:
rect.show();
// 或者
rect.visible(true);
隐藏图形:
rect.hide();
// 或
rect.visible(false);
本质其实是修改图形的 visible 属性,然后重新渲染。show 和 hide 只是语义化的封装。
鼠标样式
我们不能像操作 DOM 元素一样,直接给图形元素设置 cursor 属性。
但我们可以在光标移动到图形上的时候,动态改变 canvas 挂载的 DOM 元素的 cursor 属性。
rect.addEventListener('mouseenter', () => {
stage.container().style.cursor = 'move'
})
rect.addEventListener('mouseleave', () => {
stage.container().style.cursor = ''
})
结尾
不同形状有各自的属性,具体可以看 konva 的文档:
https://konvajs.org/api/Konva.html
边栏推荐
- Class template case - array class encapsulation
- Chapter 14 of canvas physical animation
- I log in to the RDB database and prompt that the master account authorization is required. How do I know who to call?
- 【Try to Hack】vulnhub DC2
- Alibaba cloud experience Award: use polardb-x and Flink to build a large real-time data screen
- curl: (56) Recv failure: Connection reset by peer
- 高並發軟件(網站,服務器端接口)的評價指標
- 卫龙更新招股书:年营收48亿 创始人刘卫平家族色彩浓厚
- wieshark抓包mysql协议简单分析
- Are you still reading the log by command? Use kibana, one picture is better than ten thousand lines of log
猜你喜欢

EMC-浪涌防护及退耦设计

Netease strict selection offline data warehouse quality construction practice

Digital IC code -- traffic lights

FIFO implementation with single port RAM

三角函数对应在平面坐标上画圆

《canvas》之第12章 其他应用

Uniapp problem list and experience

Alibaba cloud experience Award: use polardb-x and Flink to build a large real-time data screen

阿里云体验有奖:使用PolarDB-X与Flink搭建实时数据大屏

Trigonometric function corresponding to drawing circle on plane coordinate
随机推荐
delphi7中 url的编码
Chapter 9 of canvas: gradients and shadows
《canvas》之第14章 物理动画
Can futures accounts be opened offline? Is it safe to open an account online?
类模板案例-【数组类封装】
《canvas》之第9章 渐变与阴影
Introduction to bloom filter
Laravel - Composer 安装指定 Laravel 版本
建立自己的网站(19)
I want to search the hundreds of nodes in the data warehouse. Can I check a table used in the SQL
.NET程序配置文件操作(ini,cfg,config)
Explanation on deployment and establishment of decentraland process
驱动器实际运用案例
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
【Try to Hack】vulnhub DC2
Build your own website (19)
云上第一课 | 建个小破站有多简单?云计算老司机带你一小时搞定
go学习(四、面向接口)
idea输出台输出中文乱码问题
《canvas》之第8章 像素操作