All files / Sources/Rendering/Misc/CanvasView index.js

50% Statements 38/76
9.75% Branches 4/41
36.36% Functions 4/11
51.35% Lines 38/74

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    1x               2x         1x 1x 1x       1x           1x     1x       2x   2x                                                                 2x       2x                                       2x                                                   2x         2x 2x 2x 2x 2x             1x                       2x     2x 2x     2x 2x 2x 2x 2x 2x 2x     2x     2x   2x             2x 2x 2x     2x         1x          
import macro from 'vtk.js/Sources/macros';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkCanvasView methods
// ----------------------------------------------------------------------------
 
function vtkCanvasView(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkCanvasView');
 
  // Auto update style
  function updateWindow() {
    // Canvas size
    if (model._renderable) {
      model.canvas.setAttribute('width', model.size[0]);
      model.canvas.setAttribute('height', model.size[1]);
    }
 
    // ImageStream size
    Iif (model.viewStream) {
      // If same size that's a NoOp
      model.viewStream.setSize(model.size[0], model.size[1]);
    }
 
    // Offscreen ?
    model.canvas.style.display = model.useOffScreen ? 'none' : 'block';
 
    // Cursor type
    Iif (model.el) {
      model.el.style.cursor = model.cursorVisibility ? model.cursor : 'none';
    }
  }
  publicAPI.onModified(updateWindow);
 
  publicAPI.setContainer = (el) => {
    if (model.el && model.el !== el) {
      if (model.canvas.parentNode !== model.el) {
        vtkErrorMacro('Error: canvas parent node does not match container');
      }
 
      // Remove canvas from previous container
      model.el.removeChild(model.canvas);
 
      // If the renderer has previously added
      // a background image, remove it from the DOM.
      if (model.el.contains(model.bgImage)) {
        model.el.removeChild(model.bgImage);
      }
    }
 
    if (model.el !== el) {
      model.el = el;
      if (model.el) {
        model.el.appendChild(model.canvas);
      }
 
      // If the renderer is set to use a background
      // image, attach it to the DOM.
      if (model.useBackgroundImage) {
        model.el.appendChild(model.bgImage);
      }
 
      // Trigger modified()
      publicAPI.modified();
    }
  };
 
  publicAPI.setBackgroundImage = (img) => {
    model.bgImage.src = img.src;
  };
 
  publicAPI.setUseBackgroundImage = (value) => {
    model.useBackgroundImage = value;
 
    // Add or remove the background image from the
    // DOM as specified.
    if (
      model.useBackgroundImage &&
      model.el &&
      !model.el.contains(model.bgImage)
    ) {
      model.el.appendChild(model.bgImage);
    } else if (
      !model.useBackgroundImage &&
      model.el &&
      model.el.contains(model.bgImage)
    ) {
      model.el.removeChild(model.bgImage);
    }
  };
 
  publicAPI.setViewStream = (stream) => {
    if (model.viewStream === stream) {
      return false;
    }
    if (model.subscription) {
      model.subscription.unsubscribe();
      model.subscription = null;
    }
    model.viewStream = stream;
    if (model.viewStream) {
      // Enable display of the background image
      publicAPI.setUseBackgroundImage(true);
 
      // Bind to remote stream
      model.subscription = model.viewStream.onImageReady((e) =>
        publicAPI.setBackgroundImage(e.image)
      );
      model.viewStream.setSize(model.size[0], model.size[1]);
      model.viewStream.invalidateCache();
      model.viewStream.render();
 
      publicAPI.modified();
    }
    return true;
  };
 
  publicAPI.delete = macro.chain(publicAPI.setViewStream, publicAPI.delete);
 
  // --------------------------------------------------------------------------
  // Make us look like a View (i.e.: vtkOpenGLRenderWindow)
  // --------------------------------------------------------------------------
  model._renderable = publicAPI;
  model._renderers = [publicAPI];
  publicAPI.traverseAllPasses = () => {};
  publicAPI.isInViewport = () => true;
  publicAPI.getInteractive = () => true;
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  canvas: null,
  size: [300, 300],
  cursorVisibility: true,
  cursor: 'pointer',
  useOffScreen: false,
  useBackgroundImage: false,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Create internal instances
  model.canvas = document.createElement('canvas');
  model.canvas.style.width = '100%';
 
  // Create internal bgImage
  model.bgImage = new Image();
  model.bgImage.style.position = 'absolute';
  model.bgImage.style.left = '0';
  model.bgImage.style.top = '0';
  model.bgImage.style.width = '100%';
  model.bgImage.style.height = '100%';
  model.bgImage.style.zIndex = '-1';
 
  // Inheritance
  macro.obj(publicAPI, model, initialValues);
 
  // Build VTK API
  macro.get(publicAPI, model, ['useBackgroundImage', '_renderable']);
 
  macro.setGet(publicAPI, model, [
    'canvas',
    'cursor',
    'useOffScreen',
    'interactor',
  ]);
 
  macro.setGetArray(publicAPI, model, ['size'], 2);
  macro.getArray(publicAPI, model, ['_renderers']);
  macro.moveToProtected(publicAPI, model, ['renderable', 'renderers']);
 
  // Object methods
  vtkCanvasView(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkCanvasView');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };