AILabel使用文档

321 阅读22分钟

概述

在前端开发过程中难免遇到针对图片的缩放平移;以及在图片上进行矢量数据、文本、标注的展示; 如果你有上面的任何需求,恭喜你,找到组织了....在此背景下,AILabel.js诞生了


特性

  1. 多类型要素展示:图片/矢量数据/文本/标注
  2. 高效绘图:canvas矢量数据绘制
  3. 使用方便简单

安装和使用

npm i ailabel
import AILabel from "ailabel";

名词解释

  1. zoom:容器宽代表的实际距离宽
  2. 实际坐标系:要素或实体代表的实际坐标值所在的坐标系
  3. 屏幕标注系:用来展示的坐标系
  4. ...

AILabel.Map

实例化

// js: 伪代码
const gMap = new AILabel.Map(domId: string, mapOptions?: IMapOptions);
// js: demo【gMap将作为后续使用的容器实例,不再进行重复实例操作】
const gMap = new AILabel.Map('map', {
   center: {x: 0, y: 0},
   zoom: 800,
   mode: 'PAN' // 绘制线段
});

Params

参数说明是否必填默认类型
domIdDom容器id--string
mapOptions可选配置项--IMapOptions

IMapOptions

参数说明是否必填默认类型
zoom初始缩放级别1000number
center初始中心点坐标--IPoint
size容器大小设置null(会获取dom:clientWidth[Height])ISize
mode当前操作模式'PAN'EMapMode枚举-见下文
refreshDelayWhenZooming持续缩放是否延时刷新features(如滑轮缩放时),性能优化trueboolean
zoomWhenDrawing绘制时可滑轮缩放falseboolean
panWhenDrawing绘制时可到边界外自动平移falseboolean
featureCaptureWhenMove绘制过程中是否开启‘双击选中’tip提示,耗费性能(会持续进行move捕捉判断)falseboolean
withHotKeys快捷键开关trueboolean
zoomWheelRatio鼠标滑轮缩放大小,取值区间[0, 10),zoomWheelRatio越小,代表缩放速度越快,反之越慢5number

IPoint

参数说明是否必填默认类型
xx坐标--number
yy坐标--number

ISize

参数说明是否必填默认类型
width--number
height--number

setMode

AILabel.Map实例设置当前模式

gMap.setMode(mode: EMapMode);

Params

参数说明是否必填默认类型
mode操作模式--EMapMode枚举

EMapMode

|参数|说明|类型| |---|---|---|---|---| |PAN|平移|string| |BAN|禁用视野缩放平移|string| |POINT|绘制点|string| |CIRCLE|绘制圆|string| |LINE|绘制线|string| |POLYLINE|绘制多段线|string| |RECT|绘制矩形|string| |POLYGON|绘制多边形|string| |DRAWMASK|绘制涂抹|string| |CLEARMASK|擦除涂抹|string| |IMAGEMASK|绘制涂抹(Image形式)|string|

setZoomWheelRatio

AILabel.Map实例设置滑轮缩放比例, 取值区间[0, 10)

gMap.setZoomWheelRatio(0);

setDrawingStyle

AILabel.Map设置绘制过程中的样式

gMap.setDrawingStyle(drawingStyle: IFeatureStyle);

IFeatureStyle

canvas样式:比如lineWidth/strokeStyle/fillStyle等

参数说明是否必填默认类型
lineWidth线宽1number
strokeStyle边框颜色'#FF0000'boolean
fillStyle填充色'#FF0000'boolean
自定义:arrow是否绘制箭头(只针对线段)falseboolean
自定义:stroke是否闭合trueboolean
自定义:fill是否填充falseboolean
其他配置------

setEditingColor

AILabel.Map设置编辑时拖拽对象的绘制颜色

gMap.setEditingColor('#000');

enableDrawingTip

AILabel.Map设置绘制过程中提示文字开启【默认开启】

gMap.enableDrawingTip();

disableDrawingTip

AILabel.Map设置绘制过程中提示文字关闭

gMap.disableDrawingTip();

enableDrawingCrosshair

AILabel.Map设置绘制过程中十字丝开启

gMap.enableDrawingCrosshair();

disableDrawingCrosshair

AILabel.Map设置绘制过程中十字丝关闭

gMap.disableDrawingCrosshair();

enableHotKeys

AILabel.Map设置快捷键开启

gMap.enableHotKeys();

disableHotKeys

AILabel.Map设置快捷键关闭

gMap.disableHotKeys();

enableZoomWhenDrawing

开启绘制时可鼠标滑轮缩放

// define
enableZoomWhenDrawing()
// demo
gMap.enableZoomWhenDrawing();

disableZoomWhenDrawing

禁用绘制时可鼠标滑轮缩放

// define
disableZoomWhenDrawing()
// demo
gMap.disableZoomWhenDrawing();

enablePanWhenDrawing

开启绘制时鼠标达到边界外自动平移

// define
enablePanWhenDrawing()
// demo
gMap.enablePanWhenDrawing();

disablePanWhenDrawing

禁用绘制时鼠标达到边界外自动平移

// define
disablePanWhenDrawing()
// demo
gMap.disablePanWhenDrawing();

getSize

获取传入容器的大小

// define
getSize(): ISize
// demo
const containerSize = gMap.getSize();

ISize

