plotboilerplate 1.13.0 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/changelog.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ - 2021-12-01
4
+ - Changed the type of param of `BezierPath.scale` to XYCoords.
5
+ - Added function `BezierPath.scaleXY` for non uniform scaling.
6
+ - Changed the type of param of `Vertex.scale` to XYCoords.
7
+ - Added function `Vertex.scaleXY` for non uniform scaling.
3
8
  - 2021-11-24
4
9
  - Speeding up the demo page with async script execution.
5
10
  - Finalizing the demo page for the new `PBText` element (note: it has this name because `Text` already exists).
package/dist/index.esm.js CHANGED
@@ -358,7 +358,9 @@ class VertexListeners {
358
358
  * @modified 2020-05-26 Added functions addX(number) and addY(number).
359
359
  * @modified 2020-10-30 Changed the warnings in `sub(...)` and `add(...)` into real errors.
360
360
  * @modified 2021-03-01 Changed the second param `center` in the `rotate` function from Vertex to XYCoords.
361
- * @version 2.4.2
361
+ * @modified 2021-12-01 Changed the type of param of `scale` to XYCoords.
362
+ * @modified 2021-12-01 Added function `scaleXY` for non uniform scaling.
363
+ * @version 2.5.0
362
364
  *
363
365
  * @file Vertex
364
366
  * @public
@@ -699,20 +701,35 @@ class Vertex {
699
701
  }
700
702
  /**
701
703
  * This is a vector-like behavior and 'scales' this vertex
702
- * towards/from a given center.
704
+ * towards/from a given center by one uniform scale factor.
703
705
  *
704
706
  * @method scale
705
707
  * @param {number} factor - The factor to 'scale' this vertex; 1.0 means no change.
706
- * @param {Vertex=} center - The origin of scaling; default is (0,0).
708
+ * @param {XYCoords=} center - The origin of scaling; default is (0,0).
707
709
  * @return {Vertex} this
708
710
  * @instance
709
711
  * @memberof Vertex
710
712
  **/
711
713
  scale(factor, center) {
712
- if (!center || typeof center === "undefined")
713
- center = new Vertex(0, 0);
714
- this.x = center.x + (this.x - center.x) * factor;
715
- this.y = center.y + (this.y - center.y) * factor;
714
+ return this.scaleXY({ x: factor, y: factor }, center);
715
+ }
716
+ /**
717
+ * This is a vector-like behavior and 'scales' this vertex
718
+ * towards/from a given center by two independent x- and y- scale factors.
719
+ *
720
+ * @method scale
721
+ * @param {number} factor - The factor to 'scale' this vertex; 1.0 means no change.
722
+ * @param {XYCoords=} center - The origin of scaling; default is (0,0).
723
+ * @return {Vertex} this
724
+ * @instance
725
+ * @memberof Vertex
726
+ **/
727
+ scaleXY(factors, center) {
728
+ if (!center || typeof center === "undefined") {
729
+ center = { x: 0, y: 0 };
730
+ }
731
+ this.x = center.x + (this.x - center.x) * factors.x;
732
+ this.y = center.y + (this.y - center.y) * factors.y;
716
733
  return this;
717
734
  }
718
735
  /**
@@ -3021,6 +3038,35 @@ class BezierPath {
3021
3038
  this.updateArcLengths();
3022
3039
  return this;
3023
3040
  }
3041
+ /**
3042
+ * Scale the whole bezier path by the given uniform factor.
3043
+ *
3044
+ * @method scale
3045
+ * @param {Vertex} anchor - The scale origin to scale from.
3046
+ * @param {number} scaleFactor - The scalar to be multiplied with.
3047
+ * @instance
3048
+ * @memberof BezierPath
3049
+ * @return {BezierPath} this for chaining.
3050
+ **/
3051
+ scale(anchor, scaleFactor) {
3052
+ // var scaleFactors : XYCoords = { x : scaleFactor, y : scaleFactor };
3053
+ // for (var i = 0; i < this.bezierCurves.length; i++) {
3054
+ // var curve = this.bezierCurves[i];
3055
+ // curve.getStartPoint().scale(scaleFactor, anchor);
3056
+ // curve.getStartControlPoint().scale(scaleFactor, anchor);
3057
+ // curve.getEndControlPoint().scale(scaleFactor, anchor);
3058
+ // // Do NOT scale the end point here!
3059
+ // // Don't forget that the curves are connected and on curve's end point
3060
+ // // the the successor's start point (same instance)!
3061
+ // }
3062
+ // // Finally move the last end point (was not scaled yet)
3063
+ // if (this.bezierCurves.length > 0 && !this.adjustCircular) {
3064
+ // this.bezierCurves[this.bezierCurves.length - 1].getEndPoint().scale(scaleFactor, anchor);
3065
+ // }
3066
+ // this.updateArcLengths();
3067
+ // return this;
3068
+ return this.scaleXY({ x: scaleFactor, y: scaleFactor }, anchor);
3069
+ }
3024
3070
  /**
3025
3071
  * Scale the whole bezier path by the given (x,y)-factors.
3026
3072
  *
@@ -3031,19 +3077,19 @@ class BezierPath {
3031
3077
  * @memberof BezierPath
3032
3078
  * @return {BezierPath} this for chaining.
3033
3079
  **/
3034
- scale(anchor, scaling) {
3080
+ scaleXY(scaleFactors, anchor) {
3035
3081
  for (var i = 0; i < this.bezierCurves.length; i++) {
3036
3082
  var curve = this.bezierCurves[i];
3037
- curve.getStartPoint().scale(scaling, anchor);
3038
- curve.getStartControlPoint().scale(scaling, anchor);
3039
- curve.getEndControlPoint().scale(scaling, anchor);
3083
+ curve.getStartPoint().scaleXY(scaleFactors, anchor);
3084
+ curve.getStartControlPoint().scaleXY(scaleFactors, anchor);
3085
+ curve.getEndControlPoint().scaleXY(scaleFactors, anchor);
3040
3086
  // Do NOT scale the end point here!
3041
3087
  // Don't forget that the curves are connected and on curve's end point
3042
3088
  // the the successor's start point (same instance)!
3043
3089
  }
3044
3090
  // Finally move the last end point (was not scaled yet)
3045
3091
  if (this.bezierCurves.length > 0 && !this.adjustCircular) {
3046
- this.bezierCurves[this.bezierCurves.length - 1].getEndPoint().scale(scaling, anchor);
3092
+ this.bezierCurves[this.bezierCurves.length - 1].getEndPoint().scaleXY(scaleFactors, anchor);
3047
3093
  }
3048
3094
  this.updateArcLengths();
3049
3095
  return this;
@@ -9936,7 +9982,7 @@ class PlotBoilerplate {
9936
9982
  var blob = new Blob([svgCode], { type: "image/svg;charset=utf-8" });
9937
9983
  // See documentation for FileSaver.js for usage.
9938
9984
  // https://github.com/eligrey/FileSaver.js
9939
- if (typeof globalThis["saveAs"] != "function")
9985
+ if (typeof globalThis["saveAs"] !== "function")
9940
9986
  throw "Cannot save file; did you load the ./utils/savefile helper function and the eligrey/SaveFile library?";
9941
9987
  var _saveAs = globalThis["saveAs"];
9942
9988
  _saveAs(blob, "plotboilerplate.svg");
@@ -10042,7 +10088,6 @@ class PlotBoilerplate {
10042
10088
  add(drawable, redraw) {
10043
10089
  if (Array.isArray(drawable)) {
10044
10090
  const arr = drawable;
10045
- // for( var i in arr )
10046
10091
  for (var i = 0; i < arr.length; i++) {
10047
10092
  this.add(arr[i], false);
10048
10093
  }
@@ -10088,9 +10133,9 @@ class PlotBoilerplate {
10088
10133
  }
10089
10134
  else if (drawable instanceof Polygon) {
10090
10135
  this.drawables.push(drawable);
10091
- // for( var i in drawable.vertices )
10092
- for (var i = 0; i < drawable.vertices.length; i++)
10136
+ for (var i = 0; i < drawable.vertices.length; i++) {
10093
10137
  this.vertices.push(drawable.vertices[i]);
10138
+ }
10094
10139
  }
10095
10140
  else if (drawable instanceof Triangle) {
10096
10141
  this.drawables.push(drawable);
@@ -10102,8 +10147,9 @@ class PlotBoilerplate {
10102
10147
  this.drawables.push(drawable);
10103
10148
  const bezierPath = drawable;
10104
10149
  for (var i = 0; i < bezierPath.bezierCurves.length; i++) {
10105
- if (!drawable.adjustCircular && i == 0)
10150
+ if (!drawable.adjustCircular && i == 0) {
10106
10151
  this.vertices.push(bezierPath.bezierCurves[i].startPoint);
10152
+ }
10107
10153
  this.vertices.push(bezierPath.bezierCurves[i].endPoint);
10108
10154
  this.vertices.push(bezierPath.bezierCurves[i].startControlPoint);
10109
10155
  this.vertices.push(bezierPath.bezierCurves[i].endControlPoint);
@@ -10249,8 +10295,9 @@ class PlotBoilerplate {
10249
10295
  for (var i = 0; i < this.vertices.length; i++) {
10250
10296
  if (this.vertices[i] === vert) {
10251
10297
  this.vertices.splice(i, 1);
10252
- if (redraw)
10298
+ if (redraw) {
10253
10299
  this.redraw();
10300
+ }
10254
10301
  return;
10255
10302
  }
10256
10303
  }
@@ -10841,7 +10888,6 @@ class PlotBoilerplate {
10841
10888
  // Apply the zoom (the tolerant area should not shrink or grow when zooming)
10842
10889
  tolerance /= _self.draw.scale.x;
10843
10890
  // Search in vertices
10844
- // for( var vindex in _self.vertices ) {
10845
10891
  for (var vindex = 0; vindex < _self.vertices.length; vindex++) {
10846
10892
  var vert = _self.vertices[vindex];
10847
10893
  if ((vert.attr.draggable || vert.attr.selectable) && vert.distance(point) < tolerance) {
@@ -10861,10 +10907,7 @@ class PlotBoilerplate {
10861
10907
  * @return {void}
10862
10908
  **/
10863
10909
  handleClick(e) {
10864
- // x:number,y:number) {
10865
10910
  const _self = this;
10866
- // const x:number = e.params.pos.x;
10867
- //const y:number = e.params.pos.y;
10868
10911
  var p = this.locatePointNear(_self.transformMousePosition(e.params.pos.x, e.params.pos.y), PlotBoilerplate.DEFAULT_CLICK_TOLERANCE / Math.min(_self.config.cssScaleX, _self.config.cssScaleY));
10869
10912
  if (p) {
10870
10913
  _self.vertices[p.vindex].listeners.fireClickEvent(e);
@@ -10881,7 +10924,7 @@ class PlotBoilerplate {
10881
10924
  }
10882
10925
  _self.redraw();
10883
10926
  }
10884
- else if (this.keyHandler.isDown("y") /* && p.type=='bpath' && (p.pid==BezierPath.START_POINT || p.pid==BezierPath.END_POINT) */) {
10927
+ else if (this.keyHandler.isDown("y")) {
10885
10928
  _self.vertices[p.vindex].attr.bezierAutoAdjust = !_self.vertices[p.vindex].attr.bezierAutoAdjust;
10886
10929
  _self.redraw();
10887
10930
  }
@@ -11179,6 +11222,7 @@ class PlotBoilerplate {
11179
11222
  _self.draggedElements = [];
11180
11223
  };
11181
11224
  const afProps = {
11225
+ // touchStart: (evt: TouchEvent) => {
11182
11226
  touchStart: (evt) => {
11183
11227
  if (evt.touches.length == 1) {
11184
11228
  touchMovePos = new Vertex(relPos({ x: evt.touches[0].clientX, y: evt.touches[0].clientY }));
@@ -11281,10 +11325,13 @@ class PlotBoilerplate {
11281
11325
  _self.redraw();
11282
11326
  }
11283
11327
  }; // END afProps
11284
- if (window["createAlloyFinger"])
11328
+ if (window["createAlloyFinger"]) {
11285
11329
  window["createAlloyFinger"](this.eventCatcher ? this.eventCatcher : this.canvas, afProps);
11286
- else
11330
+ }
11331
+ else {
11332
+ /* tslint:disable-next-line */
11287
11333
  new AlloyFinger(this.eventCatcher ? this.eventCatcher : this.canvas, afProps);
11334
+ }
11288
11335
  }
11289
11336
  catch (e) {
11290
11337
  console.error("Failed to initialize AlloyFinger!");