當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Context2d.measureText方法代碼示例

本文整理匯總了TypeScript中core/util/canvas.Context2d.measureText方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Context2d.measureText方法的具體用法?TypeScript Context2d.measureText怎麽用?TypeScript Context2d.measureText使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在core/util/canvas.Context2d的用法示例。


在下文中一共展示了Context2d.measureText方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: _render

  protected _render(ctx: Context2d, indices: number[], {sx, sy, _x_offset, _y_offset, _angle, _text}: TextData): void {
    this._sys = []
    this._sxs = []
    for (const i of indices) {
      if (isNaN(sx[i] + sy[i] + _x_offset[i] + _y_offset[i] + _angle[i]) || _text[i] == null)
        continue
      this._sxs[i] = []
      this._sys[i] = []
      if (this.visuals.text.doit) {
        const text = `${_text[i]}`

        ctx.save()
        ctx.translate(sx[i] + _x_offset[i], sy[i] + _y_offset[i])
        ctx.rotate(_angle[i])
        this.visuals.text.set_vectorize(ctx, i)

        const font = this.visuals.text.cache_select("font", i)
        const {height} = measure_font(font)
        const line_height = this.visuals.text.text_line_height.value()*height
        if (text.indexOf("\n") == -1){
          ctx.fillText(text, 0, 0)
          const x0 = sx[i] + _x_offset[i]
          const y0 = sy[i] + _y_offset[i]
          const width = ctx.measureText(text).width
          const [xvalues, yvalues] = this._text_bounds(x0, y0, width, line_height)
          this._sxs[i].push(xvalues)
          this._sys[i].push(yvalues)
        } else {
          const lines = text.split("\n")
          const block_height = line_height*lines.length
          const baseline = this.visuals.text.cache_select("text_baseline", i)

          let y: number
          switch (baseline) {
            case "top": {
              y = 0
              break
            }
            case "middle": {
              y = (-block_height/2) + (line_height/2)
              break
            }
            case "bottom": {
              y = -block_height + line_height
              break
            }
            default: {
              y = 0
              console.warn(`'${baseline}' baseline not supported with multi line text`)
            }
          }

          for (const line of lines) {
            ctx.fillText(line, 0, y)

            const x0 = sx[i] + _x_offset[i]
            const y0 = y + sy[i] + _y_offset[i]
            const width = ctx.measureText(line).width
            const [xvalues, yvalues] = this._text_bounds(x0, y0, width, line_height)
            this._sxs[i].push(xvalues)
            this._sys[i].push(yvalues)

            y += line_height
          }
        }

        ctx.restore()
      }
    }
  }
開發者ID:digitalsatori,項目名稱:Bokeh,代碼行數:70,代碼來源:text.ts

示例2: _calculate_text_dimensions

 protected _calculate_text_dimensions(ctx: Context2d, text: string): [number, number] {
   const {width} = ctx.measureText(text)
   const {height} = get_text_height(this.visuals.text.font_value())
   return [width, height]
 }
開發者ID:Zyell,項目名稱:bokeh,代碼行數:5,代碼來源:text_annotation.ts

示例3: _calculate_text_dimensions

 _calculate_text_dimensions(ctx: Context2d, text) {
   const { width } = ctx.measureText(text);
   const { height } = get_text_height(this.visuals.text.font_value());
   return [width, height];
 }
開發者ID:FourtekIT-incubator,項目名稱:bokeh,代碼行數:5,代碼來源:text_annotation.ts


注:本文中的core/util/canvas.Context2d.measureText方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。