|参数|说明|类型| |---|---|---|---|---| |width|宽|number| |height|高|number|

getScale

获取当前缩放比例 (containerWidth / zoom)

// define
getScale(zoom?: number): number
// demo
const scale = gMap.getScale();

params

参数说明是否必填默认类型
zoom缩放值map.zoomnumber

setCenter

设置中心点(即容器的中心点对应的实际坐标的中心点)

// define
setCenter(center: IPoint): Map
// demo
gMap.setCenter(center);

params

参数说明是否必填默认类型
center中心点--IPoint

getCenter

获取中心点(即容器的中心点对应的实际坐标的中心点)

// define
getCenter(): IPoint
// demo
const center = gMap.getCenter();

getScreenCenter

获取屏幕中心点坐标(即containerWidth/2, containerHeight/2)

// define
getScreenCenter(): IPoint
// demo
const center = gMap.getScreenCenter();

params

参数说明是否必填默认类型
zoom缩放值map.zoomnumber

getBounds

获取视野范围

// define【option为预留参数字段】
getBounds(option?: IObject = {}): IRectShape
// demo
const bounds = gMap.getBounds();

IRectShape

参数说明类型
x左上角坐标xnumber
y左上角坐标ynumber
width宽度number
height高度number

centerAndZoom

定位并缩放到指定位置

// define
centerAndZoom(option: ICenterAndZoom): Map
// demo
const bounds = gMap.centerAndZoom({center: {x,y}, zoom: 1000});

ICenterAndZoom

参数说明是否必填默认类型
center定位到的位置map.centerIPoint
zoom缩放值map.zoomnumber

zoomIn

放大

// define
zoomIn()
// demo
gMap.zoomIn();

zoomOut

缩小

zoomOut()
// demo
gMap.zoomOut();

addLayer

添加Layer至当前Map实例

// define
addLayer(layer: Layer)
// demo
const featureLayer = new AILabel.Layer.Feature(...);
gMap.addLayer(featureLayer);

params

参数说明是否必填默认类型
layer待添加图层--Layer

Layer 详细解释见Layer:api

参数说明类型
Image图片图层AILabel.Layer.Image
Feature矢量图层AILabel.Layer.Feature
Mask涂抹图层AILabel.Layer.Mask
Text文本图层AILabel.Layer.Text

removeLayerById

删除指定layerId的图layer

// define
removeLayerById(targetLayerId: string)
// demo
const featureLayer = new AILabel.Layer.Feature(...);
gMap.addLayer(featureLayer);
gMap.removeLayerById(featureLayer.id);

params

参数说明是否必填默认类型
targetLayerId待删除图层id--string

removeAllLayers

删除所有layers【内置图层除外】

// define
removeAllLayers()
// demo
const featureLayer = new AILabel.Layer.Feature(...);
gMap.addLayer(featureLayer);
gMap.removeAllLayers();

getLayers

获取当前map实例上的所有layer

// define
getLayers(): Array<Layer>
// demo
const allLayers = gMap.getLayers();

refresh

刷新map

// define
refresh()
// demo
gMap.refresh();

resize

大小重设map,可以指定size大小或者不传入(会自动获取dom-size大小进行重设)

// define
resize(size?: ISize)
// demo
gMap.resize();

setActiveFeature

设置map当前的待编辑feature,最多只会存在一个activeFeature

// define
setActiveFeature(feature: Feature | null)
// demo
// 设置编辑feature,需要配合gMap.events.on('featureSelected')使用【后续事件部分会详细讲解】
gMap.setActiveFeature(feature);
// 取消编辑对象,需要配合gMap.events.on('featureUnselected')使用【后续事件部分会详细讲解】
gMap.setActiveFeature(null);

params

参数说明是否必填默认类型
feature待编辑feature--Feature或null

getActiveFeature

获取当前activeFeature

// define
getActiveFeature(): Feature | null
// demo
gMap.getActiveFeature();

getTargetFeatureWithPoint

获取命中的矢量Feature对象,没有命中,则返回null

// define
getTargetFeatureWithPoint(globalPoint: IPoint): Feature | null
// demo
const targetFeature = gMap.getTargetFeatureWithPoint(point);

transformScreenToGlobal

屏幕坐标转实际坐标

// define
transformScreenToGlobal(screenPoint: IPoint): IPoint
// demo
const globalPoint = gMap.transformScreenToGlobal({x, y});

params

参数说明是否必填默认类型
screenPoint待转换屏幕坐标--IPoint

transformGlobalToScreen

实际坐标转屏幕坐标

// define
transformGlobalToScreen(globalPoint: IPoint): IPoint
// demo
const screenPoint = gMap.transformGlobalToScreen({x, y});

params

参数说明是否必填默认类型
globalPoint待转换实际坐标--IPoint

exportLayersToImage

AILabel.Map将layers导出为图片(支持导出text/image/feature/mask等图层,“图片不能跨域“)

// define
exportLayersToImage(bounds: IRectShape, option: IExportOption = {})
// demo
const base64 = await gMap.exportLayersToImage(
        {x: 0, y: 0, width: 500, height: 354},
        {type: 'base64', format: 'image/png'}
    );
const blob = await gMap.exportLayersToImage(
        {x: 0, y: 0, width: 500, height: 354},
        {type: 'blob', format: 'image/png'}
    );

