render-text.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import './index.html'
  7. import { Canvas3D } from 'mol-canvas3d/canvas3d';
  8. import { TextBuilder } from 'mol-geo/geometry/text/text-builder';
  9. import { Text } from 'mol-geo/geometry/text/text';
  10. import { ParamDefinition as PD } from 'mol-util/param-definition';
  11. import { Color } from 'mol-util/color';
  12. import { Representation } from 'mol-repr/representation';
  13. import { SpheresBuilder } from 'mol-geo/geometry/spheres/spheres-builder';
  14. import { createRenderObject } from 'mol-gl/render-object';
  15. import { Spheres } from 'mol-geo/geometry/spheres/spheres';
  16. const parent = document.getElementById('app')!
  17. parent.style.width = '100%'
  18. parent.style.height = '100%'
  19. const canvas = document.createElement('canvas')
  20. canvas.style.width = '100%'
  21. canvas.style.height = '100%'
  22. parent.appendChild(canvas)
  23. const canvas3d = Canvas3D.create(canvas, parent)
  24. canvas3d.animate()
  25. function textRepr() {
  26. const props: PD.Values<Text.Params> = {
  27. ...PD.getDefaultValues(Text.Params),
  28. attachment: 'middle-center',
  29. fontQuality: 3,
  30. fontWeight: 'normal',
  31. borderWidth: 0.3,
  32. background: true
  33. }
  34. const textBuilder = TextBuilder.create(props, 1, 1)
  35. textBuilder.add('Hello world', 0, 0, 0, 0, 0)
  36. textBuilder.add('Добрый день', 0, 1, 0, 0, 0)
  37. textBuilder.add('美好的一天', 0, 2, 0, 0, 0)
  38. textBuilder.add('¿Cómo estás?', 0, -1, 0, 0, 0)
  39. textBuilder.add('αβγ Å', 0, -2, 0, 0, 0)
  40. const text = textBuilder.getText()
  41. const values = Text.Utils.createValuesSimple(text, props, Color(0xFFDD00), 1)
  42. const state = Text.Utils.createRenderableState(props)
  43. const renderObject = createRenderObject('text', values, state)
  44. console.log('text', renderObject)
  45. const repr = Representation.fromRenderObject('text', renderObject)
  46. return repr
  47. }
  48. function spheresRepr() {
  49. const spheresBuilder = SpheresBuilder.create(1, 1)
  50. spheresBuilder.add(0, 0, 0, 0)
  51. spheresBuilder.add(5, 0, 0, 0)
  52. spheresBuilder.add(-4, 1, 0, 0)
  53. const spheres = spheresBuilder.getSpheres()
  54. const values = Spheres.Utils.createValuesSimple(spheres, {}, Color(0xFF0000), 0.2)
  55. const state = Spheres.Utils.createRenderableState({})
  56. const renderObject = createRenderObject('spheres', values, state)
  57. console.log('spheres', renderObject)
  58. const repr = Representation.fromRenderObject('spheres', renderObject)
  59. return repr
  60. }
  61. canvas3d.add(textRepr())
  62. canvas3d.add(spheresRepr())
  63. canvas3d.resetCamera()