控制台

工具

本页目录

关于 @realsee/dnalogel 中工具函数和类的文档。

导入

import { Util } from '@realsee/dnalogel'

闪烁一个 dom / THREE.Object3D


// ---cut---
const target = document.getElementById('my-dom-element')!

Util.blink(target)
Util.reblink(target) // 反向闪烁

Util.FiveDomEvents

five空间中的物体的交互系统


// ---cut---
const domEvents = new Util.FiveDomEvents(five)

domEvents.addEventListener(object, 'click', () => {
  console.log('Clicked!')
})

domEvents.addEventListener(object, 'hover', () => {
  console.log('Hovered!')
})

domEvents.addEventListener(object, 'unHover', () => {
  console.log('Unhovered!')
})

Util.FivePuppet

使用独立 canvas 绘制物体,摄像机位置及视角与 five 保持一致


// ---cut---
const fivePuppet = new Util.FivePuppet(five)
fivePuppet.scene.add(object)

Util.lookObject

以最佳视角观察一个物体


// ---cut---
Util.lookObject(five, object)

Util.lookPoint

以最佳视角看向一个坐标点


const vector3 = new THREE.Vector3(0, 1, 2)
// ---cut---
Util.lookPoint(five, vector3)

Util.PointSelector

选点器,支持触摸屏长按选点


// ---cut---
const selector = new Util.PointSelector(five)

// 连续选点
selector.on('select', (intersection) => {
  console.log('Selected point:', intersection?.point)
})

// 选点后自动结束
selector.on('select', (intersection) => {
  console.log('Selected point:', intersection?.point)
  selector.disable()
})

selector.enable()

Util.sculpt

封装了一些常用的 mesh。完整数据结构:data


// ---cut---
const {
  LineMesh,
  BoxMesh,
  CircleMesh,
  CircleWithEdgeMesh,
  CylinderMesh,
  PointMesh,
  PolygonMesh,
  PrismMesh
} = Util.sculpt

// 在空间中添加 (0,1,2) 点
five.scene.add(new PointMesh({ point: [0, 1, 2] }))

// 添加线
five.scene.add(new LineMesh({ points: [[0, 1, 2], [3, 4, 5]] }))

// 添加多边形
five.scene.add(new PolygonMesh({ points: [[0, 1, 2], [3, 4, 5], [6, 7, 8]] }))

// 添加半径为3的圆,圆心为 (0,1,2) 点
five.scene.add(new CircleMesh({ center: [0, 1, 2], radius: 3 }))

// 添加带边的圆
five.scene.add(new CircleWithEdgeMesh({ center: [0, 1, 2], radius: 3 }))

// 添加柱体
five.scene.add(new CylinderMesh({ bottomCenter: [0, 1, 0], topCenter: [0, 2, 0], radius: 3 }))

// 添加 box,底面矩形为 [[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]],高点为 [0, 2, 0]
five.scene.add(new BoxMesh({ 
  points: [
    [0, 0, 0],
    [1, 0, 0],
    [1, 0, 1],
    [0, 0, 1]
  ],
  heightPoint: [0, 2, 0], 
}))

// 添加棱柱
five.scene.add(new PrismMesh({
  points: [
    [0, 0, 0],
    [1, 0, 0],
    [1, 0, 2],
    [1, 0, 1],
    [0, 0, 1]
  ],
  heightPoint: [0, 2, 0], 
}))

其他用法请参阅 Sculpt,例如绘制一个 boxMesh 并启用 box 编辑功能。


// ---cut---
const { PointSelector } = Util
const { BoxMesh, BoxMeshEditor, createBox } = Util.sculpt

// 创建 boxMesh
const boxMesh = new BoxMesh({ lineColor: 0xff0000 })
five.scene.add(boxMesh)

// 按需初始化编辑器
const editor = new BoxMeshEditor(boxMesh)

// 创建选择器
const pointSelector = new PointSelector(five)

createBox(boxMesh, pointSelector).then(() => {
  console.log('绘制完成')

  // 按需启用编辑功能
  editor.enable()
})

Util.tag

轻量化的标签


// ---cut---
const tag1 = Util.tag(five, [0, 1, 2])
tag1.container.innerHTML = '<div>tag1</div>'

const tag2 = Util.tag(five, [0, 1, 2])
tag2.container.innerHTML = '<div>tag2</div>'

或:先初始化,后设置位置


// ---cut---
const tag = Util.tag(five)
tag.container.innerHTML = '<div>tag</div>'

tag.setPosition(new THREE.Vector3(0, 1, 2))