params

参数说明是否必填默认类型
bounds导出视野范围--IRectShape
option其他可选项配置--IExportOption

IExportOption

参数说明是否必填默认类型
layers导出图层list当前Map上的layersLayer[]
type导出数据形式(支持base64或blob)base64string
format图片格式(支持image/png或image/jpeg)image/pngstring

destroy

AILabel.Map实例销毁【如果在切换实例时最好要将上一次实例进行destroy】

gMap && gMap.destroy();

events

事件监听

// define
events.on(eventType: EEventType, callback: Function);
// demo
gMap.events.on('boundsChanged', () => {console.log('bounds has changed')});

params

参数说明是否必填默认类型
eventType时间枚举类型--EEventType
callback回调函数--Function

EEventType

参数说明类型callback
boundsChanged视野范围发生变化stringBoundsChanged
featureSelected在绘制模式下双击feature触发选中stringFeatureSelected
featureUnselected当模式切换或单击其他地方触发stringFeatureUnselected
drawDone绘制结束时触发stringDrawDone
featureUpdatedfeature编辑完成触发stringFeatureUpdated
featureDeleted目前只针对点双击选中右键触发stringFeatureDeleted
draging拖动feature(dbclick之后)stringDraging
click单击事件stringMouseCallback
dblClick双击事件stringMouseCallback
mouseDown鼠标按下stringMouseCallback
mouseMove鼠标移动stringMouseCallback
mouseUp鼠标抬起stringMouseCallback
mouseOut鼠标移出stringMouseCallback
mouseOver鼠标进入stringMouseCallback

BoundsChanged

// define
callback: () => void
// demo
gMap.events.on('boundsChanged', callback);

FeatureSelected

// define
callback: (feature: Feature) => void
// demo
gMap.events.on('featureSelected', callback);

FeatureUnselected

// define
callback: (feature: Feature) => void
// demo
gMap.events.on('featureUnselected', callback);

DrawDone

// define
callback: (mapMode: EMapMode, shape) => void
// demo
gMap.events.on('drawDone', callback);

FeatureUpdated

// define
callback: (feature: Feature, shape) => void
// demo
gMap.events.on('featureUpdated', callback);

FeatureDeleted

// define [目前只针对点在选中态右键触发]
callback: (feature:PointFeature) => void
// demo
gMap.events.on('featureDeleted', callback);

Draging

// define
callback: (feature: Feature, shape) => void
// demo
gMap.events.on('draging', callback);

MouseCallback

// define
callback: (point:IBasePoint) => void
// demo
gMap.events.on('click', callback);

IBasePoint

参数说明类型
screen屏幕坐标IPoint
global实际坐标IPoint

slots

事件拦截器:callback返回值false,会阻断后续流程

// define
slots.on(eventType: EEventSlotType, callback: Function);
// demo
// 此示例为双击选中feature对象时阻止系统默认的高亮点样式,改为自定义样式
// gMap.slots.on('drawActivePoint', (point, overLayerInstance) => {
gMap.slots.on('drawActiveMiddlePoint', (point, overLayerInstance) => {
    overLayerInstance.addCircleFeature(
        {sr: 3.5, cx: point.x, cy: point.y},
        {
            clear: false,
            style: {strokeStyle: '#00f', fillStyle: '#00f', stroke: true, fill: true, lineWidth: 1}
        }
    );
    return false;
});

params

参数说明是否必填默认类型
eventType时间枚举类型--EEventSlotType
callback回调函数--Function

EEventSlotType

参数说明类型callback
drawActivePoint绘制高亮节点触发stringDrawActivePoint
drawActiveMiddlePoint绘制高亮节点中间待添加节点触发stringDrawActiveMiddlePoint

快捷键

目前AILabel中内置已有快捷键

参数说明
ctrl+z绘制多段线或者多边形过程中撤销节点
up/down/left/right选中feature时,可通过上/下/左/右快捷键对选中feature进行位置微调

AILabel.Layer.Image

图片图层

实例化

// define
constructor(id: string, image: IImageInfo, props: IObject = {}, style: ILayerStyle = {})
// demo
const gFirstImageLayer = new AILabel.Layer.Image(
   'first-layer-image', // id
   {
       src: 'https://img2.baidu.com/it/u=2053804264,1341330775&fm=26&fmt=auto&gp=0.jpg',
       width: 500, // 图片宽度
       height: 354, // 图片高度
       crossOrigin: false, // 图片是否跨域
       position: { // 图片左上角对应的坐标位置
           x: -250,
           y: 177
       },
       grid: { // 3 * 3
           columns: [{color: '#9370DB'}, {color: '#FF6347'}],
           rows: [{color: '#9370DB'}, {color: '#FF6347'}]
       }
   }, // imageInfo
   {name: '第一个图片图层'}, // props
   {zIndex: 5} // style
);
gMap.addLayer(gFirstImageLayer);

Params

参数说明是否必填默认类型
id唯一标识--string
image图片配置信息--IImageInfo
props属性信息--IObject
style样式--ILayerStyle

IImageInfo

参数说明是否必填默认类型
src图片地址--string
width图片宽度--number
height图片高度--number
crossOrigin图片是否跨域,主要用于图片导出时使用, 要根据实际情况设置,当图片导出时,需要图片的responseHeader-CORS设置允许跨域falseboolean
position图片位置{x:0,y:0}IPoint
grid图片网格{columns: [], rows: []}IGridInfo

