-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathElement.js
More file actions
1830 lines (1565 loc) · 54.8 KB
/
Element.js
File metadata and controls
1830 lines (1565 loc) · 54.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
goog.provide('acgraph.vector.Element');
goog.provide('acgraph.vector.Element.DirtyState');
goog.require('acgraph.error');
goog.require('acgraph.events');
goog.require('acgraph.events.Dragger');
goog.require('acgraph.utils.IdGenerator');
goog.require('acgraph.vector');
goog.require('goog.events.EventTarget');
goog.require('goog.events.Listenable');
goog.require('goog.math.AffineTransform');
goog.require('goog.math.Rect');
/**
<b>Abstract</b> class for all vector elements, such as groups and primitives.
Due to the fact that this class is a child of from goog.events.EventTarget
all its childs can work with events.<br/>
<b>Never call a constructor directly!</b>
@name acgraph.vector.Element
@constructor
@extends {goog.events.EventTarget}
@implements {goog.events.Listenable}
*/
acgraph.vector.Element = function() {
acgraph.vector.Element.base(this, 'constructor');
/**
* Attributes list to be set.
* @type {Object.<string, *>}
* @private
*/
this.attributes_ = {};
/**
* TODO (A.Kudryavtsev): Describe.
* @type {boolean}
* @private
*/
this.isSuspended_ = false;
/**
* TODO (A.Kudryavtsev): Describe.
* @type {number}
* @private
*/
this.suspendedState_ = 0;
// Set all supported sync flags in the beginning.
this.setDirtyState(acgraph.vector.Element.DirtyState.ALL);
};
goog.inherits(acgraph.vector.Element, goog.events.EventTarget);
//----------------------------------------------------------------------------------------------------------------------
//
// Enums
//
//----------------------------------------------------------------------------------------------------------------------
/**
* Sync state list. It is supposed that each state unsync
* can be resolved by the element itself using render() method.
* @enum {number}
*/
acgraph.vector.Element.DirtyState = {
/**
* DOM is not created, need to create it.
*/
DOM_MISSING: 1 << 0,
/**
* Visibility settings has changed.
*/
VISIBILITY: 1 << 1,
/**
* Transformation state has changed.
*/
TRANSFORMATION: 1 << 2,
/**
* Fill must be refreshed.
*/
FILL: 1 << 3,
/**
* Stroke must be refreshed.
*/
STROKE: 1 << 4,
/**
* Element data has changed (i.e. X, Y or size).
*/
DATA: 1 << 5,
/**
* Child elements have changed, need to refresh them.
*/
CHILDREN: 1 << 6,
/**
* Child set has changed (added, removed, moved).
*/
CHILDREN_SET: 1 << 7,
/**
* Parent transformation state has changed.
*/
PARENT_TRANSFORMATION: 1 << 8,
/**
* Clipping rectangle state has changed.
*/
CLIP: 1 << 9,
/**
* Need to update style.
*/
STYLE: 1 << 10,
/**
* Need to update id.
*/
ID: 1 << 11,
/**
* Need to update cursor.
*/
CURSOR: 1 << 12,
/**
* Need to update pointer events property.
*/
POINTER_EVENTS: 1 << 13,
/**
* Need to update position.
*/
POSITION: 1 << 14,
/**
* Need to update position.
*/
STROKE_SCALING: 1 << 15,
/**
* Needs to update the title.
*/
TITLE: 1 << 16,
/**
* Needs to update the desc.
*/
DESC: 1 << 17,
/**
* Needs to update attribute.
*/
ATTRIBUTE: 1 << 18,
/**
* Need to update everything.
*/
ALL: 0xFFFFFFFF
};
//----------------------------------------------------------------------------------------------------------------------
//
// Properties
//
//----------------------------------------------------------------------------------------------------------------------
/**
* Defines whether element can be moved (drag) or not.
* If it is True or instance of goog.math.Rect - element can be moved (draggable), otherwise - not.
* @type {boolean|goog.math.Rect}
* @private
*/
acgraph.vector.Element.prototype.draggable_ = false;
/**
*
* @type {boolean}
* @private
*/
acgraph.vector.Element.prototype.disableStrokeScaling_ = false;
/**
* Title element. A subnode.
* @type {?Element}
*/
acgraph.vector.Element.prototype.titleElement = null;
/**
* Text of title.
* @type {?string}
* @private
*/
acgraph.vector.Element.prototype.titleVal_ = null;
/**
* Desc element. A subnode.
* @type {?Element}
*/
acgraph.vector.Element.prototype.descElement = null;
/**
* Text of desc.
* @type {?string}
* @private
*/
acgraph.vector.Element.prototype.descVal_ = null;
/**
* Flag shows whether element is in rendering state.
* @type {boolean}
* @private
*/
acgraph.vector.Element.prototype.isRendering_ = false;
/**
* Cursor type for element.
* @type {?acgraph.vector.Cursor}
* @private
*/
acgraph.vector.Element.prototype.cursor_ = null;
/**
* Cache of parent cursor type.
* @type {?acgraph.vector.Cursor}
* @protected
*/
acgraph.vector.Element.prototype.parentCursor = null;
/**
* DOM element, created while rendering using
* {@link acgraph.vector.Element#createDomElement} method.
* @type {Element}
* @private
*/
acgraph.vector.Element.prototype.domElement_ = null;
/**
* Parent group, in which DOM element we add DOM element of the given object.
* Events will affect it too.
* @type {acgraph.vector.ILayer}
* @private
*/
acgraph.vector.Element.prototype.parent_ = null;
/**
* Previous parent. Use to notify groups about removal of DOM element from a previous
* layer. Cleared when layer is rendered.
* @type {acgraph.vector.ILayer}
* @private
*/
acgraph.vector.Element.prototype.prevParent_ = null;
/**
* Visibility flag.
* @type {boolean}
* @private
*/
acgraph.vector.Element.prototype.visible_ = true;
/**
* Events handler.
* @type {goog.events.EventHandler}
* @private
*/
acgraph.vector.Element.prototype.handler_;
/**
* Clipping rectangle or clip instance.
* @type {acgraph.vector.Clip}
* @private
*/
acgraph.vector.Element.prototype.clipElement_ = null;
/**
* Pointer events property. Specifies under what circumstances a given graphics element can be the target
* element for a pointer event.
* @type {boolean}
* @private
*/
acgraph.vector.Element.prototype.disablePointerEvents_ = false;
/**
* Element transformation.
* @type {goog.math.AffineTransform}
* @protected
*/
acgraph.vector.Element.prototype.transformation = null;
/**
* Full transformation cache.
* @type {goog.math.AffineTransform}
* @private
*/
acgraph.vector.Element.prototype.fullTransform_ = null;
/**
* Element id (DOM element id attribute value).
* @type {string}
* @private
*/
acgraph.vector.Element.prototype.id_;
/**
* Z index of the element.
* @type {number}
* @private
*/
acgraph.vector.Element.prototype.zIndex_ = 0;
/**
* An object, associated with this element.
* @type {*}
*/
acgraph.vector.Element.prototype.tag;
//----------------------------------------------------------------------------------------------------------------------
// States
//----------------------------------------------------------------------------------------------------------------------
/**
* Supported states mask. Element can handle missing DOM element
* and its visibility.
* @type {number}
*/
acgraph.vector.Element.prototype.SUPPORTED_DIRTY_STATES =
acgraph.vector.Element.DirtyState.DOM_MISSING |
acgraph.vector.Element.DirtyState.VISIBILITY |
acgraph.vector.Element.DirtyState.CURSOR |
acgraph.vector.Element.DirtyState.PARENT_TRANSFORMATION |
acgraph.vector.Element.DirtyState.TRANSFORMATION |
acgraph.vector.Element.DirtyState.CLIP |
acgraph.vector.Element.DirtyState.ID |
acgraph.vector.Element.DirtyState.POINTER_EVENTS |
acgraph.vector.Element.DirtyState.STROKE_SCALING |
acgraph.vector.Element.DirtyState.TITLE |
acgraph.vector.Element.DirtyState.DESC |
acgraph.vector.Element.DirtyState.ATTRIBUTE;
/**
* Comibination of {@link acgraph.vector.Element.DirtyState} flags that shows which part of an element
* must be synced with a DOM representation. If flag is set then then element syns its DOM representation
* on the next render() call. All flags are set initially.
* Unsupported flag can't be set.
* @type {number}
* @private
*/
acgraph.vector.Element.prototype.dirtyState_ = 0;
//----------------------------------------------------------------------------------------------------------------------
//
// Common.
//
//----------------------------------------------------------------------------------------------------------------------
/**
Gets element identifier. If it was not set, it will be generated and applied to the DOM.
@param {string=} opt_value Custom id.
@return {(!acgraph.vector.Element|string)} Returns element identifier.
*/
acgraph.vector.Element.prototype.id = function(opt_value) {
if (goog.isDef(opt_value)) {
var id = opt_value || '';
if (this.id_ != id) {
this.id_ = id;
this.setDirtyState(acgraph.vector.Element.DirtyState.ID);
}
return this;
}
if (!goog.isDef(this.id_)) {
// bad bad recursion
this.id(acgraph.utils.IdGenerator.getInstance().generateId(this));
}
return this.id_;
};
/**
* Returns elemt type prefix.
* Prefix is used to generate unique id in {@link acgraph.utils.IdGenerator}
* and is used to distinguish element type by its id.
* @return {acgraph.utils.IdGenerator.ElementTypePrefix} Element type prefix.
*/
acgraph.vector.Element.prototype.getElementTypePrefix = goog.abstractMethod;
/**
Stage object (to which the given element is bound).
@return {acgraph.vector.Stage} Stage object.
*/
acgraph.vector.Element.prototype.getStage = function() {
var parent = this.parent();
return parent ? parent.getStage() : null;
};
//----------------------------------------------------------------------------------------------------------------------
//
// DOM.
//
//----------------------------------------------------------------------------------------------------------------------
/**
Returns DOM element if element is rendered.<br/>
In case of Stage in Suspended state or unbound element – null is returned.
@return {Element} DOM element.
*/
acgraph.vector.Element.prototype.domElement = function() {
return this.domElement_;
};
/**
Returns the parent layer.
@param {acgraph.vector.ILayer=} opt_value .
@return {(acgraph.vector.ILayer|acgraph.vector.Element)} .
*/
acgraph.vector.Element.prototype.parent = function(opt_value) {
if (goog.isDef(opt_value)) {
if (opt_value) {
if (opt_value != this.parent_) {
var stage = this.getStage();
var stageChanged = (stage && stage != opt_value.getStage());
(/** @type {acgraph.vector.ILayer} */(opt_value)).addChild(this);
if (stageChanged)
this.propagateVisualStatesToChildren();
}
} else {
this.remove();
}
return this;
}
return (/** @type {acgraph.vector.ILayer} */(this.parent_));
};
/**
* Propagates dirty state recursively to children.
* @protected
*/
acgraph.vector.Element.prototype.propagateVisualStatesToChildren = function() {
var clip = this.clip();
if (clip)
clip.id(null);
this.setDirtyState(acgraph.vector.Element.DirtyState.FILL |
acgraph.vector.Element.DirtyState.STROKE |
acgraph.vector.Element.DirtyState.CLIP);
};
/**
Whether parent element is set.
@return {boolean} Whether parent element is set.
*/
acgraph.vector.Element.prototype.hasParent = function() {
return !!(this.parent_);
};
/**
Current element removes itself from the parent layer.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.remove = function() {
if (this.parent_)
this.parent_.removeChild(this);
return this;
};
/**
* Gets/sets element's title value.
* @param {?string=} opt_value - Value to be set.
* @return {(string|null|acgraph.vector.Element|undefined)} - Current value or itself for method chaining.
*/
acgraph.vector.Element.prototype.title = function(opt_value) {
if (goog.isDef(opt_value)) {
if (this.titleVal_ != opt_value) {
this.titleVal_ = opt_value;
this.setDirtyState(acgraph.vector.Element.DirtyState.TITLE);
}
return this;
}
return this.titleVal_;
};
/**
* Gets/sets element's desc value.
* @param {?string=} opt_value - Value to be set.
* @return {(string|null|acgraph.vector.Element|undefined)} - Current value or itself for method chaining.
*/
acgraph.vector.Element.prototype.desc = function(opt_value) {
if (goog.isDef(opt_value)) {
if (this.descVal_ != opt_value) {
this.descVal_ = opt_value;
this.setDirtyState(acgraph.vector.Element.DirtyState.DESC);
}
return this;
}
return this.descVal_;
};
/**
* Gets/sets attribute.
* @param {string} key - Name of attribute.
* @param {*=} opt_value - Value of attribute.
* @return {acgraph.vector.Element|*} - Attribute value or itself for method chaining.
*/
acgraph.vector.Element.prototype.attr = function(key, opt_value) {
if (goog.isDef(opt_value)) {
if (this.attributes_[key] !== opt_value) {
this.attributes_[key] = opt_value;
this.setDirtyState(acgraph.vector.Element.DirtyState.ATTRIBUTE);
}
return this;
}
if (key in this.attributes_)
return this.attributes_[key];
else
return acgraph.getRenderer().getAttribute(this.domElement_, key);
};
//----------------------------------------------------------------------------------------------------------------------
//
// Cursor
//
//----------------------------------------------------------------------------------------------------------------------
/**
Getter for cursor type.
@param {?acgraph.vector.Cursor=} opt_value .
@return {(!acgraph.vector.Element|acgraph.vector.Cursor|null)} .
*/
acgraph.vector.Element.prototype.cursor = function(opt_value) {
if (goog.isDef(opt_value)) {
this.cursor_ = opt_value;
this.cursorChanged();
return this;
}
return this.cursor_;
};
/**
* Notifies itself that its own cursor has been changed.
*/
acgraph.vector.Element.prototype.cursorChanged = function() {
this.setDirtyState(acgraph.vector.Element.DirtyState.CURSOR);
};
//----------------------------------------------------------------------------------------------------------------------
//
// Dirty state.
//
//----------------------------------------------------------------------------------------------------------------------
/**
* Checks if there is any unsync state.
* @return {boolean} If there is any unsync state.
*/
acgraph.vector.Element.prototype.isDirty = function() {
return !!this.dirtyState_;
};
/**
* Checks if an element has the given sync state.
* @param {acgraph.vector.Element.DirtyState} state State to be checked.
* @return {boolean} If the element has the given sync state.
*/
acgraph.vector.Element.prototype.hasDirtyState = function(state) {
return !!(this.dirtyState_ & state);
};
/**
* TODO (A.Kudryavtsev): Describe.
*/
acgraph.vector.Element.prototype.suspend = function() {
this.isSuspended_ = true;
};
/**
* TODO (A.Kudryavtsev): Describe.
*/
acgraph.vector.Element.prototype.resume = function() {
this.isSuspended_ = false;
this.setDirtyState(this.suspendedState_);
this.suspendedState_ = 0;
};
/**
* Sets given combination of sync states to an element
* {@link acgraph.vector.Element.DirtyState}. If element supports
* at least one, it passes it to update all children.
* @param {number} value States to be set.
*/
acgraph.vector.Element.prototype.setDirtyState = function(value) {
value &= this.SUPPORTED_DIRTY_STATES;
if (value) {
if (this.isSuspended_) {
this.suspendedState_ |= value;
} else {
this.dirtyState_ |= value;
if (this.parent_)
this.parent_.setDirtyState(acgraph.vector.Element.DirtyState.CHILDREN);
var stage = this.getStage();
if (stage && !stage.isSuspended() && !stage.isRendering() && !this.isRendering())
this.render();
}
}
};
/**
* Clears the sync state.
* @param {number} value State to clear.
*/
acgraph.vector.Element.prototype.clearDirtyState = function(value) {
this.dirtyState_ &= ~value;
};
//----------------------------------------------------------------------------------------------------------------------
//
// Internal DOM workflow.
//
//----------------------------------------------------------------------------------------------------------------------
/**
* Sets parent element.
* @param {acgraph.vector.ILayer} value Parent group.
* @return {!acgraph.vector.Element} Returns self for method chaining.
*/
acgraph.vector.Element.prototype.setParent = function(value) {
if (!this.parent_ || this.parent_ != value) {
if (!this.prevParent_)
this.prevParent_ = this.parent_;
else if (this.prevParent_ == value)
this.prevParent_ = null;
this.parent_ = value;
this.setParentEventTarget(/** @type {goog.events.EventTarget} */(value));
}
return this;
};
/**
* Notifies previous parent about DOM removal (if there was a previous parent).
* @param {boolean} doCry True, if the element should tell the previous parent to seek and remove itself from DOM cache.
* @return {!acgraph.vector.Element} Self.
*/
acgraph.vector.Element.prototype.notifyPrevParent = function(doCry) {
if (this.prevParent_) {
if (doCry)
this.prevParent_.notifyRemoved(this);
this.prevParent_ = null;
}
if (this.isDisposed())
this.finalizeDisposing();
return this;
};
//----------------------------------------------------------------------------------------------------------------------
//
// Transformations
//
//----------------------------------------------------------------------------------------------------------------------
/**
* Invoked before applying a transformation.
*/
acgraph.vector.Element.prototype.beforeTransformationChanged = goog.nullFunction;
/**
* Notifies itself that transformation state has changed. Can reset bounds cache in descendents.
* @protected
*/
acgraph.vector.Element.prototype.transformationChanged = function() {
this.fullTransform_ = null;
this.inverseFullTransform_ = null;
this.dropBoundsCache();
this.setDirtyState(acgraph.vector.Element.DirtyState.TRANSFORMATION);
this.reclip_();
};
/**
* Notifies itself that parent transformation state may be changed.
* Can reset bounds cache in descendents.
*/
acgraph.vector.Element.prototype.parentTransformationChanged = function() {
this.fullTransform_ = null;
this.dropBoundsCache();
if (acgraph.getRenderer().needsReRenderOnParentTransformationChange())
this.setDirtyState(acgraph.vector.Element.DirtyState.PARENT_TRANSFORMATION);
this.reclip_();
};
/**
* @private
*/
acgraph.vector.Element.prototype.reclip_ = function() {
if (acgraph.getRenderer().needsReClipOnBoundsChange()) {
if (this.clipElement_)
this.clipChanged();
else if (this.parent_)
this.parent_.childClipChanged();
}
};
/**
* Returns self element transformation.
* @return {goog.math.AffineTransform} Element transformation.
*/
acgraph.vector.Element.prototype.getSelfTransformation = function() {
return this.transformation;
};
/**
* Returns full transformation (self and parent transformations combined).
* @return {goog.math.AffineTransform} Full transformation.
*/
acgraph.vector.Element.prototype.getFullTransformation = function() {
if (!this.fullTransform_) {
var parentFullTransformation = this.parent_ ?
this.parent_.getFullTransformation() : null;
this.fullTransform_ = acgraph.math.concatMatrixes(parentFullTransformation, this.transformation);
}
return this.fullTransform_;
};
/**
* Returns inverted full transformation (self and parent transformations combined).
* @return {goog.math.AffineTransform} Full transformation.
*/
acgraph.vector.Element.prototype.getInverseFullTransformation = function() {
if (!this.inverseFullTransform_) {
var tx = this.getFullTransformation();
this.inverseFullTransform_ = tx ? tx.createInverse() : null;
}
return this.inverseFullTransform_;
};
/**
Rotates a shape around the given rotation point.
@param {number} degrees Rotation angle in degrees.
@param {number=} opt_cx Rotation point X.
@param {number=} opt_cy Rotation point Y.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.rotate = function(degrees, opt_cx, opt_cy) {
this.beforeTransformationChanged();
var rotation = goog.math.AffineTransform.getRotateInstance(goog.math.toRadians(degrees), opt_cx || 0, opt_cy || 0);
if (this.transformation) {
this.transformation.preConcatenate(rotation);
} else
this.transformation = rotation;
this.transformationChanged();
return this;
};
/**
Rotates a shape around the given anchor.
@param {number} degrees Rotation angle in degrees.
@param {(acgraph.vector.Anchor|string)=} opt_anchor Rotation anchor.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.rotateByAnchor = function(degrees, opt_anchor) {
/** @type {Array.<number>} */
var point = acgraph.vector.getCoordinateByAnchor(this.getBounds(),
opt_anchor || acgraph.vector.Anchor.CENTER);
return this.rotate(degrees, point[0], point[1]);
};
/**
Rotates a shape around the given point.<br/>
<b>Note:</b> See illustration at {@link acgraph.vector.Element#rotate}, the only difference
between {@link acgraph.vector.Element#rotate} and this method is the fact
that this method resets the current transformation, and {@link acgraph.vector.Element#rotate} adds rotation
to the existing transformation.
@param {number} degrees Rotation angle in degrees.
@param {number=} opt_cx Rotation point X.
@param {number=} opt_cy Rotation point Y.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.setRotation = function(degrees, opt_cx, opt_cy) {
return this.rotate(degrees - this.getRotationAngle(), opt_cx, opt_cy);
};
/**
Rotates a shape around the given anchor.<br/>
<b>Note:</b> See illustration at {@link acgraph.vector.Element#rotateByAnchor}, the only difference
between {@link acgraph.vector.Element#rotateByAnchor} and this method is the fact
that this method resets the current transformation, and and {@link acgraph.vector.Element#rotate} adds rotation
to the existing transformation.
@param {number} degrees Rotation angle in degrees.
@param {(acgraph.vector.Anchor|string)=} opt_anchor Rotation anchor.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.setRotationByAnchor = function(degrees, opt_anchor) {
return this.rotateByAnchor(degrees - this.getRotationAngle(), opt_anchor);
};
/**
Moves a shape taking an account the current transformation.
Movement happens in a shape coordinate system (not the coordinate system of the parent).
@param {number} tx X movement amount.
@param {number} ty Y movement amount.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.translate = function(tx, ty) {
this.beforeTransformationChanged();
if (this.transformation)
this.transformation.translate(tx, ty);
else
this.transformation = goog.math.AffineTransform.getTranslateInstance(tx, ty);
this.transformationChanged();
return this;
};
/**
Sets top left corner of a shape (transformation taken into account) in the coordinate system of the parent.<br/>
<b>Note:</b> See illustration at {@link acgraph.vector.Element#translate}, the only difference
between {@link acgraph.vector.Element#translate} and this method is the fact that
that this method resets the current transformation, and and {@link acgraph.vector.Element#translate} adds movement
to the existing transformation.
@param {number} x X coordinate.
@param {number} y Y coordinate.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.setPosition = function(x, y) {
var arr = [x, y, this.getX(), this.getY()];
if (this.transformation)
this.transformation.createInverse().transform(arr, 0, arr, 0, 2);
return this.translate(arr[0] - arr[2], arr[1] - arr[3]);
};
/**
* Sets movement vector. Used when we need to set layer movement
* and then populate it with other elements.
* Temporary method for AnyChart v6 only. Must be refactored with all other transformation API.
* @deprecated only for AnyChartHTML5.
* @param {number} x X coordinate.
* @param {number} y Y coordinate.
* @return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.setTranslation = function(x, y) {
this.beforeTransformationChanged();
if (this.transformation) {
var oldX = this.transformation.getTranslateX();
var oldY = this.transformation.getTranslateY();
if (x == oldX && y == oldY)
return this;
this.transformation.preTranslate(x - oldX, y - oldY);
} else
this.transformation = goog.math.AffineTransform.getTranslateInstance(x, y);
this.transformationChanged();
return this;
};
/**
Scales a shape. Scaling center is set in the coordinate system of the parent.
@param {number} sx X scaling factor.
@param {number} sy Y scaling factor.
@param {number=} opt_cx Scaling point X.
@param {number=} opt_cy Scaling point Y.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.scale = function(sx, sy, opt_cx, opt_cy) {
this.beforeTransformationChanged();
if (!this.transformation)
this.transformation = new goog.math.AffineTransform();
this.transformation.preScale(sx, sy);
this.transformation.preTranslate((opt_cx || 0) * (1 - sx), (opt_cy || 0) * (1 - sy));
this.transformationChanged();
return this;
};
/**
Scales a shape. Scaling center is set as an anchor.
@param {number} sx X scaling factor.
@param {number} sy Y scaling factor.
@param {(acgraph.vector.Anchor|string)=} opt_anchor Scaling anchor point.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.scaleByAnchor = function(sx, sy, opt_anchor) {
var point = acgraph.vector.getCoordinateByAnchor(this.getBounds(),
opt_anchor || acgraph.vector.Anchor.CENTER);
return this.scale(sx, sy, point[0], point[1]);
};
/**
Combines the current transformation with the given transformation matrix.
Combination is done via matrix multiplication (multiplication to the right).
@param {number} m00 Scale X.
@param {number} m10 Shear Y.
@param {number} m01 Shear X.
@param {number} m11 Scale Y.
@param {number} m02 Translate X.
@param {number} m12 Translate Y.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.appendTransformationMatrix = function(m00, m10, m01, m11, m02, m12) {
this.beforeTransformationChanged();
if (this.transformation)
this.transformation.concatenate(new goog.math.AffineTransform(m00, m10, m01, m11, m02, m12));
else
this.transformation = new goog.math.AffineTransform(m00, m10, m01, m11, m02, m12);
this.transformationChanged();
return this;
};
/**
Sets transformation matrix.<br/>
<b>Note:</b> See illustration at {@link acgraph.vector.Element#appendTransformationMatrix},
the difference between {@link acgraph.vector.Element#appendTransformationMatrix} and this method
is that {@link acgraph.vector.Element#appendTransformationMatrix} combined transformation with
the current, and this method resets the current.
@param {number} m00 Scale X.
@param {number} m10 Shear Y.
@param {number} m01 Shear X.
@param {number} m11 Scale Y.
@param {number} m02 Translate X.
@param {number} m12 Translate Y.
@return {!acgraph.vector.Element} {@link acgraph.vector.Element} instance for method chaining.
*/
acgraph.vector.Element.prototype.setTransformationMatrix = function(m00, m10, m01, m11, m02, m12) {
this.beforeTransformationChanged();
if (this.transformation)
this.transformation.setTransform(m00, m10, m01, m11, m02, m12);
else
this.transformation = new goog.math.AffineTransform(m00, m10, m01, m11, m02, m12);
this.transformationChanged();
return this;
};
/**
Returns the current rotation angle in degrees.
@return {number} Rotation angle.
*/
acgraph.vector.Element.prototype.getRotationAngle = function() {
return acgraph.math.getRotationAngle(this.transformation);
};
/**
Returns the current transformation matrix as an array of six elements:<br>
[<br>
{number} m00 Scale X.<br>
{number} m10 Shear Y.<br>
{number} m01 Shear X.<br>
{number} m11 Scale Y.<br>
{number} m02 Translate X.<br>
{number} m12 Translate Y.<br>
]
@return {Array.<number>} Transformation matrix array.
*/
acgraph.vector.Element.prototype.getTransformationMatrix = function() {
if (this.transformation)
return [
this.transformation.getScaleX(),
this.transformation.getShearY(),
this.transformation.getShearX(),
this.transformation.getScaleY(),
this.transformation.getTranslateX(),
this.transformation.getTranslateY()
];