|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @providesModule ARTSerializablePath |
| 10 | + */ |
| 11 | + |
| 12 | +"use strict"; |
| 13 | + |
| 14 | +// TODO: Move this into an ART mode called "serialized" or something |
| 15 | + |
| 16 | +var Class = require('art/core/class.js'); |
| 17 | +var Path = require('art/core/path.js'); |
| 18 | + |
| 19 | +var MOVE_TO = 0; |
| 20 | +var CLOSE = 1; |
| 21 | +var LINE_TO = 2; |
| 22 | +var CURVE_TO = 3; |
| 23 | +var ARC = 4; |
| 24 | + |
| 25 | +var SerializablePath = Class(Path, { |
| 26 | + |
| 27 | + initialize: function(path) { |
| 28 | + this.reset(); |
| 29 | + if (path instanceof SerializablePath) { |
| 30 | + this.path = path.path.slice(0); |
| 31 | + } else if (path) { |
| 32 | + if (path.applyToPath) { |
| 33 | + path.applyToPath(this); |
| 34 | + } else { |
| 35 | + this.push(path); |
| 36 | + } |
| 37 | + } |
| 38 | + }, |
| 39 | + |
| 40 | + onReset: function() { |
| 41 | + this.path = []; |
| 42 | + }, |
| 43 | + |
| 44 | + onMove: function(sx, sy, x, y) { |
| 45 | + this.path.push(MOVE_TO, x, y); |
| 46 | + }, |
| 47 | + |
| 48 | + onLine: function(sx, sy, x, y) { |
| 49 | + this.path.push(LINE_TO, x, y); |
| 50 | + }, |
| 51 | + |
| 52 | + onBezierCurve: function(sx, sy, p1x, p1y, p2x, p2y, x, y) { |
| 53 | + this.path.push(CURVE_TO, p1x, p1y, p2x, p2y, x, y); |
| 54 | + }, |
| 55 | + |
| 56 | + _arcToBezier: Path.prototype.onArc, |
| 57 | + |
| 58 | + onArc: function(sx, sy, ex, ey, cx, cy, rx, ry, sa, ea, ccw, rotation) { |
| 59 | + if (rx !== ry || rotation) { |
| 60 | + return this._arcToBezier( |
| 61 | + sx, sy, ex, ey, cx, cy, rx, ry, sa, ea, ccw, rotation |
| 62 | + ); |
| 63 | + } |
| 64 | + this.path.push(ARC, cx, cy, rx, sa, ea, ccw ? 0 : 1); |
| 65 | + }, |
| 66 | + |
| 67 | + onClose: function() { |
| 68 | + this.path.push(CLOSE); |
| 69 | + }, |
| 70 | + |
| 71 | + toJSON: function() { |
| 72 | + return this.path; |
| 73 | + } |
| 74 | + |
| 75 | +}); |
| 76 | + |
| 77 | +module.exports = SerializablePath; |
0 commit comments