IGridInfo

参数说明是否必填默认类型
columns列配置[]IGridItemInfo[]
rows行配置[]IGridItemInfo[]

IGridItemInfo

参数说明是否必填默认类型
color线颜色'#333333'string
width线宽1number

ILayerStyle

参数说明是否必填默认类型
zIndex层级1number
opacity透明度1.0number

updateGrid

更新网格

// define
updateGrid(gridInfo: IGridInfo)
// demo
gFirstImageLayer.updateGrid({columns: [{color: '#333'}], rows: [{color: '#666'}]});

events

事件监听

// define
events.on(eventType: ELayerImageEventType, callback: Function);
// demo
gFirstImageLayer.events.on('loadStart', (url, instance) => {
   console.log('--loadStart--');
});
gFirstImageLayer.events.on('loadEnd', (url, instance) => {
   console.log('--loadEnd--');
});
gFirstImageLayer.events.on('loadError', (url, instance) => {
   console.log('--loadError--');
});

params

参数说明是否必填默认类型
eventType时间枚举类型--ELayerImageEventType
callback回调函数--Function

ELayerImageEventType

参数说明类型
loadStart图片开始加载LoadStart
loadEnd加载成功LoadEnd
loadError加载失败LoadError

AILabel.Layer.Feature

矢量图层(用于承载Feature.Point, Feature.Line, Feature.Polyline, Feature.Polygon, Feature.Rect, Feature.Circle等矢量要素的展示)

实例化

// define
constructor(id: string, props: IObject = {}, style: ILayerStyle = {})
// demo
const gFirstFeatureLayer = new AILabel.Layer.Feature(
   'first-layer-feature', // id
   {name: '第一个矢量图层'}, // props
   {zIndex: 10} // style
);
gMap.addLayer(gFirstFeatureLayer);

Params

参数说明是否必填默认类型
id唯一标识--string
props属性信息--IObject
style样式--ILayerStyle-上文

addFeature

添加矢量要素(Feature.Point, Feature.Line, Feature.Polyline, Feature.Polygon, Feature.Rect, Feature.Circle等)

// define
addFeature(feature: Feature, option?: IFeatureAddOption)
// demo (AILabel.Feature.Polygon见下文)
const gFirstFeaturePolygon = new AILabel.Feature.Polygon(
   'first-feature-polygon', // id
   {points: [
       {x: 0, y: 0}, {x: 100, y: 100},
       {x: 100, y: 200}
   ]}, // shape
   {name: '第一个多边形'}, // props
   {strokeStyle: '#FFD500', lineWidth: 1} // style
);
gFirstFeatureLayer.addFeature(gFirstFeaturePolygon);

params

参数说明是否必填默认类型
feature待添加feature--Feature
option可选配置项{clear: false}IFeatureAddOption

IFeatureAddOption

参数说明是否必填默认类型
clear添加feature是否清空当前layer上已存在的featuresfalseboolean

removeFeatureById

移除指定featureId的feature

// define
removeFeatureById(targetFeatureId: string)
// demo
const polygonFeature = new AILabel.Feature.Polygon(...);
gFirstFeatureLayer.addFeature(polygonFeature);
gFirstFeatureLayer.removeFeatureById(polygonFeature.id);

params

参数说明是否必填默认类型
targetFeatureId待删除feature-id--string

getFeatureById

获取指定featureId的feature,如果没有,则返回null

// define
getFeatureById(targetFeatureId: string)
// demo
const polygonFeature = new AILabel.Feature.Polygon(...);
gFirstFeatureLayer.addFeature(polygonFeature);
gFirstFeatureLayer.getFeatureById(polygonFeature.id);

getAllFeatures

获取当前Layer.Feature上的所有features

// define
getAllFeatures(): Feature[]
// demo
const polygonFeature = new AILabel.Feature.Polygon(...);
gFirstFeatureLayer.addFeature(polygonFeature);
const allFeatures = gFirstFeatureLayer.getAllFeatures();

removeAllFeatures

移出当前featureLayer上所有features

// define
removeAllFeatures()
// demo
const polygonFeature = new AILabel.Feature.Polygon(...);
gFirstFeatureLayer.addFeature(polygonFeature);
gFirstFeatureLayer.removeAllFeatures();

getTargetFeatureWithPoint

根据point获取捕捉到的features中第一个,如果没有则返回null

// define
getTargetFeatureWithPoint(point: IPoint): Feature | null
// demo
gFirstFeatureLayer.getTargetFeatureWithPoint({x,y});

params

参数说明是否必填默认类型
pointtargetPoint点--IPoint

AILabel.Layer.Marker

注记层【注:此层为Map内置图层,不对外暴露二次开发进行实例】; gMap.markerLayer即为AILabel.Layer.Marker

实例化

内置实例,不需要进行二次开发进行实例

addMarker

添加marker注记

// define
addMarker(marker: Marker, option?: IObject)
// demo
const marker = new AILabel.Marker(...);
gMap.markerLayer.addMarker(marker);

params

参数说明是否必填默认类型
marker待添加marker--AILabel.Marker
option保留字段--IObject

removeMarkerById

移除指定markerId的marker

