Skip to content
Snippets Groups Projects
Select Git revision
  • d547ebf4558ab20202e5efd12cda6ffdbc28dfc8
  • 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

setupJestIntegration.js

Blame
  • CanvasAnnotationDisplay.js 2.71 KiB
    /**
     * CanvasAnnotationDisplay - class used to display a SVG and fragment based
     * annotations.
     */
    export default class CanvasAnnotationDisplay {
      /** */
      constructor({
        resource, color, zoomRatio, offset,
      }) {
        this.resource = resource;
        this.color = color;
        this.zoomRatio = zoomRatio;
        this.offset = offset;
      }
    
      /** */
      toContext(context) {
        this.context = context;
        if (this.resource.svgSelector) {
          this.svgContext();
        } else {
          this.fragmentContext();
        }
      }
    
      /** */
      get svgString() {
        return this.resource.svgSelector.value;
      }
    
      /** */
      svgContext() {
        [...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
           */
          this.context.save();
          this.context.translate(this.offset.x, this.offset.y);
          const p = new Path2D(element.attributes.d.nodeValue);
    
          // Setup styling from SVG -> Canvas
          this.context.strokeStyle = this.color;
          if (element.attributes['stroke-dasharray']) {
            this.context.setLineDash(element.attributes['stroke-dasharray'].nodeValue.split(','));
          }
          const svgToCanvasMap = {
            fill: 'fillStyle',
            stroke: 'strokeStyle',
            'stroke-dashoffset': 'lineDashOffset',
            'stroke-linecap': 'lineCap',
            'stroke-linejoin': 'lineJoin',
            'stroke-miterlimit': 'miterlimit',
            'stroke-width': 'lineWidth',
          };
          Object.keys(svgToCanvasMap).forEach((key) => {
            if (element.attributes[key]) {
              this.context[svgToCanvasMap[key]] = element.attributes[key].nodeValue;
            }
          });
    
          // Resize the stroke based off of the zoomRatio (currentZoom / maxZoom)
          this.context.lineWidth /= this.zoomRatio;
          this.context.stroke(p);
    
          // Wait to set the fill, so we can adjust the globalAlpha value if we need to
          if (element.attributes.fill && element.attributes.fill.nodeValue !== 'none') {
            if (element.attributes['fill-opacity']) {
              this.context.globalAlpha = element.attributes['fill-opacity'].nodeValue;
            }
            this.context.fill(p);
          }
          this.context.restore();
        });
      }
    
      /** */
      fragmentContext() {
        const fragment = this.resource.fragmentSelector;
        fragment[0] += this.offset.x;
        fragment[1] += this.offset.y;
        this.context.strokeStyle = this.color;
        this.context.lineWidth = 1 / this.zoomRatio;
        this.context.strokeRect(...fragment);
      }
    
      /** */
      get svgPaths() {
        const parser = new DOMParser();
        const xmlDoc = parser.parseFromString(this.svgString, 'text/xml');
        return xmlDoc.getElementsByTagName('path');
      }
    }