-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_Path.js.html
519 lines (437 loc) · 17.7 KB
/
base_Path.js.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: base/Path.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: base/Path.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
* Copyright 2020 WICKLETS LLC
*
* This file is part of Wick Engine.
*
* Wick Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wick Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wick Engine. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Represents a Wick Path.
*/
Wick.Path = class extends Wick.Base {
/**
* Create a Wick Path.
* @param {array} json - Path data exported from paper.js using exportJSON({asString:false}).
* @param {Paper.Path} path - A Paper.js Path object to use as this Path's svg data. Optional if json was not passed in.
*/
constructor (args) {
if(!args) args = {};
super(args);
this._fontStyle = 'normal';
this._fontWeight = 400;
this._isPlaceholder = args.isPlaceholder;
this._originalStyle = null;
if(args.path) {
this.json = args.path.exportJSON({asString:false});
} else if(args.json) {
this.json = args.json;
} else {
this.json = new paper.Path({insert:false}).exportJSON({asString:false});
}
}
/**
* Create a path containing an image from an ImageAsset.
* @param {Wick.ImageAsset} asset - The asset from which the image src will be loaded from
* @param {Function} callback - A function that will be called when the image is done loading.
*/
static createImagePath (asset, callback) {
var img = new Image();
img.src = asset.src;
img.onload = () => {
var raster = new paper.Raster(img);
raster.remove();
var path = new Wick.Path({
json: Wick.View.Path.exportJSON(raster),
});
callback(path);
}
}
/**
* Create a path (synchronously) containing an image from an ImageAsset.
* @param {Wick.ImageAsset} asset - The asset from which the image src will be loaded from
*/
static createImagePathSync (asset) {
var raster = new paper.Raster(asset.src);
raster.remove();
var path = new Wick.Path({
json: Wick.View.Path.exportJSON(raster),
});
return path;
}
get classname () {
return 'Path';
}
_serialize (args) {
var data = super._serialize(args);
data.json = this.json;
delete data.json[1].data;
// optimization: replace dataurls with asset uuids
if(data.json[0] === 'Raster' && data.json[1].source.startsWith('data:')) {
if(!this.project) {
console.warn('Could not replace raster image source with asset UUID, path does not belong to a project.');
} else {
this.project.getAssets('Image').forEach(imageAsset => {
if(imageAsset.src === data.json[1].source) {
data.json[1].source = 'asset:' + imageAsset.uuid;
}
})
}
}
data.fontStyle = this._fontStyle;
data.fontWeight = this._fontWeight;
data.isPlaceholder = this._isPlaceholder;
return data;
}
_deserialize (data) {
super._deserialize(data);
this.json = data.json;
this._fontStyle = data.fontStyle || 'normal';
this._fontWeight = data.fontWeight || 400;
this._isPlaceholder = data.isPlaceholder;
}
/**
* Determines if this Path is visible in the project.
*/
get onScreen () {
return this.parent.onScreen;
}
/**
* The type of path that this path is. Can be 'path', 'text', or 'image'
* @returns {string}
*/
get pathType () {
if(this.view.item instanceof paper.TextItem) {
return 'text';
} else if(this.view.item instanceof paper.Raster) {
return 'image';
} else {
return 'path';
}
}
/**
* Path data exported from paper.js using exportJSON({asString:false}).
* @type {object}
*/
get json () {
return this._json;
}
set json (json) {
this._json = json;
this.view.render();
}
/**
* The bounding box of the path.
* @type {object}
*/
get bounds () {
var paperBounds = this.view.item.bounds;
return {
top: paperBounds.top,
bottom: paperBounds.bottom,
left: paperBounds.left,
right: paperBounds.right,
width: paperBounds.width,
height: paperBounds.height,
};
}
/**
* The position of the path.
* @type {number}
*/
get x () {
return this.view.item.position.x;
}
set x (x) {
this.view.item.position.x = x;
this.json = this.view.exportJSON();
}
/**
* The position of the path.
* @type {number}
*/
get y () {
return this.view.item.position.y;
}
set y (y) {
this.view.item.position.y = y;
this.json = this.view.exportJSON();
}
/**
* The fill color of the path.
* @type {paper.Color}
*/
get fillColor () {
return this.view.item.fillColor || new paper.Color();
}
set fillColor (fillColor) {
this.view.item.fillColor = fillColor;
this.json = this.view.exportJSON();
}
/**
* The stroke color of the path.
* @type {paper.Color}
*/
get strokeColor () {
return this.view.item.strokeColor || new paper.Color();
}
set strokeColor (strokeColor) {
this.view.item.strokeColor = strokeColor;
this.json = this.view.exportJSON();
}
/**
* The stroke width of the path.
* @type {number}
*/
get strokeWidth () {
return this.view.item.strokeWidth;
}
set strokeWidth (strokeWidth) {
this.view.item.strokeWidth = strokeWidth;
this.json = this.view.exportJSON();
}
/**
* The opacity of the path.
* @type {number}
*/
get opacity () {
if(this.view.item.opacity === undefined || this.view.item.opacity === null) {
return 1.0;
}
return this.view.item.opacity;
}
set opacity (opacity) {
this.view.item.opacity = opacity;
this.json = this.view.exportJSON();
}
/**
* The font family of the path.
* @type {string}
*/
get fontFamily () {
return this.view.item.fontFamily
}
set fontFamily (fontFamily) {
this.view.item.fontFamily = fontFamily;
this.fontWeight = 400;
this.fontStyle = 'normal';
this.json = this.view.exportJSON();
}
/**
* The font size of the path.
* @type {number}
*/
get fontSize () {
return this.view.item.fontSize;
}
set fontSize (fontSize) {
this.view.item.fontSize = fontSize;
this.view.item.leading = fontSize * 1.2;
this.json = this.view.exportJSON();
}
/**
* The font weight of the path.
* @type {number}
*/
get fontWeight () {
return this._fontWeight;
}
set fontWeight (fontWeight) {
if(typeof fontWeight === 'string') {
console.error('fontWeight must be a number.');
return;
}
this._fontWeight = fontWeight
}
/**
* The font style of the path ('italic' or 'oblique').
* @type {string}
*/
get fontStyle () {
return this._fontStyle;
}
set fontStyle (fontStyle) {
this._fontStyle = fontStyle;
}
/**
* The original style of the path (used to recover the path's style if it was changed by a custom onion skin style)
* @type {object}
*/
get originalStyle () {
return this._originalStyle;
}
set originalStyle (originalStyle) {
this._originalStyle = originalStyle;
}
/**
* The content of the text.
* @type {string}
*/
get textContent () {
return this.view.item.content;
}
set textContent (textContent) {
this.view.item.content = textContent;
}
/**
* API function to change the textContent of dynamic text paths.
*/
setText (newTextContent) {
this.textContent = newTextContent;
}
/**
* Check if this path is a dynamic text object.
* @type {boolean}
*/
get isDynamicText () {
return this.pathType === 'text'
&& this.identifier !== null;
}
/**
* The image asset that this path uses, if this path is a Raster path.
* @returns {Wick.Asset[]}
*/
getLinkedAssets () {
var linkedAssets = [];
var data = this.serialize(); // just need the asset uuid...
if(data.json[0] === 'Raster') {
var uuid = data.json[1].source.split(':')[1];
linkedAssets.push(this.project.getAssetByUUID(uuid));
}
return linkedAssets;
}
/**
* Removes this path from its parent frame.
*/
remove () {
this.parentFrame.removePath(this);
}
/**
* Creates a new path using boolean unite on multiple paths. The resulting path will use the fillColor, strokeWidth, and strokeColor of the first path in the array.
* @param {Wick.Path[]} paths - an array containing the paths to process.
* @returns {Wick.Path} The path resulting from the boolean unite.
*/
static unite (paths) {
return Wick.Path.booleanOp(paths, 'unite');
}
/**
* Creates a new path using boolean subtration on multiple paths. The resulting path will use the fillColor, strokeWidth, and strokeColor of the first path in the array.
* @param {Wick.Path[]} paths - an array containing the paths to process.
* @returns {Wick.Path} The path resulting from the boolean subtraction.
*/
static subtract (paths) {
return Wick.Path.booleanOp(paths, 'subtract');
}
/**
* Creates a new path using boolean intersection on multiple paths. The resulting path will use the fillColor, strokeWidth, and strokeColor of the first path in the array.
* @param {Wick.Path[]} paths - an array containing the paths to process.
* @returns {Wick.Path} The path resulting from the boolean intersection.
*/
static intersect (paths) {
return Wick.Path.booleanOp(paths, 'intersect');
}
/**
* Perform a paper.js boolean operation on a list of paths.
* @param {Wick.Path[]} paths - a list of paths to perform the boolean operation on.
* @param {string} booleanOpName - the name of the boolean operation to perform. Currently supports "unite", "subtract", and "intersect"
*/
static booleanOp (paths, booleanOpName) {
if(!booleanOpName) {
console.error('Wick.Path.booleanOp: booleanOpName is required');
}
if(booleanOpName !== 'unite' && booleanOpName !== 'subtract' && booleanOpName !== 'intersect') {
console.error('Wick.Path.booleanOp: unsupported booleanOpName: ' + booleanOpName);
}
if(!paths || paths.length === 0) {
console.error('Wick.Path.booleanOp: a non-empty list of paths is required');
}
// Single path? Nothing to do.
if(paths.length === 1) {
return paths[0];
}
// Get paper.js path objects
paths = paths.map(path => {
return path.view.item;
});
var result = paths[0].clone({insert:false});
paths.forEach(path => {
if(path === paths[0]) return;
result = result[booleanOpName](path);
result.remove();
});
var resultWickPath = new Wick.Path({
json: result.exportJSON({asString:false}),
});
return resultWickPath;
}
/**
* Converts a stroke into fill. Only works with paths that have a strokeWidth and strokeColor, and have no fillColor. Does nothing otherwise.
* @returns {Wick.Path} A flattened version of this path. Can be null if the path cannot be flattened.
*/
flatten () {
if(this.fillColor || !this.strokeColor || !this.strokeWidth) {
return null;
}
if(!(this instanceof paper.Path)) {
return null;
}
var flatPath = new Wick.Path({
json: this.view.item.flatten().exportJSON({asString:false}),
});
flatPath.fillColor = this.strokeColor;
return flatPath;
}
/**
* Is this path used as a placeholder for preventing empty clips?
* @type {bool}
*/
set isPlaceholder (isPlaceholder) {
this._isPlaceholder = isPlaceholder;
}
get isPlaceholder () {
return this._isPlaceholder;
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BuiltinAssets.html">BuiltinAssets</a></li><li><a href="GlobalAPI.html">GlobalAPI</a></li><li><a href="paper.SelectionGUI.html">SelectionGUI</a></li><li><a href="paper-paper.Selection.html">Selection</a></li><li><a href="SelectionWidget.html">SelectionWidget</a></li><li><a href="Wick.AutoSave.html">AutoSave</a></li><li><a href="Wick.Base.html">Base</a></li><li><a href="Wick.Button.html">Button</a></li><li><a href="Wick.Clip.html">Clip</a></li><li><a href="Wick.Clipboard.html">Clipboard</a></li><li><a href="Wick.FileCache.html">FileCache</a></li><li><a href="Wick.Frame.html">Frame</a></li><li><a href="Wick.GUIElement.Project.html">Project</a></li><li><a href="Wick.GUIElement.Timeline.html">Timeline</a></li><li><a href="Wick.GUIElement-Wick.GUIElement.Breadcrumbs.html">Breadcrumbs</a></li><li><a href="Wick.GUIElement-Wick.GUIElement.Button.html">Button</a></li><li><a href="Wick.History.html">History</a></li><li><a href="Wick.HTMLExport.html">HTMLExport</a></li><li><a href="Wick.HTMLPreview.html">HTMLPreview</a></li><li><a href="Wick.ImageSequence.html">ImageSequence</a></li><li><a href="Wick.Layer.html">Layer</a></li><li><a href="Wick.Path.html">Path</a></li><li><a href="Wick.Project.html">Project</a></li><li><a href="Wick.Selection.html">Selection</a></li><li><a href="Wick.Tickable.html">Tickable</a></li><li><a href="Wick.Timeline.html">Timeline</a></li><li><a href="Wick.Tools-Wick.Tools.Brush.html">Brush</a></li><li><a href="Wick.Tools-Wick.Tools.Cursor.html">Cursor</a></li><li><a href="Wick.Tools-Wick.Tools.Ellipse.html">Ellipse</a></li><li><a href="Wick.Tools-Wick.Tools.Eraser.html">Eraser</a></li><li><a href="Wick.Tools-Wick.Tools.Eyedropper.html">Eyedropper</a></li><li><a href="Wick.Tools-Wick.Tools.FillBucket.html">FillBucket</a></li><li><a href="Wick.Tools-Wick.Tools.Interact.html">Interact</a></li><li><a href="Wick.Tools-Wick.Tools.Line.html">Line</a></li><li><a href="Wick.Tools-Wick.Tools.None.html">None</a></li><li><a href="Wick.Tools-Wick.Tools.Pan.html">Pan</a></li><li><a href="Wick.Tools-Wick.Tools.Pencil.html">Pencil</a></li><li><a href="Wick.Tools-Wick.Tools.Rectangle.html">Rectangle</a></li><li><a href="Wick.Tools-Wick.Tools.Text.html">Text</a></li><li><a href="Wick.Tools-Wick.Tools.Zoom.html">Zoom</a></li><li><a href="Wick.Transformation.html">Transformation</a></li><li><a href="Wick.Tween.html">Tween</a></li><li><a href="Wick.View-Wick.View.Clip.html">Clip</a></li><li><a href="Wick.View-Wick.View.Frame.html">Frame</a></li><li><a href="Wick.View-Wick.View.Path.html">Path</a></li><li><a href="Wick.View-Wick.View.Selection.html">Selection</a></li><li><a href="Wick.WickFile.html">WickFile</a></li><li><a href="Wick.WickObjectFile.html">WickObjectFile</a></li><li><a href="Wick.ZIPExport.html">ZIPExport</a></li><li><a href="WickObjectCache.html">WickObjectCache</a></li><li><a href="Wick-Wick.Asset.html">Asset</a></li><li><a href="Wick-Wick.AudioTrack.html">AudioTrack</a></li><li><a href="Wick-Wick.ClipAsset.html">ClipAsset</a></li><li><a href="Wick-Wick.Color.html">Color</a></li><li><a href="Wick-Wick.FileAsset.html">FileAsset</a></li><li><a href="Wick-Wick.FontAsset.html">FontAsset</a></li><li><a href="Wick-Wick.GIFAsset.html">GIFAsset</a></li><li><a href="Wick-Wick.GUIElement.html">GUIElement</a></li><li><a href="Wick-Wick.ImageAsset.html">ImageAsset</a></li><li><a href="Wick-Wick.SoundAsset.html">SoundAsset</a></li><li><a href="Wick-Wick.SVGAsset.html">SVGAsset</a></li><li><a href="Wick-Wick.Tool.html">Tool</a></li><li><a href="Wick-Wick.ToolSettings.html">ToolSettings</a></li><li><a href="Wick-Wick.View.html">View</a></li></ul><h3>Global</h3><ul><li><a href="global.html#type">type</a></li><li><a href="global.html#Wick">Wick</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Fri Apr 24 2020 15:26:13 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>