// define
removeMarkerById(targetMarkerId: string)
// demo
const marker = new AILabel.Marker(...);
gMap.markerLayer.addMarker(marker);
gMap.markerLayer.removeMarkerById(marker.id);

params

参数说明是否必填默认类型
targetMarkerId待删除marker-id--string

getMarkerById

获取指定markerId的marker,如果没有,则返回null

// define
getMarkerById(targetMarkerId: string)
// demo
const marker = new AILabel.Marker(...);
gMap.markerLayer.addMarker(marker);
gMap.markerLayer.getMarkerById(marker.id);

getAllMarkers

获取Layer.Marker上的所有markers

// define
getAllMarkers(): Marker[]
// demo
const marker = new AILabel.Marker(...);
gMap.markerLayer.addMarker(marker);
const allMarkers = gMap.markerLayer.getAllMarkers();

removeAllMarkers

移除所有markers

// define
removeAllMarkers()
// demo
const marker = new AILabel.Marker(...);
gMap.markerLayer.addMarker(marker);
gMap.markerLayer.removeAllMarkers();

AILabel.Layer.Text

文本图层,显示文本对象

实例化

// define
constructor(id: string, props: IObject = {}, style: ILayerStyle = {})
// demo
const gFirstTextLayer = new AILabel.Layer.Text(
   'first-layer-text', // id
   {name: '第一个文本图层'}, // props
   {zIndex: 12, opacity: 1} // style
);
gMap.addLayer(gFirstTextLayer);

Params

参数说明是否必填默认类型
id唯一标识--string
props属性信息--IObject
style样式--ILayerStyle-上文

addText

添加text文本

// define
addText(text: Text, option?: ITextAddOption)
// demo (AILabel.Text见下文)
const text = new AILabel.Text(...);
gFirstTextLayer.addText(text);

params

参数说明是否必填默认类型
text待添加text--Text
option可选配置项{clear: false}ITextAddOption

IFeatureAddOption

参数说明是否必填默认类型
clear添加text是否清空当前layer上已存在的textsfalseboolean

removeTextById

移除指定textId的text

// define
removeTextById(targetTextId: string)
// demo
const text = new AILabel.Text(...);
gFirstTextLayer.addText(text);
gFirstTextLayer.removeTextById(text.id);

params

参数说明是否必填默认类型
targetTextId待删除text-id--string

getTextById

获取指定textId的text,如果没有,则返回null

// define
getTextById(targetTextId: string)
// demo
const text = new AILabel.Text(...);
gFirstTextLayer.addText(text);
gFirstTextLayer.getTextById(text.id);

getAllTexts

获取Layer.Text上的所有texts对象

// define
getAllTexts(): Text[]
// demo
const text = new AILabel.Text(...);
gFirstTextLayer.addText(text);
const allTexts = gFirstTextLayer.getAllTexts();

removeAllTexts

移除所有texts

// define
removeAllTexts()
// demo
const text = new AILabel.Text(...);
gFirstTextLayer.addText(text);
gFirstTextLayer.removeAllTexts();

AILabel.Layer.Mask

文本图层,显示文本对象

实例化

// define
constructor(id: string, props: IObject = {}, style: ILayerStyle = {})
// demo
const gFirstMaskLayer = new AILabel.Layer.Mask(
   'first-layer-mask', // id
   {name: '第一个涂抹图层'}, // props
   {zIndex: 11, opacity: .5} // style
);
gMap.addLayer(gFirstMaskLayer);

Params

参数说明是否必填默认类型
id唯一标识--string
props属性信息--IObject
style样式--ILayerStyle-上文

addAction

添加涂抹Action,此处我们把绘制【DrawAction】/擦除【ClearAction】/数据回显【ImageAction】定义为action

// define
addAction(action: Action, option?: IObject)
// demo (AILabel.Action见下文)
const drawAction = new AILabel.Action.Draw(...);
gFirstMaskLayer.addAction(drawAction);

params

参数说明是否必填默认类型
action待添加Action--Action
option保留字段-可选配置项IObject

Action

参数说明类型
Draw涂抹ActionAction.Draw-见下文
Clear擦除ActionAction.Clear-见下文
Image回显数据ActionAction.Image-见下文

removeActionById

移除指定action

// define
removeActionById(targetActionId: string)
// demo (AILabel.Action见下文)
const drawAction = new AILabel.Action.Draw(...);
gFirstMaskLayer.addAction(drawAction);
gFirstMaskLayer.removeActionById(drawAction.id);

params

参数说明是否必填默认类型
targetActionId待删除action-id--string

removeAllActions

移除所有actions

// define
removeAllActions()
// demo (AILabel.Action见下文)
const drawAction = new AILabel.Action.Draw(...);
gFirstMaskLayer.addAction(drawAction);
gFirstMaskLayer.removeAllActions();

getAllActions

获取Layer.Mask上的所有actions

// define
getAllActions(): Action[]
// demo (AILabel.Action见下文)
const drawAction = new AILabel.Action.Draw(...);
gFirstMaskLayer.addAction(drawAction);
const allActions = gFirstMaskLayer.getAllActions();

getRleData

根据分类获取分类分类rle数据, 截取某个范围的rle数据

