Skip to content
Snippets Groups Projects
Select Git revision
  • 794d09fbde530045f9ce6e9406ef221930e252e6
  • mui5-annotation-on-video-stable default
  • get_setter_canvasSizeInformations
  • fix-error-div-into-p
  • annotation-on-video-v2
  • detached
  • annotation-on-video-r17
  • mui5
  • mui5-react-18
  • jacob-test
  • annotation-on-video protected
  • master
  • test-antoinev1
  • 20-fetch-thumbnail-on-annotation
  • add-research-field
  • Save
  • add-plugin
  • 14-wip-no-seek-to
  • 14-bug-on-video-time-control
  • 9_wip_videotests
  • _upgrade_material_ui
  • latest-tetras-16
  • v3.3.0
  • v3.2.0
  • v3.1.1
  • v3.1.0
  • v3.0.0
  • v3.0.0-rc.7
  • v3.0.0-rc.6
  • v3.0.0-rc.5
  • v3.0.0-rc.4
  • v3.0.0-rc.3
  • v3.0.0-rc.2
  • v3.0.0-rc.1
  • v3.0.0-beta.10
  • v3.0.0-beta.9
  • v3.0.0-beta.8
  • v3.0.0-beta.7
  • v3.0.0-beta.6
  • v3.0.0-beta.5
  • v3.0.0-beta.3
41 results

CanvasAnnotationDisplay.js

Blame
  • CanvasAnnotationDisplay.js 2.02 KiB
    /**
     * CanvasAnnotationDisplay - class used to display a SVG and fragment based
     * annotations.
     */
    export default class CanvasAnnotationDisplay {
      /** */
      constructor({
        resource, color, zoom, offset,
      }) {
        this.resource = resource;
        this.color = color;
        this.zoom = zoom;
        this.offset = offset;
      }
    
      /** */
      toContext(context) {
        if (this.resource.svgSelector) {
          this.svgContext(context);
        } else {
          this.fragmentContext(context);
        }
      }
    
      /** */
      get svgString() {
        return this.resource.svgSelector.value;
      }
    
      /** */
      svgContext(context) {
        [...this.svgPaths].forEach((element) => {
          /**
           *  Note: Path2D is not supported in IE11.
           *  TODO: Support multi canvas offset
           *  One example: https://developer.mozilla.org/en-US/docs/Web/API/Path2D/addPath
           */
          const p = new Path2D(element.attributes.d.nodeValue);
          /**
           * Note: we could do something to return the svg styling attributes as
           * some have encoded information in these values. However, how should we
           * handle highlighting and other complications?
           *  context.strokeStyle = element.attributes.stroke.nodeValue;
           *  context.lineWidth = element.attributes['stroke-width'].nodeValue;
           */
          context.strokeStyle = this.color; // eslint-disable-line no-param-reassign
          context.lineWidth = this.lineWidth(); // eslint-disable-line no-param-reassign
          context.stroke(p);
        });
      }
    
      /** */
      fragmentContext(context) {
        const fragment = this.resource.fragmentSelector;
        fragment[0] += this.offset.x;
        context.strokeStyle = this.color; // eslint-disable-line no-param-reassign
        context.lineWidth = this.lineWidth(); // eslint-disable-line no-param-reassign
        context.strokeRect(...fragment);
      }
    
      /** */
      lineWidth() {
        return Math.ceil(1 / (this.zoom * 100));
      }
    
      /** */
      get svgPaths() {
        const parser = new DOMParser();
        const xmlDoc = parser.parseFromString(this.svgString, 'text/xml');
        return xmlDoc.getElementsByTagName('path');
      }
    }