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
|
/**
* Copyright (c) 2013 Lea Verou. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
// Based on www.cubic-bezier.com by Lea Verou
// See https://github.com/LeaVerou/cubic-bezier
"use strict";
const EventEmitter = require("resource://devtools/shared/event-emitter.js");
const {
PREDEFINED,
PRESETS,
DEFAULT_PRESET_CATEGORY,
} = require("resource://devtools/client/shared/widgets/CubicBezierPresets.js");
const { getCSSLexer } = require("resource://devtools/shared/css/lexer.js");
const XHTML_NS = "http://www.w3.org/1999/xhtml";
/**
* CubicBezier data structure helper
* Accepts an array of coordinates and exposes a few useful getters
* @param {Array} coordinates i.e. [.42, 0, .58, 1]
*/
function CubicBezier(coordinates) {
if (!coordinates) {
throw new Error("No offsets were defined");
}
this.coordinates = coordinates.map(n => +n);
for (let i = 4; i--; ) {
const xy = this.coordinates[i];
if (isNaN(xy) || (!(i % 2) && (xy < 0 || xy > 1))) {
throw new Error(`Wrong coordinate at ${i}(${xy})`);
}
}
this.coordinates.toString = function () {
return (
this.map(n => {
return (Math.round(n * 100) / 100 + "").replace(/^0\./, ".");
}) + ""
);
};
}
exports.CubicBezier = CubicBezier;
CubicBezier.prototype = {
get P1() {
return this.coordinates.slice(0, 2);
},
get P2() {
return this.coordinates.slice(2);
},
toString() {
// Check first if current coords are one of css predefined functions
const predefName = Object.keys(PREDEFINED).find(key =>
coordsAreEqual(PREDEFINED[key], this.coordinates)
);
return predefName || "cubic-bezier(" + this.coordinates + ")";
},
};
/**
* Bezier curve canvas plotting class
* @param {DOMNode} canvas
* @param {CubicBezier} bezier
* @param {Array} padding Amount of horizontal,vertical padding around the graph
*/
function BezierCanvas(canvas, bezier, padding) {
this.canvas = canvas;
this.bezier = bezier;
this.padding = getPadding(padding);
// Convert to a cartesian coordinate system with axes from 0 to 1
this.ctx = this.canvas.getContext("2d");
const p = this.padding;
this.ctx.scale(
canvas.width * (1 - p[1] - p[3]),
-canvas.height * (1 - p[0] - p[2])
);
this.ctx.translate(p[3] / (1 - p[1] - p[3]), -1 - p[0] / (1 - p[0] - p[2]));
}
exports.BezierCanvas = BezierCanvas;
BezierCanvas.prototype = {
/**
* Get P1 and P2 current top/left offsets so they can be positioned
* @return {Array} Returns an array of 2 {top:String,left:String} objects
*/
get offsets() {
const p = this.padding,
w = this.canvas.width,
h = this.canvas.height;
return [
{
left:
w * (this.bezier.coordinates[0] * (1 - p[3] - p[1]) - p[3]) + "px",
top:
h * (1 - this.bezier.coordinates[1] * (1 - p[0] - p[2]) - p[0]) +
"px",
},
{
left:
w * (this.bezier.coordinates[2] * (1 - p[3] - p[1]) - p[3]) + "px",
top:
h * (1 - this.bezier.coordinates[3] * (1 - p[0] - p[2]) - p[0]) +
"px",
},
];
},
/**
* Convert an element's left/top offsets into coordinates
*/
offsetsToCoordinates(element) {
const w = this.canvas.width,
h = this.canvas.height;
// Convert padding percentage to actual padding
const p = this.padding.map((a, i) => a * (i % 2 ? w : h));
return [
(parseFloat(element.style.left) - p[3]) / (w + p[1] + p[3]),
(h - parseFloat(element.style.top) - p[2]) / (h - p[0] - p[2]),
];
},
/**
* Draw the cubic bezier curve for the current coordinates
*/
plot(settings = {}) {
const xy = this.bezier.coordinates;
const defaultSettings = {
handleColor: "#666",
handleThickness: 0.008,
bezierColor: "#4C9ED9",
bezierThickness: 0.015,
drawHandles: true,
};
for (const setting in settings) {
defaultSettings[setting] = settings[setting];
}
// Clear the canvas –making sure to clear the
// whole area by resetting the transform first.
this.ctx.save();
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.restore();
if (defaultSettings.drawHandles) {
// Draw control handles
this.ctx.beginPath();
this.ctx.fillStyle = defaultSettings.handleColor;
this.ctx.lineWidth = defaultSettings.handleThickness;
this.ctx.strokeStyle = defaultSettings.handleColor;
this.ctx.moveTo(0, 0);
this.ctx.lineTo(xy[0], xy[1]);
this.ctx.moveTo(1, 1);
this.ctx.lineTo(xy[2], xy[3]);
this.ctx.stroke();
this.ctx.closePath();
const circle = (ctx, cx, cy, r) => {
ctx.beginPath();
ctx.arc(cx, cy, r, 0, 2 * Math.PI, !1);
ctx.closePath();
};
circle(this.ctx, xy[0], xy[1], 1.5 * defaultSettings.handleThickness);
this.ctx.fill();
circle(this.ctx, xy[2], xy[3], 1.5 * defaultSettings.handleThickness);
this.ctx.fill();
}
// Draw bezier curve
this.ctx.beginPath();
this.ctx.lineWidth = defaultSettings.bezierThickness;
this.ctx.strokeStyle = defaultSettings.bezierColor;
this.ctx.moveTo(0, 0);
this.ctx.bezierCurveTo(xy[0], xy[1], xy[2], xy[3], 1, 1);
this.ctx.stroke();
this.ctx.closePath();
},
};
/**
* Cubic-bezier widget. Uses the BezierCanvas class to draw the curve and
* adds the control points and user interaction
* @param {DOMNode} parent The container where the graph should be created
* @param {Array} coordinates Coordinates of the curve to be drawn
*
* Emits "updated" events whenever the curve is changed. Along with the event is
* sent a CubicBezier object
*/
function CubicBezierWidget(
parent,
coordinates = PRESETS["ease-in"]["ease-in-sine"]
) {
EventEmitter.decorate(this);
this.parent = parent;
const { curve, p1, p2 } = this._initMarkup();
this.curveBoundingBox = curve.getBoundingClientRect();
this.curve = curve;
this.p1 = p1;
this.p2 = p2;
// Create and plot the bezier curve
this.bezierCanvas = new BezierCanvas(
this.curve,
new CubicBezier(coordinates),
[0.3, 0]
);
this.bezierCanvas.plot();
// Place the control points
const offsets = this.bezierCanvas.offsets;
this.p1.style.left = offsets[0].left;
this.p1.style.top = offsets[0].top;
this.p2.style.left = offsets[1].left;
this.p2.style.top = offsets[1].top;
this._onPointMouseDown = this._onPointMouseDown.bind(this);
this._onPointKeyDown = this._onPointKeyDown.bind(this);
this._onCurveClick = this._onCurveClick.bind(this);
this._onNewCoordinates = this._onNewCoordinates.bind(this);
this.onPrefersReducedMotionChange =
this.onPrefersReducedMotionChange.bind(this);
// Add preset preview menu
this.presets = new CubicBezierPresetWidget(parent);
// Add the timing function previewer
// if prefers-reduced-motion is not set
this.reducedMotion = parent.ownerGlobal.matchMedia(
"(prefers-reduced-motion)"
);
if (!this.reducedMotion.matches) {
this.timingPreview = new TimingFunctionPreviewWidget(parent);
}
// add event listener to change prefers-reduced-motion
// of the timing function preview during runtime
this.reducedMotion.addEventListener(
"change",
this.onPrefersReducedMotionChange
);
this._initEvents();
}
exports.CubicBezierWidget = CubicBezierWidget;
CubicBezierWidget.prototype = {
_initMarkup() {
const doc = this.parent.ownerDocument;
const wrap = doc.createElementNS(XHTML_NS, "div");
wrap.className = "display-wrap";
const plane = doc.createElementNS(XHTML_NS, "div");
plane.className = "coordinate-plane";
const p1 = doc.createElementNS(XHTML_NS, "button");
p1.className = "control-point";
plane.appendChild(p1);
const p2 = doc.createElementNS(XHTML_NS, "button");
p2.className = "control-point";
plane.appendChild(p2);
const curve = doc.createElementNS(XHTML_NS, "canvas");
curve.setAttribute("width", 150);
curve.setAttribute("height", 370);
curve.className = "curve";
plane.appendChild(curve);
wrap.appendChild(plane);
this.parent.appendChild(wrap);
return {
p1,
p2,
curve,
};
},
onPrefersReducedMotionChange(event) {
// if prefers-reduced-motion is enabled destroy timing function preview
// else create it if it does not exist
if (event.matches) {
if (this.timingPreview) {
this.timingPreview.destroy();
}
this.timingPreview = undefined;
} else if (!this.timingPreview) {
this.timingPreview = new TimingFunctionPreviewWidget(this.parent);
}
},
_removeMarkup() {
this.parent.querySelector(".display-wrap").remove();
},
_initEvents() {
this.p1.addEventListener("mousedown", this._onPointMouseDown);
this.p2.addEventListener("mousedown", this._onPointMouseDown);
this.p1.addEventListener("keydown", this._onPointKeyDown);
this.p2.addEventListener("keydown", this._onPointKeyDown);
this.curve.addEventListener("click", this._onCurveClick);
this.presets.on("new-coordinates", this._onNewCoordinates);
},
_removeEvents() {
this.p1.removeEventListener("mousedown", this._onPointMouseDown);
this.p2.removeEventListener("mousedown", this._onPointMouseDown);
this.p1.removeEventListener("keydown", this._onPointKeyDown);
this.p2.removeEventListener("keydown", this._onPointKeyDown);
this.curve.removeEventListener("click", this._onCurveClick);
this.presets.off("new-coordinates", this._onNewCoordinates);
},
_onPointMouseDown(event) {
// Updating the boundingbox in case it has changed
this.curveBoundingBox = this.curve.getBoundingClientRect();
const point = event.target;
const doc = point.ownerDocument;
const self = this;
doc.onmousemove = function drag(e) {
let x = e.pageX;
const y = e.pageY;
const left = self.curveBoundingBox.left;
const top = self.curveBoundingBox.top;
if (x === 0 && y == 0) {
return;
}
// Constrain x
x = Math.min(Math.max(left, x), left + self.curveBoundingBox.width);
point.style.left = x - left + "px";
point.style.top = y - top + "px";
self._updateFromPoints();
};
doc.onmouseup = function () {
point.focus();
doc.onmousemove = doc.onmouseup = null;
};
},
_onPointKeyDown(event) {
const point = event.target;
const code = event.keyCode;
if (code >= 37 && code <= 40) {
event.preventDefault();
// Arrow keys pressed
const left = parseInt(point.style.left, 10);
const top = parseInt(point.style.top, 10);
const offset = 3 * (event.shiftKey ? 10 : 1);
switch (code) {
case 37:
point.style.left = left - offset + "px";
break;
case 38:
point.style.top = top - offset + "px";
break;
case 39:
point.style.left = left + offset + "px";
break;
case 40:
point.style.top = top + offset + "px";
break;
}
this._updateFromPoints();
}
},
_onCurveClick(event) {
this.curveBoundingBox = this.curve.getBoundingClientRect();
const left = this.curveBoundingBox.left;
const top = this.curveBoundingBox.top;
const x = event.pageX - left;
const y = event.pageY - top;
// Find which point is closer
const distP1 = distance(
x,
y,
parseInt(this.p1.style.left, 10),
parseInt(this.p1.style.top, 10)
);
const distP2 = distance(
x,
y,
parseInt(this.p2.style.left, 10),
parseInt(this.p2.style.top, 10)
);
const point = distP1 < distP2 ? this.p1 : this.p2;
point.style.left = x + "px";
point.style.top = y + "px";
this._updateFromPoints();
},
_onNewCoordinates(coordinates) {
this.coordinates = coordinates;
},
/**
* Get the current point coordinates and redraw the curve to match
*/
_updateFromPoints() {
// Get the new coordinates from the point's offsets
let coordinates = this.bezierCanvas.offsetsToCoordinates(this.p1);
coordinates = coordinates.concat(
this.bezierCanvas.offsetsToCoordinates(this.p2)
);
this.presets.refreshMenu(coordinates);
this._redraw(coordinates);
},
/**
* Redraw the curve
* @param {Array} coordinates The array of control point coordinates
*/
_redraw(coordinates) {
// Provide a new CubicBezier to the canvas and plot the curve
this.bezierCanvas.bezier = new CubicBezier(coordinates);
this.bezierCanvas.plot();
this.emit("updated", this.bezierCanvas.bezier);
if (this.timingPreview) {
this.timingPreview.preview(this.bezierCanvas.bezier.toString());
}
},
/**
* Set new coordinates for the control points and redraw the curve
* @param {Array} coordinates
*/
set coordinates(coordinates) {
this._redraw(coordinates);
// Move the points
const offsets = this.bezierCanvas.offsets;
this.p1.style.left = offsets[0].left;
this.p1.style.top = offsets[0].top;
this.p2.style.left = offsets[1].left;
this.p2.style.top = offsets[1].top;
},
/**
* Set new coordinates for the control point and redraw the curve
* @param {String} value A string value. E.g. "linear",
* "cubic-bezier(0,0,1,1)"
*/
set cssCubicBezierValue(value) {
if (!value) {
return;
}
value = value.trim();
// Try with one of the predefined values
const coordinates = parseTimingFunction(value);
this.presets.refreshMenu(coordinates);
this.coordinates = coordinates;
},
destroy() {
this._removeEvents();
this._removeMarkup();
// remove prefers-reduced-motion event listener
this.reducedMotion.removeEventListener(
"change",
this.onPrefersReducedMotionChange
);
this.reducedMotion = null;
if (this.timingPreview) {
this.timingPreview.destroy();
this.timingPreview = null;
}
this.presets.destroy();
this.curve = this.p1 = this.p2 = null;
},
};
/**
* CubicBezierPreset widget.
* Builds a menu of presets from CubicBezierPresets
* @param {DOMNode} parent The container where the preset panel should be
* created
*
* Emits "new-coordinate" event along with the coordinates
* whenever a preset is selected.
*/
function CubicBezierPresetWidget(parent) {
this.parent = parent;
const { presetPane, presets, categories } = this._initMarkup();
this.presetPane = presetPane;
this.presets = presets;
this.categories = categories;
this._activeCategory = null;
this._activePresetList = null;
this._activePreset = null;
this._onCategoryClick = this._onCategoryClick.bind(this);
this._onPresetClick = this._onPresetClick.bind(this);
EventEmitter.decorate(this);
this._initEvents();
}
exports.CubicBezierPresetWidget = CubicBezierPresetWidget;
CubicBezierPresetWidget.prototype = {
/*
* Constructs a list of all preset categories and a list
* of presets for each category.
*
* High level markup:
* div .preset-pane
* div .preset-categories
* div .category
* div .category
* ...
* div .preset-container
* div .presetList
* div .preset
* ...
* div .presetList
* div .preset
* ...
*/
_initMarkup() {
const doc = this.parent.ownerDocument;
const presetPane = doc.createElementNS(XHTML_NS, "div");
presetPane.className = "preset-pane";
const categoryList = doc.createElementNS(XHTML_NS, "div");
categoryList.id = "preset-categories";
const presetContainer = doc.createElementNS(XHTML_NS, "div");
presetContainer.id = "preset-container";
Object.keys(PRESETS).forEach(categoryLabel => {
const category = this._createCategory(categoryLabel);
categoryList.appendChild(category);
const presetList = this._createPresetList(categoryLabel);
presetContainer.appendChild(presetList);
});
presetPane.appendChild(categoryList);
presetPane.appendChild(presetContainer);
this.parent.appendChild(presetPane);
const allCategories = presetPane.querySelectorAll(".category");
const allPresets = presetPane.querySelectorAll(".preset");
return {
presetPane,
presets: allPresets,
categories: allCategories,
};
},
_createCategory(categoryLabel) {
const doc = this.parent.ownerDocument;
const category = doc.createElementNS(XHTML_NS, "div");
category.id = categoryLabel;
category.classList.add("category");
const categoryDisplayLabel = this._normalizeCategoryLabel(categoryLabel);
category.textContent = categoryDisplayLabel;
category.setAttribute("title", categoryDisplayLabel);
return category;
},
_normalizeCategoryLabel(categoryLabel) {
return categoryLabel.replace("/-/g", " ");
},
_createPresetList(categoryLabel) {
const doc = this.parent.ownerDocument;
const presetList = doc.createElementNS(XHTML_NS, "div");
presetList.id = "preset-category-" + categoryLabel;
presetList.classList.add("preset-list");
Object.keys(PRESETS[categoryLabel]).forEach(presetLabel => {
const preset = this._createPreset(categoryLabel, presetLabel);
presetList.appendChild(preset);
});
return presetList;
},
_createPreset(categoryLabel, presetLabel) {
const doc = this.parent.ownerDocument;
const preset = doc.createElementNS(XHTML_NS, "div");
preset.classList.add("preset");
preset.id = presetLabel;
preset.coordinates = PRESETS[categoryLabel][presetLabel];
// Create preset preview
const curve = doc.createElementNS(XHTML_NS, "canvas");
const bezier = new CubicBezier(preset.coordinates);
curve.setAttribute("height", 50);
curve.setAttribute("width", 50);
preset.bezierCanvas = new BezierCanvas(curve, bezier, [0.15, 0]);
preset.bezierCanvas.plot({
drawHandles: false,
bezierThickness: 0.025,
});
preset.appendChild(curve);
// Create preset label
const presetLabelElem = doc.createElementNS(XHTML_NS, "p");
const presetDisplayLabel = this._normalizePresetLabel(
categoryLabel,
presetLabel
);
presetLabelElem.textContent = presetDisplayLabel;
preset.appendChild(presetLabelElem);
preset.setAttribute("title", presetDisplayLabel);
return preset;
},
_normalizePresetLabel(categoryLabel, presetLabel) {
return presetLabel.replace(categoryLabel + "-", "").replace("/-/g", " ");
},
_initEvents() {
for (const category of this.categories) {
category.addEventListener("click", this._onCategoryClick);
}
for (const preset of this.presets) {
preset.addEventListener("click", this._onPresetClick);
}
},
_removeEvents() {
for (const category of this.categories) {
category.removeEventListener("click", this._onCategoryClick);
}
for (const preset of this.presets) {
preset.removeEventListener("click", this._onPresetClick);
}
},
_onPresetClick(event) {
this.emit("new-coordinates", event.currentTarget.coordinates);
this.activePreset = event.currentTarget;
},
_onCategoryClick(event) {
this.activeCategory = event.target;
},
_setActivePresetList(presetListId) {
const presetList = this.presetPane.querySelector("#" + presetListId);
swapClassName("active-preset-list", this._activePresetList, presetList);
this._activePresetList = presetList;
},
set activeCategory(category) {
swapClassName("active-category", this._activeCategory, category);
this._activeCategory = category;
this._setActivePresetList("preset-category-" + category.id);
},
get activeCategory() {
return this._activeCategory;
},
set activePreset(preset) {
swapClassName("active-preset", this._activePreset, preset);
this._activePreset = preset;
},
get activePreset() {
return this._activePreset;
},
/**
* Called by CubicBezierWidget onload and when
* the curve is modified via the canvas.
* Attempts to match the new user setting with an
* existing preset.
* @param {Array} coordinates new coords [i, j, k, l]
*/
refreshMenu(coordinates) {
// If we cannot find a matching preset, keep
// menu on last known preset category.
let category = this._activeCategory;
// If we cannot find a matching preset
// deselect any selected preset.
let preset = null;
// If a category has never been viewed before
// show the default category.
if (!category) {
category = this.parent.querySelector("#" + DEFAULT_PRESET_CATEGORY);
}
// If the new coordinates do match a preset,
// set its category and preset button as active.
Object.keys(PRESETS).forEach(categoryLabel => {
Object.keys(PRESETS[categoryLabel]).forEach(presetLabel => {
if (coordsAreEqual(PRESETS[categoryLabel][presetLabel], coordinates)) {
category = this.parent.querySelector("#" + categoryLabel);
preset = this.parent.querySelector("#" + presetLabel);
}
});
});
this.activeCategory = category;
this.activePreset = preset;
},
destroy() {
this._removeEvents();
this.parent.querySelector(".preset-pane").remove();
},
};
/**
* The TimingFunctionPreviewWidget animates a dot on a scale with a given
* timing-function
* @param {DOMNode} parent The container where this widget should go
*/
function TimingFunctionPreviewWidget(parent) {
this.previousValue = null;
this.parent = parent;
this._initMarkup();
}
TimingFunctionPreviewWidget.prototype = {
PREVIEW_DURATION: 1000,
_initMarkup() {
const doc = this.parent.ownerDocument;
const container = doc.createElementNS(XHTML_NS, "div");
container.className = "timing-function-preview";
this.dot = doc.createElementNS(XHTML_NS, "div");
this.dot.className = "dot";
container.appendChild(this.dot);
const scale = doc.createElementNS(XHTML_NS, "div");
scale.className = "scale";
container.appendChild(scale);
this.parent.appendChild(container);
},
destroy() {
this.dot.getAnimations().forEach(anim => anim.cancel());
this.parent.querySelector(".timing-function-preview").remove();
this.parent = this.dot = null;
},
/**
* Preview a new timing function. The current preview will only be stopped if
* the supplied function value is different from the previous one. If the
* supplied function is invalid, the preview will stop.
* @param {String} value
*/
preview(value) {
// Don't restart the preview animation if the value is the same
if (value === this.previousValue) {
return;
}
if (parseTimingFunction(value)) {
this.restartAnimation(value);
}
this.previousValue = value;
},
/**
* Re-start the preview animation from the beginning.
* @param {String} timingFunction The value for the timing-function.
*/
restartAnimation(timingFunction) {
// Cancel the previous animation if there was any.
this.dot.getAnimations().forEach(anim => anim.cancel());
// And start the new one.
// The animation consists of a few keyframes that move the dot to the right of the
// container, and then move it back to the left.
// It also contains some pause where the dot is semi transparent, before it moves to
// the right, and once again, before it comes back to the left.
// The timing function passed to this function is applied to the keyframes that
// actually move the dot. This way it can be previewed in both direction, instead of
// being spread over the whole animation.
this.dot.animate(
[
{ left: "-7px", opacity: 0.5, offset: 0 },
{ left: "-7px", opacity: 0.5, offset: 0.19 },
{ left: "-7px", opacity: 1, offset: 0.2, easing: timingFunction },
{ left: "143px", opacity: 1, offset: 0.5 },
{ left: "143px", opacity: 0.5, offset: 0.51 },
{ left: "143px", opacity: 0.5, offset: 0.7 },
{ left: "143px", opacity: 1, offset: 0.71, easing: timingFunction },
{ left: "-7px", opacity: 1, offset: 1 },
],
{
duration: this.PREVIEW_DURATION * 2,
iterations: Infinity,
}
);
},
};
// Helpers
function getPadding(padding) {
const p = typeof padding === "number" ? [padding] : padding;
if (p.length === 1) {
p[1] = p[0];
}
if (p.length === 2) {
p[2] = p[0];
}
if (p.length === 3) {
p[3] = p[1];
}
return p;
}
function distance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
/**
* Parse a string to see whether it is a valid timing function.
* If it is, return the coordinates as an array.
* Otherwise, return undefined.
* @param {String} value
* @return {Array} of coordinates, or undefined
*/
function parseTimingFunction(value) {
if (value in PREDEFINED) {
return PREDEFINED[value];
}
const tokenStream = getCSSLexer(value);
const getNextToken = () => {
while (true) {
const token = tokenStream.nextToken();
if (
!token ||
(token.tokenType !== "whitespace" && token.tokenType !== "comment")
) {
return token;
}
}
};
let token = getNextToken();
if (token.tokenType !== "function" || token.text !== "cubic-bezier") {
return undefined;
}
const result = [];
for (let i = 0; i < 4; ++i) {
token = getNextToken();
if (!token || token.tokenType !== "number") {
return undefined;
}
result.push(token.number);
token = getNextToken();
if (
!token ||
token.tokenType !== "symbol" ||
token.text !== (i == 3 ? ")" : ",")
) {
return undefined;
}
}
return result;
}
exports.parseTimingFunction = parseTimingFunction;
/**
* Removes a class from a node and adds it to another.
* @param {String} className the class to swap
* @param {DOMNode} from the node to remove the class from
* @param {DOMNode} to the node to add the class to
*/
function swapClassName(className, from, to) {
if (from !== null) {
from.classList.remove(className);
}
if (to !== null) {
to.classList.add(className);
}
}
/**
* Compares two arrays of coordinates [i, j, k, l]
* @param {Array} c1 first coordinate array to compare
* @param {Array} c2 second coordinate array to compare
* @return {Boolean}
*/
function coordsAreEqual(c1, c2) {
return c1.reduce((prev, curr, index) => prev && curr === c2[index], true);
}
|