// define
getRleData(bounds: IRectShape)
// demo (AILabel.Action见下文)
const imageAction = new AILabel.Action.Image(...);
const drawAction = new AILabel.Action.Draw(...);
const clearAction = new AILabel.Action.Clear(...);
gFirstMaskLayer.addAction(imageAction);
gFirstMaskLayer.addAction(drawAction);
gFirstMaskLayer.addAction(clearAction);
const rleData = gFirstMaskLayer.getRleData({x: -250, y: 177, width: 500, height: 354});

params

参数说明是否必填默认类型
bounds矩形范围--IRectShape-见上文

AILabel.Feature

Feature.Point, Feature.Line, Feature.Polyline, Feature.Polygon, Feature.Rect, Feature.Circle等子类的基类

实例化

子类复写构造函数

updateShape

改变矢量要素shape信息

// define
updateShape(shape: IFeatureShape)
// demo
const feature = new AILabel.Feature.Point(...);
feature.updateShape(newPointShape)

params

参数说明是否必填默认类型
shape待更新的图形shape信息--IFeatureShape

IFeatureShape枚举

参数说明类型
shape待更新的图形shape信息IFeatureShape

setStyle

设置feature样式

// define
setStyle(style: IFeatureStyle, option?: IObject)
// demo
const feature = new AILabel.Feature.Point(...);
feature.setStyle({fillStyle: '#FF0000'});

params

参数说明是否必填默认类型
style待更新的图形style--IFeatureStyle
option保留字段-可选配置项--IObject

captureWithPoint

判断point是否捕捉到当前feature

// define
captureWithPoint(point: IPoint): boolean
// demo
const feature = new AILabel.Feature.Point(...);
const isCaptured = feature.captureWithPoint{x, y};

params

参数说明是否必填默认类型
point捕捉点--IPoint

AILabel.Feature.Point

点对象【实心】

实例化

// define
constructor(id: string, shape: IPointShape, props: IObject = {}, style: IFeatureStyle = {}, option?: IObject)
// demo
const gFirstFeaturePoint = new AILabel.Feature.Point(
   'first-feature-point', // id
   {x: -100, y: -100, sr: 3}, // shape
   {name: '第一个点'}, // props
   {fillStyle: '#00f'} // style
);
gFirstFeatureLayer.addFeature(gFirstFeaturePoint);

Params

参数说明是否必填默认类型
id唯一标识--string
shape空间信息--IPointShape
props属性信息--IObject
style样式--IFeatureStyle-上文
option保留字段可选配置项--IObject

IPointShape

参数说明是否必填默认类型
x坐标x--number
y坐标y--number
r半径(实际坐标系半径,会伴随放大缩小变化)--number
sr半径(屏幕坐标系半径,不会伴随放大缩小变化)--number
注:r 和 sr 【sr与r只会存在一个,如果同时存在,r优先级高】

updateShape

见AILabel.Feature

setStyle

见AILabel.Feature

captureWithPoint

见AILabel.Feature

AILabel.Feature.Line

线段对象

实例化

// define
constructor(id: string, shape: ILineShape, props: IObject = {}, style: IFeatureStyle = {})
// demo
const gFirstFeatureLine = new AILabel.Feature.Line(
   'first-feature-line', // id
   {start: {x: 10, y: 10}, end: {x: 100, y: -100}, width: 10}, // shape
   {name: '第一个线段'}, // props
   {strokeStyle: '#FF4500', lineCap: 'round'} // style
);
gFirstFeatureLayer.addFeature(gFirstFeatureLine);

Params

参数说明是否必填默认类型
id唯一标识--string
shape空间信息--ILineShape
props属性信息--IObject
style样式--IFeatureStyle-上文

ILineShape

参数说明是否必填默认类型
start起点--IPoint-见上文
end终点--IPoint-见上文
width线宽(实际坐标系宽);如果不设置,将会取style-lineWidth绘制--number

updateShape

见AILabel.Feature

setStyle

见AILabel.Feature

captureWithPoint

见AILabel.Feature

AILabel.Feature.Polyline

多段线对象

实例化

// define
constructor(id: string, shape: IPolylineShape, props: IObject = {}, style: IFeatureStyle = {})
// demo
const polylineFeature = new AILabel.Feature.Polyline(
  'first-feature-polyline', // id
  {points: data, width}, // shape
  {name: '第一个矢量图层'}, // props
  drawingStyle // style
);
gFirstFeatureLayer.addFeature(polylineFeature);

Params

参数说明是否必填默认类型
id唯一标识--string
shape空间信息--IPolylineShape
props属性信息--IObject
style样式--IFeatureStyle-上文

IPolylineShape

参数说明是否必填默认类型
points多段线节点集合--IPoint[]-见上文
width线宽(实际坐标系宽);如果不设置,将会取style-lineWidth绘制--number

updateShape

见AILabel.Feature

setStyle

见AILabel.Feature

captureWithPoint

见AILabel.Feature

AILabel.Feature.Rect

矩形对象

实例化

// define
constructor(id: string, shape: IRectShape, props: IObject = {}, style: IFeatureStyle = {})
// demo
const gFirstFeatureRect = new AILabel.Feature.Rect(
   'first-feature-rect', // id
   {x: -50, y: 50, width: 100, height: 100}, // shape
   {name: '第一个矢量图层'}, // props
   {strokeStyle: '#808080', lineWidth: 1} // style
);
gFirstFeatureLayer.addFeature(gFirstFeatureRect);

