blob: 885d7e2528025c04ba95bec56b7c8a2d4b8fb4b1 [file] [log] [blame]
Kevin Lubick217056c2018-09-20 17:39:31 -04001// Adds JS functions to augment the CanvasKit interface.
2// For example, if there is a wrapper around the C++ call or logic to allow
3// chaining, it should go here.
4(function(CanvasKit){
5 // CanvasKit.onRuntimeInitialized is called after the WASM library has loaded.
6 // Anything that modifies an exposed class (e.g. SkPath) should be set
7 // after onRuntimeInitialized, otherwise, it can happen outside of that scope.
8 CanvasKit.onRuntimeInitialized = function() {
9 // All calls to 'this' need to go in externs.js so closure doesn't minify them away.
10 CanvasKit.SkPath.prototype.addPath = function() {
11 // Takes 1, 2, or 10 args, where the first arg is always the path.
12 // The options for the remaining args are:
13 // - an array of 9 parameters
14 // - the 9 parameters of a full Matrix
15 if (arguments.length === 1) {
16 // Add path, unchanged. Use identify matrix
17 this._addPath(arguments[0], 1, 0, 0,
18 0, 1, 0,
19 0, 0, 1);
20 } else if (arguments.length === 2) {
21 // User provided the 9 params of a full matrix as an array.
22 var sm = arguments[1];
23 this._addPath(arguments[0], a[1], a[2], a[3],
24 a[4], a[5], a[6],
25 a[7], a[8], a[9]);
26 } else if (arguments.length === 10) {
27 // User provided the 9 params of a (full) matrix directly.
28 // These are in the same order as what Skia expects.
29 var a = arguments;
30 this._addPath(arguments[0], a[1], a[2], a[3],
31 a[4], a[5], a[6],
32 a[7], a[8], a[9]);
33 } else {
34 console.err('addPath expected to take 1, 2, or 10 args. Got ' + arguments.length);
35 return null;
36 }
37 return this;
38 };
39
40 CanvasKit.SkPath.prototype.arcTo = function(x1, y1, x2, y2, radius) {
41 this._arcTo(x1, y1, x2, y2, radius);
42 return this;
43 };
44
45 CanvasKit.SkPath.prototype.close = function() {
46 this._close();
47 return this;
48 };
49
50 CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
51 this._conicTo(x1, y1, x2, y2, w);
52 return this;
53 };
54
55 CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
56 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
57 return this;
58 };
59
60 CanvasKit.SkPath.prototype.lineTo = function(x, y) {
61 this._lineTo(x, y);
62 return this;
63 };
64
65 CanvasKit.SkPath.prototype.moveTo = function(x, y) {
66 this._moveTo(x, y);
67 return this;
68 };
69
70 CanvasKit.SkPath.prototype.op = function(otherPath, op) {
71 if (this._op(otherPath, op)) {
72 return this;
73 }
74 return null;
75 };
76
77 CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
78 this._quadTo(cpx, cpy, x, y);
79 return this;
80 };
81
82 CanvasKit.SkPath.prototype.simplify = function() {
83 if (this._simplify()) {
84 return this;
85 }
86 return null;
87 };
88
89 CanvasKit.SkPath.prototype.transform = function() {
90 // Takes 1 or 9 args
91 if (arguments.length === 1) {
92 // argument 1 should be a 9 element array.
93 var a = arguments[0];
94 this._transform(a[0], a[1], a[2],
95 a[3], a[4], a[5],
96 a[6], a[7], a[8]);
97 } else if (arguments.length === 9) {
98 // these arguments are the 9 members of the matrix
99 var a = arguments;
100 this._transform(a[0], a[1], a[2],
101 a[3], a[4], a[5],
102 a[6], a[7], a[8]);
103 } else {
104 console.err('transform expected to take 1 or 9 arguments. Got ' + arguments.length);
105 return null;
106 }
107 return this;
108 };
Kevin Lubick53965c92018-10-11 08:51:55 -0400109
Kevin Lubick5b90b842018-10-17 07:57:18 -0400110 // Run through the JS files that are added at compile time.
111 if (CanvasKit._extraInitializations) {
112 CanvasKit._extraInitializations.forEach(function(init) {
113 init();
114 });
Kevin Lubick217056c2018-09-20 17:39:31 -0400115 }
Kevin Lubick3d99b1e2018-10-16 10:15:01 -0400116 } // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubick53965c92018-10-11 08:51:55 -0400117
Kevin Lubick217056c2018-09-20 17:39:31 -0400118 // Likely only used for tests.
119 CanvasKit.LTRBRect = function(l, t, r, b) {
120 return {
121 fLeft: l,
122 fTop: t,
123 fRight: r,
124 fBottom: b,
125 };
126 }
127
128 CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
129 if (!phase) {
130 phase = 0;
131 }
132 if (!intervals.length || intervals.length % 2 === 1) {
133 throw 'Intervals array must have even length';
134 }
135 if (!(intervals instanceof Float32Array)) {
136 intervals = Float32Array.from(intervals);
137 }
138 var BYTES_PER_ELEMENT = 4; // Float32Array always has 4 bytes per element
139 var ptr = CanvasKit._malloc(intervals.length * BYTES_PER_ELEMENT);
140 CanvasKit.HEAPF32.set(intervals, ptr / BYTES_PER_ELEMENT);
141 return CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
142 }
143
144}(Module)); // When this file is loaded in, the high level object is "Module";