All files / lib/item SymbolItem.ts

92.3% Statements 48/52
100% Branches 11/11
77.77% Functions 7/9
92.3% Lines 48/52

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                              1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x   1x 1x 1x 1x                                                                                 1x           29x 29x 29x   1x         1x 1x 1x               1x 9x 9x   1x 29x 29x 29x           1x 1x   1x 19x 19x   1x 40x   40x 40x   1x       2x 2x     2x 2x 2x   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 { Point } from '~/basic/Point';
import { Change } from './ChangeFlag';
import { Item } from './Item';
import { SymbolDefinition } from './SymbolDefinition';
 
/**
 * @name SymbolItem
 *
 * @class A symbol item represents an instance of a symbol which has been
 * placed in a Paper.js project.
 *
 * @extends Item
 */
export const SymbolItem = Item.extend(
  /** @lends SymbolItem# */ {
    _class: 'SymbolItem',
    _applyMatrix: false,
    _canApplyMatrix: false,
    // SymbolItem uses strokeBounds for bounds
    _boundsOptions: { stroke: true },
    _serializeFields: {
      symbol: null,
    },
 
    /**
     * Creates a new symbol item.
     *
     * @name SymbolItem#initialize
     * @param {SymbolDefinition|Item} definition the definition to place or an
     *     item to place as a symbol
     * @param {Point} [point] the center point of the placed symbol
     *
     * @example {@paperscript split=true height=240}
     * // Placing 100 instances of a symbol:
     * // Create a star shaped path at {x: 0, y: 0}:
     * var path = new Path.Star({
     *     center: new Point(0, 0),
     *     points: 6,
     *     radius1: 5,
     *     radius2: 13,
     *     fillColor: 'white',
     *     strokeColor: 'black'
     * });
     *
     * // Create a symbol definition from the path:
     * var definition = new SymbolDefinition(path);
     *
     * // Place 100 instances of the symbol:
     * for (var i = 0; i < 100; i++) {
     *     // Place an instance of the symbol in the project:
     *     var instance = new SymbolItem(definition);
     *
     *     // Move the instance to a random position within the view:
     *     instance.position = Point.random() * view.size;
     *
     *     // Rotate the instance by a random amount between
     *     // 0 and 360 degrees:
     *     instance.rotate(Math.random() * 360);
     *
     *     // Scale the instance between 0.25 and 1:
     *     instance.scale(0.25 + Math.random() * 0.75);
     * }
     */
    initialize: function SymbolItem(arg0, arg1) {
      // Support two forms of item initialization: Passing one object literal
      // describing all the different properties to be set, or a symbol (arg0)
      // and a point where it should be placed (arg1).
      // If _initialize can set properties through object literal, we're done.
      // Otherwise we need to set symbol from arg0.
      if (!this._initialize(arg0, arg1 !== undefined && Point.read(arguments, 1)))
        this.setDefinition(arg0 instanceof SymbolDefinition ? arg0 : new SymbolDefinition(arg0));
    },
 
    _equals: function (item) {
      // TODO: Compare position too!
      return this._definition === item._definition;
    },
 
    copyContent: function (source) {
      this.setDefinition(source._definition);
    },
 
    /**
     * The symbol definition that the placed symbol refers to.
     *
     * @bean
     * @type SymbolDefinition
     */
    getDefinition: function () {
      return this._definition;
    },
 
    setDefinition: function (definition) {
      this._definition = definition;
      this._changed(/*#=*/ Change.GEOMETRY);
    },
 
    /**
     * @bean
     * @deprecated use {@link #definition} instead.
     */
    getSymbol: '#getDefinition',
    setSymbol: '#setDefinition',
 
    isEmpty: function () {
      return this._definition._item.isEmpty();
    },
 
    _getBounds: function (matrix, options) {
      var item = this._definition._item;
      // Redirect the call to the definition item to calculate the bounds.
      return item._getCachedBounds(item._matrix.prepended(matrix), options);
    },
 
    _hitTestSelf: function (point, options, viewMatrix) {
      // We need to call definition item hit test with `options.all = false`,
      // as otherwise it would populate the array with its own matches.
      // Instead we want only returning one match per symbol-item, see #1680
      var opts = options.extend({ all: false });
      var res = this._definition._item._hitTest(point, opts, viewMatrix);
      // TODO: When the symbol's definition is a path, should hitResult
      // contain information like HitResult#curve?
      if (res) res.item = this;
      return res;
    },
 
    _draw: function (ctx, param) {
      this._definition._item.draw(ctx, param);
    },
 
    // TODO: SymbolItem#embed()
  }
);
 
ref.SymbolItem = SymbolItem;