Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[g] 在 document 上提供 elementsFromPoint 方法 #948

Closed
xiaoiver opened this issue Apr 8, 2022 · 4 comments
Closed

[g] 在 document 上提供 elementsFromPoint 方法 #948

xiaoiver opened this issue Apr 8, 2022 · 4 comments
Assignees
Labels

Comments

@xiaoiver
Copy link
Contributor

xiaoiver commented Apr 8, 2022

将拾取方法挂载到 document 对象上:

返回该位置最顶层的一个图形:https://developer.mozilla.org/zh-CN/docs/Web/API/Document/elementFromPoint
返回该位置最顶层的一组图形:https://developer.mozilla.org/zh-CN/docs/Web/API/Document/elementsFromPoint

const topMostElement = document.elementFromPoint(30, 20); // circle1
const elements = document.elementsFromPoint(30, 20); // [circle1, circle2]

如果点在可视范围外,返回 null 和空数组。

@xiaoiver xiaoiver self-assigned this Apr 8, 2022
@zhanba
Copy link
Member

zhanba commented Apr 8, 2022

这个API应用场景是什么,第一次听说。。

@xiaoiver
Copy link
Contributor Author

xiaoiver commented Apr 12, 2022

这个API应用场景是什么,第一次听说。。

目前有两个场景:

  • G2 的新版交互语法需要使用,并不一定由鼠标、触摸屏这类交互事件触发
  • 在 WebWorker 中配合 OffscreenCanvas 渲染时,如果要实现交互也需要在主线程中把当前鼠标位置通过 postMessage 传递给 Worker 线程,再通过这样的方式完成拾取(当然也可以在 Worker 中手动触发事件)。

只需要把 G 内部实现的拾取方法暴露出来就行:

  • g-canvas 使用 Rbush 空间索引做区域查询后,再使用数学方法 + isPointInPath
  • g-svg 直接使用 document.element(s)FromPoint

考虑到 g-webgl 使用 GPU 拾取是一个异步的流程,因此这俩方法也只能是异步的。

最后在 g-webgl 中实现 “多层拾取” 时,需要将第一个拾取到的对象 encoded color 修改为透明,再进行下一个拾取,直到从这个像素点再也无法读取到颜色编码。

可以参考 luma.gl 的做法:visgl/luma.gl#432
deck.gl 的实现:https://github.com/visgl/deck.gl/pull/1730/files#diff-d4bc662539cc14905f14364c5993222533cab8aeb3cc36a9536618e83ded0b66

@xiaoiver
Copy link
Contributor Author

目前 g-webgl 在 elementsFromPoint 方法上性能有问题。批量更新 interactive 导致

xiaoiver added a commit that referenced this issue Apr 28, 2022
* fix: test whether native event came from context's dom element #928

* fix: add offscreenCanvas in Canvas config  #940

* feat: add CSS API, includes CSS Typed OM & Layout API

* fix: add shadow for Line / Polyline / Path #959

* feat: add  for multiple-layer picking #948

* fix: put raf/cancelRaf to Canvas #953

* fix: add limits query such as  to Canvas #953

* fix: account for 'none' value in <color> #952

* feat: support multi-layer picking in g-webgl #948

* fix: support  when picking in g-webgl #952

* feat: support inheritance in style system

* fix: encode html entity in g-svg #963

* feat: add get/setLocalMatrix for legacy G4.0

* fix: animations example

* feat: add pointerEvents property #942

* fix: skip number2string when parsing number

* Publish

 - @antv/g-canvas@1.0.20
 - @antv/g-components@1.0.18
 - @antv/g-devtool@0.3.4
 - @antv/g-math@1.0.18
 - @antv/g-mobile-canvas@0.0.2
 - @antv/g-mobile-svg@0.0.2
 - @antv/g-mobile-webgl@0.0.2
 - @antv/g-mobile@1.0.5
 - @antv/g-plugin-3d@1.0.22
 - @antv/g-plugin-box2d@1.0.11
 - @antv/g-plugin-canvas-picker@1.0.18
 - @antv/g-plugin-canvas-renderer@1.0.18
 - @antv/g-plugin-control@1.0.19
 - @antv/g-plugin-css-select@1.0.18
 - @antv/g-plugin-dom-interaction@1.0.18
 - @antv/g-plugin-gpgpu@1.0.19
 - @antv/g-plugin-html-renderer@1.0.19
 - @antv/g-plugin-matterjs@1.0.8
 - @antv/g-plugin-mobile-interaction@0.0.7
 - @antv/g-plugin-physx@1.0.11
 - @antv/g-plugin-svg-picker@1.0.18
 - @antv/g-plugin-svg-renderer@1.0.18
 - @antv/g-plugin-webgl-renderer@1.0.24
 - @antv/g-plugin-yoga@1.0.8
 - @antv/g-shader-components@1.0.16
 - @antv/g-svg@1.0.18
 - @antv/g-web-component@1.0.5
 - @antv/g-webgl@1.0.24
 - @antv/g-webgpu-compiler@1.0.19
 - @antv/g@5.0.21
 - @antv/react-g@1.0.14
 - @antv/g-site@1.0.24

* publish

Co-authored-by: yuqi.pyq <yuqi.pyq@antgroup.com>
@xiaoiver xiaoiver reopened this May 3, 2022
@xiaoiver
Copy link
Contributor Author

xiaoiver commented May 3, 2022

来自:https://www.khronos.org/events/webgl-meetup-april-2022
在拾取时可以利用相机偏移,仅渲染鼠标(拾取点)周围一小块区域(1 x 1),而非整个屏幕,可以显著提升性能。

在 Three.js 中通过 setViewOffset 对相机进行偏移:
https://r105.threejsfundamentals.org/threejs/lessons/threejs-picking.html

暂时不提供基于 Raycast 的 CPU 拾取。

@xiaoiver xiaoiver closed this as completed May 4, 2022
xiaoiver added a commit that referenced this issue May 4, 2022
* fix: replace @antv/util with lodash-es #894

* fix: use setViewOffset when picking in g-webgl to enhance perf #948

Co-authored-by: yuqi.pyq <yuqi.pyq@antgroup.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants