render-text.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. import { resizeCanvas } from '../../mol-canvas3d/util';
  17. const parent = document.getElementById('app')!
  18. parent.style.width = '100%'
  19. parent.style.height = '100%'
  20. const canvas = document.createElement('canvas')
  21. parent.appendChild(canvas)
  22. resizeCanvas(canvas, parent)
  23. const canvas3d = Canvas3D.fromCanvas(canvas)
  24. canvas3d.animate()
  25. function textRepr() {
  26. const props: PD.Values<Text.Params> = {
  27. ...PD.getDefaultValues(Text.Params),
  28. attachment: 'top-right',
  29. fontQuality: 3,
  30. fontWeight: 'normal',
  31. borderWidth: 0.3,
  32. background: true,
  33. backgroundOpacity: 0.5,
  34. tether: true,
  35. tetherLength: 1.5,
  36. tetherBaseWidth: 0.5,
  37. }
  38. const textBuilder = TextBuilder.create(props, 1, 1)
  39. textBuilder.add('Hello world', 0, 0, 0, 1, 1, 0)
  40. // textBuilder.add('Добрый день', 0, 1, 0, 0, 0)
  41. // textBuilder.add('美好的一天', 0, 2, 0, 0, 0)
  42. // textBuilder.add('¿Cómo estás?', 0, -1, 0, 0, 0)
  43. // textBuilder.add('αβγ Å', 0, -2, 0, 0, 0)
  44. const text = textBuilder.getText()
  45. const values = Text.Utils.createValuesSimple(text, props, Color(0xFFDD00), 1)
  46. const state = Text.Utils.createRenderableState(props)
  47. const renderObject = createRenderObject('text', values, state, -1)
  48. console.log('text', renderObject, props)
  49. const repr = Representation.fromRenderObject('text', renderObject)
  50. return repr
  51. }
  52. function spheresRepr() {
  53. const spheresBuilder = SpheresBuilder.create(1, 1)
  54. spheresBuilder.add(0, 0, 0, 0)
  55. spheresBuilder.add(5, 0, 0, 0)
  56. spheresBuilder.add(-4, 1, 0, 0)
  57. const spheres = spheresBuilder.getSpheres()
  58. const values = Spheres.Utils.createValuesSimple(spheres, {}, Color(0xFF0000), 0.2)
  59. const state = Spheres.Utils.createRenderableState({})
  60. const renderObject = createRenderObject('spheres', values, state, -1)
  61. console.log('spheres', renderObject)
  62. const repr = Representation.fromRenderObject('spheres', renderObject)
  63. return repr
  64. }
  65. canvas3d.add(textRepr())
  66. canvas3d.add(spheresRepr())
  67. canvas3d.resetCamera()