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.
@@ -73,8 +73,7 @@ export class CanvasSpace extends MultiTouchSpace {
73
73
  callback(this.bound, this._canvas);
74
74
  }
75
75
  setup(opt) {
76
- if (opt.bgcolor)
77
- this._bgcolor = opt.bgcolor;
76
+ this._bgcolor = opt.bgcolor ? opt.bgcolor : "transparent";
78
77
  this.autoResize = (opt.resize != undefined) ? opt.resize : false;
79
78
  if (opt.retina !== false) {
80
79
  let r1 = window ? window.devicePixelRatio || 1 : 1;
@@ -89,6 +88,9 @@ export class CanvasSpace extends MultiTouchSpace {
89
88
  else {
90
89
  this._offscreen = false;
91
90
  }
91
+ if (opt.pixelDensity) {
92
+ this._pixelScale = opt.pixelDensity;
93
+ }
92
94
  return this;
93
95
  }
94
96
  set autoResize(auto) {
@@ -166,17 +168,15 @@ export class CanvasSpace extends MultiTouchSpace {
166
168
  if (bg)
167
169
  this._bgcolor = bg;
168
170
  const lastColor = this._ctx.fillStyle;
169
- if (this._bgcolor) {
170
- if (this._bgcolor === "transparent") {
171
+ if (!this._bgcolor || this._bgcolor === "transparent") {
172
+ this._ctx.clearRect(-1, -1, this._canvas.width + 1, this._canvas.height + 1);
173
+ }
174
+ else {
175
+ if (this._bgcolor.indexOf("rgba") === 0 || (this._bgcolor.length === 9 && this._bgcolor.indexOf("#") === 0)) {
171
176
  this._ctx.clearRect(-1, -1, this._canvas.width + 1, this._canvas.height + 1);
172
177
  }
173
- else {
174
- if (this._bgcolor.indexOf("rgba") === 0 || (this._bgcolor.length === 9 && this._bgcolor.indexOf("#") === 0)) {
175
- this._ctx.clearRect(-1, -1, this._canvas.width + 1, this._canvas.height + 1);
176
- }
177
- this._ctx.fillStyle = this._bgcolor;
178
- this._ctx.fillRect(-1, -1, this._canvas.width + 1, this._canvas.height + 1);
179
- }
178
+ this._ctx.fillStyle = this._bgcolor;
179
+ this._ctx.fillRect(-1, -1, this._canvas.width + 1, this._canvas.height + 1);
180
180
  }
181
181
  this._ctx.fillStyle = lastColor;
182
182
  return this;
@@ -213,6 +213,24 @@ export class CanvasSpace extends MultiTouchSpace {
213
213
  this.removeAll();
214
214
  return this;
215
215
  }
216
+ recorder(downloadOrCallback, filetype = "webm", bitrate = 15000000) {
217
+ let stream = this._canvas.captureStream();
218
+ const recorder = new MediaRecorder(stream, { mimeType: `video/${filetype}`, bitsPerSecond: bitrate });
219
+ recorder.ondataavailable = function (d) {
220
+ let url = URL.createObjectURL(new Blob([d.data], { type: `video/${filetype}` }));
221
+ if (typeof downloadOrCallback === "function") {
222
+ downloadOrCallback(url);
223
+ }
224
+ else if (downloadOrCallback) {
225
+ let a = document.createElement("a");
226
+ a.href = url;
227
+ a.download = `canvas_video.${filetype}`;
228
+ a.click();
229
+ a.remove();
230
+ }
231
+ };
232
+ return recorder;
233
+ }
216
234
  }
217
235
  export class CanvasForm extends VisualForm {
218
236
  constructor(space) {
@@ -222,15 +240,25 @@ export class CanvasForm extends VisualForm {
222
240
  lineWidth: 1, lineJoin: "bevel", lineCap: "butt",
223
241
  globalAlpha: 1
224
242
  };
225
- this._space = space;
226
- this._space.add({ start: () => {
227
- this._ctx = this._space.ctx;
228
- this._ctx.fillStyle = this._style.fillStyle;
229
- this._ctx.strokeStyle = this._style.strokeStyle;
230
- this._ctx.lineJoin = "bevel";
231
- this._ctx.font = this._font.value;
232
- this._ready = true;
233
- } });
243
+ if (!space)
244
+ return this;
245
+ const _setup = (ctx) => {
246
+ this._ctx = ctx;
247
+ this._ctx.fillStyle = this._style.fillStyle;
248
+ this._ctx.strokeStyle = this._style.strokeStyle;
249
+ this._ctx.lineJoin = "bevel";
250
+ this._ctx.font = this._font.value;
251
+ this._ready = true;
252
+ };
253
+ if (space instanceof CanvasRenderingContext2D) {
254
+ _setup(space);
255
+ }
256
+ else {
257
+ this._space = space;
258
+ this._space.add({ start: () => {
259
+ _setup(this._space.ctx);
260
+ } });
261
+ }
234
262
  }
235
263
  get space() { return this._space; }
236
264
  get ctx() { return this._space.ctx; }
@@ -396,17 +424,6 @@ export class CanvasForm extends VisualForm {
396
424
  if (this._stroked)
397
425
  this._ctx.stroke();
398
426
  }
399
- static paint(ctx, fn, fill, stroke, strokeWidth) {
400
- if (fill)
401
- ctx.fillStyle = fill;
402
- if (stroke)
403
- ctx.strokeStyle = stroke;
404
- if (strokeWidth)
405
- ctx.lineWidth = strokeWidth;
406
- fn(ctx);
407
- ctx.fill();
408
- ctx.stroke();
409
- }
410
427
  static point(ctx, p, radius = 5, shape = "square") {
411
428
  if (!p)
412
429
  return;
@@ -115,10 +115,10 @@ export class Color extends Pt {
115
115
  return `#${_hex(this[0])}${_hex(this[1])}${_hex(this[2])}`;
116
116
  }
117
117
  else if (format == "rgba") {
118
- return `rgba(${Math.floor(this[0])},${Math.floor(this[1])},${Math.floor(this[2])},${this.alpha}`;
118
+ return `rgba(${Math.floor(this[0])},${Math.floor(this[1])},${Math.floor(this[2])},${this.alpha})`;
119
119
  }
120
120
  else if (format == "rgb") {
121
- return `rgb(${Math.floor(this[0])},${Math.floor(this[1])},${Math.floor(this[2])}`;
121
+ return `rgb(${Math.floor(this[0])},${Math.floor(this[1])},${Math.floor(this[2])})`;
122
122
  }
123
123
  else {
124
124
  return `${this._mode}(${this[0]},${this[1]},${this[2]},${this.alpha})`;
@@ -252,12 +252,12 @@ export class Color extends Pt {
252
252
  static XYZtoRGB(xyz, normalizedInput = false, normalizedOutput = false) {
253
253
  let [x, y, z] = (!normalizedInput) ? xyz.$normalize() : xyz;
254
254
  let rgb = [
255
- x * 3.2404542 + y * -1.5371385 + z * -0.4985314,
256
- x * -0.9692660 + y * 1.8760108 + z * 0.0415560,
257
- x * 0.0556434 + y * -0.2040259 + z * 1.0572252
255
+ x * 3.2406254773200533 + y * -1.5372079722103187 + z * -0.4986285986982479,
256
+ x * -0.9689307147293197 + y * 1.8757560608852415 + z * 0.041517523842953964,
257
+ x * 0.055710120445510616 + y * -0.2040210505984867 + z * 1.0569959422543882
258
258
  ];
259
259
  for (let i = 0; i < 3; i++) {
260
- 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]);
260
+ rgb[i] = (rgb[i] > 0.0031308) ? (1.055 * Math.pow(rgb[i], 1 / 2.4) - 0.055) : (12.92 * rgb[i]);
261
261
  rgb[i] = Math.max(0, Math.min(1, rgb[i]));
262
262
  if (!normalizedOutput)
263
263
  rgb[i] = Math.round(rgb[i] * 255);
@@ -267,8 +267,10 @@ export class Color extends Pt {
267
267
  }
268
268
  static XYZtoLAB(xyz, normalizedInput = false, normalizedOutput = false) {
269
269
  let c = (normalizedInput) ? xyz.$normalize(false) : xyz.clone();
270
+ const eps = 0.00885645167;
271
+ const kap = 903.296296296;
270
272
  c.divide(Color.D65);
271
- let fn = (n) => (n > 0.008856) ? Math.pow(n, 1 / 3) : (7.787 * n) + 16 / 116;
273
+ let fn = (n) => (n > eps) ? Math.pow(n, 1 / 3) : (kap * n + 16) / 116;
272
274
  let cy = fn(c[1]);
273
275
  let cc = Color.lab((116 * cy) - 16, 500 * (fn(c[0]) - cy), 200 * (cy - fn(c[2])), xyz.alpha);
274
276
  return (normalizedOutput) ? cc.normalize() : cc;
@@ -278,12 +280,12 @@ export class Color extends Pt {
278
280
  let y = (c[0] + 16) / 116;
279
281
  let x = (c[1] / 500) + y;
280
282
  let z = y - c[2] / 200;
281
- let fn = (n) => {
282
- let nnn = n * n * n;
283
- return (nnn > 0.008856) ? nnn : (n - 16 / 116) / 7.787;
284
- };
283
+ const eps = 0.00885645167;
284
+ const kap = 903.296296296;
285
285
  let d = Color.D65;
286
- 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);
286
+ const xxx = Math.pow(x, 3);
287
+ const zzz = Math.pow(z, 3);
288
+ 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);
287
289
  return (normalizedOutput) ? cc.normalize() : cc;
288
290
  }
289
291
  static XYZtoLUV(xyz, normalizedInput = false, normalizedOutput = false) {
@@ -8,11 +8,11 @@ export class Create {
8
8
  static distributeRandom(bound, count, dimensions = 2) {
9
9
  let pts = new Group();
10
10
  for (let i = 0; i < count; i++) {
11
- let p = [bound.x + Math.random() * bound.width];
11
+ let p = [bound.x + Num.random() * bound.width];
12
12
  if (dimensions > 1)
13
- p.push(bound.y + Math.random() * bound.height);
13
+ p.push(bound.y + Num.random() * bound.height);
14
14
  if (dimensions > 2)
15
- p.push(bound.z + Math.random() * bound.depth);
15
+ p.push(bound.z + Num.random() * bound.depth);
16
16
  pts.push(new Pt(p));
17
17
  }
18
18
  return pts;
@@ -58,7 +58,7 @@ export class Create {
58
58
  return g;
59
59
  }
60
60
  static noisePts(pts, dx = 0.01, dy = 0.01, rows = 0, columns = 0) {
61
- let seed = Math.random();
61
+ let seed = Num.random();
62
62
  let g = new Group();
63
63
  let i = 0;
64
64
  for (let p of pts) {
@@ -1,11 +1,13 @@
1
1
  import { Pt } from "./Pt";
2
2
  export class Img {
3
- constructor(editable = false, pixelScale = 1) {
3
+ constructor(editable = false, pixelScale = 1, crossOrigin) {
4
4
  this._scale = 1;
5
5
  this._loaded = false;
6
6
  this._editable = editable;
7
7
  this._scale = pixelScale;
8
8
  this._img = new Image();
9
+ if (crossOrigin)
10
+ this._img.crossOrigin = "Anonymous";
9
11
  }
10
12
  static load(src, editable = false, pixelScale = 1, ready) {
11
13
  let img = new Img(editable, pixelScale);
@@ -86,15 +88,41 @@ export class Img {
86
88
  }
87
89
  filter(css) {
88
90
  this._ctx.filter = css;
91
+ this._ctx.drawImage(this._cv, 0, 0);
92
+ this._ctx.filter = "none";
89
93
  return this;
90
94
  }
91
- static fromBlob(blob, editable = false) {
95
+ cleanup() {
96
+ if (this._cv)
97
+ this._cv.remove();
98
+ if (this._img)
99
+ this._img.remove();
100
+ this._data = null;
101
+ }
102
+ static fromBlob(blob, editable = false, pixelScale = 1) {
92
103
  let url = URL.createObjectURL(blob);
93
- return new Img(editable).load(url);
104
+ return new Img(editable, pixelScale).load(url);
105
+ }
106
+ static imageDataToBlob(data) {
107
+ return new Promise(function (resolve) {
108
+ let cv = document.createElement("canvas");
109
+ cv.width = data.width;
110
+ cv.height = data.height;
111
+ cv.getContext("2d").putImageData(data, 0, 0);
112
+ cv.toBlob(blob => {
113
+ resolve(blob);
114
+ cv.remove();
115
+ });
116
+ });
94
117
  }
95
118
  toBase64() {
96
119
  return this._cv.toDataURL();
97
120
  }
121
+ toBlob() {
122
+ return new Promise((resolve) => {
123
+ this._cv.toBlob(blob => resolve(blob));
124
+ });
125
+ }
98
126
  get current() {
99
127
  return this._editable ? this._cv : this._img;
100
128
  }
@@ -3,6 +3,7 @@ import { Const, Util } from "./Util";
3
3
  import { Curve } from "./Op";
4
4
  import { Pt, Group } from "./Pt";
5
5
  import { Vec, Mat } from "./LinearAlgebra";
6
+ import generator from './uheprng';
6
7
  export class Num {
7
8
  static equals(a, b, threshold = 0.00001) {
8
9
  return Math.abs(a - b) < threshold;
@@ -27,14 +28,14 @@ export class Num {
27
28
  }
28
29
  static randomRange(a, b = 0) {
29
30
  let r = (a > b) ? (a - b) : (b - a);
30
- return a + Math.random() * r;
31
+ return a + Num.random() * r;
31
32
  }
32
33
  static randomPt(a, b) {
33
34
  let p = new Pt(a.length);
34
35
  let range = b ? Vec.subtract(b, a) : a;
35
36
  let start = b ? a : new Pt(a.length).fill(0);
36
37
  for (let i = 0, len = p.length; i < len; i++) {
37
- p[i] = Math.random() * range[i] + start[i];
38
+ p[i] = Num.random() * range[i] + start[i];
38
39
  }
39
40
  return p;
40
41
  }
@@ -65,6 +66,14 @@ export class Num {
65
66
  let max = Math.max(targetA, targetB);
66
67
  return Num.normalizeValue(n, currA, currB) * (max - min) + min;
67
68
  }
69
+ static seed(seed) {
70
+ this.generator = generator(seed);
71
+ }
72
+ static random() {
73
+ return this.generator
74
+ ? this.generator.random()
75
+ : Math.random();
76
+ }
68
77
  }
69
78
  export class Geom {
70
79
  static boundAngle(angle) {
package/dist/es2015/Pt.js CHANGED
@@ -17,7 +17,7 @@ export class Pt extends Float32Array {
17
17
  p.fill(defaultValue);
18
18
  if (randomize) {
19
19
  for (let i = 0, len = p.length; i < len; i++) {
20
- p[i] = p[i] * Math.random();
20
+ p[i] = p[i] * Num.random();
21
21
  }
22
22
  }
23
23
  return new Pt(p);
@@ -127,8 +127,8 @@ export class MultiTouchSpace extends Space {
127
127
  bindCanvas(evt, callback, options = {}) {
128
128
  this._canvas.addEventListener(evt, callback, options);
129
129
  }
130
- unbindCanvas(evt, callback) {
131
- this._canvas.removeEventListener(evt, callback);
130
+ unbindCanvas(evt, callback, options = {}) {
131
+ this._canvas.removeEventListener(evt, callback, options);
132
132
  }
133
133
  bindMouse(_bind = true) {
134
134
  if (_bind) {
@@ -153,18 +153,18 @@ export class MultiTouchSpace extends Space {
153
153
  }
154
154
  return this;
155
155
  }
156
- bindTouch(_bind = true) {
157
- if (_bind) {
158
- this.bindCanvas("touchstart", this._touchStart.bind(this), { passive: true });
156
+ bindTouch(bind = true, passive = false) {
157
+ if (bind) {
158
+ this.bindCanvas("touchstart", this._touchStart.bind(this), { passive: passive });
159
159
  this.bindCanvas("touchend", this._mouseUp.bind(this));
160
- this.bindCanvas("touchmove", this._touchMove.bind(this), { passive: true });
160
+ this.bindCanvas("touchmove", this._touchMove.bind(this), { passive: passive });
161
161
  this.bindCanvas("touchcancel", this._mouseOut.bind(this));
162
162
  this._hasTouch = true;
163
163
  }
164
164
  else {
165
- this.unbindCanvas("touchstart", this._touchStart.bind(this));
165
+ this.unbindCanvas("touchstart", this._touchStart.bind(this), { passive: passive });
166
166
  this.unbindCanvas("touchend", this._mouseUp.bind(this));
167
- this.unbindCanvas("touchmove", this._touchMove.bind(this));
167
+ this.unbindCanvas("touchmove", this._touchMove.bind(this), { passive: passive });
168
168
  this.unbindCanvas("touchcancel", this._mouseOut.bind(this));
169
169
  this._hasTouch = false;
170
170
  }
@@ -1,4 +1,5 @@
1
1
  /*! Source code licensed under Apache License 2.0. Copyright © 2017-current William Ngan and contributors. (https://github.com/williamngan/pts) */
2
+ import { Num } from "./Num";
2
3
  import { Group } from "./Pt";
3
4
  export const Const = {
4
5
  xy: "xy",
@@ -70,7 +71,7 @@ export class Util {
70
71
  }
71
72
  static randomInt(range, start = 0) {
72
73
  Util.warn("Util.randomInt is deprecated. Please use `Num.randomRange`");
73
- return Math.floor(Math.random() * range) + start;
74
+ return Math.floor(Num.random() * range) + start;
74
75
  }
75
76
  static split(pts, size, stride, loopBack = false, matchSize = true) {
76
77
  let chunks = [];
@@ -14,4 +14,5 @@ export * from "./Typography";
14
14
  export * from "./Physics";
15
15
  export * from "./Play";
16
16
  export * from "./UI";
17
+ export * from "./Image";
17
18
  //# sourceMappingURL=_module.js.map
@@ -0,0 +1,69 @@
1
+ 'use strict';
2
+ function Mash() {
3
+ var n = 0xefc8249d;
4
+ var mash = function (data) {
5
+ if (data) {
6
+ data = data.toString();
7
+ for (var i = 0; i < data.length; i++) {
8
+ n += data.charCodeAt(i);
9
+ var h = 0.02519603282416938 * n;
10
+ n = h >>> 0;
11
+ h -= n;
12
+ h *= n;
13
+ n = h >>> 0;
14
+ h -= n;
15
+ n += h * 0x100000000;
16
+ }
17
+ return (n >>> 0) * 2.3283064365386963e-10;
18
+ }
19
+ else
20
+ n = 0xefc8249d;
21
+ };
22
+ return mash;
23
+ }
24
+ export default function (seed) {
25
+ var o = 48;
26
+ var c = 1;
27
+ var p = o;
28
+ var s = new Array(o);
29
+ var i, j, k = 0;
30
+ var mash = Mash();
31
+ for (i = 0; i < o; i++)
32
+ s[i] = mash(Math.random().toString());
33
+ function initState() {
34
+ mash();
35
+ for (i = 0; i < o; i++)
36
+ s[i] = mash(' ');
37
+ c = 1;
38
+ p = o;
39
+ }
40
+ function cleanString(inStr) {
41
+ inStr = inStr.replace(/(^\s*)|(\s*$)/gi, '');
42
+ inStr = inStr.replace(/[\x00-\x1F]/gi, '');
43
+ inStr = inStr.replace(/\n /, '\n');
44
+ return inStr;
45
+ }
46
+ function hashString(inStr) {
47
+ inStr = cleanString(inStr);
48
+ mash(inStr);
49
+ for (i = 0; i < inStr.length; i++) {
50
+ k = inStr.charCodeAt(i);
51
+ for (j = 0; j < o; j++) {
52
+ s[j] -= mash(k.toString());
53
+ if (s[j] < 0)
54
+ s[j] += 1;
55
+ }
56
+ }
57
+ }
58
+ initState();
59
+ hashString(seed);
60
+ return {
61
+ random() {
62
+ if (++p >= o)
63
+ p = 0;
64
+ var t = 1768863 * s[p] + c * 2.3283064365386963e-10;
65
+ return (s[p] = t - (c = t | 0));
66
+ }
67
+ };
68
+ }
69
+ //# sourceMappingURL=uheprng.js.map