# All files / src Label.ts

0.84% Statements 1/118
0% Branches 0/56
0% Functions 0/6
0.93% Lines 1/107

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282

    18x

     import {
      SpriteAlignment,
      Color,
      CC,
      Texture,
      SpriteMaterial,
      Sprite,
    } from "./WebGL";
    import { Gradient } from "./Gradient";
    //Adapted from the text sprite example from http://stemkoski.github.io/Three.js/index.html

    export let LabelCount = 0;

    // function for drawing rounded rectangles - for Label drawing
    function roundRect(ctx, x, y, w, h, r, drawBorder) {
      ctx.beginPath();
      ctx.moveTo(x + r, y);
      ctx.lineTo(x + w - r, y);
      ctx.quadraticCurveTo(x + w, y, x + w, y + r);
      ctx.lineTo(x + w, y + h - r);
      ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
      ctx.lineTo(x + r, y + h);
      ctx.quadraticCurveTo(x, y + h, x, y + h - r);
      ctx.lineTo(x, y + r);
      ctx.quadraticCurveTo(x, y, x + r, y);
      ctx.closePath();
      ctx.fill();
      Iif (drawBorder) ctx.stroke();
    }

    //do all the checks to figure out what color is desired
    function getColor(style, stylealpha?: any, init?: any) {
      var ret = init;
      Iif (typeof style != "undefined") {
        //convet regular colors
        if (style instanceof Color) ret = style.scaled();
        else {
          //hex or name
          ret = CC.color(style);
          Iif (typeof ret.scaled != "undefined") {
            ret = ret.scaled(); //not already scaled to 255
          }
        }
      }
      Iif (typeof stylealpha != "undefined") {
        ret.a = parseFloat(stylealpha);
      }
      return ret;
    }

    /**
     * Renderable labels
     * @constructor $3Dmol.Label
     * @param {string} tag - Label text
     * @param {LabelSpec} parameters Label style and font specifications
     */
    export class Label {
      id: number;
      stylespec: any;
      canvas: HTMLCanvasElement;
      context: any;
      sprite: any;
      text: any;
      frame: any;
      constructor(text, parameters) {
        this.id = LabelCount++;
        this.stylespec = parameters || {};

        this.canvas = document.createElement("canvas");
        //todo: implement resizing canvas..
        this.canvas.width = 134;
        this.canvas.height = 35;
        this.context = this.canvas.getContext("2d");
        this.sprite = new Sprite();
        this.text = text;
        this.frame = this.stylespec.frame;
      }

      getStyle() {
        return this.stylespec;
      }

      setContext() {
        var style = this.stylespec;
        var useScreen =
          typeof style.useScreen == "undefined" ? false : style.useScreen;

        var showBackground = style.showBackground;
        Iif (showBackground === "0" || showBackground === "false")
          showBackground = false;
        Iif (typeof showBackground == "undefined") showBackground = true; //default
        var font = style.font ? style.font : "sans-serif";

        var fontSize = parseInt(style.fontSize) ? parseInt(style.fontSize) : 18;

        var fontColor = getColor(style.fontColor, style.fontOpacity, {
          r: 255,
          g: 255,
          b: 255,
          a: 1.0,
        });

        var padding = style.padding ? style.padding : 4;
        var borderThickness = style.borderThickness ? style.borderThickness : 0;

        var backgroundColor = getColor(
          style.backgroundColor,
          style.backgroundOpacity,
          {
            r: 0,
            g: 0,
            b: 0,
            a: 1.0,
          }
        );

        var borderColor = getColor(
          style.borderColor,
          style.borderOpacity,
          backgroundColor
        );

        var position = style.position
          ? style.position
          : {
            x: -10,
            y: 1,
            z: 1,
          };

        // Should labels always be in front of model?
        var inFront = style.inFront !== undefined ? style.inFront : true;
        Iif (inFront === "false" || inFront === "0") inFront = false;

        // clear canvas

        var spriteAlignment = style.alignment || SpriteAlignment.topLeft;
        Iif (
          typeof spriteAlignment == "string" &&
          spriteAlignment in SpriteAlignment
        ) {
          spriteAlignment = SpriteAlignment[spriteAlignment];
        }

        var bold = "";
        Iif (style.bold) bold = "bold ";
        this.context.font = bold + fontSize + "px  " + font;

        var metrics = this.context.measureText(this.text);
        var textWidth = metrics.width;

        Iif (!showBackground) borderThickness = 0;

        var width = textWidth + 2.5 * borderThickness + 2 * padding;
        var height = fontSize * 1.25 + 2 * borderThickness + 2 * padding; // 1.25 is extra height factor for text below baseline: g,j,p,q.

        Iif (style.backgroundImage) {
          var img = style.backgroundImage;
          var w = style.backgroundWidth ? style.backgroundWidth : img.width;
          var h = style.backgroundHeight ? style.backgroundHeight : img.height;
          Iif (w > width) width = w;
          Iif (h > height) height = h;
        }

        this.canvas.width = width;
        this.canvas.height = height;
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);

        bold = "";
        Iif (style.bold) bold = "bold ";
        this.context.font = bold + fontSize + "px  " + font;

        // background color
        this.context.fillStyle =
          "rgba(" +
          backgroundColor.r +
          "," +
          backgroundColor.g +
          "," +
          backgroundColor.b +
          "," +
          backgroundColor.a +
          ")";
        // border color
        this.context.strokeStyle =
          "rgba(" +
          borderColor.r +
          "," +
          borderColor.g +
          "," +
          borderColor.b +
          "," +
          borderColor.a +
          ")";

        Iif (style.backgroundGradient) {
          let gradient = this.context.createLinearGradient(
            0,
            height / 2,
            width,
            height / 2
          );
          let g = Gradient.getGradient(style.backgroundGradient);
          let minmax = g.range();
          let min = -1;
          let max = 1;
          Iif (minmax) {
            min = minmax[0];
            max = minmax[1];
          }
          let d = max - min;
          for (let i = 0; i < 1.01; i += 0.1) {
            let c = getColor(g.valueToHex(min + d * i));
            let cname = "rgba(" + c.r + "," + c.g + "," + c.b + "," + c.a + ")";
            gradient.addColorStop(i, cname);
          }
          this.context.fillStyle = gradient;
        }

        this.context.lineWidth = borderThickness;
        Iif (showBackground) {
          roundRect(
            this.context,
            borderThickness,
            borderThickness,
            width - 2 * borderThickness,
            height - 2 * borderThickness,
            6,
            borderThickness > 0
          );
        }

        Iif (style.backgroundImage) {
          let img = style.backgroundImage;
          let w = style.backgroundWidth ? style.backgroundWidth : img.width;
          let h = style.backgroundHeight ? style.backgroundHeight : img.height;
          this.context.drawImage(img, 0, 0, w, h);
        }

        // text color
        this.context.fillStyle =
          "rgba(" +
          fontColor.r +
          "," +
          fontColor.g +
          "," +
          fontColor.b +
          "," +
          fontColor.a +
          ")";

        this.context.fillText(
          this.text,
          borderThickness + padding,
          fontSize + borderThickness + padding,
          textWidth
        );

        // canvas contents will be used for a texture
        var texture = new Texture(this.canvas);
        texture.needsUpdate = true;
        this.sprite.material = new SpriteMaterial({
          map: texture,
          useScreenCoordinates: useScreen,
          alignment: spriteAlignment,
          depthTest: !inFront,
          screenOffset: style.screenOffset || null,
        });

        this.sprite.scale.set(1, 1, 1);

        this.sprite.position.set(position.x, position.y, position.z);
      }

      // clean up material and texture
      dispose() {
        Iif (this.sprite.material.map !== undefined)
          this.sprite.material.map.dispose();
        Iif (this.sprite.material !== undefined) this.sprite.material.dispose();
      }
    }