Params

参数说明是否必填默认类型
id唯一标识--string
shape空间信息--IRectShape
props属性信息--IObject
style样式--IFeatureStyle-上文

IRectShape 见上文

getPoints

获取矩形四个顶点坐标集合

// define
getPoints(): IPoint[]
// demo
const gFirstFeatureRect = new AILabel.Feature.Rect(...);
const rectPoints = gFirstFeatureRect.getPoints();

updateShape

见AILabel.Feature

setStyle

见AILabel.Feature

captureWithPoint

见AILabel.Feature

AILabel.Feature.Polygon

多边形对象

实例化

// define
constructor(id: string, shape: IPolygonShape, props: IObject = {}, style: IFeatureStyle = {})
// demo
const gFirstFeaturePolygon = new AILabel.Feature.Polygon(
   'first-feature-polygon', // id
   {points: [
       {x: 0, y: 0}, {x: 100, y: 100},
       {x: 100, y: 200}
   ]}, // shape
   {name: '第一个多边形'}, // props
   {strokeStyle: '#FFD500', lineWidth: 1} // style
);
gFirstFeatureLayer.addFeature(gFirstFeaturePolygon);

Params

参数说明是否必填默认类型
id唯一标识--string
shape空间信息--IPolygonShape
props属性信息--IObject
style样式--IFeatureStyle-上文

IPolygonShape

参数说明是否必填默认类型
points多边形顶点集合--IPoint[]-见上文
inner保留字段--[]

updateShape

见AILabel.Feature

setStyle

见AILabel.Feature

captureWithPoint

见AILabel.Feature

AILabel.Feature.Circle

圆对象

实例化

// define
constructor(id: string, shape: ICircleShape, props: IObject = {}, style: IFeatureStyle = {})
// demo
const gFirstFeatureCircle = new AILabel.Feature.Circle(
   'first-feature-circle', // id
   {cx: 0, cy: 0, r: 100}, // shape
   {name: '第一个矢量图层'}, // props
   {fillStyle: '#F4A460', strokeStyle: '#D2691E', globalAlpha: 1} // style
);
gFirstFeatureLayer.addFeature(gFirstFeatureCircle);

Params

参数说明是否必填默认类型
id唯一标识--string
shape空间信息--ICircleShape
props属性信息--IObject
style样式--IFeatureStyle-上文

ICircleShape

参数说明是否必填默认类型
cx坐标x--number
cy坐标y--number
r半径(实际坐标系半径,会伴随放大缩小变化)--number
sr半径(屏幕坐标系半径,不会伴随放大缩小变化)--number
注:r 和 sr 【sr与r只会存在一个,如果同时存在,r优先级高】

updateShape

见AILabel.Feature

setStyle

见AILabel.Feature

captureWithPoint

见AILabel.Feature

AILabel.Action

Action.Draw, Action.Clear, Action.Image等子类的基类

实例化

子类复写构造函数

AILabel.Action.Draw

涂抹对象【众所周知,涂抹/擦除的动作分为down->move->up,实际上绘制的就是一条具有宽度的具有多个点的多段线】

实例化

// define
constructor(id: string, category: string, shape: IDrawActionShape, props: IObject = {}, style: IFeatureStyle = {})
// demo
const drawMaskAction = new AILabel.Mask.Draw(
  'first-action-draw', // id
  '铅笔',
  {points: data, width}, // shape
  {name: '港币', price: '1元'}, // props
  {strokeStyle: '#FF0000'} // style
);
gFirstMaskLayer.addAction(drawMaskAction);

Params

参数说明是否必填默认类型
id唯一标识--string
category当前action类型--string
shape空间信息--IDrawActionShape
props属性信息--IObject
style样式--IFeatureStyle-上文

IDrawActionShape

参数说明是否必填默认类型
points多段线节点集合--IPoint[]-见上文
width线宽(实际坐标系宽);如果不设置,将会取style-lineWidth绘制涂抹--number

setStyle

设置action样式【除Action.Image】

// define
setStyle(style: IFeatureStyle, option?: IObject)
// demo
const drawAction = new AILabel.Action.Draw(...);
drawAction.setStyle({strokeStyle: '#FF0000'});

params

参数说明是否必填默认类型
style待更新的图形style--IFeatureStyle
option保留字段-可选配置项--IObject

AILabel.Action.Clear

涂抹擦除对象【众所周知,涂抹/擦除的动作分为down->move->up,实际上绘制的就是一条具有宽度的具有多个点的多段线】

实例化

// define
constructor(id: string, shape: IDrawActionShape, props: IObject = {}, style: IFeatureStyle = {})
// demo
const clearMaskAction = new AILabel.Mask.Clear(
  'first-action-clear', // id
  {points: data, width} // shape
);
gFirstMaskLayer.addAction(clearMaskAction);

Params

参数说明是否必填默认类型
id唯一标识--string
shape空间信息--IDrawActionShape
props原则上不需要--IObject
style原则上不需要--IFeatureStyle-上文

IDrawActionShape

参数说明是否必填默认类型
points多段线节点集合--IPoint[]-见上文
width线宽(实际坐标系宽);如果不设置,将会取style-lineWidth绘制擦除--number

AILabel.Action.Image

