Node
new Konva.Node(config)
Node 构造函数。Node 是可以进行变换、分层并绑定事件的实体。stage、layers、groups 和 shapes 都扩展自 Node。
参数
| 名称 | 类型 | 描述 |
|---|---|---|
| config | Object | |
| x(可选) | Number | |
| y(可选) | Number | |
| width(可选) | Number | |
| height(可选) | Number | |
| visible(可选) | Boolean | |
| listening(可选) | Boolean | 节点是否监听事件 |
| id(可选) | String | 唯一 id |
| name(可选) | String | 非唯一名称 |
| opacity(可选) | Number | 确定节点不透明度。可以是 0 到 1 之间的任意数字 |
| scale(可选) | Object | 设置缩放 |
| scaleX(可选) | Number | 设置 x 轴缩放 |
| scaleY(可选) | Number | 设置 y 轴缩放 |
| rotation(可选) | Number | 以度为单位的旋转角度 |
| offset(可选) | Object | 相对于中心点和旋转点的偏移 |
| offsetX(可选) | Number | 设置 x 轴偏移 |
| offsetY(可选) | Number | 设置 y 轴偏移 |
| draggable(可选) | Boolean | 使节点可拖动。当 stage 可拖动时,可以通过拖动 stage 的任意部分来拖放整个 stage |
| dragDistance(可选) | Number | |
| dragBoundFunc(可选) | function |
自有方法
clearCache()
清除缓存的 canvas
返回值: Konva.Node
示例:
node.clearCache();
cache(config)
缓存节点以提升绘制性能、应用滤镜或创建更准确的命中区域。对于所有基本图形,缓存 canvas 的大小会自动检测。如果需要缓存自定义的 Konva.Shape 实例,则必须传入图形的边界框属性。更多信息请参阅 https://konvajs.org/docs/performance/Shape_Caching.html。
参数:
config(Object)(可选)config.x(Number)(可选)config.y(Number)(可选)config.width(Number)(可选)config.height(Number)(可选)config.offset(Number)(可选):使 canvas 在所有方向上增加offset个像素的大小。config.drawBorder(Boolean)(可选):设置为 true 时,将在缓存区域周围绘制红色边框,以便调试config.pixelRatio(Number)(可选):更改缓存图像的质量(或像素比率)。pixelRatio = 2 将生成大小为 2 倍的缓存。config.imageSmoothingEnabled(Boolean)(可选):控制创建的缓存 canvas 的 imageSmoothingEnabled 属性config.hitCanvasPixelRatio(Number)(可选):更改缓存命中 canvas 的质量(或像素比率)。
返回值: Konva.Node
示例:
// cache a shape with the x,y position of the bounding box at the center and
// the width and height of the bounding box equal to the width and height of
// the shape obtained from shape.width() and shape.height()
image.cache();
// cache a node and define the bounding box position and size
node.cache({
x: -30,
y: -30,
width: 100,
height: 200
});
// cache a node and draw a red border around the bounding box
// for debugging purposes
node.cache({
x: -30,
y: -30,
width: 100,
height: 200,
offset : 10,
drawBorder: true
});
isCached()
确定节点当前是否已缓存
返回值: Boolean
getClientRect(config)
返回节点的客户端矩形 {x, y, width, height}。此矩形还包括所有样式(描边、阴影等)。此方法的用途类似于 DOM 的 getBoundingClientRect API。
参数:
config(Object)config.skipTransform(Boolean)(可选):计算矩形时是否应对节点应用变换?config.skipShadow(Boolean)(可选):计算边界框时是否应对节点应用阴影?config.skipStroke(Boolean)(可选):计算边界框时是否应对节点应用描边?config.relativeTo(Object)(可选):计算相对于某个父节点的客户端矩形
返回值: Object 具有 {x, y, width, height} 属性的矩形
示例:
var rect = new Konva.Rect({
width : 100,
height : 100,
x : 50,
y : 50,
strokeWidth : 4,
stroke : 'black',
offsetX : 50,
scaleY : 2
});
// get client rect without think off transformations (position, rotation, scale, offset, etc)
rect.getClientRect({ skipTransform: true});
// returns {
// x : -2, // two pixels for stroke / 2
// y : -2,
// width : 104, // increased by 4 for stroke
// height : 104
//}
// get client rect with transformation applied
rect.getClientRect();
// returns Object {x: -2, y: 46, width: 104, height: 208}
off(evtStr)
移除节点上的事件绑定。传入以空格分隔的事件类型字符串,可以一次移除多个事件绑定,例如 'mousedown mouseup mousemove'。包含命名空间时,可以按名称移除事件绑定,例如 'click.foobar'。如果只提供类似 '.foobar' 的名称,则会移除该命名空间中的所有事件。
参数:
evtStr(String):例如 'click'、'mousedown touchstart'、'.foobar'
返回值: Konva.Node
示例:
// remove listener
node.off('click');
// remove multiple listeners
node.off('click touchstart');
// remove listener by name
node.off('click.foo');
remove()
从父节点中移除节点,但不销毁节点。之后可以重新使用该节点。
返回值: Konva.Node
示例:
node.remove();
destroy()
移除并销毁节点。终止节点并永久删除!调用 destroy() 后不应重新使用该节点。如果节点是容器(Group、Stage 或 Layer),也会销毁所有子节点。
示例:
node.destroy();
getAttr(attr)
获取属性
参数:
attr(String)
返回值: Integer|String|Object|Array
示例:
var x = node.getAttr('x');
getAncestors()
获取祖先节点
返回值: Array
示例:
shape.getAncestors().forEach(function(node) {
console.log(node.id());
})
getAttrs()
获取属性对象字面量
返回值: Object
setAttrs(config)
使用对象字面量一次设置多个属性
参数:
config(Object):包含键值对的对象
返回值: Konva.Node
示例:
node.setAttrs({
x: 5,
fill: 'red'
});
isListening()
综合考虑祖先节点,确定节点是否监听事件。 父节点 | 自身 | isListening listening | listening | ----------+-----------+------------ T | T | T T | F | F F | T | F F | F | F
返回值: Boolean
isVisible()
综合考虑祖先节点,确定节点是否可见。 父节点 | 自身 | isVisible visible | visible | ----------+-----------+------------ T | T | T T | F | F F | T | F F | F | F
返回值: Boolean
show()
显示节点。设置 visible = true
返回值: Konva.Node