mirror of
https://gitee.com/antv/g6.git
synced 2024-11-30 02:38:20 +08:00
docs: add custom behavior to drag canvas with two fingers and wheel docs.
This commit is contained in:
parent
2dafd0dd32
commit
7b3d0d6f4d
@ -0,0 +1,59 @@
|
||||
import G6 from '@antv/g6';
|
||||
|
||||
/**
|
||||
* This demo shows how to custom a behavior to allow drag canvas with two fingers on touchpad and wheel
|
||||
* By Shiwu
|
||||
*/
|
||||
G6.registerBehavior('double-finger-drag-canvas', {
|
||||
getEvents: function getEvents() {
|
||||
return {
|
||||
wheel: 'onWheel'
|
||||
};
|
||||
},
|
||||
|
||||
onWheel: function onWheel(ev) {
|
||||
if (ev.ctrlKey) {
|
||||
const canvas = graph.get('canvas');
|
||||
const pixelRatio = canvas.get('pixelRatio');
|
||||
const point = canvas.getPointByClient(ev.clientX, ev.clientY);
|
||||
let ratio = graph.getZoom();
|
||||
if (ev.wheelDelta > 0) {
|
||||
ratio = ratio + ratio * 0.05;
|
||||
} else {
|
||||
ratio = ratio - ratio * 0.05;
|
||||
}
|
||||
graph.zoomTo(ratio, {
|
||||
x: point.x / pixelRatio,
|
||||
y: point.y / pixelRatio
|
||||
});
|
||||
} else {
|
||||
const x = ev.deltaX || ev.movementX;
|
||||
const y = ev.deltaY || ev.movementY;
|
||||
graph.translate(-x, -y);
|
||||
}
|
||||
ev.preventDefault();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const width = document.getElementById('container').scrollWidth;
|
||||
const height = document.getElementById('container').scrollHeight || 500;
|
||||
const graph = new G6.Graph({
|
||||
container: 'container',
|
||||
width,
|
||||
height,
|
||||
modes: {
|
||||
default: ['double-finger-drag-canvas']
|
||||
},
|
||||
layout: {
|
||||
type: 'force'
|
||||
},
|
||||
});
|
||||
|
||||
fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/relations.json')
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
graph.data(data);
|
||||
graph.render();
|
||||
});
|
||||
|
16
examples/interaction/customBehavior/demo/meta.json
Normal file
16
examples/interaction/customBehavior/demo/meta.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"title": {
|
||||
"zh": "中文分类",
|
||||
"en": "Category"
|
||||
},
|
||||
"demos": [
|
||||
{
|
||||
"filename": "dragCanvasTwoFingers.js",
|
||||
"title": {
|
||||
"zh": "两指平移画布",
|
||||
"en": "Drag Canvas by Two fingers"
|
||||
},
|
||||
"screenshot": "https://gw.alipayobjects.com/mdn/rms_f8c6a0/afts/img/A*wc1-QaqOPsgAAAAAAAAAAABkARQnAQ"
|
||||
}
|
||||
]
|
||||
}
|
10
examples/interaction/customBehavior/index.en.md
Normal file
10
examples/interaction/customBehavior/index.en.md
Normal file
@ -0,0 +1,10 @@
|
||||
---
|
||||
title: Custom Behavior
|
||||
order: 10
|
||||
---
|
||||
|
||||
Custom a behavior when the [built-in behaviors](/en/docs/manual/middle/states/defaultBehavior) do not meet your requirements.
|
||||
|
||||
## Usage
|
||||
|
||||
Custom a behavior with `graph.registerBehavior`, refer to [Custom Behavior Doc](en/docs/manual/advanced/custom-behavior).
|
10
examples/interaction/customBehavior/index.zh.md
Normal file
10
examples/interaction/customBehavior/index.zh.md
Normal file
@ -0,0 +1,10 @@
|
||||
---
|
||||
title: 自定义交互
|
||||
order: 10
|
||||
---
|
||||
|
||||
当[内置 behavior](/zh/docs/manual/middle/states/defaultBehavior)不能满足需求时,可以自定义交互。
|
||||
|
||||
## 使用指南
|
||||
|
||||
使用 `graph.registerBehavior` 自定义交互,教程参见 [自定义交互](zh/docs/manual/advanced/custom-behavior)。
|
Loading…
Reference in New Issue
Block a user