涂抹数据的回显时,如果返回rle数据,前端需要需要进行像素级处理,此时相当耗性能,此对象的设计就是为了既能满足涂抹数据的回显,又能尽可能最大的优化性能而产生; 注:对后端要求:需要按照把每一个分类涂抹rle数据生成图片指定大小的二值.png图片

实例化

// define
constructor(id: string, category: string, image: IImageInfo, props: IObject = {}, style: IFeatureStyle = {})
// demo
const gFirstMaskImageAction = new AILabel.Mask.Image(
   'first-image-action', // id
   '钢笔',
   {
       src: './mask_min.png',
       width: 500,
       height: 354,
       crossOrigin: false,
       position: { // 图片左上角坐标
           x: -250,
           y: 177
       }
   }, // imageInfo
   {}
);
gFirstMaskLayer.addAction(gFirstMaskImageAction);

Params

参数说明是否必填默认类型
id唯一标识--string
shape空间信息--IImageInfo
props属性信息--IObject
style原则上不需要--IFeatureStyle-上文

IImageInfo 见上文

AILabel.Marker

注记marker对象

实例化

// define
constructor(id: string, marker: IMarkerInfo, props: IObject = {}, option: IObject = {})
// demo
const gFirstMarker = new AILabel.Marker(
   'first-marker', // id
   {
       src: './marker.png',
       position: { // marker坐标位置
           x: 0,
           y: 0
       },
       offset: {
           x: -16,
           y: 32
       }
   }, // markerInfo
   {name: '第一个marker注记'} // props
);
gMap.markerLayer.addMarker(gFirstMarker);

Params

参数说明是否必填默认类型
id唯一标识--string
markermarker信息--IMarkerInfo
props属性信息--IObject
option保留字段--IObject

IMarkerInfo

参数说明是否必填默认类型
srcicon路径--string
position位置信息--IPoint
offset偏移量(屏幕坐标){x: 0, y: 0}IPoint

enableDragging

启用marker可拖拽【默认不可拖拽】

// define
enableDragging(): void
// demo
const gFirstMarker = new AILabel.Marker(...);
gFirstMarker.enableDragging();

disableDragging

禁用marker可拖拽【默认不可拖拽】

// define
disableDragging(): void
// demo
const gFirstMarker = new AILabel.Marker(...);
gFirstMarker.disableDragging();

updatePosition

更新marker的位置

// define
updatePosition(position: IPoint)
// demo
const gFirstMarker = new AILabel.Marker(...);
gFirstMarker.updatePosition({x: 0, y: 0});

Params

参数说明是否必填默认类型
position位置信息--IPoint

events

事件监听

// define
events.on(eventType: EMarkerEventType, callback: Function);
// demo
marker.events.on('click', () => {console.log('marker clicked')});

params

参数说明是否必填默认类型
eventType时间枚举类型--EMarkerEventType
callback回调函数--Function

EMarkerEventType

|参数|说明|类型| |---|---|---|---| |click|单击事件|string| |mouseDown|mouseDown事件|string| |mouseUp|mouseUp事件|string| |mouseOver|鼠标移入|string| |mouseOut|鼠标移出|string| |dragStart|拖拽前|string| |dragging|拖拽中|string| |dragEnd|拖拽结束(会返回拖拽后的最新位置信息)|string| |rightClick|右键单击|string|

AILabel.Text

text文本对象

实例化

// define
constructor(id: string, text: ITextInfo, props: IObject = {}, style: ITextStyle = {})
// demo
const gFirstText = new AILabel.Text(
   'first-text', // id
   {text: '中华人民共和国', position: {x: 0, y: 0}, offset: {x: 0, y: 0}}, // shape
   {name: '第一个文本对象'}, // props
   {fillStyle: '#F4A460', strokeStyle: '#D2691E', background: true, globalAlpha: 1, fontColor: '#0f0'} // style
);
gFirstTextLayer.addText(gFirstText);

Params

参数说明是否必填默认类型
id唯一标识--string
texttext信息--ITextInfo
props属性信息--IObject
style样式信息--ITextStyle

ITextInfo

参数说明是否必填默认类型
texttext文字--string
position位置信息--IPoint
offset偏移量(屏幕坐标){x: 0, y: 0}IPoint

ITextStyle、 继承自IFeatureStyle,新增属性字段如下

参数说明是否必填默认类型
background是否展示文字背景--boolean
fontColor字体颜色--string

updatePosition

更新text的位置

// define
updatePosition(position: IPoint)
// demo
const gFirstText = new AILabel.Text(...);
gFirstText.updatePosition({x: 0, y: 0});

Params

参数说明是否必填默认类型
position位置信息--IPoint

updateText

更新text的文本

// define
updateText(text: string)
// demo
const gFirstText = new AILabel.Text(...);
gFirstText.updateText('中华人民共和国');

Params

参数说明是否必填默认类型
text待更新文本--string

AILabel.Util

通用工具方法

MathUtil.getMiddlePoint

获取两点之间的中心点

getMiddlePoint(start: IPoint, end: IPoint): IPoint

MathUtil.distance

计算两点之间的距离

distance(start: IPoint, end: IPoint): number

EventUtil.getButtonIndex

获取鼠标左右键index

getButtonIndex(event: MouseEvent): number

联系作者

author

开源协议

Apache License

请遵循:Apache License 2.0