All files / lib/item HitResult.ts

100% Statements 35/35
100% Branches 4/4
100% Functions 2/2
100% Lines 35/35

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                              1x 1x                 1x 1x 1x   1x 247x 247x       247x 247x                                                                                                                                         1x           1x 265x 265x 265x     265x   265x   265x     265x     265x   265x     265x   265x   265x   265x   265x   265x 265x 265x 265x 265x 1x 1x 1x   1x  
/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
 * http://juerglehni.com/ & https://puckey.studio/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */
 
// TODO: remove eslint-disable comment and deal with errors over time
/* eslint-disable */
 
import { ref } from '~/globals';
import { Base } from '~/straps';
 
/**
 * @name HitResult
 *
 * @class A HitResult object contains information about the results of a hit
 * test. It is returned by {@link Item#hitTest(point)} and
 * {@link Project#hitTest(point)}.
 */
export const HitResult = Base.extend(
  /** @lends HitResult# */ {
    _class: 'HitResult',
 
    initialize: function HitResult(type, item, values) {
      this.type = type;
      this.item = item;
      // Inject passed values, so we can be flexible about the HitResult
      // properties.
      // This allows the definition of getters too, e.g. for 'pixel'.
      if (values) this.inject(values);
    },
 
    /**
     * Describes the type of the hit result. For example, if you hit a segment
     * point, the type would be `'segment'`.
     *
     * @name HitResult#type
     * @property
     * @type String
     * @values 'segment', 'handle-in', 'handle-out', 'curve', 'stroke', 'fill',
     *     'bounds', 'center', 'pixel'
     */
 
    /**
     * If the HitResult has a {@link HitResult#type} of `'bounds'`, this
     * property describes which corner of the bounding rectangle was hit.
     *
     * @name HitResult#name
     * @property
     * @type String
     * @values 'top-left', 'top-right', 'bottom-left', 'bottom-right',
     *     'left-center', 'top-center', 'right-center', 'bottom- center'
     */
 
    /**
     * The item that was hit.
     *
     * @name HitResult#item
     * @property
     * @type Item
     */
 
    /**
     * If the HitResult has a type of 'curve' or 'stroke', this property gives
     * more information about the exact position that was hit on the path.
     *
     * @name HitResult#location
     * @property
     * @type CurveLocation
     */
 
    /**
     * If the HitResult has a type of 'pixel', this property refers to the color
     * of the pixel on the {@link Raster} that was hit.
     *
     * @name HitResult#color
     * @property
     * @type ?Color
     */
 
    /**
     * If the HitResult has a type of 'stroke', 'segment', 'handle-in' or
     * 'handle-out', this property refers to the segment that was hit or that
     * is closest to the hitResult.location on the curve.
     *
     * @name HitResult#segment
     * @property
     * @type Segment
     */
 
    /**
     * Describes the actual coordinates of the segment, handle or bounding box
     * corner that was hit.
     *
     * @name HitResult#point
     * @property
     * @type Point
     */
 
    statics: {
      /**
       * Merges default options into options hash for #hitTest() calls.
       *
       * @private
       */
      getOptions: function (args) {
        var options = args && Base.read(args);
        return new Base(
          {
            // Type of item, for instanceof check: Group, Layer, Path,
            // CompoundPath, Shape, Raster, SymbolItem, ...
            type: null,
            // Tolerance
            tolerance: ref.paper.settings.hitTolerance,
            // Hit the fill of items
            fill: !options,
            // Hit the curves of path items, taking into account the stroke
            // width.
            stroke: !options,
            // Hit the part of segments that curves pass through, excluding
            // its segments (Segment#point)
            segments: !options,
            // Hit the parts of segments that define the curvature
            handles: false,
            // Only first or last segment hits on path (mutually exclusive
            // with segments: true)
            ends: false,
            // Hit test the item position
            position: false,
            // Hit test the center of the bounds
            center: false,
            // Hit test the corners and side-centers of the bounding box
            bounds: false,
            //  Hit items that are marked as guides
            guides: false,
            // Only hit selected objects
            selected: false,
          },
          options
        );
      },
    },
  }
);
 
ref.HitResult = HitResult;