g-math
为
G
提供几何图形运算的库
安装下载
tnpm i --save @antv/g-math
// 所有的 api 是都这么引入,名字不同而已
import { line } from '@antv/g-math';
const len = line.length(x1, x2, x2, y2);
API 文档
目前使用到的、且推荐使用的 API 文档,不在文档内的不建议使用。
Geometry util
为
G
提供几何图形运算的库
tnpm i --save @antv/g-math
// 所有的 api 是都这么引入,名字不同而已
import { line } from '@antv/g-math';
const len = line.length(x1, x2, x2, y2);
目前使用到的、且推荐使用的 API 文档,不在文档内的不建议使用。
以下版本号以 @antv/g 为准。
number | string
#895font-size
,效果等同驼峰写法。同时增加以下属性作为已由属性的别名 stroke-width
stroke-dasharray
g-canvas
共用部分 Canvas2D 插件 g-plugin-canvas-renderer g-plugin-canvas-picker #910tap
事件,使用 click
事件代替0.3.0
,使用 container.unload()
写法getBounds
不再会返回 null,而是空的 AABB(长宽中心点坐标都为 0)displayObject.getRotation()
等方式获取/设置旋转四元数requestAnimationFrame/cancelAnimationFrame
设置自定义函数,适用于非浏览器环境canvas
,如果传入 G 不会再自动创建 <canvas>
,因此 container
不再是必填项。当用户想使用已有 <canvas>
或在 WebWorker 中使用 OffscreenCanvas,现在 时适用devicePixelRatio
await canvas.ready;
等待画布完成初始化工作无新增特性。重命名 dist/index.umd.js
-> dist/index.umd.min.js
修复 g-plugin-3d
UMD 打包问题。
使用 mana-syringe 替换 inversify。
[path-util] 转换无效 A 命令出错,例如 GUI 中使用的:
[['A', 0, 0, 0, 0, 0, 0, 0]];
明确了 G 目前使用到的坐标系,提供各个坐标系间的转换方法:Client <-> Viewport <-> Canvas
支持节点克隆,cloneNode(deep?: boolean)
在任何时刻都可以进行图形的克隆,例如:
circle.style.r = 20;
circle.setPosition(10, 20);
const clonedCircle = circle.cloneNode();
clonedCircle.style.r; // 20
clonedCircle.getPosition(); // [10, 20]
注意事项:
appendChild
将其加入画布才会被渲染支持使用 document.createElement() 创建图形,作为 new Circle()
的另一种选择,类似旧版 G 的 addShape()
:
import { Shape, Circle } from '@antv/g';
const circle = canvas.document.createElement(Shape.CIRCLE, { style: { r: 100 } });
// 或者
const circle = new Circle({ style: { r: 100 } });
除了内置图形,自定义图形如果也想用这种方式创建,需要先注册:
import { MyCustomShape } from 'my-custom-shape';
canvas.customElements.define(MyCustomShape.tag, MyCustomShape);
const myCustomShape = canvas.document.createElement(MyCustomShape.tag, {});
updateTiming
resetLocalTransform 重置局部坐标系下的变换 https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/resetTransform
circle.resetLocalTransform();
// 等价于
circle.setLocalScale(1);
circle.setLocalPosition(0, 0);
circle.setLocalEulerAngles(0);
const geometryBounds = circle.getGeometryBounds();
circle.appendChild(child1); // 添加一堆子元素
circle.appendChild(child2);
circle.appendChild(child3);
circle.getGeometryBounds(); // 尺寸依然等于 geometryBounds
removeAllEventListeners
移除元素上所有事件监听器:https://g-next.antv.vision/zh/docs/api/event#removealleventlistenerscircle.removeAllEventListeners();
// 或者
circle.off(); // 兼容旧版 API
getLineBoundingRects
获取 Text 多行文本包围盒:https://g-next.antv.vision/zh/docs/api/basic/text#getlineboundingrects-rectangletext.getLineBoundingRects(); // Rectangle[]
getRootNode
返回当前节点的根节点find
查询满足条件的第一个子节点findAll
查询满足条件的所有子节点列表// 以下写法等价
solarSystem.querySelector('[name=sun]');
solarSystem.find((element) => element.name === 'sun');
append
在当前节点的子节点列表末尾批量添加一组节点prepend
在当前节点的子节点列表头部批量添加一组节点after
在当前节点之后批量添加一些兄弟节点before
在当前节点之前批量添加一些兄弟节点replaceChild
用指定的节点替换当前节点的一个子节点,并返回被替换掉的节点replaceWith
在父节点的子节点列表中,用传入的节点列表替换该节点replaceChildren
替换该节点的所有子节点。不传参数时则会清空该节点的所有子节点Canvas 提供获取入口和场景图根节点方法:https://g-next.antv.vision/zh/docs/api/canvas#%E5%85%A5%E5%8F%A3%E4%B8%8E%E6%A0%B9%E8%8A%82%E7%82%B9
canvas.document; // 入口
canvas.document.documentElement; // 场景图根节点
// 或者
canvas.getRoot(); // 场景图根节点
// 向场景图根节点中添加节点
canvas.appendChild(circle);
// 或者
canvas.document.documentElement.appendChild(circle);
[width / 2, height / 2, 500]
,视点为 [width / 2, height / 2, 0]
。因此 z > 500 代表图形出现在了相机之后,内置的剔除插件会将其剔除,因此正确的展示效果为该图形不可见。