pts 0.10.12 → 0.11.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/dist/{pts.d.ts → index.d.ts} +1245 -1207
- package/dist/index.js +6825 -6660
- package/dist/index.mjs +6785 -0
- package/package.json +12 -33
- package/tsup.browser.js +21 -0
- package/tsup.min.js +23 -0
- package/tsup.module.js +15 -0
- package/dist/es2015/Canvas.js +0 -684
- package/dist/es2015/Canvas.js.map +0 -1
- package/dist/es2015/Color.js +0 -337
- package/dist/es2015/Color.js.map +0 -1
- package/dist/es2015/Create.js +0 -275
- package/dist/es2015/Create.js.map +0 -1
- package/dist/es2015/Dom.js +0 -448
- package/dist/es2015/Dom.js.map +0 -1
- package/dist/es2015/Form.js +0 -80
- package/dist/es2015/Form.js.map +0 -1
- package/dist/es2015/Image.js +0 -220
- package/dist/es2015/Image.js.map +0 -1
- package/dist/es2015/LinearAlgebra.js +0 -286
- package/dist/es2015/LinearAlgebra.js.map +0 -1
- package/dist/es2015/Num.js +0 -477
- package/dist/es2015/Num.js.map +0 -1
- package/dist/es2015/Op.js +0 -978
- package/dist/es2015/Op.js.map +0 -1
- package/dist/es2015/Physics.js +0 -383
- package/dist/es2015/Physics.js.map +0 -1
- package/dist/es2015/Play.js +0 -339
- package/dist/es2015/Play.js.map +0 -1
- package/dist/es2015/Pt.js +0 -463
- package/dist/es2015/Pt.js.map +0 -1
- package/dist/es2015/Space.js +0 -331
- package/dist/es2015/Space.js.map +0 -1
- package/dist/es2015/Svg.js +0 -367
- package/dist/es2015/Svg.js.map +0 -1
- package/dist/es2015/Types.js +0 -3
- package/dist/es2015/Types.js.map +0 -1
- package/dist/es2015/Typography.js +0 -40
- package/dist/es2015/Typography.js.map +0 -1
- package/dist/es2015/UI.js +0 -273
- package/dist/es2015/UI.js.map +0 -1
- package/dist/es2015/Util.js +0 -200
- package/dist/es2015/Util.js.map +0 -1
- package/dist/es2015/_lib.js +0 -45
- package/dist/es2015/_lib.js.map +0 -1
- package/dist/es2015/_module.js +0 -18
- package/dist/es2015/_module.js.map +0 -1
- package/dist/es2015/uheprng.js +0 -69
- package/dist/es2015/uheprng.js.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/es2015/Canvas.js
DELETED
|
@@ -1,684 +0,0 @@
|
|
|
1
|
-
/*! Pts.js is licensed under Apache License 2.0. Copyright © 2017-current William Ngan and contributors. (https://github.com/williamngan/pts) */
|
|
2
|
-
import { MultiTouchSpace } from './Space';
|
|
3
|
-
import { VisualForm, Font } from "./Form";
|
|
4
|
-
import { Pt, Group, Bound } from "./Pt";
|
|
5
|
-
import { Const, Util } from "./Util";
|
|
6
|
-
import { Typography as Typo } from "./Typography";
|
|
7
|
-
import { Rectangle } from './Op';
|
|
8
|
-
import { Img } from './Image';
|
|
9
|
-
export class CanvasSpace extends MultiTouchSpace {
|
|
10
|
-
constructor(elem, callback) {
|
|
11
|
-
super();
|
|
12
|
-
this._pixelScale = 1;
|
|
13
|
-
this._autoResize = true;
|
|
14
|
-
this._bgcolor = "#e1e9f0";
|
|
15
|
-
this._offscreen = false;
|
|
16
|
-
this._initialResize = false;
|
|
17
|
-
var _selector = null;
|
|
18
|
-
var _existed = false;
|
|
19
|
-
this.id = "pt";
|
|
20
|
-
if (elem instanceof Element) {
|
|
21
|
-
_selector = elem;
|
|
22
|
-
this.id = "pts_existing_space";
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
let id = elem;
|
|
26
|
-
id = (elem[0] === "#" || elem[0] === ".") ? elem : "#" + elem;
|
|
27
|
-
_selector = document.querySelector(id);
|
|
28
|
-
_existed = true;
|
|
29
|
-
this.id = id.substr(1);
|
|
30
|
-
}
|
|
31
|
-
if (!_selector) {
|
|
32
|
-
this._container = this._createElement("div", this.id + "_container");
|
|
33
|
-
this._canvas = this._createElement("canvas", this.id);
|
|
34
|
-
this._container.appendChild(this._canvas);
|
|
35
|
-
document.body.appendChild(this._container);
|
|
36
|
-
_existed = false;
|
|
37
|
-
}
|
|
38
|
-
else if (_selector.nodeName.toLowerCase() != "canvas") {
|
|
39
|
-
this._container = _selector;
|
|
40
|
-
this._canvas = this._createElement("canvas", this.id + "_canvas");
|
|
41
|
-
this._container.appendChild(this._canvas);
|
|
42
|
-
this._initialResize = true;
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
this._canvas = _selector;
|
|
46
|
-
this._container = _selector.parentElement;
|
|
47
|
-
this._autoResize = false;
|
|
48
|
-
}
|
|
49
|
-
setTimeout(this._ready.bind(this, callback), 100);
|
|
50
|
-
this._ctx = this._canvas.getContext('2d');
|
|
51
|
-
}
|
|
52
|
-
_createElement(elem = "div", id) {
|
|
53
|
-
let d = document.createElement(elem);
|
|
54
|
-
d.setAttribute("id", id);
|
|
55
|
-
return d;
|
|
56
|
-
}
|
|
57
|
-
_ready(callback) {
|
|
58
|
-
if (!this._container)
|
|
59
|
-
throw new Error(`Cannot initiate #${this.id} element`);
|
|
60
|
-
this._isReady = true;
|
|
61
|
-
this._resizeHandler(null);
|
|
62
|
-
this.clear(this._bgcolor);
|
|
63
|
-
this._canvas.dispatchEvent(new Event("ready"));
|
|
64
|
-
for (let k in this.players) {
|
|
65
|
-
if (this.players.hasOwnProperty(k)) {
|
|
66
|
-
if (this.players[k].start)
|
|
67
|
-
this.players[k].start(this.bound.clone(), this);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
this._pointer = this.center;
|
|
71
|
-
this._initialResize = false;
|
|
72
|
-
if (callback)
|
|
73
|
-
callback(this.bound, this._canvas);
|
|
74
|
-
}
|
|
75
|
-
setup(opt) {
|
|
76
|
-
this._bgcolor = opt.bgcolor ? opt.bgcolor : "transparent";
|
|
77
|
-
this.autoResize = (opt.resize != undefined) ? opt.resize : false;
|
|
78
|
-
if (opt.retina !== false) {
|
|
79
|
-
let r1 = window ? window.devicePixelRatio || 1 : 1;
|
|
80
|
-
let r2 = this._ctx.webkitBackingStorePixelRatio || this._ctx.mozBackingStorePixelRatio || this._ctx.msBackingStorePixelRatio || this._ctx.oBackingStorePixelRatio || this._ctx.backingStorePixelRatio || 1;
|
|
81
|
-
this._pixelScale = Math.max(1, r1 / r2);
|
|
82
|
-
}
|
|
83
|
-
if (opt.offscreen) {
|
|
84
|
-
this._offscreen = true;
|
|
85
|
-
this._offCanvas = this._createElement("canvas", this.id + "_offscreen");
|
|
86
|
-
this._offCtx = this._offCanvas.getContext('2d');
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
this._offscreen = false;
|
|
90
|
-
}
|
|
91
|
-
if (opt.pixelDensity) {
|
|
92
|
-
this._pixelScale = opt.pixelDensity;
|
|
93
|
-
}
|
|
94
|
-
return this;
|
|
95
|
-
}
|
|
96
|
-
set autoResize(auto) {
|
|
97
|
-
if (!window)
|
|
98
|
-
return;
|
|
99
|
-
this._autoResize = auto;
|
|
100
|
-
if (auto) {
|
|
101
|
-
window.addEventListener('resize', this._resizeHandler.bind(this));
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
window.removeEventListener('resize', this._resizeHandler.bind(this));
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
get autoResize() { return this._autoResize; }
|
|
108
|
-
resize(b, evt) {
|
|
109
|
-
this.bound = b;
|
|
110
|
-
this._canvas.width = Math.ceil(this.bound.size.x) * this._pixelScale;
|
|
111
|
-
this._canvas.height = Math.ceil(this.bound.size.y) * this._pixelScale;
|
|
112
|
-
this._canvas.style.width = Math.ceil(this.bound.size.x) + "px";
|
|
113
|
-
this._canvas.style.height = Math.ceil(this.bound.size.y) + "px";
|
|
114
|
-
if (this._offscreen) {
|
|
115
|
-
this._offCanvas.width = Math.ceil(this.bound.size.x) * this._pixelScale;
|
|
116
|
-
this._offCanvas.height = Math.ceil(this.bound.size.y) * this._pixelScale;
|
|
117
|
-
}
|
|
118
|
-
if (this._pixelScale != 1) {
|
|
119
|
-
this._ctx.scale(this._pixelScale, this._pixelScale);
|
|
120
|
-
if (this._offscreen) {
|
|
121
|
-
this._offCtx.scale(this._pixelScale, this._pixelScale);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
for (let k in this.players) {
|
|
125
|
-
if (this.players.hasOwnProperty(k)) {
|
|
126
|
-
let p = this.players[k];
|
|
127
|
-
if (p.resize)
|
|
128
|
-
p.resize(this.bound, evt);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
this.render(this._ctx);
|
|
132
|
-
if (evt && !this.isPlaying)
|
|
133
|
-
this.playOnce(0);
|
|
134
|
-
return this;
|
|
135
|
-
}
|
|
136
|
-
_resizeHandler(evt) {
|
|
137
|
-
if (!window)
|
|
138
|
-
return;
|
|
139
|
-
let b = (this._autoResize || this._initialResize) ? this._container.getBoundingClientRect() : this._canvas.getBoundingClientRect();
|
|
140
|
-
if (b) {
|
|
141
|
-
let box = Bound.fromBoundingRect(b);
|
|
142
|
-
box.center = box.center.add(window.pageXOffset, window.pageYOffset);
|
|
143
|
-
this.resize(box, evt);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
set background(bg) { this._bgcolor = bg; }
|
|
147
|
-
get background() { return this._bgcolor; }
|
|
148
|
-
get pixelScale() {
|
|
149
|
-
return this._pixelScale;
|
|
150
|
-
}
|
|
151
|
-
get hasOffscreen() {
|
|
152
|
-
return this._offscreen;
|
|
153
|
-
}
|
|
154
|
-
get offscreenCtx() { return this._offCtx; }
|
|
155
|
-
get offscreenCanvas() { return this._offCanvas; }
|
|
156
|
-
getForm() { return new CanvasForm(this); }
|
|
157
|
-
get element() {
|
|
158
|
-
return this._canvas;
|
|
159
|
-
}
|
|
160
|
-
get parent() {
|
|
161
|
-
return this._container;
|
|
162
|
-
}
|
|
163
|
-
get ready() {
|
|
164
|
-
return this._isReady;
|
|
165
|
-
}
|
|
166
|
-
get ctx() { return this._ctx; }
|
|
167
|
-
clear(bg) {
|
|
168
|
-
if (bg)
|
|
169
|
-
this._bgcolor = bg;
|
|
170
|
-
const lastColor = this._ctx.fillStyle;
|
|
171
|
-
const px = Math.ceil(this.pixelScale);
|
|
172
|
-
if (!this._bgcolor || this._bgcolor === "transparent") {
|
|
173
|
-
this._ctx.clearRect(-px, -px, this._canvas.width + px, this._canvas.height + px);
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
if (this._bgcolor.indexOf("rgba") === 0 || (this._bgcolor.length === 9 && this._bgcolor.indexOf("#") === 0)) {
|
|
177
|
-
this._ctx.clearRect(-px, -px, this._canvas.width + px, this._canvas.height + px);
|
|
178
|
-
}
|
|
179
|
-
this._ctx.fillStyle = this._bgcolor;
|
|
180
|
-
this._ctx.fillRect(-px, -px, this._canvas.width + px, this._canvas.height + px);
|
|
181
|
-
}
|
|
182
|
-
this._ctx.fillStyle = lastColor;
|
|
183
|
-
return this;
|
|
184
|
-
}
|
|
185
|
-
clearOffscreen(bg) {
|
|
186
|
-
if (this._offscreen) {
|
|
187
|
-
const px = Math.ceil(this.pixelScale);
|
|
188
|
-
if (bg) {
|
|
189
|
-
this._offCtx.fillStyle = bg;
|
|
190
|
-
this._offCtx.fillRect(-px, -px, this._canvas.width + px, this._canvas.height + px);
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
this._offCtx.clearRect(-px, -px, this._offCanvas.width + px, this._offCanvas.height + px);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return this;
|
|
197
|
-
}
|
|
198
|
-
playItems(time) {
|
|
199
|
-
if (this._isReady) {
|
|
200
|
-
this._ctx.save();
|
|
201
|
-
if (this._offscreen)
|
|
202
|
-
this._offCtx.save();
|
|
203
|
-
super.playItems(time);
|
|
204
|
-
this._ctx.restore();
|
|
205
|
-
if (this._offscreen)
|
|
206
|
-
this._offCtx.restore();
|
|
207
|
-
this.render(this._ctx);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
dispose() {
|
|
211
|
-
if (!window)
|
|
212
|
-
return;
|
|
213
|
-
window.removeEventListener('resize', this._resizeHandler.bind(this));
|
|
214
|
-
this.stop();
|
|
215
|
-
this.removeAll();
|
|
216
|
-
return this;
|
|
217
|
-
}
|
|
218
|
-
recorder(downloadOrCallback, filetype = "webm", bitrate = 15000000) {
|
|
219
|
-
let stream = this._canvas.captureStream();
|
|
220
|
-
const recorder = new MediaRecorder(stream, { mimeType: `video/${filetype}`, bitsPerSecond: bitrate });
|
|
221
|
-
recorder.ondataavailable = function (d) {
|
|
222
|
-
let url = URL.createObjectURL(new Blob([d.data], { type: `video/${filetype}` }));
|
|
223
|
-
if (typeof downloadOrCallback === "function") {
|
|
224
|
-
downloadOrCallback(url);
|
|
225
|
-
}
|
|
226
|
-
else if (downloadOrCallback) {
|
|
227
|
-
let a = document.createElement("a");
|
|
228
|
-
a.href = url;
|
|
229
|
-
a.download = `canvas_video.${filetype}`;
|
|
230
|
-
a.click();
|
|
231
|
-
a.remove();
|
|
232
|
-
}
|
|
233
|
-
};
|
|
234
|
-
return recorder;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
export class CanvasForm extends VisualForm {
|
|
238
|
-
constructor(space) {
|
|
239
|
-
super();
|
|
240
|
-
this._style = {
|
|
241
|
-
fillStyle: "#f03", strokeStyle: "#fff",
|
|
242
|
-
lineWidth: 1, lineJoin: "bevel", lineCap: "butt",
|
|
243
|
-
globalAlpha: 1
|
|
244
|
-
};
|
|
245
|
-
if (!space)
|
|
246
|
-
return this;
|
|
247
|
-
const _setup = (ctx) => {
|
|
248
|
-
this._ctx = ctx;
|
|
249
|
-
this._ctx.fillStyle = this._style.fillStyle;
|
|
250
|
-
this._ctx.strokeStyle = this._style.strokeStyle;
|
|
251
|
-
this._ctx.lineJoin = "bevel";
|
|
252
|
-
this._ctx.font = this._font.value;
|
|
253
|
-
this._ready = true;
|
|
254
|
-
};
|
|
255
|
-
if (space instanceof CanvasRenderingContext2D) {
|
|
256
|
-
_setup(space);
|
|
257
|
-
}
|
|
258
|
-
else {
|
|
259
|
-
this._space = space;
|
|
260
|
-
this._space.add({ start: () => {
|
|
261
|
-
_setup(this._space.ctx);
|
|
262
|
-
} });
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
get space() { return this._space; }
|
|
266
|
-
get ctx() { return this._ctx; }
|
|
267
|
-
useOffscreen(off = true, clear = false) {
|
|
268
|
-
if (clear)
|
|
269
|
-
this._space.clearOffscreen((typeof clear == "string") ? clear : null);
|
|
270
|
-
this._ctx = (this._space.hasOffscreen && off) ? this._space.offscreenCtx : this._space.ctx;
|
|
271
|
-
return this;
|
|
272
|
-
}
|
|
273
|
-
renderOffscreen(offset = [0, 0]) {
|
|
274
|
-
if (this._space.hasOffscreen) {
|
|
275
|
-
this._space.ctx.drawImage(this._space.offscreenCanvas, offset[0], offset[1], this._space.width, this._space.height);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
alpha(a) {
|
|
279
|
-
this._ctx.globalAlpha = a;
|
|
280
|
-
this._style.globalAlpha = a;
|
|
281
|
-
return this;
|
|
282
|
-
}
|
|
283
|
-
fill(c) {
|
|
284
|
-
if (typeof c == "boolean") {
|
|
285
|
-
this.filled = c;
|
|
286
|
-
}
|
|
287
|
-
else {
|
|
288
|
-
this.filled = true;
|
|
289
|
-
this._style.fillStyle = c;
|
|
290
|
-
this._ctx.fillStyle = c;
|
|
291
|
-
}
|
|
292
|
-
return this;
|
|
293
|
-
}
|
|
294
|
-
stroke(c, width, linejoin, linecap) {
|
|
295
|
-
if (typeof c == "boolean") {
|
|
296
|
-
this.stroked = c;
|
|
297
|
-
}
|
|
298
|
-
else {
|
|
299
|
-
this.stroked = true;
|
|
300
|
-
this._style.strokeStyle = c;
|
|
301
|
-
this._ctx.strokeStyle = c;
|
|
302
|
-
if (width) {
|
|
303
|
-
this._ctx.lineWidth = width;
|
|
304
|
-
this._style.lineWidth = width;
|
|
305
|
-
}
|
|
306
|
-
if (linejoin) {
|
|
307
|
-
this._ctx.lineJoin = linejoin;
|
|
308
|
-
this._style.lineJoin = linejoin;
|
|
309
|
-
}
|
|
310
|
-
if (linecap) {
|
|
311
|
-
this._ctx.lineCap = linecap;
|
|
312
|
-
this._style.lineCap = linecap;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
return this;
|
|
316
|
-
}
|
|
317
|
-
applyFillStroke(filled = true, stroked = true, strokeWidth = 1) {
|
|
318
|
-
if (filled) {
|
|
319
|
-
if (typeof filled === 'string')
|
|
320
|
-
this.fill(filled);
|
|
321
|
-
this._ctx.fill();
|
|
322
|
-
}
|
|
323
|
-
if (stroked) {
|
|
324
|
-
if (typeof stroked === 'string')
|
|
325
|
-
this.stroke(stroked, strokeWidth);
|
|
326
|
-
this._ctx.stroke();
|
|
327
|
-
}
|
|
328
|
-
return this;
|
|
329
|
-
}
|
|
330
|
-
gradient(stops) {
|
|
331
|
-
let vals = [];
|
|
332
|
-
if (stops.length < 2)
|
|
333
|
-
stops.push([0.99, "#000"], [1, "#000"]);
|
|
334
|
-
for (let i = 0, len = stops.length; i < len; i++) {
|
|
335
|
-
let t = typeof stops[i] === 'string' ? i * (1 / (stops.length - 1)) : stops[i][0];
|
|
336
|
-
let v = typeof stops[i] === 'string' ? stops[i] : stops[i][1];
|
|
337
|
-
vals.push([t, v]);
|
|
338
|
-
}
|
|
339
|
-
return (area1, area2) => {
|
|
340
|
-
area1 = area1.map(a => a.abs());
|
|
341
|
-
if (area2)
|
|
342
|
-
area2.map(a => a.abs());
|
|
343
|
-
let grad = area2
|
|
344
|
-
? this._ctx.createRadialGradient(area1[0][0], area1[0][1], area1[1][0], area2[0][0], area2[0][1], area2[1][0])
|
|
345
|
-
: this._ctx.createLinearGradient(area1[0][0], area1[0][1], area1[1][0], area1[1][1]);
|
|
346
|
-
for (let i = 0, len = vals.length; i < len; i++) {
|
|
347
|
-
grad.addColorStop(vals[i][0], vals[i][1]);
|
|
348
|
-
}
|
|
349
|
-
return grad;
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
composite(mode = 'source-over') {
|
|
353
|
-
this._ctx.globalCompositeOperation = mode;
|
|
354
|
-
return this;
|
|
355
|
-
}
|
|
356
|
-
clip() {
|
|
357
|
-
this._ctx.clip();
|
|
358
|
-
return this;
|
|
359
|
-
}
|
|
360
|
-
dash(segments = true, offset = 0) {
|
|
361
|
-
if (!segments) {
|
|
362
|
-
this._ctx.setLineDash([]);
|
|
363
|
-
this._ctx.lineDashOffset = 0;
|
|
364
|
-
}
|
|
365
|
-
else {
|
|
366
|
-
if (segments === true) {
|
|
367
|
-
segments = [5, 5];
|
|
368
|
-
}
|
|
369
|
-
this._ctx.setLineDash([segments[0], segments[1]]);
|
|
370
|
-
this._ctx.lineDashOffset = offset;
|
|
371
|
-
}
|
|
372
|
-
return this;
|
|
373
|
-
}
|
|
374
|
-
font(sizeOrFont, weight, style, lineHeight, family) {
|
|
375
|
-
if (typeof sizeOrFont == "number") {
|
|
376
|
-
this._font.size = sizeOrFont;
|
|
377
|
-
if (family)
|
|
378
|
-
this._font.face = family;
|
|
379
|
-
if (weight)
|
|
380
|
-
this._font.weight = weight;
|
|
381
|
-
if (style)
|
|
382
|
-
this._font.style = style;
|
|
383
|
-
if (lineHeight)
|
|
384
|
-
this._font.lineHeight = lineHeight;
|
|
385
|
-
}
|
|
386
|
-
else {
|
|
387
|
-
this._font = sizeOrFont;
|
|
388
|
-
}
|
|
389
|
-
this._ctx.font = this._font.value;
|
|
390
|
-
if (this._estimateTextWidth)
|
|
391
|
-
this.fontWidthEstimate(true);
|
|
392
|
-
return this;
|
|
393
|
-
}
|
|
394
|
-
fontWidthEstimate(estimate = true) {
|
|
395
|
-
this._estimateTextWidth = (estimate) ? Typo.textWidthEstimator(((c) => this._ctx.measureText(c).width)) : undefined;
|
|
396
|
-
return this;
|
|
397
|
-
}
|
|
398
|
-
getTextWidth(c) {
|
|
399
|
-
return (!this._estimateTextWidth) ? this._ctx.measureText(c + " .").width : this._estimateTextWidth(c);
|
|
400
|
-
}
|
|
401
|
-
_textTruncate(str, width, tail = "") {
|
|
402
|
-
return Typo.truncate(this.getTextWidth.bind(this), str, width, tail);
|
|
403
|
-
}
|
|
404
|
-
_textAlign(box, vertical, offset, center) {
|
|
405
|
-
let _box = Util.iterToArray(box);
|
|
406
|
-
if (!Util.arrayCheck(_box))
|
|
407
|
-
return;
|
|
408
|
-
if (!center)
|
|
409
|
-
center = Rectangle.center(_box);
|
|
410
|
-
var px = _box[0][0];
|
|
411
|
-
if (this._ctx.textAlign == "end" || this._ctx.textAlign == "right") {
|
|
412
|
-
px = _box[1][0];
|
|
413
|
-
}
|
|
414
|
-
else if (this._ctx.textAlign == "center" || this._ctx.textAlign == "middle") {
|
|
415
|
-
px = center[0];
|
|
416
|
-
}
|
|
417
|
-
var py = center[1];
|
|
418
|
-
if (vertical == "top" || vertical == "start") {
|
|
419
|
-
py = _box[0][1];
|
|
420
|
-
}
|
|
421
|
-
else if (vertical == "end" || vertical == "bottom") {
|
|
422
|
-
py = _box[1][1];
|
|
423
|
-
}
|
|
424
|
-
return (offset) ? new Pt(px + offset[0], py + offset[1]) : new Pt(px, py);
|
|
425
|
-
}
|
|
426
|
-
reset() {
|
|
427
|
-
for (let k in this._style) {
|
|
428
|
-
if (this._style.hasOwnProperty(k)) {
|
|
429
|
-
this._ctx[k] = this._style[k];
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
this._font = new Font();
|
|
433
|
-
this._ctx.font = this._font.value;
|
|
434
|
-
return this;
|
|
435
|
-
}
|
|
436
|
-
_paint() {
|
|
437
|
-
if (this._filled)
|
|
438
|
-
this._ctx.fill();
|
|
439
|
-
if (this._stroked)
|
|
440
|
-
this._ctx.stroke();
|
|
441
|
-
}
|
|
442
|
-
static point(ctx, p, radius = 5, shape = "square") {
|
|
443
|
-
if (!p)
|
|
444
|
-
return;
|
|
445
|
-
if (!CanvasForm[shape])
|
|
446
|
-
throw new Error(`${shape} is not a static function of CanvasForm`);
|
|
447
|
-
CanvasForm[shape](ctx, p, radius);
|
|
448
|
-
}
|
|
449
|
-
point(p, radius = 5, shape = "square") {
|
|
450
|
-
CanvasForm.point(this._ctx, p, radius, shape);
|
|
451
|
-
this._paint();
|
|
452
|
-
return this;
|
|
453
|
-
}
|
|
454
|
-
static circle(ctx, pt, radius = 10) {
|
|
455
|
-
if (!pt)
|
|
456
|
-
return;
|
|
457
|
-
ctx.beginPath();
|
|
458
|
-
ctx.arc(pt[0], pt[1], radius, 0, Const.two_pi, false);
|
|
459
|
-
ctx.closePath();
|
|
460
|
-
}
|
|
461
|
-
circle(pts) {
|
|
462
|
-
let p = Util.iterToArray(pts);
|
|
463
|
-
CanvasForm.circle(this._ctx, p[0], p[1][0]);
|
|
464
|
-
this._paint();
|
|
465
|
-
return this;
|
|
466
|
-
}
|
|
467
|
-
static ellipse(ctx, pt, radius, rotation = 0, startAngle = 0, endAngle = Const.two_pi, cc = false) {
|
|
468
|
-
if (!pt || !radius)
|
|
469
|
-
return;
|
|
470
|
-
ctx.beginPath();
|
|
471
|
-
ctx.ellipse(pt[0], pt[1], radius[0], radius[1], rotation, startAngle, endAngle, cc);
|
|
472
|
-
}
|
|
473
|
-
ellipse(pt, radius, rotation = 0, startAngle = 0, endAngle = Const.two_pi, cc = false) {
|
|
474
|
-
CanvasForm.ellipse(this._ctx, pt, radius, rotation, startAngle, endAngle, cc);
|
|
475
|
-
this._paint();
|
|
476
|
-
return this;
|
|
477
|
-
}
|
|
478
|
-
static arc(ctx, pt, radius, startAngle, endAngle, cc) {
|
|
479
|
-
if (!pt)
|
|
480
|
-
return;
|
|
481
|
-
ctx.beginPath();
|
|
482
|
-
ctx.arc(pt[0], pt[1], radius, startAngle, endAngle, cc);
|
|
483
|
-
}
|
|
484
|
-
arc(pt, radius, startAngle, endAngle, cc) {
|
|
485
|
-
CanvasForm.arc(this._ctx, pt, radius, startAngle, endAngle, cc);
|
|
486
|
-
this._paint();
|
|
487
|
-
return this;
|
|
488
|
-
}
|
|
489
|
-
static square(ctx, pt, halfsize) {
|
|
490
|
-
if (!pt)
|
|
491
|
-
return;
|
|
492
|
-
let x1 = pt[0] - halfsize;
|
|
493
|
-
let y1 = pt[1] - halfsize;
|
|
494
|
-
let x2 = pt[0] + halfsize;
|
|
495
|
-
let y2 = pt[1] + halfsize;
|
|
496
|
-
ctx.beginPath();
|
|
497
|
-
ctx.moveTo(x1, y1);
|
|
498
|
-
ctx.lineTo(x1, y2);
|
|
499
|
-
ctx.lineTo(x2, y2);
|
|
500
|
-
ctx.lineTo(x2, y1);
|
|
501
|
-
ctx.closePath();
|
|
502
|
-
}
|
|
503
|
-
square(pt, halfsize) {
|
|
504
|
-
CanvasForm.square(this._ctx, pt, halfsize);
|
|
505
|
-
this._paint();
|
|
506
|
-
return this;
|
|
507
|
-
}
|
|
508
|
-
static line(ctx, pts) {
|
|
509
|
-
if (!Util.arrayCheck(pts))
|
|
510
|
-
return;
|
|
511
|
-
let i = 0;
|
|
512
|
-
ctx.beginPath();
|
|
513
|
-
for (let it of pts) {
|
|
514
|
-
if (it) {
|
|
515
|
-
if (i++ > 0) {
|
|
516
|
-
ctx.lineTo(it[0], it[1]);
|
|
517
|
-
}
|
|
518
|
-
else {
|
|
519
|
-
ctx.moveTo(it[0], it[1]);
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
line(pts) {
|
|
525
|
-
CanvasForm.line(this._ctx, pts);
|
|
526
|
-
this._paint();
|
|
527
|
-
return this;
|
|
528
|
-
}
|
|
529
|
-
static polygon(ctx, pts) {
|
|
530
|
-
if (!Util.arrayCheck(pts))
|
|
531
|
-
return;
|
|
532
|
-
CanvasForm.line(ctx, pts);
|
|
533
|
-
ctx.closePath();
|
|
534
|
-
}
|
|
535
|
-
polygon(pts) {
|
|
536
|
-
CanvasForm.polygon(this._ctx, pts);
|
|
537
|
-
this._paint();
|
|
538
|
-
return this;
|
|
539
|
-
}
|
|
540
|
-
static rect(ctx, pts) {
|
|
541
|
-
let p = Util.iterToArray(pts);
|
|
542
|
-
if (!Util.arrayCheck(p))
|
|
543
|
-
return;
|
|
544
|
-
ctx.beginPath();
|
|
545
|
-
ctx.moveTo(p[0][0], p[0][1]);
|
|
546
|
-
ctx.lineTo(p[0][0], p[1][1]);
|
|
547
|
-
ctx.lineTo(p[1][0], p[1][1]);
|
|
548
|
-
ctx.lineTo(p[1][0], p[0][1]);
|
|
549
|
-
ctx.closePath();
|
|
550
|
-
}
|
|
551
|
-
rect(pts) {
|
|
552
|
-
CanvasForm.rect(this._ctx, pts);
|
|
553
|
-
this._paint();
|
|
554
|
-
return this;
|
|
555
|
-
}
|
|
556
|
-
static image(ctx, ptOrRect, img, orig) {
|
|
557
|
-
let t = Util.iterToArray(ptOrRect);
|
|
558
|
-
let pos;
|
|
559
|
-
if (typeof t[0] === "number") {
|
|
560
|
-
pos = t;
|
|
561
|
-
}
|
|
562
|
-
else {
|
|
563
|
-
if (orig) {
|
|
564
|
-
let o = Util.iterToArray(orig);
|
|
565
|
-
pos = [o[0][0], o[0][1], o[1][0] - o[0][0], o[1][1] - o[0][1],
|
|
566
|
-
t[0][0], t[0][1], t[1][0] - t[0][0], t[1][1] - t[0][1]];
|
|
567
|
-
}
|
|
568
|
-
else {
|
|
569
|
-
pos = [t[0][0], t[0][1], t[1][0] - t[0][0], t[1][1] - t[0][1]];
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
if (img instanceof Img) {
|
|
573
|
-
if (img.loaded) {
|
|
574
|
-
ctx.drawImage(img.image, ...pos);
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
else {
|
|
578
|
-
ctx.drawImage(img, ...pos);
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
image(ptOrRect, img, orig) {
|
|
582
|
-
if (img instanceof Img) {
|
|
583
|
-
if (img.loaded) {
|
|
584
|
-
CanvasForm.image(this._ctx, ptOrRect, img.image, orig);
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
else {
|
|
588
|
-
CanvasForm.image(this._ctx, ptOrRect, img, orig);
|
|
589
|
-
}
|
|
590
|
-
return this;
|
|
591
|
-
}
|
|
592
|
-
static imageData(ctx, ptOrRect, img) {
|
|
593
|
-
let t = Util.iterToArray(ptOrRect);
|
|
594
|
-
if (typeof t[0] === "number") {
|
|
595
|
-
ctx.putImageData(img, t[0], t[1]);
|
|
596
|
-
}
|
|
597
|
-
else {
|
|
598
|
-
ctx.putImageData(img, t[0][0], t[0][1], t[0][0], t[0][1], t[1][0], t[1][1]);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
imageData(ptOrRect, img) {
|
|
602
|
-
CanvasForm.imageData(this._ctx, ptOrRect, img);
|
|
603
|
-
return this;
|
|
604
|
-
}
|
|
605
|
-
static text(ctx, pt, txt, maxWidth) {
|
|
606
|
-
if (!pt)
|
|
607
|
-
return;
|
|
608
|
-
ctx.fillText(txt, pt[0], pt[1], maxWidth);
|
|
609
|
-
}
|
|
610
|
-
text(pt, txt, maxWidth) {
|
|
611
|
-
CanvasForm.text(this._ctx, pt, txt, maxWidth);
|
|
612
|
-
return this;
|
|
613
|
-
}
|
|
614
|
-
textBox(box, txt, verticalAlign = "middle", tail = "", overrideBaseline = true) {
|
|
615
|
-
if (overrideBaseline)
|
|
616
|
-
this._ctx.textBaseline = verticalAlign;
|
|
617
|
-
let size = Rectangle.size(box);
|
|
618
|
-
let t = this._textTruncate(txt, size[0], tail);
|
|
619
|
-
this.text(this._textAlign(box, verticalAlign), t[0]);
|
|
620
|
-
return this;
|
|
621
|
-
}
|
|
622
|
-
paragraphBox(box, txt, lineHeight = 1.2, verticalAlign = "top", crop = true) {
|
|
623
|
-
let b = Util.iterToArray(box);
|
|
624
|
-
let size = Rectangle.size(b);
|
|
625
|
-
this._ctx.textBaseline = "top";
|
|
626
|
-
let lstep = this._font.size * lineHeight;
|
|
627
|
-
let nextLine = (sub, buffer = [], cc = 0) => {
|
|
628
|
-
if (!sub)
|
|
629
|
-
return buffer;
|
|
630
|
-
if (crop && cc * lstep > size[1] - lstep * 2)
|
|
631
|
-
return buffer;
|
|
632
|
-
if (cc > 10000)
|
|
633
|
-
throw new Error("max recursion reached (10000)");
|
|
634
|
-
let t = this._textTruncate(sub, size[0], "");
|
|
635
|
-
let newln = t[0].indexOf("\n");
|
|
636
|
-
if (newln >= 0) {
|
|
637
|
-
buffer.push(t[0].substr(0, newln));
|
|
638
|
-
return nextLine(sub.substr(newln + 1), buffer, cc + 1);
|
|
639
|
-
}
|
|
640
|
-
let dt = t[0].lastIndexOf(" ") + 1;
|
|
641
|
-
if (dt <= 0 || t[1] === sub.length)
|
|
642
|
-
dt = undefined;
|
|
643
|
-
let line = t[0].substr(0, dt);
|
|
644
|
-
buffer.push(line);
|
|
645
|
-
return (t[1] <= 0 || t[1] === sub.length) ? buffer : nextLine(sub.substr((dt || t[1])), buffer, cc + 1);
|
|
646
|
-
};
|
|
647
|
-
let lines = nextLine(txt);
|
|
648
|
-
let lsize = lines.length * lstep;
|
|
649
|
-
let lbox = b;
|
|
650
|
-
if (verticalAlign == "middle" || verticalAlign == "center") {
|
|
651
|
-
let lpad = (size[1] - lsize) / 2;
|
|
652
|
-
if (crop)
|
|
653
|
-
lpad = Math.max(0, lpad);
|
|
654
|
-
lbox = new Group(b[0].$add(0, lpad), b[1].$subtract(0, lpad));
|
|
655
|
-
}
|
|
656
|
-
else if (verticalAlign == "bottom") {
|
|
657
|
-
lbox = new Group(b[0].$add(0, size[1] - lsize), b[1]);
|
|
658
|
-
}
|
|
659
|
-
else {
|
|
660
|
-
lbox = new Group(b[0], b[0].$add(size[0], lsize));
|
|
661
|
-
}
|
|
662
|
-
let center = Rectangle.center(lbox);
|
|
663
|
-
for (let i = 0, len = lines.length; i < len; i++) {
|
|
664
|
-
this.text(this._textAlign(lbox, "top", [0, i * lstep], center), lines[i]);
|
|
665
|
-
}
|
|
666
|
-
return this;
|
|
667
|
-
}
|
|
668
|
-
alignText(alignment = "left", baseline = "alphabetic") {
|
|
669
|
-
if (baseline == "center")
|
|
670
|
-
baseline = "middle";
|
|
671
|
-
if (baseline == "baseline")
|
|
672
|
-
baseline = "alphabetic";
|
|
673
|
-
this._ctx.textAlign = alignment;
|
|
674
|
-
this._ctx.textBaseline = baseline;
|
|
675
|
-
return this;
|
|
676
|
-
}
|
|
677
|
-
log(txt) {
|
|
678
|
-
let w = this._ctx.measureText(txt).width + 20;
|
|
679
|
-
this.stroke(false).fill("rgba(0,0,0,.4)").rect([[0, 0], [w, 20]]);
|
|
680
|
-
this.fill("#fff").text([10, 14], txt);
|
|
681
|
-
return this;
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
//# sourceMappingURL=Canvas.js.map
|