pts 0.10.3 → 0.10.7

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/dist/pts.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * pts.js 0.10.3 - Copyright © 2017-2021 William Ngan and contributors.
2
+ * pts.js 0.10.7 - Copyright © 2017-2021 William Ngan and contributors.
3
3
  * Licensed under Apache 2.0 License.
4
4
  * See https://github.com/williamngan/pts for details.
5
5
  */
@@ -187,8 +187,7 @@ class CanvasSpace extends Space_1.MultiTouchSpace {
187
187
  callback(this.bound, this._canvas);
188
188
  }
189
189
  setup(opt) {
190
- if (opt.bgcolor)
191
- this._bgcolor = opt.bgcolor;
190
+ this._bgcolor = opt.bgcolor ? opt.bgcolor : "transparent";
192
191
  this.autoResize = (opt.resize != undefined) ? opt.resize : false;
193
192
  if (opt.retina !== false) {
194
193
  let r1 = window ? window.devicePixelRatio || 1 : 1;
@@ -203,6 +202,9 @@ class CanvasSpace extends Space_1.MultiTouchSpace {
203
202
  else {
204
203
  this._offscreen = false;
205
204
  }
205
+ if (opt.pixelDensity) {
206
+ this._pixelScale = opt.pixelDensity;
207
+ }
206
208
  return this;
207
209
  }
208
210
  set autoResize(auto) {
@@ -280,17 +282,15 @@ class CanvasSpace extends Space_1.MultiTouchSpace {
280
282
  if (bg)
281
283
  this._bgcolor = bg;
282
284
  const lastColor = this._ctx.fillStyle;
283
- if (this._bgcolor) {
284
- if (this._bgcolor === "transparent") {
285
+ if (!this._bgcolor || this._bgcolor === "transparent") {
286
+ this._ctx.clearRect(-1, -1, this._canvas.width + 1, this._canvas.height + 1);
287
+ }
288
+ else {
289
+ if (this._bgcolor.indexOf("rgba") === 0 || (this._bgcolor.length === 9 && this._bgcolor.indexOf("#") === 0)) {
285
290
  this._ctx.clearRect(-1, -1, this._canvas.width + 1, this._canvas.height + 1);
286
291
  }
287
- else {
288
- if (this._bgcolor.indexOf("rgba") === 0 || (this._bgcolor.length === 9 && this._bgcolor.indexOf("#") === 0)) {
289
- this._ctx.clearRect(-1, -1, this._canvas.width + 1, this._canvas.height + 1);
290
- }
291
- this._ctx.fillStyle = this._bgcolor;
292
- this._ctx.fillRect(-1, -1, this._canvas.width + 1, this._canvas.height + 1);
293
- }
292
+ this._ctx.fillStyle = this._bgcolor;
293
+ this._ctx.fillRect(-1, -1, this._canvas.width + 1, this._canvas.height + 1);
294
294
  }
295
295
  this._ctx.fillStyle = lastColor;
296
296
  return this;
@@ -327,6 +327,24 @@ class CanvasSpace extends Space_1.MultiTouchSpace {
327
327
  this.removeAll();
328
328
  return this;
329
329
  }
330
+ recorder(downloadOrCallback, filetype = "webm", bitrate = 15000000) {
331
+ let stream = this._canvas.captureStream();
332
+ const recorder = new MediaRecorder(stream, { mimeType: `video/${filetype}`, bitsPerSecond: bitrate });
333
+ recorder.ondataavailable = function (d) {
334
+ let url = URL.createObjectURL(new Blob([d.data], { type: `video/${filetype}` }));
335
+ if (typeof downloadOrCallback === "function") {
336
+ downloadOrCallback(url);
337
+ }
338
+ else if (downloadOrCallback) {
339
+ let a = document.createElement("a");
340
+ a.href = url;
341
+ a.download = `canvas_video.${filetype}`;
342
+ a.click();
343
+ a.remove();
344
+ }
345
+ };
346
+ return recorder;
347
+ }
330
348
  }
331
349
  exports.CanvasSpace = CanvasSpace;
332
350
  class CanvasForm extends Form_1.VisualForm {
@@ -337,15 +355,25 @@ class CanvasForm extends Form_1.VisualForm {
337
355
  lineWidth: 1, lineJoin: "bevel", lineCap: "butt",
338
356
  globalAlpha: 1
339
357
  };
340
- this._space = space;
341
- this._space.add({ start: () => {
342
- this._ctx = this._space.ctx;
343
- this._ctx.fillStyle = this._style.fillStyle;
344
- this._ctx.strokeStyle = this._style.strokeStyle;
345
- this._ctx.lineJoin = "bevel";
346
- this._ctx.font = this._font.value;
347
- this._ready = true;
348
- } });
358
+ if (!space)
359
+ return this;
360
+ const _setup = (ctx) => {
361
+ this._ctx = ctx;
362
+ this._ctx.fillStyle = this._style.fillStyle;
363
+ this._ctx.strokeStyle = this._style.strokeStyle;
364
+ this._ctx.lineJoin = "bevel";
365
+ this._ctx.font = this._font.value;
366
+ this._ready = true;
367
+ };
368
+ if (space instanceof CanvasRenderingContext2D) {
369
+ _setup(space);
370
+ }
371
+ else {
372
+ this._space = space;
373
+ this._space.add({ start: () => {
374
+ _setup(this._space.ctx);
375
+ } });
376
+ }
349
377
  }
350
378
  get space() { return this._space; }
351
379
  get ctx() { return this._space.ctx; }
@@ -511,17 +539,6 @@ class CanvasForm extends Form_1.VisualForm {
511
539
  if (this._stroked)
512
540
  this._ctx.stroke();
513
541
  }
514
- static paint(ctx, fn, fill, stroke, strokeWidth) {
515
- if (fill)
516
- ctx.fillStyle = fill;
517
- if (stroke)
518
- ctx.strokeStyle = stroke;
519
- if (strokeWidth)
520
- ctx.lineWidth = strokeWidth;
521
- fn(ctx);
522
- ctx.fill();
523
- ctx.stroke();
524
- }
525
542
  static point(ctx, p, radius = 5, shape = "square") {
526
543
  if (!p)
527
544
  return;
@@ -897,10 +914,10 @@ class Color extends Pt_1.Pt {
897
914
  return `#${_hex(this[0])}${_hex(this[1])}${_hex(this[2])}`;
898
915
  }
899
916
  else if (format == "rgba") {
900
- return `rgba(${Math.floor(this[0])},${Math.floor(this[1])},${Math.floor(this[2])},${this.alpha}`;
917
+ return `rgba(${Math.floor(this[0])},${Math.floor(this[1])},${Math.floor(this[2])},${this.alpha})`;
901
918
  }
902
919
  else if (format == "rgb") {
903
- return `rgb(${Math.floor(this[0])},${Math.floor(this[1])},${Math.floor(this[2])}`;
920
+ return `rgb(${Math.floor(this[0])},${Math.floor(this[1])},${Math.floor(this[2])})`;
904
921
  }
905
922
  else {
906
923
  return `${this._mode}(${this[0]},${this[1]},${this[2]},${this.alpha})`;
@@ -1034,12 +1051,12 @@ class Color extends Pt_1.Pt {
1034
1051
  static XYZtoRGB(xyz, normalizedInput = false, normalizedOutput = false) {
1035
1052
  let [x, y, z] = (!normalizedInput) ? xyz.$normalize() : xyz;
1036
1053
  let rgb = [
1037
- x * 3.2404542 + y * -1.5371385 + z * -0.4985314,
1038
- x * -0.9692660 + y * 1.8760108 + z * 0.0415560,
1039
- x * 0.0556434 + y * -0.2040259 + z * 1.0572252
1054
+ x * 3.2406254773200533 + y * -1.5372079722103187 + z * -0.4986285986982479,
1055
+ x * -0.9689307147293197 + y * 1.8757560608852415 + z * 0.041517523842953964,
1056
+ x * 0.055710120445510616 + y * -0.2040210505984867 + z * 1.0569959422543882
1040
1057
  ];
1041
1058
  for (let i = 0; i < 3; i++) {
1042
- rgb[i] = (rgb[i] < 0) ? 0 : (rgb[i] > 0.0031308) ? (1.055 * Math.pow(rgb[i], 1 / 2.4) - 0.055) : (12.92 * rgb[i]);
1059
+ rgb[i] = (rgb[i] > 0.0031308) ? (1.055 * Math.pow(rgb[i], 1 / 2.4) - 0.055) : (12.92 * rgb[i]);
1043
1060
  rgb[i] = Math.max(0, Math.min(1, rgb[i]));
1044
1061
  if (!normalizedOutput)
1045
1062
  rgb[i] = Math.round(rgb[i] * 255);
@@ -1049,8 +1066,10 @@ class Color extends Pt_1.Pt {
1049
1066
  }
1050
1067
  static XYZtoLAB(xyz, normalizedInput = false, normalizedOutput = false) {
1051
1068
  let c = (normalizedInput) ? xyz.$normalize(false) : xyz.clone();
1069
+ const eps = 0.00885645167;
1070
+ const kap = 903.296296296;
1052
1071
  c.divide(Color.D65);
1053
- let fn = (n) => (n > 0.008856) ? Math.pow(n, 1 / 3) : (7.787 * n) + 16 / 116;
1072
+ let fn = (n) => (n > eps) ? Math.pow(n, 1 / 3) : (kap * n + 16) / 116;
1054
1073
  let cy = fn(c[1]);
1055
1074
  let cc = Color.lab((116 * cy) - 16, 500 * (fn(c[0]) - cy), 200 * (cy - fn(c[2])), xyz.alpha);
1056
1075
  return (normalizedOutput) ? cc.normalize() : cc;
@@ -1060,12 +1079,12 @@ class Color extends Pt_1.Pt {
1060
1079
  let y = (c[0] + 16) / 116;
1061
1080
  let x = (c[1] / 500) + y;
1062
1081
  let z = y - c[2] / 200;
1063
- let fn = (n) => {
1064
- let nnn = n * n * n;
1065
- return (nnn > 0.008856) ? nnn : (n - 16 / 116) / 7.787;
1066
- };
1082
+ const eps = 0.00885645167;
1083
+ const kap = 903.296296296;
1067
1084
  let d = Color.D65;
1068
- let cc = Color.xyz(Math.max(0, d[0] * fn(x)), Math.max(0, d[1] * fn(y)), Math.max(0, d[2] * fn(z)), lab.alpha);
1085
+ const xxx = Math.pow(x, 3);
1086
+ const zzz = Math.pow(z, 3);
1087
+ let cc = Color.xyz(d[0] * ((xxx > eps) ? xxx : (116 * x - 16) / kap), d[1] * ((c[0] > kap * eps) ? Math.pow((c[0] + 16) / 116, 3) : c[0] / kap), d[2] * ((zzz > eps) ? zzz : (116 * z - 16) / kap), lab.alpha);
1069
1088
  return (normalizedOutput) ? cc.normalize() : cc;
1070
1089
  }
1071
1090
  static XYZtoLUV(xyz, normalizedInput = false, normalizedOutput = false) {
@@ -1140,11 +1159,11 @@ class Create {
1140
1159
  static distributeRandom(bound, count, dimensions = 2) {
1141
1160
  let pts = new Pt_1.Group();
1142
1161
  for (let i = 0; i < count; i++) {
1143
- let p = [bound.x + Math.random() * bound.width];
1162
+ let p = [bound.x + Num_1.Num.random() * bound.width];
1144
1163
  if (dimensions > 1)
1145
- p.push(bound.y + Math.random() * bound.height);
1164
+ p.push(bound.y + Num_1.Num.random() * bound.height);
1146
1165
  if (dimensions > 2)
1147
- p.push(bound.z + Math.random() * bound.depth);
1166
+ p.push(bound.z + Num_1.Num.random() * bound.depth);
1148
1167
  pts.push(new Pt_1.Pt(p));
1149
1168
  }
1150
1169
  return pts;
@@ -1190,7 +1209,7 @@ class Create {
1190
1209
  return g;
1191
1210
  }
1192
1211
  static noisePts(pts, dx = 0.01, dy = 0.01, rows = 0, columns = 0) {
1193
- let seed = Math.random();
1212
+ let seed = Num_1.Num.random();
1194
1213
  let g = new Pt_1.Group();
1195
1214
  let i = 0;
1196
1215
  for (let p of pts) {
@@ -1986,12 +2005,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
1986
2005
  exports.Img = void 0;
1987
2006
  const Pt_1 = __webpack_require__(/*! ./Pt */ "./src/Pt.ts");
1988
2007
  class Img {
1989
- constructor(editable = false, pixelScale = 1) {
2008
+ constructor(editable = false, pixelScale = 1, crossOrigin) {
1990
2009
  this._scale = 1;
1991
2010
  this._loaded = false;
1992
2011
  this._editable = editable;
1993
2012
  this._scale = pixelScale;
1994
2013
  this._img = new Image();
2014
+ if (crossOrigin)
2015
+ this._img.crossOrigin = "Anonymous";
1995
2016
  }
1996
2017
  static load(src, editable = false, pixelScale = 1, ready) {
1997
2018
  let img = new Img(editable, pixelScale);
@@ -2072,15 +2093,41 @@ class Img {
2072
2093
  }
2073
2094
  filter(css) {
2074
2095
  this._ctx.filter = css;
2096
+ this._ctx.drawImage(this._cv, 0, 0);
2097
+ this._ctx.filter = "none";
2075
2098
  return this;
2076
2099
  }
2077
- static fromBlob(blob, editable = false) {
2100
+ cleanup() {
2101
+ if (this._cv)
2102
+ this._cv.remove();
2103
+ if (this._img)
2104
+ this._img.remove();
2105
+ this._data = null;
2106
+ }
2107
+ static fromBlob(blob, editable = false, pixelScale = 1) {
2078
2108
  let url = URL.createObjectURL(blob);
2079
- return new Img(editable).load(url);
2109
+ return new Img(editable, pixelScale).load(url);
2110
+ }
2111
+ static imageDataToBlob(data) {
2112
+ return new Promise(function (resolve) {
2113
+ let cv = document.createElement("canvas");
2114
+ cv.width = data.width;
2115
+ cv.height = data.height;
2116
+ cv.getContext("2d").putImageData(data, 0, 0);
2117
+ cv.toBlob(blob => {
2118
+ resolve(blob);
2119
+ cv.remove();
2120
+ });
2121
+ });
2080
2122
  }
2081
2123
  toBase64() {
2082
2124
  return this._cv.toDataURL();
2083
2125
  }
2126
+ toBlob() {
2127
+ return new Promise((resolve) => {
2128
+ this._cv.toBlob(blob => resolve(blob));
2129
+ });
2130
+ }
2084
2131
  get current() {
2085
2132
  return this._editable ? this._cv : this._img;
2086
2133
  }
@@ -2397,6 +2444,7 @@ const Util_1 = __webpack_require__(/*! ./Util */ "./src/Util.ts");
2397
2444
  const Op_1 = __webpack_require__(/*! ./Op */ "./src/Op.ts");
2398
2445
  const Pt_1 = __webpack_require__(/*! ./Pt */ "./src/Pt.ts");
2399
2446
  const LinearAlgebra_1 = __webpack_require__(/*! ./LinearAlgebra */ "./src/LinearAlgebra.ts");
2447
+ const uheprng_1 = __webpack_require__(/*! ./uheprng */ "./src/uheprng.ts");
2400
2448
  class Num {
2401
2449
  static equals(a, b, threshold = 0.00001) {
2402
2450
  return Math.abs(a - b) < threshold;
@@ -2421,14 +2469,14 @@ class Num {
2421
2469
  }
2422
2470
  static randomRange(a, b = 0) {
2423
2471
  let r = (a > b) ? (a - b) : (b - a);
2424
- return a + Math.random() * r;
2472
+ return a + Num.random() * r;
2425
2473
  }
2426
2474
  static randomPt(a, b) {
2427
2475
  let p = new Pt_1.Pt(a.length);
2428
2476
  let range = b ? LinearAlgebra_1.Vec.subtract(b, a) : a;
2429
2477
  let start = b ? a : new Pt_1.Pt(a.length).fill(0);
2430
2478
  for (let i = 0, len = p.length; i < len; i++) {
2431
- p[i] = Math.random() * range[i] + start[i];
2479
+ p[i] = Num.random() * range[i] + start[i];
2432
2480
  }
2433
2481
  return p;
2434
2482
  }
@@ -2459,6 +2507,14 @@ class Num {
2459
2507
  let max = Math.max(targetA, targetB);
2460
2508
  return Num.normalizeValue(n, currA, currB) * (max - min) + min;
2461
2509
  }
2510
+ static seed(seed) {
2511
+ this.generator = uheprng_1.default(seed);
2512
+ }
2513
+ static random() {
2514
+ return this.generator
2515
+ ? this.generator.random()
2516
+ : Math.random();
2517
+ }
2462
2518
  }
2463
2519
  exports.Num = Num;
2464
2520
  class Geom {
@@ -4650,7 +4706,7 @@ class Pt extends Float32Array {
4650
4706
  p.fill(defaultValue);
4651
4707
  if (randomize) {
4652
4708
  for (let i = 0, len = p.length; i < len; i++) {
4653
- p[i] = p[i] * Math.random();
4709
+ p[i] = p[i] * Num_1.Num.random();
4654
4710
  }
4655
4711
  }
4656
4712
  return new Pt(p);
@@ -5238,8 +5294,8 @@ class MultiTouchSpace extends Space {
5238
5294
  bindCanvas(evt, callback, options = {}) {
5239
5295
  this._canvas.addEventListener(evt, callback, options);
5240
5296
  }
5241
- unbindCanvas(evt, callback) {
5242
- this._canvas.removeEventListener(evt, callback);
5297
+ unbindCanvas(evt, callback, options = {}) {
5298
+ this._canvas.removeEventListener(evt, callback, options);
5243
5299
  }
5244
5300
  bindMouse(_bind = true) {
5245
5301
  if (_bind) {
@@ -5264,18 +5320,18 @@ class MultiTouchSpace extends Space {
5264
5320
  }
5265
5321
  return this;
5266
5322
  }
5267
- bindTouch(_bind = true) {
5268
- if (_bind) {
5269
- this.bindCanvas("touchstart", this._touchStart.bind(this), { passive: true });
5323
+ bindTouch(bind = true, passive = false) {
5324
+ if (bind) {
5325
+ this.bindCanvas("touchstart", this._touchStart.bind(this), { passive: passive });
5270
5326
  this.bindCanvas("touchend", this._mouseUp.bind(this));
5271
- this.bindCanvas("touchmove", this._touchMove.bind(this), { passive: true });
5327
+ this.bindCanvas("touchmove", this._touchMove.bind(this), { passive: passive });
5272
5328
  this.bindCanvas("touchcancel", this._mouseOut.bind(this));
5273
5329
  this._hasTouch = true;
5274
5330
  }
5275
5331
  else {
5276
- this.unbindCanvas("touchstart", this._touchStart.bind(this));
5332
+ this.unbindCanvas("touchstart", this._touchStart.bind(this), { passive: passive });
5277
5333
  this.unbindCanvas("touchend", this._mouseUp.bind(this));
5278
- this.unbindCanvas("touchmove", this._touchMove.bind(this));
5334
+ this.unbindCanvas("touchmove", this._touchMove.bind(this), { passive: passive });
5279
5335
  this.unbindCanvas("touchcancel", this._mouseOut.bind(this));
5280
5336
  this._hasTouch = false;
5281
5337
  }
@@ -6134,6 +6190,7 @@ exports.UIDragger = UIDragger;
6134
6190
  /*! Source code licensed under Apache License 2.0. Copyright © 2017-current William Ngan and contributors. (https://github.com/williamngan/pts) */
6135
6191
  Object.defineProperty(exports, "__esModule", { value: true });
6136
6192
  exports.Util = exports.Const = void 0;
6193
+ const Num_1 = __webpack_require__(/*! ./Num */ "./src/Num.ts");
6137
6194
  const Pt_1 = __webpack_require__(/*! ./Pt */ "./src/Pt.ts");
6138
6195
  exports.Const = {
6139
6196
  xy: "xy",
@@ -6205,7 +6262,7 @@ class Util {
6205
6262
  }
6206
6263
  static randomInt(range, start = 0) {
6207
6264
  Util.warn("Util.randomInt is deprecated. Please use `Num.randomRange`");
6208
- return Math.floor(Math.random() * range) + start;
6265
+ return Math.floor(Num_1.Num.random() * range) + start;
6209
6266
  }
6210
6267
  static split(pts, size, stride, loopBack = false, matchSize = true) {
6211
6268
  let chunks = [];
@@ -6392,6 +6449,88 @@ let quickStart = (id, bg = "#9ab") => {
6392
6449
  exports.quickStart = quickStart;
6393
6450
 
6394
6451
 
6452
+ /***/ }),
6453
+
6454
+ /***/ "./src/uheprng.ts":
6455
+ /*!************************!*\
6456
+ !*** ./src/uheprng.ts ***!
6457
+ \************************/
6458
+ /*! no static exports found */
6459
+ /***/ (function(module, exports, __webpack_require__) {
6460
+
6461
+ "use strict";
6462
+
6463
+ Object.defineProperty(exports, "__esModule", { value: true });
6464
+ function Mash() {
6465
+ var n = 0xefc8249d;
6466
+ var mash = function (data) {
6467
+ if (data) {
6468
+ data = data.toString();
6469
+ for (var i = 0; i < data.length; i++) {
6470
+ n += data.charCodeAt(i);
6471
+ var h = 0.02519603282416938 * n;
6472
+ n = h >>> 0;
6473
+ h -= n;
6474
+ h *= n;
6475
+ n = h >>> 0;
6476
+ h -= n;
6477
+ n += h * 0x100000000;
6478
+ }
6479
+ return (n >>> 0) * 2.3283064365386963e-10;
6480
+ }
6481
+ else
6482
+ n = 0xefc8249d;
6483
+ };
6484
+ return mash;
6485
+ }
6486
+ function default_1(seed) {
6487
+ var o = 48;
6488
+ var c = 1;
6489
+ var p = o;
6490
+ var s = new Array(o);
6491
+ var i, j, k = 0;
6492
+ var mash = Mash();
6493
+ for (i = 0; i < o; i++)
6494
+ s[i] = mash(Math.random().toString());
6495
+ function initState() {
6496
+ mash();
6497
+ for (i = 0; i < o; i++)
6498
+ s[i] = mash(' ');
6499
+ c = 1;
6500
+ p = o;
6501
+ }
6502
+ function cleanString(inStr) {
6503
+ inStr = inStr.replace(/(^\s*)|(\s*$)/gi, '');
6504
+ inStr = inStr.replace(/[\x00-\x1F]/gi, '');
6505
+ inStr = inStr.replace(/\n /, '\n');
6506
+ return inStr;
6507
+ }
6508
+ function hashString(inStr) {
6509
+ inStr = cleanString(inStr);
6510
+ mash(inStr);
6511
+ for (i = 0; i < inStr.length; i++) {
6512
+ k = inStr.charCodeAt(i);
6513
+ for (j = 0; j < o; j++) {
6514
+ s[j] -= mash(k.toString());
6515
+ if (s[j] < 0)
6516
+ s[j] += 1;
6517
+ }
6518
+ }
6519
+ }
6520
+ initState();
6521
+ hashString(seed);
6522
+ return {
6523
+ random() {
6524
+ if (++p >= o)
6525
+ p = 0;
6526
+ var t = 1768863 * s[p] + c * 2.3283064365386963e-10;
6527
+ return (s[p] = t - (c = t | 0));
6528
+ }
6529
+ };
6530
+ }
6531
+ exports.default = default_1;
6532
+
6533
+
6395
6534
  /***/ })
6396
6535
 
6397
6536
  /******/ });