blob: e3d6b3aabd07816b08cf17c6362196f60c56ccf8 [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.
Kevin Lubickb5ae3b52018-11-03 07:51:19 -04004(function(CanvasKit) {
Kevin Lubick217056c2018-09-20 17:39:31 -04005 // 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.
Kevin Lubick1a05fce2018-11-20 12:51:16 -050010
11
12 // Add some helpers for matrices. This is ported from SkMatrix.cpp
13 // to save complexity and overhead of going back and forth between
14 // C++ and JS layers.
15 CanvasKit.SkMatrix = {};
16 function sdot(a, b, c, d, e, f) {
17 e = e || 0;
18 f = f || 0;
19 return a * b + c * d + e * f;
20 }
21
Kevin Lubickb9db3902018-11-26 11:47:54 -050022 CanvasKit.SkMatrix.identity = function() {
23 return [
24 1, 0, 0,
25 0, 1, 0,
26 0, 0, 1,
27 ];
28 };
29
30 // Maps the given points according to the passed in matrix.
31 // Results are done in place.
32 // See SkMatrix.h::mapPoints for the docs on the math.
33 CanvasKit.SkMatrix.mapPoints = function(matrix, ptArr) {
34 if (ptArr.length % 2) {
35 throw 'mapPoints requires an even length arr';
36 }
37 for (var i = 0; i < ptArr.length; i+=2) {
38 var x = ptArr[i], y = ptArr[i+1];
39 // Gx+Hy+I
40 var denom = matrix[6]*x + matrix[7]*y + matrix[8];
41 // Ax+By+C
42 var xTrans = matrix[0]*x + matrix[1]*y + matrix[2];
43 // Dx+Ey+F
44 var yTrans = matrix[3]*x + matrix[4]*y + matrix[5];
45 ptArr[i] = xTrans/denom;
46 ptArr[i+1] = yTrans/denom;
47 }
48 return ptArr;
49 };
50
51 CanvasKit.SkMatrix.multiply = function(m1, m2) {
52 var result = [0,0,0, 0,0,0, 0,0,0];
53 for (var r = 0; r < 3; r++) {
54 for (var c = 0; c < 3; c++) {
55 // m1 and m2 are 1D arrays pretending to be 2D arrays
56 result[3*r + c] = sdot(m1[3*r + 0], m2[3*0 + c],
57 m1[3*r + 1], m2[3*1 + c],
58 m1[3*r + 2], m2[3*2 + c]);
59 }
60 }
61 return result;
62 }
63
64 // Return a matrix representing a rotation by n radians.
Kevin Lubick1a05fce2018-11-20 12:51:16 -050065 // px, py optionally say which point the rotation should be around
66 // with the default being (0, 0);
Kevin Lubickb9db3902018-11-26 11:47:54 -050067 CanvasKit.SkMatrix.rotated = function(radians, px, py) {
Kevin Lubick1a05fce2018-11-20 12:51:16 -050068 px = px || 0;
69 py = py || 0;
Kevin Lubickb9db3902018-11-26 11:47:54 -050070 var sinV = Math.sin(radians);
71 var cosV = Math.cos(radians);
Kevin Lubick1a05fce2018-11-20 12:51:16 -050072 return [
73 cosV, -sinV, sdot( sinV, py, 1 - cosV, px),
74 sinV, cosV, sdot(-sinV, px, 1 - cosV, py),
75 0, 0, 1,
76 ];
77 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -050078
Kevin Lubickb9db3902018-11-26 11:47:54 -050079 CanvasKit.SkMatrix.scaled = function(sx, sy, px, py) {
80 px = px || 0;
81 py = py || 0;
82 return [
83 sx, 0, px - sx * px,
84 0, sy, py - sy * py,
85 0, 0, 1,
86 ];
87 };
88
89 CanvasKit.SkMatrix.skewed = function(kx, ky, px, py) {
90 px = px || 0;
91 py = py || 0;
92 return [
93 1, kx, -kx * px,
94 ky, 1, -ky * py,
95 0, 0, 1,
96 ];
97 };
98
99 CanvasKit.SkMatrix.translated = function(dx, dy) {
100 return [
101 1, 0, dx,
102 0, 1, dy,
103 0, 0, 1,
104 ];
105 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500106
107 CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) {
108 // see arc() for the HTMLCanvas version
109 // note input angles are degrees.
110 this._addArc(oval, startAngle, sweepAngle);
111 return this;
112 };
113
Kevin Lubick217056c2018-09-20 17:39:31 -0400114 CanvasKit.SkPath.prototype.addPath = function() {
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500115 // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
116 // The last arg is optional and chooses between add or extend mode.
Kevin Lubick217056c2018-09-20 17:39:31 -0400117 // The options for the remaining args are:
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500118 // - an array of 6 or 9 parameters (perspective is optional)
119 // - the 9 parameters of a full matrix or
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400120 // the 6 non-perspective params of a matrix.
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500121 var args = Array.prototype.slice.call(arguments);
122 var path = args[0];
123 var extend = false;
124 if (typeof args[args.length-1] === "boolean") {
125 extend = args.pop();
126 }
127 if (args.length === 1) {
128 // Add path, unchanged. Use identity matrix
129 this._addPath(path, 1, 0, 0,
130 0, 1, 0,
131 0, 0, 1,
132 extend);
133 } else if (args.length === 2) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400134 // User provided the 9 params of a full matrix as an array.
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500135 var a = args[1];
136 this._addPath(path, a[0], a[1], a[2],
137 a[3], a[4], a[5],
138 a[6] || 0, a[7] || 0, a[8] || 1,
139 extend);
140 } else if (args.length === 7 || args.length === 10) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400141 // User provided the 9 params of a (full) matrix directly.
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400142 // (or just the 6 non perspective ones)
Kevin Lubick217056c2018-09-20 17:39:31 -0400143 // These are in the same order as what Skia expects.
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500144 var a = args;
145 this._addPath(path, a[1], a[2], a[3],
146 a[4], a[5], a[6],
147 a[7] || 0, a[8] || 0, a[9] || 1,
148 extend);
Kevin Lubick217056c2018-09-20 17:39:31 -0400149 } else {
Kevin Lubick6fccc9d2018-11-20 15:55:10 -0500150 SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500151 return null;
152 }
153 return this;
154 };
155
156 CanvasKit.SkPath.prototype.addRect = function() {
157 // Takes 1, 2, 4 or 5 args
158 // - SkRect
159 // - SkRect, isCCW
160 // - left, top, right, bottom
161 // - left, top, right, bottom, isCCW
162 if (arguments.length === 1 || arguments.length === 2) {
163 var r = arguments[0];
164 var ccw = arguments[1] || false;
165 this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw);
166 } else if (arguments.length === 4 || arguments.length === 5) {
167 var a = arguments;
168 this._addRect(a[0], a[1], a[2], a[3], a[4] || false);
169 } else {
Kevin Lubick6fccc9d2018-11-20 15:55:10 -0500170 SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length);
Kevin Lubick217056c2018-09-20 17:39:31 -0400171 return null;
172 }
173 return this;
174 };
175
Alexander Khovansky3e119332018-11-15 02:01:19 +0300176 CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500177 // emulates the HTMLCanvas behavior. See addArc() for the SkPath version.
178 // Note input angles are radians.
179 var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
180 var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
181 var temp = new CanvasKit.SkPath();
182 temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
183 this.addPath(temp, true);
184 temp.delete();
Alexander Khovansky3e119332018-11-15 02:01:19 +0300185 return this;
186 };
187
Kevin Lubick217056c2018-09-20 17:39:31 -0400188 CanvasKit.SkPath.prototype.arcTo = function(x1, y1, x2, y2, radius) {
189 this._arcTo(x1, y1, x2, y2, radius);
190 return this;
191 };
192
193 CanvasKit.SkPath.prototype.close = function() {
194 this._close();
195 return this;
196 };
197
198 CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
199 this._conicTo(x1, y1, x2, y2, w);
200 return this;
201 };
202
203 CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
204 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
205 return this;
206 };
207
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400208 CanvasKit.SkPath.prototype.dash = function(on, off, phase) {
209 if (this._dash(on, off, phase)) {
210 return this;
211 }
212 return null;
213 };
214
Kevin Lubick217056c2018-09-20 17:39:31 -0400215 CanvasKit.SkPath.prototype.lineTo = function(x, y) {
216 this._lineTo(x, y);
217 return this;
218 };
219
220 CanvasKit.SkPath.prototype.moveTo = function(x, y) {
221 this._moveTo(x, y);
222 return this;
223 };
224
225 CanvasKit.SkPath.prototype.op = function(otherPath, op) {
226 if (this._op(otherPath, op)) {
227 return this;
228 }
229 return null;
230 };
231
232 CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
233 this._quadTo(cpx, cpy, x, y);
234 return this;
235 };
236
237 CanvasKit.SkPath.prototype.simplify = function() {
238 if (this._simplify()) {
239 return this;
240 }
241 return null;
242 };
243
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400244 CanvasKit.SkPath.prototype.stroke = function(opts) {
245 // Fill out any missing values with the default values.
246 /**
247 * See externs.js for this definition
248 * @type {StrokeOpts}
249 */
250 opts = opts || {};
251 opts.width = opts.width || 1;
252 opts.miter_limit = opts.miter_limit || 4;
Kevin Lubickb9db3902018-11-26 11:47:54 -0500253 opts.cap = opts.cap || CanvasKit.StrokeCap.Butt;
254 opts.join = opts.join || CanvasKit.StrokeJoin.Miter;
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400255 if (this._stroke(opts)) {
256 return this;
257 }
258 return null;
259 };
260
Kevin Lubick217056c2018-09-20 17:39:31 -0400261 CanvasKit.SkPath.prototype.transform = function() {
262 // Takes 1 or 9 args
263 if (arguments.length === 1) {
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400264 // argument 1 should be a 6 or 9 element array.
Kevin Lubick217056c2018-09-20 17:39:31 -0400265 var a = arguments[0];
266 this._transform(a[0], a[1], a[2],
267 a[3], a[4], a[5],
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400268 a[6] || 0, a[7] || 0, a[8] || 1);
269 } else if (arguments.length === 6 || arguments.length === 9) {
270 // these arguments are the 6 or 9 members of the matrix
Kevin Lubick217056c2018-09-20 17:39:31 -0400271 var a = arguments;
272 this._transform(a[0], a[1], a[2],
273 a[3], a[4], a[5],
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400274 a[6] || 0, a[7] || 0, a[8] || 1);
Kevin Lubick217056c2018-09-20 17:39:31 -0400275 } else {
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400276 throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
Kevin Lubick217056c2018-09-20 17:39:31 -0400277 }
278 return this;
279 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400280 // isComplement is optional, defaults to false
281 CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
282 if (this._trim(startT, stopT, !!isComplement)) {
283 return this;
284 }
285 return null;
286 };
287
288 // bones should be a 3d array.
289 // Each bone is a 3x2 transformation matrix in column major order:
290 // | scaleX skewX transX |
291 // | skewY scaleY transY |
292 // and bones is an array of those matrices.
293 // Returns a copy of this (SkVertices) with the bones applied.
294 CanvasKit.SkVertices.prototype.applyBones = function(bones) {
295 var bPtr = copy3dArray(bones, CanvasKit.HEAPF32);
296 var vert = this._applyBones(bPtr, bones.length);
297 CanvasKit._free(bPtr);
298 return vert;
299 }
Kevin Lubick53965c92018-10-11 08:51:55 -0400300
Alexander Khovansky3e119332018-11-15 02:01:19 +0300301 CanvasKit.SkImage.prototype.encodeToData = function() {
302 if (arguments.length === 0) {
303 return this._encodeToData();
304 }
305
306 if (arguments.length === 2) {
307 var a = arguments;
308 return this._encodeToDataWithFormat(a[0], a[1]);
309 }
310
311 throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
312 }
313
Kevin Lubick5b90b842018-10-17 07:57:18 -0400314 // Run through the JS files that are added at compile time.
315 if (CanvasKit._extraInitializations) {
316 CanvasKit._extraInitializations.forEach(function(init) {
317 init();
318 });
Kevin Lubick217056c2018-09-20 17:39:31 -0400319 }
Kevin Lubick3d99b1e2018-10-16 10:15:01 -0400320 } // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubick53965c92018-10-11 08:51:55 -0400321
Kevin Lubick217056c2018-09-20 17:39:31 -0400322 CanvasKit.LTRBRect = function(l, t, r, b) {
323 return {
324 fLeft: l,
325 fTop: t,
326 fRight: r,
327 fBottom: b,
328 };
329 }
330
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500331 CanvasKit.XYWHRect = function(x, y, w, h) {
332 return {
333 fLeft: x,
334 fTop: y,
335 fRight: x+w,
336 fBottom: y+h,
337 };
338 }
339
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400340 var nullptr = 0; // emscripten doesn't like to take null as uintptr_t
341
342 // arr can be a normal JS array or a TypedArray
343 // dest is something like CanvasKit.HEAPF32
344 function copy1dArray(arr, dest) {
345 if (!arr || !arr.length) {
346 return nullptr;
347 }
348 var ptr = CanvasKit._malloc(arr.length * dest.BYTES_PER_ELEMENT);
349 // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single
350 // byte elements. When we run _malloc, we always get an offset/pointer into
351 // that block of memory.
352 // CanvasKit exposes some different views to make it easier to work with
353 // different types. HEAPF32 for example, exposes it as a float*
354 // However, to make the ptr line up, we have to do some pointer arithmetic.
355 // Concretely, we need to convert ptr to go from an index into a 1-byte-wide
356 // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32)
357 // and thus we divide ptr by 4.
358 dest.set(arr, ptr / dest.BYTES_PER_ELEMENT);
359 return ptr;
360 }
361
362 // arr should be a non-jagged 2d JS array (TypeyArrays can't be nested
363 // inside themselves.)
364 // dest is something like CanvasKit.HEAPF32
365 function copy2dArray(arr, dest) {
366 if (!arr || !arr.length) {
367 return nullptr;
368 }
369 var ptr = CanvasKit._malloc(arr.length * arr[0].length * dest.BYTES_PER_ELEMENT);
370 var idx = 0;
371 var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
372 for (var r = 0; r < arr.length; r++) {
373 for (var c = 0; c < arr[0].length; c++) {
374 dest[adjustedPtr + idx] = arr[r][c];
375 idx++;
376 }
377 }
378 return ptr;
379 }
380
381 // arr should be a non-jagged 3d JS array (TypeyArrays can't be nested
382 // inside themselves.)
383 // dest is something like CanvasKit.HEAPF32
384 function copy3dArray(arr, dest) {
385 if (!arr || !arr.length || !arr[0].length) {
386 return nullptr;
387 }
388 var ptr = CanvasKit._malloc(arr.length * arr[0].length * arr[0][0].length * dest.BYTES_PER_ELEMENT);
389 var idx = 0;
390 var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
391 for (var x = 0; x < arr.length; x++) {
392 for (var y = 0; y < arr[0].length; y++) {
393 for (var z = 0; z < arr[0][0].length; z++) {
394 dest[adjustedPtr + idx] = arr[x][y][z];
395 idx++;
396 }
397 }
398 }
399 return ptr;
400 }
401
Kevin Lubick217056c2018-09-20 17:39:31 -0400402 CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
403 if (!phase) {
404 phase = 0;
405 }
406 if (!intervals.length || intervals.length % 2 === 1) {
407 throw 'Intervals array must have even length';
408 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400409 var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
410 var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
411 CanvasKit._free(ptr);
412 return dpe;
413 }
414
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500415 // data is a TypedArray or ArrayBuffer
416 CanvasKit.MakeImageFromEncoded = function(data) {
417 data = new Uint8Array(data);
418
419 var iptr = CanvasKit._malloc(data.byteLength);
420 CanvasKit.HEAPU8.set(data, iptr);
421 var img = CanvasKit._decodeImage(iptr, data.byteLength);
422 if (!img) {
423 SkDebug('Could not decode image');
424 CanvasKit._free(iptr);
425 return null;
426 }
427 var realDelete = img.delete.bind(img);
428 img.delete = function() {
429 CanvasKit._free(iptr);
430 realDelete();
431 }
432 return img;
433 }
434
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400435 CanvasKit.MakeImageShader = function(imgData, xTileMode, yTileMode) {
436 var iptr = CanvasKit._malloc(imgData.byteLength);
437 CanvasKit.HEAPU8.set(new Uint8Array(imgData), iptr);
438 // No need to _free iptr, ImageShader takes it with SkData::MakeFromMalloc
439
440 return CanvasKit._MakeImageShader(iptr, imgData.byteLength, xTileMode, yTileMode);
441 }
442
443 CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) {
444 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
445 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
446 flags = flags || 0;
447
448 if (localMatrix) {
449 // Add perspective args if not provided.
450 if (localMatrix.length === 6) {
451 localMatrix.push(0, 0, 1);
452 }
453 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
454 colors.length, mode, flags, localMatrix);
455 } else {
456 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
457 colors.length, mode, flags);
Kevin Lubick217056c2018-09-20 17:39:31 -0400458 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400459
460 CanvasKit._free(colorPtr);
461 CanvasKit._free(posPtr);
462 return lgs;
463 }
464
465 CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400466 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
467 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
468 flags = flags || 0;
469
470 if (localMatrix) {
471 // Add perspective args if not provided.
472 if (localMatrix.length === 6) {
473 localMatrix.push(0, 0, 1);
474 }
475 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
476 colors.length, mode, flags, localMatrix);
477 } else {
478 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
479 colors.length, mode, flags);
480 }
481
482 CanvasKit._free(colorPtr);
483 CanvasKit._free(posPtr);
484 return rgs;
485 }
486
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500487 CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
488 colors, pos, mode, localMatrix, flags) {
489 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
490 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
491 flags = flags || 0;
492
493 if (localMatrix) {
494 // Add perspective args if not provided.
495 if (localMatrix.length === 6) {
496 localMatrix.push(0, 0, 1);
497 }
498 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
499 start, startRadius, end, endRadius,
500 colorPtr, posPtr, colors.length, mode, flags, localMatrix);
501 } else {
502 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
503 start, startRadius, end, endRadius,
504 colorPtr, posPtr, colors.length, mode, flags);
505 }
506
507 CanvasKit._free(colorPtr);
508 CanvasKit._free(posPtr);
509 return rgs;
510 }
511
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400512 CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
513 boneIndices, boneWeights, indices) {
514 var positionPtr = copy2dArray(positions, CanvasKit.HEAPF32);
515 var texPtr = copy2dArray(textureCoordinates, CanvasKit.HEAPF32);
516 // Since we write the colors to memory as signed integers (JSColor), we can
517 // read them out on the other side as unsigned ints (SkColor) just fine
518 // - it's effectively casting.
519 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
520
521 var boneIdxPtr = copy2dArray(boneIndices, CanvasKit.HEAP32);
522 var boneWtPtr = copy2dArray(boneWeights, CanvasKit.HEAPF32);
523 var idxPtr = copy1dArray(indices, CanvasKit.HEAPU16);
524
525 var idxCount = (indices && indices.length) || 0;
526 // _MakeVertices will copy all the values in, so we are free to release
527 // the memory after.
528 var vertices = CanvasKit._MakeSkVertices(mode, positions.length, positionPtr,
529 texPtr, colorPtr, boneIdxPtr, boneWtPtr,
530 idxCount, idxPtr);
531 positionPtr && CanvasKit._free(positionPtr);
532 texPtr && CanvasKit._free(texPtr);
533 colorPtr && CanvasKit._free(colorPtr);
534 idxPtr && CanvasKit._free(idxPtr);
535 boneIdxPtr && CanvasKit._free(boneIdxPtr);
536 boneWtPtr && CanvasKit._free(boneWtPtr);
537 return vertices;
Kevin Lubick217056c2018-09-20 17:39:31 -0400538 }
539
Kevin Lubick134be1d2018-10-30 15:05:04 -0400540 CanvasKit.MakeNimaActor = function(nimaFile, nimaTexture) {
541 var nptr = CanvasKit._malloc(nimaFile.byteLength);
542 CanvasKit.HEAPU8.set(new Uint8Array(nimaFile), nptr);
543 var tptr = CanvasKit._malloc(nimaTexture.byteLength);
544 CanvasKit.HEAPU8.set(new Uint8Array(nimaTexture), tptr);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400545 // No need to _free these ptrs, NimaActor takes them with SkData::MakeFromMalloc
Kevin Lubick134be1d2018-10-30 15:05:04 -0400546
547 return CanvasKit._MakeNimaActor(nptr, nimaFile.byteLength, tptr, nimaTexture.byteLength);
548 }
549
Kevin Lubick217056c2018-09-20 17:39:31 -0400550}(Module)); // When this file is loaded in, the high level object is "Module";
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500551
552// Intentionally added outside the scope to allow usage in canvas2d.js and other
553// pre-js files. These names are unlikely to cause emscripten collisions.
554function radiansToDegrees(rad) {
555 return (rad / Math.PI) * 180;
556}
557
558function degreesToRadians(deg) {
559 return (deg / 180) * Math.PI;
560}
561