1 /* 2 Copyright 2008-2025 3 Matthias Ehmann, 4 Michael Gerhaeuser, 5 Carsten Miller, 6 Bianca Valentin, 7 Alfred Wassermann, 8 Peter Wilfahrt 9 10 This file is part of JSXGraph. 11 12 JSXGraph is free software dual licensed under the GNU LGPL or MIT License. 13 14 You can redistribute it and/or modify it under the terms of the 15 16 * GNU Lesser General Public License as published by 17 the Free Software Foundation, either version 3 of the License, or 18 (at your option) any later version 19 OR 20 * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 21 22 JSXGraph is distributed in the hope that it will be useful, 23 but WITHOUT ANY WARRANTY; without even the implied warranty of 24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 GNU Lesser General Public License for more details. 26 27 You should have received a copy of the GNU Lesser General Public License and 28 the MIT License along with JSXGraph. If not, see <https://www.gnu.org/licenses/> 29 and <https://opensource.org/licenses/MIT/>. 30 */ 31 32 /*global JXG: true, define: true*/ 33 /*jslint nomen: true, plusplus: true, unparam: true*/ 34 35 import JXG from "../jxg.js"; 36 import Const from "./constants.js"; 37 import Coords from "./coords.js"; 38 import Mat from "../math/math.js"; 39 import Statistics from "../math/statistics.js"; 40 import Options from "../options.js"; 41 import EventEmitter from "../utils/event.js"; 42 import Color from "../utils/color.js"; 43 import Type from "../utils/type.js"; 44 45 /** 46 * Constructs a new GeometryElement object. 47 * @class This is the parent class for all geometry elements like points, circles, lines, curves... 48 * @constructor 49 * @param {JXG.Board} board Reference to the board the element is constructed on. 50 * @param {Object} attributes Hash of attributes and their values. 51 * @param {Number} type Element type (a <tt>JXG.OBJECT_TYPE_</tt> value). 52 * @param {Number} oclass The element's class (a <tt>JXG.OBJECT_CLASS_</tt> value). 53 * @borrows JXG.EventEmitter#on as this.on 54 * @borrows JXG.EventEmitter#off as this.off 55 * @borrows JXG.EventEmitter#triggerEventHandlers as this.triggerEventHandlers 56 * @borrows JXG.EventEmitter#eventHandlers as this.eventHandlers 57 */ 58 JXG.GeometryElement = function (board, attributes, type, oclass) { 59 var name, key, attr; 60 61 /** 62 * Controls if updates are necessary 63 * @type Boolean 64 * @default true 65 */ 66 this.needsUpdate = true; 67 68 /** 69 * Controls if this element can be dragged. In GEONExT only 70 * free points and gliders can be dragged. 71 * @type Boolean 72 * @default false 73 */ 74 this.isDraggable = false; 75 76 /** 77 * If element is in two dimensional real space this is true, else false. 78 * @type Boolean 79 * @default true 80 */ 81 this.isReal = true; 82 83 /** 84 * Stores all dependent objects to be updated when this point is moved. 85 * @type Object 86 */ 87 this.childElements = {}; 88 89 /** 90 * If element has a label subelement then this property will be set to true. 91 * @type Boolean 92 * @default false 93 */ 94 this.hasLabel = false; 95 96 /** 97 * True, if the element is currently highlighted. 98 * @type Boolean 99 * @default false 100 */ 101 this.highlighted = false; 102 103 /** 104 * Stores all Intersection Objects which in this moment are not real and 105 * so hide this element. 106 * @type Object 107 */ 108 this.notExistingParents = {}; 109 110 /** 111 * Keeps track of all objects drawn as part of the trace of the element. 112 * @see JXG.GeometryElement#clearTrace 113 * @see JXG.GeometryElement#numTraces 114 * @type Object 115 */ 116 this.traces = {}; 117 118 /** 119 * Counts the number of objects drawn as part of the trace of the element. 120 * @see JXG.GeometryElement#clearTrace 121 * @see JXG.GeometryElement#traces 122 * @type Number 123 */ 124 this.numTraces = 0; 125 126 /** 127 * Stores the transformations which are applied during update in an array 128 * @type Array 129 * @see JXG.Transformation 130 */ 131 this.transformations = []; 132 133 /** 134 * @type JXG.GeometryElement 135 * @default null 136 * @private 137 */ 138 this.baseElement = null; 139 140 /** 141 * Elements depending on this element are stored here. 142 * @type Object 143 */ 144 this.descendants = {}; 145 146 /** 147 * Elements on which this element depends on are stored here. 148 * @type Object 149 */ 150 this.ancestors = {}; 151 152 /** 153 * Ids of elements on which this element depends directly are stored here. 154 * @type Object 155 */ 156 this.parents = []; 157 158 /** 159 * Stores variables for symbolic computations 160 * @type Object 161 */ 162 this.symbolic = {}; 163 164 /** 165 * Stores the SVG (or VML) rendering node for the element. This enables low-level 166 * access to SVG nodes. The properties of such an SVG node can then be changed 167 * by calling setAttribute(). Note that there are a few elements which consist 168 * of more than one SVG nodes: 169 * <ul> 170 * <li> Elements with arrow tail or head: rendNodeTriangleStart, rendNodeTriangleEnd 171 * <li> SVG (or VML) texts: rendNodeText 172 * <li> Button: rendNodeForm, rendNodeButton, rendNodeTag 173 * <li> Checkbox: rendNodeForm, rendNodeCheckbox, rendNodeLabel, rendNodeTag 174 * <li> Input: rendNodeForm, rendNodeInput, rendNodeLabel, rendNodeTag 175 * </ul> 176 * 177 * Here is are two examples: The first example shows how to access the SVG node, 178 * the second example demonstrates how to change SVG attributes. 179 * @example 180 * var p1 = board.create('point', [0, 0]); 181 * console.log(p1.rendNode); 182 * // returns the full SVG node details of the point p1, something like: 183 * // <ellipse id='box_jxgBoard1P6' stroke='#ff0000' stroke-opacity='1' stroke-width='2px' 184 * // fill='#ff0000' fill-opacity='1' cx='250' cy='250' rx='4' ry='4' 185 * // style='position: absolute;'> 186 * // </ellipse> 187 * 188 * @example 189 * var s = board.create('segment', [p1, p2], {strokeWidth: 60}); 190 * s.rendNode.setAttribute('stroke-linecap', 'round'); 191 * 192 * @type Object 193 */ 194 this.rendNode = null; 195 196 /** 197 * The string used with {@link JXG.Board#create} 198 * @type String 199 */ 200 this.elType = ""; 201 202 /** 203 * The element is saved with an explicit entry in the file (<tt>true</tt>) or implicitly 204 * via a composition. 205 * @type Boolean 206 * @default true 207 */ 208 this.dump = true; 209 210 /** 211 * Subs contains the subelements, created during the create method. 212 * @type Object 213 */ 214 this.subs = {}; 215 216 /** 217 * Inherits contains the subelements, which may have an attribute 218 * (in particular the attribute 'visible') having value 'inherit'. 219 * @type Object 220 */ 221 this.inherits = []; 222 223 /** 224 * The position of this element inside the {@link JXG.Board#objectsList}. 225 * @type Number 226 * @default -1 227 * @private 228 */ 229 this._pos = -1; 230 231 /** 232 * [c, b0, b1, a, k, r, q0, q1] 233 * 234 * See 235 * A.E. Middleditch, T.W. Stacey, and S.B. Tor: 236 * "Intersection Algorithms for Lines and Circles", 237 * ACM Transactions on Graphics, Vol. 8, 1, 1989, pp 25-40. 238 * 239 * The meaning of the parameters is: 240 * Circle: points p=[p0, p1] on the circle fulfill 241 * a<p, p> + <b, p> + c = 0 242 * For convenience we also store 243 * r: radius 244 * k: discriminant = sqrt(<b,b>-4ac) 245 * q=[q0, q1] center 246 * 247 * Points have radius = 0. 248 * Lines have radius = infinity. 249 * b: normalized vector, representing the direction of the line. 250 * 251 * Should be put into Coords, when all elements possess Coords. 252 * @type Array 253 * @default [1, 0, 0, 0, 1, 1, 0, 0] 254 */ 255 this.stdform = [1, 0, 0, 0, 1, 1, 0, 0]; 256 257 /** 258 * The methodMap determines which methods can be called from within JessieCode and under which name it 259 * can be used. The map is saved in an object, the name of a property is the name of the method used in JessieCode, 260 * the value of a property is the name of the method in JavaScript. 261 * @type Object 262 */ 263 this.methodMap = { 264 setLabel: "setLabel", 265 label: "label", 266 setName: "setName", 267 getName: "getName", 268 Name: "getName", 269 addTransform: "addTransform", 270 setProperty: "setAttribute", 271 setAttribute: "setAttribute", 272 addChild: "addChild", 273 animate: "animate", 274 on: "on", 275 off: "off", 276 trigger: "trigger", 277 addTicks: "addTicks", 278 removeTicks: "removeTicks", 279 removeAllTicks: "removeAllTicks", 280 Bounds: "bounds" 281 }; 282 283 /** 284 * Quadratic form representation of circles (and conics) 285 * @type Array 286 * @default [[1,0,0],[0,1,0],[0,0,1]] 287 */ 288 this.quadraticform = [ 289 [1, 0, 0], 290 [0, 1, 0], 291 [0, 0, 1] 292 ]; 293 294 /** 295 * An associative array containing all visual properties. 296 * @type Object 297 * @default empty object 298 */ 299 this.visProp = {}; 300 301 /** 302 * An associative array containing visual properties which are calculated from 303 * the attribute values (i.e. visProp) and from other constraints. 304 * An example: if an intersection point does not have real coordinates, 305 * visPropCalc.visible is set to false. 306 * Additionally, the user can control visibility with the attribute "visible", 307 * even by supplying a functions as value. 308 * 309 * @type Object 310 * @default empty object 311 */ 312 this.visPropCalc = { 313 visible: false 314 }; 315 316 EventEmitter.eventify(this); 317 318 /** 319 * Is the mouse over this element? 320 * @type Boolean 321 * @default false 322 */ 323 this.mouseover = false; 324 325 /** 326 * Time stamp containing the last time this element has been dragged. 327 * @type Date 328 * @default creation time 329 */ 330 this.lastDragTime = new Date(); 331 332 this.view = null; 333 334 if (arguments.length > 0) { 335 /** 336 * Reference to the board associated with the element. 337 * @type JXG.Board 338 */ 339 this.board = board; 340 341 /** 342 * Type of the element. 343 * @constant 344 * @type Number 345 */ 346 this.type = type; 347 348 /** 349 * Original type of the element at construction time. Used for removing glider property. 350 * @constant 351 * @type Number 352 */ 353 this._org_type = type; 354 355 /** 356 * The element's class. 357 * @constant 358 * @type Number 359 */ 360 this.elementClass = oclass || Const.OBJECT_CLASS_OTHER; 361 362 /** 363 * Unique identifier for the element. Equivalent to id-attribute of renderer element. 364 * @type String 365 */ 366 this.id = attributes.id; 367 368 name = attributes.name; 369 /* If name is not set or null or even undefined, generate an unique name for this object */ 370 if (!Type.exists(name)) { 371 name = this.board.generateName(this); 372 } 373 374 if (name !== "") { 375 this.board.elementsByName[name] = this; 376 } 377 378 /** 379 * Not necessarily unique name for the element. 380 * @type String 381 * @default Name generated by {@link JXG.Board#generateName}. 382 * @see JXG.Board#generateName 383 */ 384 this.name = name; 385 386 this.needsRegularUpdate = attributes.needsregularupdate; 387 388 // create this.visPropOld and set default values 389 Type.clearVisPropOld(this); 390 391 attr = this.resolveShortcuts(attributes); 392 for (key in attr) { 393 if (attr.hasOwnProperty(key)) { 394 this._set(key, attr[key]); 395 } 396 } 397 398 this.visProp.draft = attr.draft && attr.draft.draft; 399 //this.visProp.gradientangle = '270'; 400 // this.visProp.gradientsecondopacity = this.evalVisProp('fillopacity'); 401 //this.visProp.gradientpositionx = 0.5; 402 //this.visProp.gradientpositiony = 0.5; 403 } 404 }; 405 406 JXG.extend( 407 JXG.GeometryElement.prototype, 408 /** @lends JXG.GeometryElement.prototype */ { 409 /** 410 * Add an element as a child to the current element. Can be used to model dependencies between geometry elements. 411 * @param {JXG.GeometryElement} obj The dependent object. 412 */ 413 addChild: function (obj) { 414 var el, el2; 415 416 this.childElements[obj.id] = obj; 417 this.addDescendants(obj); // TODO TomBerend removed this. Check if it is possible. 418 obj.ancestors[this.id] = this; 419 420 for (el in this.descendants) { 421 if (this.descendants.hasOwnProperty(el)) { 422 this.descendants[el].ancestors[this.id] = this; 423 424 for (el2 in this.ancestors) { 425 if (this.ancestors.hasOwnProperty(el2)) { 426 this.descendants[el].ancestors[this.ancestors[el2].id] = 427 this.ancestors[el2]; 428 } 429 } 430 } 431 } 432 433 for (el in this.ancestors) { 434 if (this.ancestors.hasOwnProperty(el)) { 435 for (el2 in this.descendants) { 436 if (this.descendants.hasOwnProperty(el2)) { 437 this.ancestors[el].descendants[this.descendants[el2].id] = 438 this.descendants[el2]; 439 } 440 } 441 } 442 } 443 return this; 444 }, 445 446 /** 447 * @param {JXG.GeometryElement} obj The element that is to be added to the descendants list. 448 * @private 449 * @return this 450 */ 451 // Adds the given object to the descendants list of this object and all its child objects. 452 addDescendants: function (obj) { 453 var el; 454 455 this.descendants[obj.id] = obj; 456 for (el in obj.childElements) { 457 if (obj.childElements.hasOwnProperty(el)) { 458 this.addDescendants(obj.childElements[el]); 459 } 460 } 461 return this; 462 }, 463 464 /** 465 * Adds ids of elements to the array this.parents. This method needs to be called if some dependencies 466 * can not be detected automatically by JSXGraph. For example if a function graph is given by a function 467 * which refers to coordinates of a point, calling addParents() is necessary. 468 * 469 * @param {Array} parents Array of elements or ids of elements. 470 * Alternatively, one can give a list of objects as parameters. 471 * @returns {JXG.Object} reference to the object itself. 472 * 473 * @example 474 * // Movable function graph 475 * var A = board.create('point', [1, 0], {name:'A'}), 476 * B = board.create('point', [3, 1], {name:'B'}), 477 * f = board.create('functiongraph', function(x) { 478 * var ax = A.X(), 479 * ay = A.Y(), 480 * bx = B.X(), 481 * by = B.Y(), 482 * a = (by - ay) / ( (bx - ax) * (bx - ax) ); 483 * return a * (x - ax) * (x - ax) + ay; 484 * }, {fixed: false}); 485 * f.addParents([A, B]); 486 * </pre><div class="jxgbox" id="JXG7c91d4d2-986c-4378-8135-24505027f251" style="width: 400px; height: 400px;"></div> 487 * <script type="text/javascript"> 488 * (function() { 489 * var board = JXG.JSXGraph.initBoard('JXG7c91d4d2-986c-4378-8135-24505027f251', {boundingbox: [-1, 9, 9, -1], axis: true, showcopyright: false, shownavigation: false}); 490 * var A = board.create('point', [1, 0], {name:'A'}), 491 * B = board.create('point', [3, 1], {name:'B'}), 492 * f = board.create('functiongraph', function(x) { 493 * var ax = A.X(), 494 * ay = A.Y(), 495 * bx = B.X(), 496 * by = B.Y(), 497 * a = (by - ay) / ( (bx - ax) * (bx - ax) ); 498 * return a * (x - ax) * (x - ax) + ay; 499 * }, {fixed: false}); 500 * f.addParents([A, B]); 501 * })(); 502 * </script><pre> 503 * 504 **/ 505 addParents: function (parents) { 506 var i, len, par; 507 508 if (Type.isArray(parents)) { 509 par = parents; 510 } else { 511 par = arguments; 512 } 513 514 len = par.length; 515 for (i = 0; i < len; ++i) { 516 if (!Type.exists(par[i])) { 517 continue; 518 } 519 if (Type.isId(this.board, par[i])) { 520 this.parents.push(par[i]); 521 } else if (Type.exists(par[i].id)) { 522 this.parents.push(par[i].id); 523 } 524 } 525 this.parents = Type.uniqueArray(this.parents); 526 }, 527 528 /** 529 * Sets ids of elements to the array this.parents. 530 * First, this.parents is cleared. See {@link JXG.GeometryElement#addParents}. 531 * @param {Array} parents Array of elements or ids of elements. 532 * Alternatively, one can give a list of objects as parameters. 533 * @returns {JXG.Object} reference to the object itself. 534 **/ 535 setParents: function (parents) { 536 this.parents = []; 537 this.addParents(parents); 538 }, 539 540 /** 541 * Add dependence on elements in JessieCode functions. 542 * @param {Array} function_array Array of functions containing potential properties "deps" with 543 * elements the function depends on. 544 * @returns {JXG.Object} reference to the object itself 545 * @private 546 */ 547 addParentsFromJCFunctions: function (function_array) { 548 var i, e, obj; 549 for (i = 0; i < function_array.length; i++) { 550 for (e in function_array[i].deps) { 551 obj = function_array[i].deps[e]; 552 this.addParents(obj); 553 obj.addChild(this); 554 } 555 } 556 return this; 557 }, 558 559 /** 560 * Remove an element as a child from the current element. 561 * @param {JXG.GeometryElement} obj The dependent object. 562 * @returns {JXG.Object} reference to the object itself 563 */ 564 removeChild: function (obj) { 565 //var el, el2; 566 567 delete this.childElements[obj.id]; 568 this.removeDescendants(obj); 569 delete obj.ancestors[this.id]; 570 571 /* 572 // I do not know if these addDescendants stuff has to be adapted to removeChild. A.W. 573 for (el in this.descendants) { 574 if (this.descendants.hasOwnProperty(el)) { 575 delete this.descendants[el].ancestors[this.id]; 576 577 for (el2 in this.ancestors) { 578 if (this.ancestors.hasOwnProperty(el2)) { 579 this.descendants[el].ancestors[this.ancestors[el2].id] = this.ancestors[el2]; 580 } 581 } 582 } 583 } 584 585 for (el in this.ancestors) { 586 if (this.ancestors.hasOwnProperty(el)) { 587 for (el2 in this.descendants) { 588 if (this.descendants.hasOwnProperty(el2)) { 589 this.ancestors[el].descendants[this.descendants[el2].id] = this.descendants[el2]; 590 } 591 } 592 } 593 } 594 */ 595 return this; 596 }, 597 598 /** 599 * Removes the given object from the descendants list of this object and all its child objects. 600 * @param {JXG.GeometryElement} obj The element that is to be removed from the descendants list. 601 * @private 602 * @returns {JXG.Object} reference to the object itself 603 */ 604 removeDescendants: function (obj) { 605 var el; 606 607 delete this.descendants[obj.id]; 608 for (el in obj.childElements) { 609 if (obj.childElements.hasOwnProperty(el)) { 610 this.removeDescendants(obj.childElements[el]); 611 } 612 } 613 return this; 614 }, 615 616 /** 617 * Counts the direct children of an object without counting labels. 618 * @private 619 * @returns {number} Number of children 620 */ 621 countChildren: function () { 622 var prop, 623 d, 624 s = 0; 625 626 d = this.childElements; 627 for (prop in d) { 628 if (d.hasOwnProperty(prop) && prop.indexOf('Label') < 0) { 629 s++; 630 } 631 } 632 return s; 633 }, 634 635 /** 636 * Returns the elements name. Used in JessieCode. 637 * @returns {String} 638 */ 639 getName: function () { 640 return this.name; 641 }, 642 643 /** 644 * Add transformations to this element. 645 * @param {JXG.Transformation|Array} transform Either one {@link JXG.Transformation} 646 * or an array of {@link JXG.Transformation}s. 647 * @returns {JXG.GeometryElement} Reference to the element. 648 */ 649 addTransform: function (transform) { 650 return this; 651 }, 652 653 /** 654 * Decides whether an element can be dragged. This is used in 655 * {@link JXG.GeometryElement#setPositionDirectly} methods 656 * where all parent elements are checked if they may be dragged, too. 657 * @private 658 * @returns {boolean} 659 */ 660 draggable: function () { 661 return ( 662 this.isDraggable && 663 !this.evalVisProp('fixed') && 664 // !this.visProp.frozen && 665 this.type !== Const.OBJECT_TYPE_GLIDER 666 ); 667 }, 668 669 /** 670 * Translates the object by <tt>(x, y)</tt>. In case the element is defined by points, the defining points are 671 * translated, e.g. a circle constructed by a center point and a point on the circle line. 672 * @param {Number} method The type of coordinates used here. 673 * Possible values are {@link JXG.COORDS_BY_USER} and {@link JXG.COORDS_BY_SCREEN}. 674 * @param {Array} coords array of translation vector. 675 * @returns {JXG.GeometryElement} Reference to the element object. 676 * 677 * @see JXG.GeometryElement3D#setPosition2D 678 */ 679 setPosition: function (method, coords) { 680 var parents = [], 681 el, 682 i, len, t; 683 684 if (!Type.exists(this.parents)) { 685 return this; 686 } 687 688 len = this.parents.length; 689 for (i = 0; i < len; ++i) { 690 el = this.board.select(this.parents[i]); 691 if (Type.isPoint(el)) { 692 if (!el.draggable()) { 693 return this; 694 } 695 parents.push(el); 696 } 697 } 698 699 if (coords.length === 3) { 700 coords = coords.slice(1); 701 } 702 703 t = this.board.create("transform", coords, { type: "translate" }); 704 705 // We distinguish two cases: 706 // 1) elements which depend on free elements, i.e. arcs and sectors 707 // 2) other elements 708 // 709 // In the first case we simply transform the parents elements 710 // In the second case we add a transform to the element. 711 // 712 len = parents.length; 713 if (len > 0) { 714 t.applyOnce(parents); 715 716 // Handle dragging of a 3D element 717 if (Type.exists(this.view) && this.view.elType === 'view3d') { 718 for (i = 0; i < this.parents.length; ++i) { 719 // Search for the parent 3D element 720 el = this.view.select(this.parents[i]); 721 if (Type.exists(el.setPosition2D)) { 722 el.setPosition2D(t); 723 } 724 } 725 } 726 727 } else { 728 if ( 729 this.transformations.length > 0 && 730 this.transformations[this.transformations.length - 1].isNumericMatrix 731 ) { 732 this.transformations[this.transformations.length - 1].melt(t); 733 } else { 734 this.addTransform(t); 735 } 736 } 737 738 /* 739 * If - against the default configuration - defining gliders are marked as 740 * draggable, then their position has to be updated now. 741 */ 742 for (i = 0; i < len; ++i) { 743 if (parents[i].type === Const.OBJECT_TYPE_GLIDER) { 744 parents[i].updateGlider(); 745 } 746 } 747 748 return this; 749 }, 750 751 /** 752 * Moves an element by the difference of two coordinates. 753 * @param {Number} method The type of coordinates used here. 754 * Possible values are {@link JXG.COORDS_BY_USER} and {@link JXG.COORDS_BY_SCREEN}. 755 * @param {Array} coords coordinates in screen/user units 756 * @param {Array} oldcoords previous coordinates in screen/user units 757 * @returns {JXG.GeometryElement} this element 758 */ 759 setPositionDirectly: function (method, coords, oldcoords) { 760 var c = new Coords(method, coords, this.board, false), 761 oldc = new Coords(method, oldcoords, this.board, false), 762 dc = Statistics.subtract(c.usrCoords, oldc.usrCoords); 763 764 this.setPosition(Const.COORDS_BY_USER, dc); 765 766 return this; 767 }, 768 769 /** 770 * Array of strings containing the polynomials defining the element. 771 * Used for determining geometric loci the groebner way. 772 * @returns {Array} An array containing polynomials describing the locus of the current object. 773 * @public 774 */ 775 generatePolynomial: function () { 776 return []; 777 }, 778 779 /** 780 * Animates properties for that object like stroke or fill color, opacity and maybe 781 * even more later. 782 * @param {Object} hash Object containing properties with target values for the animation. 783 * @param {number} time Number of milliseconds to complete the animation. 784 * @param {Object} [options] Optional settings for the animation:<ul><li>callback: A function that is called as soon as the animation is finished.</li></ul> 785 * @returns {JXG.GeometryElement} A reference to the object 786 */ 787 animate: function (hash, time, options) { 788 options = options || {}; 789 var r, 790 p, 791 i, 792 delay = this.board.attr.animationdelay, 793 steps = Math.ceil(time / delay), 794 self = this, 795 animateColor = function (startRGB, endRGB, property) { 796 var hsv1, hsv2, sh, ss, sv; 797 hsv1 = Color.rgb2hsv(startRGB); 798 hsv2 = Color.rgb2hsv(endRGB); 799 800 sh = (hsv2[0] - hsv1[0]) / steps; 801 ss = (hsv2[1] - hsv1[1]) / steps; 802 sv = (hsv2[2] - hsv1[2]) / steps; 803 self.animationData[property] = []; 804 805 for (i = 0; i < steps; i++) { 806 self.animationData[property][steps - i - 1] = Color.hsv2rgb( 807 hsv1[0] + (i + 1) * sh, 808 hsv1[1] + (i + 1) * ss, 809 hsv1[2] + (i + 1) * sv 810 ); 811 } 812 }, 813 animateFloat = function (start, end, property, round) { 814 var tmp, s; 815 816 start = parseFloat(start); 817 end = parseFloat(end); 818 819 // we can't animate without having valid numbers. 820 // And parseFloat returns NaN if the given string doesn't contain 821 // a valid float number. 822 if (isNaN(start) || isNaN(end)) { 823 return; 824 } 825 826 s = (end - start) / steps; 827 self.animationData[property] = []; 828 829 for (i = 0; i < steps; i++) { 830 tmp = start + (i + 1) * s; 831 self.animationData[property][steps - i - 1] = round 832 ? Math.floor(tmp) 833 : tmp; 834 } 835 }; 836 837 this.animationData = {}; 838 839 for (r in hash) { 840 if (hash.hasOwnProperty(r)) { 841 p = r.toLowerCase(); 842 843 switch (p) { 844 case "strokecolor": 845 case "fillcolor": 846 animateColor(this.visProp[p], hash[r], p); 847 break; 848 case "size": 849 if (!Type.isPoint(this)) { 850 break; 851 } 852 animateFloat(this.visProp[p], hash[r], p, true); 853 break; 854 case "strokeopacity": 855 case "strokewidth": 856 case "fillopacity": 857 animateFloat(this.visProp[p], hash[r], p, false); 858 break; 859 } 860 } 861 } 862 863 this.animationCallback = options.callback; 864 this.board.addAnimation(this); 865 return this; 866 }, 867 868 /** 869 * General update method. Should be overwritten by the element itself. 870 * Can be used sometimes to commit changes to the object. 871 * @return {JXG.GeometryElement} Reference to the element 872 */ 873 update: function () { 874 if (this.evalVisProp('trace')) { 875 this.cloneToBackground(); 876 } 877 return this; 878 }, 879 880 /** 881 * Provide updateRenderer method. 882 * @return {JXG.GeometryElement} Reference to the element 883 * @private 884 */ 885 updateRenderer: function () { 886 return this; 887 }, 888 889 /** 890 * Run through the full update chain of an element. 891 * @param {Boolean} visible Set visibility in case the elements attribute value is 'inherit'. null is allowed. 892 * @return {JXG.GeometryElement} Reference to the element 893 * @private 894 */ 895 fullUpdate: function (visible) { 896 return this.prepareUpdate().update().updateVisibility(visible).updateRenderer(); 897 }, 898 899 /** 900 * Show the element or hide it. If hidden, it will still exist but not be 901 * visible on the board. 902 * <p> 903 * Sets also the display of the inherits elements. These can be 904 * JSXGraph elements or arrays of JSXGraph elements. 905 * However, deeper nesting than this is not supported. 906 * 907 * @param {Boolean} val true: show the element, false: hide the element 908 * @return {JXG.GeometryElement} Reference to the element 909 * @private 910 */ 911 setDisplayRendNode: function (val) { 912 var i, len, s, len_s, obj; 913 914 if (val === undefined) { 915 val = this.visPropCalc.visible; 916 } 917 918 if (val === this.visPropOld.visible) { 919 return this; 920 } 921 922 // Set display of the element itself 923 this.board.renderer.display(this, val); 924 925 // Set the visibility of elements which inherit the attribute 'visible' 926 len = this.inherits.length; 927 for (s = 0; s < len; s++) { 928 obj = this.inherits[s]; 929 if (Type.isArray(obj)) { 930 len_s = obj.length; 931 for (i = 0; i < len_s; i++) { 932 if ( 933 Type.exists(obj[i]) && 934 Type.exists(obj[i].rendNode) && 935 obj[i].evalVisProp('visible') === 'inherit' 936 ) { 937 obj[i].setDisplayRendNode(val); 938 } 939 } 940 } else { 941 if ( 942 Type.exists(obj) && 943 Type.exists(obj.rendNode) && 944 obj.evalVisProp('visible') === 'inherit' 945 ) { 946 obj.setDisplayRendNode(val); 947 } 948 } 949 } 950 951 // Set the visibility of the label if it inherits the attribute 'visible' 952 if (this.hasLabel && Type.exists(this.label) && Type.exists(this.label.rendNode)) { 953 if (this.label.evalVisProp('visible') === 'inherit') { 954 this.label.setDisplayRendNode(val); 955 } 956 } 957 958 return this; 959 }, 960 961 /** 962 * Hide the element. It will still exist but not be visible on the board. 963 * Alias for "element.setAttribute({visible: false});" 964 * @return {JXG.GeometryElement} Reference to the element 965 */ 966 hide: function () { 967 this.setAttribute({ visible: false }); 968 return this; 969 }, 970 971 /** 972 * Hide the element. It will still exist but not be visible on the board. 973 * Alias for {@link JXG.GeometryElement#hide} 974 * @returns {JXG.GeometryElement} Reference to the element 975 */ 976 hideElement: function () { 977 this.hide(); 978 return this; 979 }, 980 981 /** 982 * Make the element visible. 983 * Alias for "element.setAttribute({visible: true});" 984 * @return {JXG.GeometryElement} Reference to the element 985 */ 986 show: function () { 987 this.setAttribute({ visible: true }); 988 return this; 989 }, 990 991 /** 992 * Make the element visible. 993 * Alias for {@link JXG.GeometryElement#show} 994 * @returns {JXG.GeometryElement} Reference to the element 995 */ 996 showElement: function () { 997 this.show(); 998 return this; 999 }, 1000 1001 /** 1002 * Set the visibility of an element. The visibility is influenced by 1003 * (listed in ascending priority): 1004 * <ol> 1005 * <li> The value of the element's attribute 'visible' 1006 * <li> The visibility of a parent element. (Example: label) 1007 * This overrules the value of the element's attribute value only if 1008 * this attribute value of the element is 'inherit'. 1009 * <li> being inside of the canvas 1010 * </ol> 1011 * <p> 1012 * This method is called three times for most elements: 1013 * <ol> 1014 * <li> between {@link JXG.GeometryElement#update} 1015 * and {@link JXG.GeometryElement#updateRenderer}. In case the value is 'inherit', nothing is done. 1016 * <li> Recursively, called by itself for child elements. Here, 'inherit' is overruled by the parent's value. 1017 * <li> In {@link JXG.GeometryElement#updateRenderer}, if the element is outside of the canvas. 1018 * </ol> 1019 * 1020 * @param {Boolean} parent_val Visibility of the parent element. 1021 * @return {JXG.GeometryElement} Reference to the element. 1022 * @private 1023 */ 1024 updateVisibility: function (parent_val) { 1025 var i, len, s, len_s, obj, val; 1026 1027 if (this.needsUpdate) { 1028 if (Type.exists(this.view) && this.view.evalVisProp('visible') === false) { 1029 // Handle hiding of view3d 1030 this.visPropCalc.visible = false; 1031 1032 } else { 1033 // Handle the element 1034 if (parent_val !== undefined) { 1035 this.visPropCalc.visible = parent_val; 1036 } else { 1037 val = this.evalVisProp('visible'); 1038 1039 // infobox uses hiddenByParent 1040 if (Type.exists(this.hiddenByParent) && this.hiddenByParent) { 1041 val = false; 1042 } 1043 if (val !== 'inherit') { 1044 this.visPropCalc.visible = val; 1045 } 1046 } 1047 1048 // Handle elements which inherit the visibility 1049 len = this.inherits.length; 1050 for (s = 0; s < len; s++) { 1051 obj = this.inherits[s]; 1052 if (Type.isArray(obj)) { 1053 len_s = obj.length; 1054 for (i = 0; i < len_s; i++) { 1055 if ( 1056 Type.exists(obj[i]) /*&& Type.exists(obj[i].rendNode)*/ && 1057 obj[i].evalVisProp('visible') === 'inherit' 1058 ) { 1059 obj[i] 1060 .prepareUpdate() 1061 .updateVisibility(this.visPropCalc.visible); 1062 } 1063 } 1064 } else { 1065 if ( 1066 Type.exists(obj) /*&& Type.exists(obj.rendNode)*/ && 1067 obj.evalVisProp('visible') === 'inherit' 1068 ) { 1069 obj.prepareUpdate().updateVisibility(this.visPropCalc.visible); 1070 } 1071 } 1072 } 1073 } 1074 1075 // Handle the label if it inherits the visibility 1076 if ( 1077 Type.exists(this.label) && 1078 Type.exists(this.label.visProp) && 1079 this.label.evalVisProp('visible') 1080 ) { 1081 this.label.prepareUpdate().updateVisibility(this.visPropCalc.visible); 1082 } 1083 } 1084 return this; 1085 }, 1086 1087 /** 1088 * Sets the value of attribute <tt>key</tt> to <tt>value</tt>. 1089 * Here, mainly hex strings for rga(a) colors are parsed and values of type object get a special treatment. 1090 * Other values are just set to the key. 1091 * 1092 * @param {String} key The attribute's name. 1093 * @param value The new value 1094 * @private 1095 */ 1096 _set: function (key, value) { 1097 var el; 1098 1099 key = key.toLocaleLowerCase(); 1100 1101 // Search for entries in visProp with "color" as part of the key name 1102 // and containing a RGBA string 1103 if ( 1104 this.visProp.hasOwnProperty(key) && 1105 key.indexOf('color') >= 0 && 1106 Type.isString(value) && 1107 value.length === 9 && 1108 value.charAt(0) === "#" 1109 ) { 1110 value = Color.rgba2rgbo(value); 1111 this.visProp[key] = value[0]; 1112 // Previously: *=. But then, we can only decrease opacity. 1113 this.visProp[key.replace("color", 'opacity')] = value[1]; 1114 } else { 1115 if ( 1116 value !== null && 1117 Type.isObject(value) && 1118 !Type.exists(value.id) && 1119 !Type.exists(value.name) 1120 ) { 1121 // value is of type {prop: val, prop: val,...} 1122 // Convert these attributes to lowercase, too 1123 this.visProp[key] = {}; 1124 for (el in value) { 1125 if (value.hasOwnProperty(el)) { 1126 this.visProp[key][el.toLocaleLowerCase()] = value[el]; 1127 } 1128 } 1129 } else { 1130 this.visProp[key] = value; 1131 } 1132 } 1133 }, 1134 1135 /** 1136 * Resolves attribute shortcuts like <tt>color</tt> and expands them, e.g. <tt>strokeColor</tt> and <tt>fillColor</tt>. 1137 * Writes the expanded attributes back to the given <tt>attributes</tt>. 1138 * @param {Object} attributes object 1139 * @returns {Object} The given attributes object with shortcuts expanded. 1140 * @private 1141 */ 1142 resolveShortcuts: function (attributes) { 1143 var key, i, j, 1144 subattr = ["traceattributes", "traceAttributes"]; 1145 1146 for (key in Options.shortcuts) { 1147 if (Options.shortcuts.hasOwnProperty(key)) { 1148 if (Type.exists(attributes[key])) { 1149 for (i = 0; i < Options.shortcuts[key].length; i++) { 1150 if (!Type.exists(attributes[Options.shortcuts[key][i]])) { 1151 attributes[Options.shortcuts[key][i]] = attributes[key]; 1152 } 1153 } 1154 } 1155 for (j = 0; j < subattr.length; j++) { 1156 if (Type.isObject(attributes[subattr[j]])) { 1157 attributes[subattr[j]] = this.resolveShortcuts(attributes[subattr[j]]); 1158 } 1159 } 1160 } 1161 } 1162 return attributes; 1163 }, 1164 1165 /** 1166 * Sets a label and its text 1167 * If label doesn't exist, it creates one 1168 * @param {String} str 1169 */ 1170 setLabel: function (str) { 1171 if (!this.hasLabel) { 1172 this.setAttribute({ withlabel: true }); 1173 } 1174 this.setLabelText(str); 1175 }, 1176 1177 /** 1178 * Updates the element's label text, strips all html. 1179 * @param {String} str 1180 */ 1181 setLabelText: function (str) { 1182 if (Type.exists(this.label)) { 1183 str = str.replace(/</g, "<").replace(/>/g, ">"); 1184 this.label.setText(str); 1185 } 1186 1187 return this; 1188 }, 1189 1190 /** 1191 * Updates the element's label text and the element's attribute "name", strips all html. 1192 * @param {String} str 1193 */ 1194 setName: function (str) { 1195 str = str.replace(/</g, "<").replace(/>/g, ">"); 1196 if (this.elType !== 'slider') { 1197 this.setLabelText(str); 1198 } 1199 this.setAttribute({ name: str }); 1200 }, 1201 1202 /** 1203 * Deprecated alias for {@link JXG.GeometryElement#setAttribute}. 1204 * @deprecated Use {@link JXG.GeometryElement#setAttribute}. 1205 */ 1206 setProperty: function () { 1207 JXG.deprecated("setProperty()", "setAttribute()"); 1208 this.setAttribute.apply(this, arguments); 1209 }, 1210 1211 /** 1212 * Sets an arbitrary number of attributes. This method has one or more 1213 * parameters of the following types: 1214 * <ul> 1215 * <li> object: {key1:value1,key2:value2,...} 1216 * <li> string: 'key:value' 1217 * <li> array: ['key', value] 1218 * </ul> 1219 * @param {Object} attributes An object with attributes. 1220 * @returns {JXG.GeometryElement} A reference to the element. 1221 * 1222 * @function 1223 * @example 1224 * // Set attribute directly on creation of an element using the attributes object parameter 1225 * var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 5, 5, 1]}; 1226 * var p = board.create('point', [2, 2], {visible: false}); 1227 * 1228 * // Now make this point visible and fixed: 1229 * p.setAttribute({ 1230 * fixed: true, 1231 * visible: true 1232 * }); 1233 */ 1234 setAttribute: function (attr) { 1235 var i, j, le, key, value, arg, 1236 opacity, pair, oldvalue, 1237 attributes = {}; 1238 1239 // Normalize the user input 1240 for (i = 0; i < arguments.length; i++) { 1241 arg = arguments[i]; 1242 if (Type.isString(arg)) { 1243 // pairRaw is string of the form 'key:value' 1244 pair = arg.split(":"); 1245 attributes[Type.trim(pair[0])] = Type.trim(pair[1]); 1246 } else if (!Type.isArray(arg)) { 1247 // pairRaw consists of objects of the form {key1:value1,key2:value2,...} 1248 JXG.extend(attributes, arg); 1249 } else { 1250 // pairRaw consists of array [key,value] 1251 attributes[arg[0]] = arg[1]; 1252 } 1253 } 1254 1255 // Handle shortcuts 1256 attributes = this.resolveShortcuts(attributes); 1257 1258 for (i in attributes) { 1259 if (attributes.hasOwnProperty(i)) { 1260 key = i.replace(/\s+/g, "").toLowerCase(); 1261 value = attributes[i]; 1262 1263 // This handles the subobjects, if the key:value pairs are contained in an object. 1264 // Example: 1265 // ticks.setAttribute({ 1266 // strokeColor: 'blue', 1267 // label: { 1268 // visible: false 1269 // } 1270 // }) 1271 // Now, only the supplied label attributes are overwritten. 1272 // Otherwise, the value of label would be {visible:false} only. 1273 if (Type.isObject(value) && Type.exists(this.visProp[key])) { 1274 // this.visProp[key] = Type.merge(this.visProp[key], value); 1275 if (!Type.isObject(this.visProp[key]) && value !== null && Type.isObject(value)) { 1276 // Handle cases like key=firstarrow and 1277 // firstarrow==false and value = { type:1 }. 1278 // That is a primitive type is replaced by an object. 1279 this.visProp[key] = {}; 1280 } 1281 Type.mergeAttr(this.visProp[key], value); 1282 1283 // First, handle the special case 1284 // ticks.setAttribute({label: {anchorX: "right", ..., visible: true}); 1285 if (this.type === Const.OBJECT_TYPE_TICKS && Type.exists(this.labels)) { 1286 le = this.labels.length; 1287 for (j = 0; j < le; j++) { 1288 this.labels[j].setAttribute(value); 1289 } 1290 } else if (Type.exists(this[key])) { 1291 // Attribute looks like: point1: {...} 1292 // Handle this in the sub-element: this.point1.setAttribute({...}) 1293 if (Type.isArray(this[key])) { 1294 for (j = 0; j < this[key].length; j++) { 1295 this[key][j].setAttribute(value); 1296 } 1297 } else { 1298 this[key].setAttribute(value); 1299 } 1300 } else { 1301 // Cases like firstarrow: {...} 1302 oldvalue = null; 1303 this.triggerEventHandlers(["attribute:" + key], [oldvalue, value, this]); 1304 } 1305 continue; 1306 } 1307 1308 oldvalue = this.visProp[key]; 1309 switch (key) { 1310 case "checked": 1311 // checkbox Is not available on initial call. 1312 if (Type.exists(this.rendNodeTag)) { 1313 this.rendNodeCheckbox.checked = !!value; 1314 } 1315 break; 1316 case "disabled": 1317 // button, checkbox, input. Is not available on initial call. 1318 if (Type.exists(this.rendNodeTag)) { 1319 this.rendNodeTag.disabled = !!value; 1320 } 1321 break; 1322 case "face": 1323 if (Type.isPoint(this)) { 1324 this.visProp.face = value; 1325 this.board.renderer.changePointStyle(this); 1326 } 1327 break; 1328 case "generatelabelvalue": 1329 if ( 1330 this.type === Const.OBJECT_TYPE_TICKS && 1331 Type.isFunction(value) 1332 ) { 1333 this.generateLabelValue = value; 1334 } 1335 break; 1336 case "gradient": 1337 this.visProp.gradient = value; 1338 this.board.renderer.setGradient(this); 1339 break; 1340 case "gradientsecondcolor": 1341 value = Color.rgba2rgbo(value); 1342 this.visProp.gradientsecondcolor = value[0]; 1343 this.visProp.gradientsecondopacity = value[1]; 1344 this.board.renderer.updateGradient(this); 1345 break; 1346 case "gradientsecondopacity": 1347 this.visProp.gradientsecondopacity = value; 1348 this.board.renderer.updateGradient(this); 1349 break; 1350 case "infoboxtext": 1351 if (Type.isString(value)) { 1352 this.infoboxText = value; 1353 } else { 1354 this.infoboxText = false; 1355 } 1356 break; 1357 case "labelcolor": 1358 value = Color.rgba2rgbo(value); 1359 opacity = value[1]; 1360 value = value[0]; 1361 if (opacity === 0) { 1362 if (Type.exists(this.label) && this.hasLabel) { 1363 this.label.hideElement(); 1364 } 1365 } 1366 if (Type.exists(this.label) && this.hasLabel) { 1367 this.label.visProp.strokecolor = value; 1368 this.board.renderer.setObjectStrokeColor( 1369 this.label, 1370 value, 1371 opacity 1372 ); 1373 } 1374 if (this.elementClass === Const.OBJECT_CLASS_TEXT) { 1375 this.visProp.strokecolor = value; 1376 this.visProp.strokeopacity = opacity; 1377 this.board.renderer.setObjectStrokeColor(this, value, opacity); 1378 } 1379 break; 1380 case "layer": 1381 this.board.renderer.setLayer(this, this.eval(value)); 1382 this._set(key, value); 1383 break; 1384 case "maxlength": 1385 // input. Is not available on initial call. 1386 if (Type.exists(this.rendNodeTag)) { 1387 this.rendNodeTag.maxlength = !!value; 1388 } 1389 break; 1390 case "name": 1391 oldvalue = this.name; 1392 delete this.board.elementsByName[this.name]; 1393 this.name = value; 1394 this.board.elementsByName[this.name] = this; 1395 break; 1396 case "needsregularupdate": 1397 this.needsRegularUpdate = !(value === "false" || value === false); 1398 this.board.renderer.setBuffering( 1399 this, 1400 this.needsRegularUpdate ? "auto" : "static" 1401 ); 1402 break; 1403 case "onpolygon": 1404 if (this.type === Const.OBJECT_TYPE_GLIDER) { 1405 this.onPolygon = !!value; 1406 } 1407 break; 1408 case "radius": 1409 if ( 1410 this.type === Const.OBJECT_TYPE_ANGLE || 1411 this.type === Const.OBJECT_TYPE_SECTOR 1412 ) { 1413 this.setRadius(value); 1414 } 1415 break; 1416 case "rotate": 1417 if ( 1418 (this.elementClass === Const.OBJECT_CLASS_TEXT && 1419 this.evalVisProp('display') === 'internal') || 1420 this.type === Const.OBJECT_TYPE_IMAGE 1421 ) { 1422 this.addRotation(value); 1423 } 1424 break; 1425 case "tabindex": 1426 if (Type.exists(this.rendNode)) { 1427 this.rendNode.setAttribute("tabindex", value); 1428 this._set(key, value); 1429 } 1430 break; 1431 // case "ticksdistance": 1432 // if (this.type === Const.OBJECT_TYPE_TICKS && Type.isNumber(value)) { 1433 // this.ticksFunction = this.makeTicksFunction(value); 1434 // } 1435 // break; 1436 case "trace": 1437 if (value === "false" || value === false) { 1438 this.clearTrace(); 1439 this.visProp.trace = false; 1440 } else if (value === 'pause') { 1441 this.visProp.trace = false; 1442 } else { 1443 this.visProp.trace = true; 1444 } 1445 break; 1446 case "visible": 1447 if (value === 'false') { 1448 this.visProp.visible = false; 1449 } else if (value === 'true') { 1450 this.visProp.visible = true; 1451 } else { 1452 this.visProp.visible = value; 1453 } 1454 1455 this.setDisplayRendNode(this.evalVisProp('visible')); 1456 if ( 1457 this.evalVisProp('visible') && 1458 Type.exists(this.updateSize) 1459 ) { 1460 this.updateSize(); 1461 } 1462 1463 break; 1464 case "withlabel": 1465 this.visProp.withlabel = value; 1466 if (!this.evalVisProp('withlabel')) { 1467 if (this.label && this.hasLabel) { 1468 //this.label.hideElement(); 1469 this.label.setAttribute({ visible: false }); 1470 } 1471 } else { 1472 if (!this.label) { 1473 this.createLabel(); 1474 } 1475 //this.label.showElement(); 1476 this.label.setAttribute({ visible: 'inherit' }); 1477 //this.label.setDisplayRendNode(this.evalVisProp('visible')); 1478 } 1479 this.hasLabel = value; 1480 break; 1481 case "straightfirst": 1482 case "straightlast": 1483 this._set(key, value); 1484 for (j in this.childElements) { 1485 if (this.childElements.hasOwnProperty(j) && this.childElements[j].elType === 'glider') { 1486 this.childElements[j].fullUpdate(); 1487 } 1488 } 1489 break; 1490 default: 1491 if (Type.exists(this.visProp[key]) && 1492 (!JXG.Validator[key] || // No validator for this key => OK 1493 (JXG.Validator[key] && JXG.Validator[key](value)) || // Value passes the validator => OK 1494 (JXG.Validator[key] && // Value is function, function value passes the validator => OK 1495 Type.isFunction(value) && JXG.Validator[key](value(this)) 1496 ) 1497 ) 1498 ) { 1499 value = (value.toLowerCase && value.toLowerCase() === 'false') 1500 ? false 1501 : value; 1502 this._set(key, value); 1503 } else { 1504 if (!(key in Options.shortcuts)) { 1505 JXG.warn("attribute '" + key + "' does not accept type '" + (typeof value) + "' of value " + value + '.'); 1506 } 1507 } 1508 break; 1509 } 1510 this.triggerEventHandlers(["attribute:" + key], [oldvalue, value, this]); 1511 } 1512 } 1513 1514 this.triggerEventHandlers(["attribute"], [attributes, this]); 1515 1516 if (!this.evalVisProp('needsregularupdate')) { 1517 this.board.fullUpdate(); 1518 } else { 1519 this.board.update(this); 1520 } 1521 if (this.elementClass === Const.OBJECT_CLASS_TEXT) { 1522 this.updateSize(); 1523 } 1524 1525 return this; 1526 }, 1527 1528 /** 1529 * Deprecated alias for {@link JXG.GeometryElement#getAttribute}. 1530 * @deprecated Use {@link JXG.GeometryElement#getAttribute}. 1531 */ 1532 getProperty: function () { 1533 JXG.deprecated("getProperty()", "getAttribute()"); 1534 this.getProperty.apply(this, arguments); 1535 }, 1536 1537 /** 1538 * Get the value of the property <tt>key</tt>. 1539 * @param {String} key The name of the property you are looking for 1540 * @returns The value of the property 1541 */ 1542 getAttribute: function (key) { 1543 var result; 1544 key = key.toLowerCase(); 1545 1546 switch (key) { 1547 case "needsregularupdate": 1548 result = this.needsRegularUpdate; 1549 break; 1550 case "labelcolor": 1551 result = this.label.visProp.strokecolor; 1552 break; 1553 case "infoboxtext": 1554 result = this.infoboxText; 1555 break; 1556 case "withlabel": 1557 result = this.hasLabel; 1558 break; 1559 default: 1560 result = this.visProp[key]; 1561 break; 1562 } 1563 1564 return result; 1565 }, 1566 1567 /** 1568 * Get value of an attribute. If the value that attribute is a function, call the function and return its value. 1569 * In that case, the function is called with the GeometryElement as (only) parameter. For label elements (i.e. 1570 * if the attribute "islabel" is true), the anchor element is supplied. The label element can be accessed as 1571 * sub-object "label". 1572 * If the attribute does not exist, undefined will be returned. 1573 * 1574 * @param {String} key Attribute key 1575 * @returns {String|Number|Boolean} value of attribute "key" (evaluated in case of a function) or undefined 1576 * 1577 * @see GeometryElement#eval 1578 * @see JXG#evaluate 1579 */ 1580 evalVisProp: function (key) { 1581 var val, arr, i, le, 1582 e, o, found; 1583 1584 key = key.toLowerCase(); 1585 if (key.indexOf('.') === -1) { 1586 // e.g. 'visible' 1587 val = this.visProp[key]; 1588 } else { 1589 // e.g. label.visible 1590 arr = key.split('.'); 1591 le = arr.length; 1592 val = this.visProp; 1593 for (i = 0; i < le; i++) { 1594 if (Type.exists(val)) { 1595 val = val[arr[i]]; 1596 } 1597 } 1598 } 1599 1600 if (JXG.isFunction(val)) { 1601 // For labels supply the anchor element as parameter. 1602 if (this.visProp.islabel === true && Type.exists(this.visProp.anchor)) { 1603 // 3D: supply the 3D element 1604 if (this.visProp.anchor.visProp.element3d !== null) { 1605 return val(this.visProp.anchor.visProp.element3d); 1606 } 1607 // 2D: supply the 2D element 1608 return val(this.visProp.anchor); 1609 } 1610 // For 2D elements representing 3D elements, return the 3D element. 1611 if (JXG.exists(this.visProp.element3d)) { 1612 return val(this.visProp.element3d); 1613 } 1614 // In all other cases, return the element itself 1615 return val(this); 1616 } 1617 // val is not of type function 1618 1619 if (val === 'inherit') { 1620 for (e in this.descendants) { 1621 if (this.descendants.hasOwnProperty(e)) { 1622 o = this.descendants[e]; 1623 // Check if this is in inherits of one of its descendant 1624 found = false; 1625 le = o.inherits.length; 1626 for (i = 0; i < le; i++) { 1627 if (this.id === o.inherits[i].id) { 1628 found = true; 1629 break; 1630 } 1631 } 1632 if (found) { 1633 val = o.evalVisProp(key); 1634 break; 1635 } 1636 } 1637 } 1638 } 1639 1640 return val; 1641 }, 1642 1643 /** 1644 * Get value of a parameter. If the parameter is a function, call the function and return its value. 1645 * In that case, the function is called with the GeometryElement as (only) parameter. For label elements (i.e. 1646 * if the attribute "islabel" is true), the anchor element is supplied. The label of an element can be accessed as 1647 * sub-object "label" then. 1648 * 1649 * @param {String|Number|Function|Object} val If not a function, it will be returned as is. If function it will be evaluated, where the GeometryElement is 1650 * supplied as the (only) parameter of that function. 1651 * @returns {String|Number|Object} 1652 * 1653 * @see GeometryElement#evalVisProp 1654 * @see JXG#evaluate 1655 */ 1656 eval: function(val) { 1657 if (JXG.isFunction(val)) { 1658 // For labels supply the anchor element as parameter. 1659 if (this.visProp.islabel === true && Type.exists(this.visProp.anchor)) { 1660 // 3D: supply the 3D element 1661 if (this.visProp.anchor.visProp.element3d !== null) { 1662 return val(this.visProp.anchor.visProp.element3d); 1663 } 1664 // 2D: supply the 2D element 1665 return val(this.visProp.anchor); 1666 } 1667 // For 2D elements representing 3D elements, return the 3D element. 1668 if (this.visProp.element3d !== null) { 1669 return val(this.visProp.element3d); 1670 } 1671 // In all other cases, return the element itself 1672 return val(this); 1673 } 1674 // val is not of type function 1675 return val; 1676 }, 1677 1678 /** 1679 * Set the dash style of an object. See {@link JXG.GeometryElement#dash} 1680 * for a list of available dash styles. 1681 * You should use {@link JXG.GeometryElement#setAttribute} instead of this method. 1682 * 1683 * @param {number} dash Indicates the new dash style 1684 * @private 1685 */ 1686 setDash: function (dash) { 1687 this.setAttribute({ dash: dash }); 1688 return this; 1689 }, 1690 1691 /** 1692 * Notify all child elements for updates. 1693 * @private 1694 */ 1695 prepareUpdate: function () { 1696 this.needsUpdate = true; 1697 return this; 1698 }, 1699 1700 /** 1701 * Removes the element from the construction. This only removes the SVG or VML node of the element and its label (if available) from 1702 * the renderer, to remove the element completely you should use {@link JXG.Board#removeObject}. 1703 */ 1704 remove: function () { 1705 // this.board.renderer.remove(this.board.renderer.getElementById(this.id)); 1706 this.board.renderer.remove(this.rendNode); 1707 1708 if (this.hasLabel) { 1709 this.board.renderer.remove(this.board.renderer.getElementById(this.label.id)); 1710 } 1711 return this; 1712 }, 1713 1714 /** 1715 * Returns the coords object where a text that is bound to the element shall be drawn. 1716 * Differs in some cases from the values that getLabelAnchor returns. 1717 * @returns {JXG.Coords} JXG.Coords Place where the text shall be drawn. 1718 * @see JXG.GeometryElement#getLabelAnchor 1719 */ 1720 getTextAnchor: function () { 1721 return new Coords(Const.COORDS_BY_USER, [0, 0], this.board); 1722 }, 1723 1724 /** 1725 * Returns the coords object where the label of the element shall be drawn. 1726 * Differs in some cases from the values that getTextAnchor returns. 1727 * @returns {JXG.Coords} JXG.Coords Place where the text shall be drawn. 1728 * @see JXG.GeometryElement#getTextAnchor 1729 */ 1730 getLabelAnchor: function () { 1731 return new Coords(Const.COORDS_BY_USER, [0, 0], this.board); 1732 }, 1733 1734 /** 1735 * Determines whether the element has arrows at start or end of the arc. 1736 * If it is set to be a "typical" vector, ie lastArrow == true, 1737 * then the element.type is set to VECTOR. 1738 * @param {Boolean} firstArrow True if there is an arrow at the start of the arc, false otherwise. 1739 * @param {Boolean} lastArrow True if there is an arrow at the end of the arc, false otherwise. 1740 */ 1741 setArrow: function (firstArrow, lastArrow) { 1742 this.visProp.firstarrow = firstArrow; 1743 this.visProp.lastarrow = lastArrow; 1744 if (lastArrow) { 1745 this.type = Const.OBJECT_TYPE_VECTOR; 1746 this.elType = 'arrow'; 1747 } 1748 1749 this.prepareUpdate().update().updateVisibility().updateRenderer(); 1750 return this; 1751 }, 1752 1753 /** 1754 * Creates a gradient nodes in the renderer. 1755 * @see JXG.SVGRenderer#setGradient 1756 * @private 1757 */ 1758 createGradient: function () { 1759 var ev_g = this.evalVisProp('gradient'); 1760 if (ev_g === "linear" || ev_g === 'radial') { 1761 this.board.renderer.setGradient(this); 1762 } 1763 }, 1764 1765 /** 1766 * Creates a label element for this geometry element. 1767 * @see JXG.GeometryElement#addLabelToElement 1768 */ 1769 createLabel: function () { 1770 var attr, 1771 that = this; 1772 1773 // this is a dirty hack to resolve the text-dependency. If there is no text element available, 1774 // just don't create a label. This method is usually not called by a user, so we won't throw 1775 // an exception here and simply output a warning via JXG.debug. 1776 if (JXG.elements.text) { 1777 attr = Type.deepCopy(this.visProp.label, null); 1778 attr.id = this.id + 'Label'; 1779 attr.isLabel = true; 1780 attr.anchor = this; 1781 attr.priv = this.visProp.priv; 1782 1783 if (this.visProp.withlabel) { 1784 this.label = JXG.elements.text( 1785 this.board, 1786 [ 1787 0, 1788 0, 1789 function () { 1790 if (Type.isFunction(that.name)) { 1791 return that.name(that); 1792 } 1793 return that.name; 1794 } 1795 ], 1796 attr 1797 ); 1798 this.label.needsUpdate = true; 1799 this.label.dump = false; 1800 this.label.fullUpdate(); 1801 1802 this.hasLabel = true; 1803 } 1804 } else { 1805 JXG.debug( 1806 "JSXGraph: Can't create label: text element is not available. Make sure you include base/text" 1807 ); 1808 } 1809 1810 return this; 1811 }, 1812 1813 /** 1814 * Highlights the element. 1815 * @private 1816 * @param {Boolean} [force=false] Force the highlighting 1817 * @returns {JXG.Board} 1818 */ 1819 highlight: function (force) { 1820 force = Type.def(force, false); 1821 // I know, we have the JXG.Board.highlightedObjects AND JXG.GeometryElement.highlighted and YES we need both. 1822 // Board.highlightedObjects is for the internal highlighting and GeometryElement.highlighted is for user highlighting 1823 // initiated by the user, e.g. through custom DOM events. We can't just pick one because this would break user 1824 // defined highlighting in many ways: 1825 // * if overriding the highlight() methods the user had to handle the highlightedObjects stuff, otherwise he'd break 1826 // everything (e.g. the pie chart example https://jsxgraph.org/wiki/index.php/Pie_chart (not exactly 1827 // user defined but for this type of chart the highlight method was overridden and not adjusted to the changes in here) 1828 // where it just kept highlighting until the radius of the pie was far beyond infinity... 1829 // * user defined highlighting would get pointless, everytime the user highlights something using .highlight(), it would get 1830 // dehighlighted immediately, because highlight puts the element into highlightedObjects and from there it gets dehighlighted 1831 // through dehighlightAll. 1832 1833 // highlight only if not highlighted 1834 if (this.evalVisProp('highlight') && (!this.highlighted || force)) { 1835 this.highlighted = true; 1836 this.board.highlightedObjects[this.id] = this; 1837 this.board.renderer.highlight(this); 1838 } 1839 return this; 1840 }, 1841 1842 /** 1843 * Uses the "normal" properties of the element. 1844 * @returns {JXG.Board} 1845 */ 1846 noHighlight: function () { 1847 // see comment in JXG.GeometryElement.highlight() 1848 1849 // dehighlight only if not highlighted 1850 if (this.highlighted) { 1851 this.highlighted = false; 1852 delete this.board.highlightedObjects[this.id]; 1853 this.board.renderer.noHighlight(this); 1854 } 1855 return this; 1856 }, 1857 1858 /** 1859 * Removes all objects generated by the trace function. 1860 */ 1861 clearTrace: function () { 1862 var obj; 1863 1864 for (obj in this.traces) { 1865 if (this.traces.hasOwnProperty(obj)) { 1866 this.board.renderer.remove(this.traces[obj]); 1867 } 1868 } 1869 1870 this.numTraces = 0; 1871 return this; 1872 }, 1873 1874 /** 1875 * Copy the element to background. This is used for tracing elements. 1876 * @returns {JXG.GeometryElement} A reference to the element 1877 */ 1878 cloneToBackground: function () { 1879 return this; 1880 }, 1881 1882 /** 1883 * Dimensions of the smallest rectangle enclosing the element. 1884 * @returns {Array} The coordinates of the enclosing rectangle in a format 1885 * like the bounding box in {@link JXG.Board#setBoundingBox}. 1886 * 1887 * @returns {Array} similar to {@link JXG.Board#setBoundingBox}. 1888 */ 1889 bounds: function () { 1890 return [0, 0, 0, 0]; 1891 }, 1892 1893 /** 1894 * Normalize the element's standard form. 1895 * @private 1896 */ 1897 normalize: function () { 1898 this.stdform = Mat.normalize(this.stdform); 1899 return this; 1900 }, 1901 1902 /** 1903 * EXPERIMENTAL. Generate JSON object code of visProp and other properties. 1904 * @type String 1905 * @private 1906 * @ignore 1907 * @deprecated 1908 * @returns JSON string containing element's properties. 1909 */ 1910 toJSON: function () { 1911 var vis, 1912 key, 1913 json = ['{"name":', this.name]; 1914 1915 json.push(", " + '"id":' + this.id); 1916 1917 vis = []; 1918 for (key in this.visProp) { 1919 if (this.visProp.hasOwnProperty(key)) { 1920 if (Type.exists(this.visProp[key])) { 1921 vis.push('"' + key + '":' + this.visProp[key]); 1922 } 1923 } 1924 } 1925 json.push(', "visProp":{' + vis.toString() + "}"); 1926 json.push("}"); 1927 1928 return json.join(""); 1929 }, 1930 1931 /** 1932 * Rotate texts or images by a given degree. 1933 * @param {number} angle The degree of the rotation (90 means vertical text). 1934 * @see JXG.GeometryElement#rotate 1935 */ 1936 addRotation: function (angle) { 1937 var tOffInv, 1938 tOff, 1939 tS, 1940 tSInv, 1941 tRot, 1942 that = this; 1943 1944 if ( 1945 (this.elementClass === Const.OBJECT_CLASS_TEXT || 1946 this.type === Const.OBJECT_TYPE_IMAGE) && 1947 angle !== 0 1948 ) { 1949 tOffInv = this.board.create( 1950 "transform", 1951 [ 1952 function () { 1953 return -that.X(); 1954 }, 1955 function () { 1956 return -that.Y(); 1957 } 1958 ], 1959 { type: "translate" } 1960 ); 1961 1962 tOff = this.board.create( 1963 "transform", 1964 [ 1965 function () { 1966 return that.X(); 1967 }, 1968 function () { 1969 return that.Y(); 1970 } 1971 ], 1972 { type: "translate" } 1973 ); 1974 1975 tS = this.board.create( 1976 "transform", 1977 [ 1978 function () { 1979 return that.board.unitX / that.board.unitY; 1980 }, 1981 function () { 1982 return 1; 1983 } 1984 ], 1985 { type: "scale" } 1986 ); 1987 1988 tSInv = this.board.create( 1989 "transform", 1990 [ 1991 function () { 1992 return that.board.unitY / that.board.unitX; 1993 }, 1994 function () { 1995 return 1; 1996 } 1997 ], 1998 { type: "scale" } 1999 ); 2000 2001 tRot = this.board.create( 2002 "transform", 2003 [ 2004 function () { 2005 return (that.eval(angle) * Math.PI) / 180; 2006 } 2007 ], 2008 { type: "rotate" } 2009 ); 2010 2011 tOffInv.bindTo(this); 2012 tS.bindTo(this); 2013 tRot.bindTo(this); 2014 tSInv.bindTo(this); 2015 tOff.bindTo(this); 2016 } 2017 2018 return this; 2019 }, 2020 2021 /** 2022 * Set the highlightStrokeColor of an element 2023 * @ignore 2024 * @name JXG.GeometryElement#highlightStrokeColorMethod 2025 * @param {String} sColor String which determines the stroke color of an object when its highlighted. 2026 * @see JXG.GeometryElement#highlightStrokeColor 2027 * @deprecated Use {@link JXG.GeometryElement#setAttribute} 2028 */ 2029 highlightStrokeColor: function (sColor) { 2030 JXG.deprecated("highlightStrokeColor()", "setAttribute()"); 2031 this.setAttribute({ highlightStrokeColor: sColor }); 2032 return this; 2033 }, 2034 2035 /** 2036 * Set the strokeColor of an element 2037 * @ignore 2038 * @name JXG.GeometryElement#strokeColorMethod 2039 * @param {String} sColor String which determines the stroke color of an object. 2040 * @see JXG.GeometryElement#strokeColor 2041 * @deprecated Use {@link JXG.GeometryElement#setAttribute} 2042 */ 2043 strokeColor: function (sColor) { 2044 JXG.deprecated("strokeColor()", "setAttribute()"); 2045 this.setAttribute({ strokeColor: sColor }); 2046 return this; 2047 }, 2048 2049 /** 2050 * Set the strokeWidth of an element 2051 * @ignore 2052 * @name JXG.GeometryElement#strokeWidthMethod 2053 * @param {Number} width Integer which determines the stroke width of an outline. 2054 * @see JXG.GeometryElement#strokeWidth 2055 * @deprecated Use {@link JXG.GeometryElement#setAttribute} 2056 */ 2057 strokeWidth: function (width) { 2058 JXG.deprecated("strokeWidth()", "setAttribute()"); 2059 this.setAttribute({ strokeWidth: width }); 2060 return this; 2061 }, 2062 2063 /** 2064 * Set the fillColor of an element 2065 * @ignore 2066 * @name JXG.GeometryElement#fillColorMethod 2067 * @param {String} fColor String which determines the fill color of an object. 2068 * @see JXG.GeometryElement#fillColor 2069 * @deprecated Use {@link JXG.GeometryElement#setAttribute} 2070 */ 2071 fillColor: function (fColor) { 2072 JXG.deprecated("fillColor()", "setAttribute()"); 2073 this.setAttribute({ fillColor: fColor }); 2074 return this; 2075 }, 2076 2077 /** 2078 * Set the highlightFillColor of an element 2079 * @ignore 2080 * @name JXG.GeometryElement#highlightFillColorMethod 2081 * @param {String} fColor String which determines the fill color of an object when its highlighted. 2082 * @see JXG.GeometryElement#highlightFillColor 2083 * @deprecated Use {@link JXG.GeometryElement#setAttribute} 2084 */ 2085 highlightFillColor: function (fColor) { 2086 JXG.deprecated("highlightFillColor()", "setAttribute()"); 2087 this.setAttribute({ highlightFillColor: fColor }); 2088 return this; 2089 }, 2090 2091 /** 2092 * Set the labelColor of an element 2093 * @ignore 2094 * @param {String} lColor String which determines the text color of an object's label. 2095 * @see JXG.GeometryElement#labelColor 2096 * @deprecated Use {@link JXG.GeometryElement#setAttribute} 2097 */ 2098 labelColor: function (lColor) { 2099 JXG.deprecated("labelColor()", "setAttribute()"); 2100 this.setAttribute({ labelColor: lColor }); 2101 return this; 2102 }, 2103 2104 /** 2105 * Set the dash type of an element 2106 * @ignore 2107 * @name JXG.GeometryElement#dashMethod 2108 * @param {Number} d Integer which determines the way of dashing an element's outline. 2109 * @see JXG.GeometryElement#dash 2110 * @deprecated Use {@link JXG.GeometryElement#setAttribute} 2111 */ 2112 dash: function (d) { 2113 JXG.deprecated("dash()", "setAttribute()"); 2114 this.setAttribute({ dash: d }); 2115 return this; 2116 }, 2117 2118 /** 2119 * Set the visibility of an element 2120 * @ignore 2121 * @name JXG.GeometryElement#visibleMethod 2122 * @param {Boolean} v Boolean which determines whether the element is drawn. 2123 * @see JXG.GeometryElement#visible 2124 * @deprecated Use {@link JXG.GeometryElement#setAttribute} 2125 */ 2126 visible: function (v) { 2127 JXG.deprecated("visible()", "setAttribute()"); 2128 this.setAttribute({ visible: v }); 2129 return this; 2130 }, 2131 2132 /** 2133 * Set the shadow of an element 2134 * @ignore 2135 * @name JXG.GeometryElement#shadowMethod 2136 * @param {Boolean} s Boolean which determines whether the element has a shadow or not. 2137 * @see JXG.GeometryElement#shadow 2138 * @deprecated Use {@link JXG.GeometryElement#setAttribute} 2139 */ 2140 shadow: function (s) { 2141 JXG.deprecated("shadow()", "setAttribute()"); 2142 this.setAttribute({ shadow: s }); 2143 return this; 2144 }, 2145 2146 /** 2147 * The type of the element as used in {@link JXG.Board#create}. 2148 * @returns {String} 2149 */ 2150 getType: function () { 2151 return this.elType; 2152 }, 2153 2154 /** 2155 * List of the element ids resp. values used as parents in {@link JXG.Board#create}. 2156 * @returns {Array} 2157 */ 2158 getParents: function () { 2159 return Type.isArray(this.parents) ? this.parents : []; 2160 }, 2161 2162 /** 2163 * @ignore 2164 * Snaps the element to the grid. Only works for points, lines and circles. Points will snap to the grid 2165 * as defined in their properties {@link JXG.Point#snapSizeX} and {@link JXG.Point#snapSizeY}. Lines and circles 2166 * will snap their parent points to the grid, if they have {@link JXG.Point#snapToGrid} set to true. 2167 * @private 2168 * @returns {JXG.GeometryElement} Reference to the element. 2169 */ 2170 snapToGrid: function () { 2171 return this; 2172 }, 2173 2174 /** 2175 * Snaps the element to points. Only works for points. Points will snap to the next point 2176 * as defined in their properties {@link JXG.Point#attractorDistance} and {@link JXG.Point#attractorUnit}. 2177 * Lines and circles 2178 * will snap their parent points to points. 2179 * @private 2180 * @returns {JXG.GeometryElement} Reference to the element. 2181 */ 2182 snapToPoints: function () { 2183 return this; 2184 }, 2185 2186 /** 2187 * Retrieve a copy of the current visProp. 2188 * @returns {Object} 2189 */ 2190 getAttributes: function () { 2191 var attributes = Type.deepCopy(this.visProp), 2192 /* 2193 cleanThis = ['attractors', 'snatchdistance', 'traceattributes', 'frozen', 2194 'shadow', 'gradientangle', 'gradientsecondopacity', 'gradientpositionx', 'gradientpositiony', 2195 'needsregularupdate', 'zoom', 'layer', 'offset'], 2196 */ 2197 cleanThis = [], 2198 i, 2199 len = cleanThis.length; 2200 2201 attributes.id = this.id; 2202 attributes.name = this.name; 2203 2204 for (i = 0; i < len; i++) { 2205 delete attributes[cleanThis[i]]; 2206 } 2207 2208 return attributes; 2209 }, 2210 2211 /** 2212 * Checks whether (x,y) is near the element. 2213 * @param {Number} x Coordinate in x direction, screen coordinates. 2214 * @param {Number} y Coordinate in y direction, screen coordinates. 2215 * @returns {Boolean} True if (x,y) is near the element, False otherwise. 2216 */ 2217 hasPoint: function (x, y) { 2218 return false; 2219 }, 2220 2221 /** 2222 * Adds ticks to this line or curve. Ticks can be added to a curve or any kind of line: line, arrow, and axis. 2223 * @param {JXG.Ticks} ticks Reference to a ticks object which is describing the ticks (color, distance, how many, etc.). 2224 * @returns {String} Id of the ticks object. 2225 */ 2226 addTicks: function (ticks) { 2227 if (ticks.id === "" || !Type.exists(ticks.id)) { 2228 ticks.id = this.id + "_ticks_" + (this.ticks.length + 1); 2229 } 2230 2231 this.board.renderer.drawTicks(ticks); 2232 this.ticks.push(ticks); 2233 2234 return ticks.id; 2235 }, 2236 2237 /** 2238 * Removes all ticks from a line or curve. 2239 */ 2240 removeAllTicks: function () { 2241 var t; 2242 if (Type.exists(this.ticks)) { 2243 for (t = this.ticks.length - 1; t >= 0; t--) { 2244 this.removeTicks(this.ticks[t]); 2245 } 2246 this.ticks = []; 2247 this.board.update(); 2248 } 2249 }, 2250 2251 /** 2252 * Removes ticks identified by parameter named tick from this line or curve. 2253 * @param {JXG.Ticks} tick Reference to tick object to remove. 2254 */ 2255 removeTicks: function (tick) { 2256 var t, j; 2257 2258 if (Type.exists(this.defaultTicks) && this.defaultTicks === tick) { 2259 this.defaultTicks = null; 2260 } 2261 2262 if (Type.exists(this.ticks)) { 2263 for (t = this.ticks.length - 1; t >= 0; t--) { 2264 if (this.ticks[t] === tick) { 2265 this.board.removeObject(this.ticks[t]); 2266 2267 if (this.ticks[t].ticks) { 2268 for (j = 0; j < this.ticks[t].ticks.length; j++) { 2269 if (Type.exists(this.ticks[t].labels[j])) { 2270 this.board.removeObject(this.ticks[t].labels[j]); 2271 } 2272 } 2273 } 2274 2275 delete this.ticks[t]; 2276 break; 2277 } 2278 } 2279 } 2280 }, 2281 2282 /** 2283 * Determine values of snapSizeX and snapSizeY. If the attributes 2284 * snapSizex and snapSizeY are greater than zero, these values are taken. 2285 * Otherwise, determine the distance between major ticks of the 2286 * default axes. 2287 * @returns {Array} containing the snap sizes for x and y direction. 2288 * @private 2289 */ 2290 getSnapSizes: function () { 2291 var sX, sY, ticks; 2292 2293 sX = this.evalVisProp('snapsizex'); 2294 sY = this.evalVisProp('snapsizey'); 2295 2296 if (sX <= 0 && this.board.defaultAxes && this.board.defaultAxes.x.defaultTicks) { 2297 ticks = this.board.defaultAxes.x.defaultTicks; 2298 sX = ticks.ticksDelta * (ticks.evalVisProp('minorticks') + 1); 2299 } 2300 2301 if (sY <= 0 && this.board.defaultAxes && this.board.defaultAxes.y.defaultTicks) { 2302 ticks = this.board.defaultAxes.y.defaultTicks; 2303 sY = ticks.ticksDelta * (ticks.evalVisProp('minorticks') + 1); 2304 } 2305 2306 return [sX, sY]; 2307 }, 2308 2309 /** 2310 * Move an element to its nearest grid point. 2311 * The function uses the coords object of the element as 2312 * its actual position. If there is no coords object or if the object is fixed, nothing is done. 2313 * @param {Boolean} force force snapping independent from what the snaptogrid attribute says 2314 * @param {Boolean} fromParent True if the drag comes from a child element. This is the case if a line 2315 * through two points is dragged. In this case we do not try to force the points to stay inside of 2316 * the visible board, but the distance between the two points stays constant. 2317 * @returns {JXG.GeometryElement} Reference to this element 2318 */ 2319 handleSnapToGrid: function (force, fromParent) { 2320 var x, y, rx, ry, rcoords, 2321 mi, ma, 2322 boardBB, res, sX, sY, 2323 needsSnapToGrid = false, 2324 attractToGrid = this.evalVisProp('attracttogrid'), 2325 ev_au = this.evalVisProp('attractorunit'), 2326 ev_ad = this.evalVisProp('attractordistance'); 2327 2328 if (!Type.exists(this.coords) || this.evalVisProp('fixed')) { 2329 return this; 2330 } 2331 2332 needsSnapToGrid = 2333 this.evalVisProp('snaptogrid') || attractToGrid || force === true; 2334 2335 if (needsSnapToGrid) { 2336 x = this.coords.usrCoords[1]; 2337 y = this.coords.usrCoords[2]; 2338 res = this.getSnapSizes(); 2339 sX = res[0]; 2340 sY = res[1]; 2341 2342 // If no valid snap sizes are available, don't change the coords. 2343 if (sX > 0 && sY > 0) { 2344 boardBB = this.board.getBoundingBox(); 2345 rx = Math.round(x / sX) * sX; 2346 ry = Math.round(y / sY) * sY; 2347 2348 rcoords = new JXG.Coords(Const.COORDS_BY_USER, [rx, ry], this.board); 2349 if ( 2350 !attractToGrid || 2351 rcoords.distance( 2352 ev_au === "screen" ? Const.COORDS_BY_SCREEN : Const.COORDS_BY_USER, 2353 this.coords 2354 ) < ev_ad 2355 ) { 2356 x = rx; 2357 y = ry; 2358 // Checking whether x and y are still within boundingBox. 2359 // If not, adjust them to remain within the board. 2360 // Otherwise a point may become invisible. 2361 if (!fromParent) { 2362 mi = Math.min(boardBB[0], boardBB[2]); 2363 ma = Math.max(boardBB[0], boardBB[2]); 2364 if (x < mi && x > mi - sX) { 2365 x += sX; 2366 } else if (x > ma && x < ma + sX) { 2367 x -= sX; 2368 } 2369 2370 mi = Math.min(boardBB[1], boardBB[3]); 2371 ma = Math.max(boardBB[1], boardBB[3]); 2372 if (y < mi && y > mi - sY) { 2373 y += sY; 2374 } else if (y > ma && y < ma + sY) { 2375 y -= sY; 2376 } 2377 } 2378 this.coords.setCoordinates(Const.COORDS_BY_USER, [x, y]); 2379 } 2380 } 2381 } 2382 return this; 2383 }, 2384 2385 getBoundingBox: function () { 2386 var i, le, v, 2387 x, y, r, 2388 bb = [Infinity, Infinity, -Infinity, -Infinity]; 2389 2390 if (this.type === Const.OBJECT_TYPE_POLYGON) { 2391 le = this.vertices.length - 1; 2392 if (le <= 0) { 2393 return bb; 2394 } 2395 for (i = 0; i < le; i++) { 2396 v = this.vertices[i].X(); 2397 bb[0] = v < bb[0] ? v : bb[0]; 2398 bb[2] = v > bb[2] ? v : bb[2]; 2399 v = this.vertices[i].Y(); 2400 bb[1] = v < bb[1] ? v : bb[1]; 2401 bb[3] = v > bb[3] ? v : bb[3]; 2402 } 2403 } else if (this.elementClass === Const.OBJECT_CLASS_CIRCLE) { 2404 x = this.center.X(); 2405 y = this.center.Y(); 2406 bb = [x - this.radius, y + this.radius, x + this.radius, y - this.radius]; 2407 } else if (this.elementClass === Const.OBJECT_CLASS_CURVE) { 2408 le = this.points.length; 2409 if (le === 0) { 2410 return bb; 2411 } 2412 for (i = 0; i < le; i++) { 2413 v = this.points[i].usrCoords[1]; 2414 bb[0] = v < bb[0] ? v : bb[0]; 2415 bb[2] = v > bb[2] ? v : bb[2]; 2416 v = this.points[i].usrCoords[2]; 2417 bb[1] = v < bb[1] ? v : bb[1]; 2418 bb[3] = v > bb[3] ? v : bb[3]; 2419 } 2420 } else if (this.elementClass === Const.OBJECT_CLASS_POINT) { 2421 x = this.X(); 2422 y = this.Y(); 2423 r = this.evalVisProp('size'); 2424 bb = [x - r / this.board.unitX, y - r / this.board.unitY, x + r / this.board.unitX, y + r / this.board.unitY]; 2425 } else if (this.elementClass === Const.OBJECT_CLASS_LINE) { 2426 v = this.point1.coords.usrCoords[1]; 2427 bb[0] = v < bb[0] ? v : bb[0]; 2428 bb[2] = v > bb[2] ? v : bb[2]; 2429 v = this.point1.coords.usrCoords[2]; 2430 bb[1] = v < bb[1] ? v : bb[1]; 2431 bb[3] = v > bb[3] ? v : bb[3]; 2432 2433 v = this.point2.coords.usrCoords[1]; 2434 bb[0] = v < bb[0] ? v : bb[0]; 2435 bb[2] = v > bb[2] ? v : bb[2]; 2436 v = this.point2.coords.usrCoords[2]; 2437 bb[1] = v < bb[1] ? v : bb[1]; 2438 bb[3] = v > bb[3] ? v : bb[3]; 2439 } 2440 2441 return bb; 2442 }, 2443 2444 /** 2445 * Alias of {@link JXG.EventEmitter.on}. 2446 * 2447 * @name addEvent 2448 * @memberof JXG.GeometryElement 2449 * @function 2450 */ 2451 addEvent: JXG.shortcut(JXG.GeometryElement.prototype, 'on'), 2452 2453 /** 2454 * Alias of {@link JXG.EventEmitter.off}. 2455 * 2456 * @name removeEvent 2457 * @memberof JXG.GeometryElement 2458 * @function 2459 */ 2460 removeEvent: JXG.shortcut(JXG.GeometryElement.prototype, 'off'), 2461 2462 /** 2463 * Format a number according to the locale set in the attribute "intl". 2464 * If in the options of the intl-attribute "maximumFractionDigits" is not set, 2465 * the optional parameter digits is used instead. 2466 * See <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat</a> 2467 * for more information about internationalization. 2468 * 2469 * @param {Number} value Number to be formatted 2470 * @param {Number} [digits=undefined] Optional number of digits 2471 * @returns {String|Number} string containing the formatted number according to the locale 2472 * or the number itself of the formatting is not possible. 2473 */ 2474 formatNumberLocale: function (value, digits) { 2475 var loc, opt, key, 2476 optCalc = {}, 2477 // These options are case sensitive: 2478 translate = { 2479 maximumfractiondigits: 'maximumFractionDigits', 2480 minimumfractiondigits: 'minimumFractionDigits', 2481 compactdisplay: 'compactDisplay', 2482 currencydisplay: 'currencyDisplay', 2483 currencysign: 'currencySign', 2484 localematcher: 'localeMatcher', 2485 numberingsystem: 'numberingSystem', 2486 signdisplay: 'signDisplay', 2487 unitdisplay: 'unitDisplay', 2488 usegrouping: 'useGrouping', 2489 roundingmode: 'roundingMode', 2490 roundingpriority: 'roundingPriority', 2491 roundingincrement: 'roundingIncrement', 2492 trailingzerodisplay: 'trailingZeroDisplay', 2493 minimumintegerdigits: 'minimumIntegerDigits', 2494 minimumsignificantdigits: 'minimumSignificantDigits', 2495 maximumsignificantdigits: 'maximumSignificantDigits' 2496 }; 2497 2498 if (Type.exists(Intl) && 2499 this.useLocale()) { 2500 2501 loc = this.evalVisProp('intl.locale') || 2502 this.eval(this.board.attr.intl.locale); 2503 opt = this.evalVisProp('intl.options') || {}; 2504 2505 // Transfer back to camel case if necessary and evaluate 2506 for (key in opt) { 2507 if (opt.hasOwnProperty(key)) { 2508 if (translate.hasOwnProperty(key)) { 2509 optCalc[translate[key]] = this.eval(opt[key]); 2510 } else { 2511 optCalc[key] = this.eval(opt[key]); 2512 } 2513 } 2514 } 2515 2516 // If maximumfractiondigits is not set, 2517 // the value of the attribute "digits" is taken instead. 2518 key = 'maximumfractiondigits'; 2519 if (!Type.exists(opt[key])) { 2520 optCalc[translate[key]] = digits; 2521 2522 // key = 'minimumfractiondigits'; 2523 // if (!this.eval(opt[key]) || this.eval(opt[key]) > digits) { 2524 // optCalc[translate[key]] = digits; 2525 // } 2526 } 2527 2528 return Intl.NumberFormat(loc, optCalc).format(value); 2529 } 2530 2531 return value; 2532 }, 2533 2534 /** 2535 * Checks if locale is enabled in the attribute. This may be in the attributes of the board, 2536 * or in the attributes of the text. The latter has higher priority. The board attribute is taken if 2537 * attribute "intl.enabled" of the text element is set to 'inherit'. 2538 * 2539 * @returns {Boolean} if locale can be used for number formatting. 2540 */ 2541 useLocale: function () { 2542 var val; 2543 2544 // Check if element supports intl 2545 if (!Type.exists(this.visProp.intl) || 2546 !Type.exists(this.visProp.intl.enabled)) { 2547 return false; 2548 } 2549 2550 // Check if intl is supported explicitly enabled for this element 2551 val = this.evalVisProp('intl.enabled'); 2552 2553 if (val === true) { 2554 return true; 2555 } 2556 2557 // Check intl attribute of the board 2558 if (val === 'inherit') { 2559 if (this.eval(this.board.attr.intl.enabled) === true) { 2560 return true; 2561 } 2562 } 2563 2564 return false; 2565 }, 2566 2567 /* ************************** 2568 * EVENT DEFINITION 2569 * for documentation purposes 2570 * ************************** */ 2571 2572 //region Event handler documentation 2573 /** 2574 * @event 2575 * @description This event is fired whenever the user is hovering over an element. 2576 * @name JXG.GeometryElement#over 2577 * @param {Event} e The browser's event object. 2578 */ 2579 __evt__over: function (e) { }, 2580 2581 /** 2582 * @event 2583 * @description This event is fired whenever the user puts the mouse over an element. 2584 * @name JXG.GeometryElement#mouseover 2585 * @param {Event} e The browser's event object. 2586 */ 2587 __evt__mouseover: function (e) { }, 2588 2589 /** 2590 * @event 2591 * @description This event is fired whenever the user is leaving an element. 2592 * @name JXG.GeometryElement#out 2593 * @param {Event} e The browser's event object. 2594 */ 2595 __evt__out: function (e) { }, 2596 2597 /** 2598 * @event 2599 * @description This event is fired whenever the user puts the mouse away from an element. 2600 * @name JXG.GeometryElement#mouseout 2601 * @param {Event} e The browser's event object. 2602 */ 2603 __evt__mouseout: function (e) { }, 2604 2605 /** 2606 * @event 2607 * @description This event is fired whenever the user is moving over an element. 2608 * @name JXG.GeometryElement#move 2609 * @param {Event} e The browser's event object. 2610 */ 2611 __evt__move: function (e) { }, 2612 2613 /** 2614 * @event 2615 * @description This event is fired whenever the user is moving the mouse over an element. 2616 * @name JXG.GeometryElement#mousemove 2617 * @param {Event} e The browser's event object. 2618 */ 2619 __evt__mousemove: function (e) { }, 2620 2621 /** 2622 * @event 2623 * @description This event is fired whenever the user drags an element. 2624 * @name JXG.GeometryElement#drag 2625 * @param {Event} e The browser's event object. 2626 */ 2627 __evt__drag: function (e) { }, 2628 2629 /** 2630 * @event 2631 * @description This event is fired whenever the user drags the element with a mouse. 2632 * @name JXG.GeometryElement#mousedrag 2633 * @param {Event} e The browser's event object. 2634 */ 2635 __evt__mousedrag: function (e) { }, 2636 2637 /** 2638 * @event 2639 * @description This event is fired whenever the user drags the element with a pen. 2640 * @name JXG.GeometryElement#pendrag 2641 * @param {Event} e The browser's event object. 2642 */ 2643 __evt__pendrag: function (e) { }, 2644 2645 /** 2646 * @event 2647 * @description This event is fired whenever the user drags the element on a touch device. 2648 * @name JXG.GeometryElement#touchdrag 2649 * @param {Event} e The browser's event object. 2650 */ 2651 __evt__touchdrag: function (e) { }, 2652 2653 /** 2654 * @event 2655 * @description This event is fired whenever the user drags the element by pressing arrow keys 2656 * on the keyboard. 2657 * @name JXG.GeometryElement#keydrag 2658 * @param {Event} e The browser's event object. 2659 */ 2660 __evt__keydrag: function (e) { }, 2661 2662 /** 2663 * @event 2664 * @description Whenever the user starts to touch or click an element. 2665 * @name JXG.GeometryElement#down 2666 * @param {Event} e The browser's event object. 2667 */ 2668 __evt__down: function (e) { }, 2669 2670 /** 2671 * @event 2672 * @description Whenever the user starts to click an element. 2673 * @name JXG.GeometryElement#mousedown 2674 * @param {Event} e The browser's event object. 2675 */ 2676 __evt__mousedown: function (e) { }, 2677 2678 /** 2679 * @event 2680 * @description Whenever the user taps an element with the pen. 2681 * @name JXG.GeometryElement#pendown 2682 * @param {Event} e The browser's event object. 2683 */ 2684 __evt__pendown: function (e) { }, 2685 2686 /** 2687 * @event 2688 * @description Whenever the user starts to touch an element. 2689 * @name JXG.GeometryElement#touchdown 2690 * @param {Event} e The browser's event object. 2691 */ 2692 __evt__touchdown: function (e) { }, 2693 2694 /** 2695 * @event 2696 * @description Whenever the user clicks on an element. 2697 * @name JXG.Board#click 2698 * @param {Event} e The browser's event object. 2699 */ 2700 __evt__click: function (e) { }, 2701 2702 /** 2703 * @event 2704 * @description Whenever the user double clicks on an element. 2705 * This event works on desktop browser, but is undefined 2706 * on mobile browsers. 2707 * @name JXG.Board#dblclick 2708 * @param {Event} e The browser's event object. 2709 * @see JXG.Board#clickDelay 2710 * @see JXG.Board#dblClickSuppressClick 2711 */ 2712 __evt__dblclick: function (e) { }, 2713 2714 /** 2715 * @event 2716 * @description Whenever the user clicks on an element with a mouse device. 2717 * @name JXG.Board#mouseclick 2718 * @param {Event} e The browser's event object. 2719 */ 2720 __evt__mouseclick: function (e) { }, 2721 2722 /** 2723 * @event 2724 * @description Whenever the user double clicks on an element with a mouse device. 2725 * @name JXG.Board#mousedblclick 2726 * @param {Event} e The browser's event object. 2727 */ 2728 __evt__mousedblclick: function (e) { }, 2729 2730 /** 2731 * @event 2732 * @description Whenever the user clicks on an element with a pointer device. 2733 * @name JXG.Board#pointerclick 2734 * @param {Event} e The browser's event object. 2735 */ 2736 __evt__pointerclick: function (e) { }, 2737 2738 /** 2739 * @event 2740 * @description Whenever the user double clicks on an element with a pointer device. 2741 * This event works on desktop browser, but is undefined 2742 * on mobile browsers. 2743 * @name JXG.Board#pointerdblclick 2744 * @param {Event} e The browser's event object. 2745 */ 2746 __evt__pointerdblclick: function (e) { }, 2747 2748 /** 2749 * @event 2750 * @description Whenever the user stops to touch or click an element. 2751 * @name JXG.GeometryElement#up 2752 * @param {Event} e The browser's event object. 2753 */ 2754 __evt__up: function (e) { }, 2755 2756 /** 2757 * @event 2758 * @description Whenever the user releases the mousebutton over an element. 2759 * @name JXG.GeometryElement#mouseup 2760 * @param {Event} e The browser's event object. 2761 */ 2762 __evt__mouseup: function (e) { }, 2763 2764 /** 2765 * @event 2766 * @description Whenever the user lifts the pen over an element. 2767 * @name JXG.GeometryElement#penup 2768 * @param {Event} e The browser's event object. 2769 */ 2770 __evt__penup: function (e) { }, 2771 2772 /** 2773 * @event 2774 * @description Whenever the user stops touching an element. 2775 * @name JXG.GeometryElement#touchup 2776 * @param {Event} e The browser's event object. 2777 */ 2778 __evt__touchup: function (e) { }, 2779 2780 /** 2781 * @event 2782 * @description Notify every time an attribute is changed. 2783 * @name JXG.GeometryElement#attribute 2784 * @param {Object} o A list of changed attributes and their new value. 2785 * @param {Object} el Reference to the element 2786 */ 2787 __evt__attribute: function (o, el) { }, 2788 2789 /** 2790 * @event 2791 * @description This is a generic event handler. It exists for every possible attribute that can be set for 2792 * any element, e.g. if you want to be notified everytime an element's strokecolor is changed, is the event 2793 * <tt>attribute:strokecolor</tt>. 2794 * @name JXG.GeometryElement#attribute:key 2795 * @param val The old value. 2796 * @param nval The new value 2797 * @param {Object} el Reference to the element 2798 */ 2799 __evt__attribute_: function (val, nval, el) { }, 2800 2801 /** 2802 * @ignore 2803 */ 2804 __evt: function () { } 2805 //endregion 2806 } 2807 ); 2808 2809 export default JXG.GeometryElement; 2810 // const GeometryElement = JXG.GeometryElement; 2811 // export { GeometryElement as default, GeometryElement }; 2812