blob: 2cb6216eb6a5e1afa882cb2a5de2187ae3c97e7d [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() {
Kevin Lubick52b9f372018-12-04 13:57:36 -0500302 if (!arguments.length) {
Alexander Khovansky3e119332018-11-15 02:01:19 +0300303 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 Lubick52b9f372018-12-04 13:57:36 -0500314 // returns Uint8Array
315 CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType,
316 colorType, dstRowBytes) {
317 // supply defaults (which are compatible with HTMLCanvas's getImageData)
318 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
319 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
320 dstRowBytes = dstRowBytes || (4 * w);
321
322 var len = h * dstRowBytes
323 var pptr = CanvasKit._malloc(len);
324 var ok = this._readPixels({
325 'width': w,
326 'height': h,
327 'colorType': colorType,
328 'alphaType': alphaType,
329 }, pptr, dstRowBytes, x, y);
330 if (!ok) {
331 CanvasKit._free(pptr);
332 return null;
333 }
334
335 // The first typed array is just a view into memory. Because we will
336 // be free-ing that, we call slice to make a persistent copy.
337 var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pptr, len).slice();
338 CanvasKit._free(pptr);
339 return pixels;
340 }
341
342 // pixels is a TypedArray. No matter the input size, it will be treated as
343 // a Uint8Array (essentially, a byte array).
344 CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
345 destX, destY, alphaType, colorType) {
346 if (pixels.byteLength % (srcWidth * srcHeight)) {
347 throw 'pixels length must be a multiple of the srcWidth * srcHeight';
348 }
349 var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
350 // supply defaults (which are compatible with HTMLCanvas's putImageData)
351 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
352 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
353 var srcRowBytes = bytesPerPixel * srcWidth;
354
355 var pptr = CanvasKit._malloc(pixels.byteLength);
356 CanvasKit.HEAPU8.set(pixels, pptr);
357
358 var ok = this._writePixels({
359 'width': srcWidth,
360 'height': srcHeight,
361 'colorType': colorType,
362 'alphaType': alphaType,
363 }, pptr, srcRowBytes, destX, destY);
364
365 CanvasKit._free(pptr);
366 return ok;
367 }
368
Kevin Lubick5b90b842018-10-17 07:57:18 -0400369 // Run through the JS files that are added at compile time.
370 if (CanvasKit._extraInitializations) {
371 CanvasKit._extraInitializations.forEach(function(init) {
372 init();
373 });
Kevin Lubick217056c2018-09-20 17:39:31 -0400374 }
Kevin Lubick3d99b1e2018-10-16 10:15:01 -0400375 } // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubick53965c92018-10-11 08:51:55 -0400376
Kevin Lubick217056c2018-09-20 17:39:31 -0400377 CanvasKit.LTRBRect = function(l, t, r, b) {
378 return {
379 fLeft: l,
380 fTop: t,
381 fRight: r,
382 fBottom: b,
383 };
384 }
385
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500386 CanvasKit.XYWHRect = function(x, y, w, h) {
387 return {
388 fLeft: x,
389 fTop: y,
390 fRight: x+w,
391 fBottom: y+h,
392 };
393 }
394
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400395 var nullptr = 0; // emscripten doesn't like to take null as uintptr_t
396
397 // arr can be a normal JS array or a TypedArray
398 // dest is something like CanvasKit.HEAPF32
399 function copy1dArray(arr, dest) {
400 if (!arr || !arr.length) {
401 return nullptr;
402 }
403 var ptr = CanvasKit._malloc(arr.length * dest.BYTES_PER_ELEMENT);
404 // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single
405 // byte elements. When we run _malloc, we always get an offset/pointer into
406 // that block of memory.
407 // CanvasKit exposes some different views to make it easier to work with
408 // different types. HEAPF32 for example, exposes it as a float*
409 // However, to make the ptr line up, we have to do some pointer arithmetic.
410 // Concretely, we need to convert ptr to go from an index into a 1-byte-wide
411 // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32)
412 // and thus we divide ptr by 4.
413 dest.set(arr, ptr / dest.BYTES_PER_ELEMENT);
414 return ptr;
415 }
416
417 // arr should be a non-jagged 2d JS array (TypeyArrays can't be nested
418 // inside themselves.)
419 // dest is something like CanvasKit.HEAPF32
420 function copy2dArray(arr, dest) {
421 if (!arr || !arr.length) {
422 return nullptr;
423 }
424 var ptr = CanvasKit._malloc(arr.length * arr[0].length * dest.BYTES_PER_ELEMENT);
425 var idx = 0;
426 var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
427 for (var r = 0; r < arr.length; r++) {
428 for (var c = 0; c < arr[0].length; c++) {
429 dest[adjustedPtr + idx] = arr[r][c];
430 idx++;
431 }
432 }
433 return ptr;
434 }
435
436 // arr should be a non-jagged 3d JS array (TypeyArrays can't be nested
437 // inside themselves.)
438 // dest is something like CanvasKit.HEAPF32
439 function copy3dArray(arr, dest) {
440 if (!arr || !arr.length || !arr[0].length) {
441 return nullptr;
442 }
443 var ptr = CanvasKit._malloc(arr.length * arr[0].length * arr[0][0].length * dest.BYTES_PER_ELEMENT);
444 var idx = 0;
445 var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
446 for (var x = 0; x < arr.length; x++) {
447 for (var y = 0; y < arr[0].length; y++) {
448 for (var z = 0; z < arr[0][0].length; z++) {
449 dest[adjustedPtr + idx] = arr[x][y][z];
450 idx++;
451 }
452 }
453 }
454 return ptr;
455 }
456
Kevin Lubick217056c2018-09-20 17:39:31 -0400457 CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
458 if (!phase) {
459 phase = 0;
460 }
461 if (!intervals.length || intervals.length % 2 === 1) {
462 throw 'Intervals array must have even length';
463 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400464 var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
465 var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
466 CanvasKit._free(ptr);
467 return dpe;
468 }
469
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500470 // data is a TypedArray or ArrayBuffer
471 CanvasKit.MakeImageFromEncoded = function(data) {
472 data = new Uint8Array(data);
473
474 var iptr = CanvasKit._malloc(data.byteLength);
475 CanvasKit.HEAPU8.set(data, iptr);
476 var img = CanvasKit._decodeImage(iptr, data.byteLength);
477 if (!img) {
478 SkDebug('Could not decode image');
479 CanvasKit._free(iptr);
480 return null;
481 }
482 var realDelete = img.delete.bind(img);
483 img.delete = function() {
484 CanvasKit._free(iptr);
485 realDelete();
486 }
487 return img;
488 }
489
Kevin Lubick52b9f372018-12-04 13:57:36 -0500490 // imgData is an ArrayBuffer of data, e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400491 CanvasKit.MakeImageShader = function(imgData, xTileMode, yTileMode) {
492 var iptr = CanvasKit._malloc(imgData.byteLength);
493 CanvasKit.HEAPU8.set(new Uint8Array(imgData), iptr);
494 // No need to _free iptr, ImageShader takes it with SkData::MakeFromMalloc
495
496 return CanvasKit._MakeImageShader(iptr, imgData.byteLength, xTileMode, yTileMode);
497 }
498
Kevin Lubick52b9f372018-12-04 13:57:36 -0500499 // pixels is a Uint8Array
500 CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) {
501 var bytesPerPixel = pixels.byteLength / (width * height);
502 var info = {
503 'width': width,
504 'height': height,
505 'alphaType': alphaType,
506 'colorType': colorType,
507 };
508 var pptr = CanvasKit._malloc(pixels.byteLength);
509 CanvasKit.HEAPU8.set(pixels, pptr);
510 // No need to _free iptr, Image takes it with SkData::MakeFromMalloc
511
512 return CanvasKit._MakeImage(info, pptr, pixels.byteLength, width * bytesPerPixel);
513 }
514
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400515 CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) {
516 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
517 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
518 flags = flags || 0;
519
520 if (localMatrix) {
521 // Add perspective args if not provided.
522 if (localMatrix.length === 6) {
523 localMatrix.push(0, 0, 1);
524 }
525 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
526 colors.length, mode, flags, localMatrix);
527 } else {
528 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
529 colors.length, mode, flags);
Kevin Lubick217056c2018-09-20 17:39:31 -0400530 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400531
532 CanvasKit._free(colorPtr);
533 CanvasKit._free(posPtr);
534 return lgs;
535 }
536
537 CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400538 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
539 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
540 flags = flags || 0;
541
542 if (localMatrix) {
543 // Add perspective args if not provided.
544 if (localMatrix.length === 6) {
545 localMatrix.push(0, 0, 1);
546 }
547 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
548 colors.length, mode, flags, localMatrix);
549 } else {
550 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
551 colors.length, mode, flags);
552 }
553
554 CanvasKit._free(colorPtr);
555 CanvasKit._free(posPtr);
556 return rgs;
557 }
558
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500559 CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
560 colors, pos, mode, localMatrix, flags) {
561 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
562 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
563 flags = flags || 0;
564
565 if (localMatrix) {
566 // Add perspective args if not provided.
567 if (localMatrix.length === 6) {
568 localMatrix.push(0, 0, 1);
569 }
570 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
571 start, startRadius, end, endRadius,
572 colorPtr, posPtr, colors.length, mode, flags, localMatrix);
573 } else {
574 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
575 start, startRadius, end, endRadius,
576 colorPtr, posPtr, colors.length, mode, flags);
577 }
578
579 CanvasKit._free(colorPtr);
580 CanvasKit._free(posPtr);
581 return rgs;
582 }
583
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400584 CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
585 boneIndices, boneWeights, indices) {
586 var positionPtr = copy2dArray(positions, CanvasKit.HEAPF32);
587 var texPtr = copy2dArray(textureCoordinates, CanvasKit.HEAPF32);
588 // Since we write the colors to memory as signed integers (JSColor), we can
589 // read them out on the other side as unsigned ints (SkColor) just fine
590 // - it's effectively casting.
591 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
592
593 var boneIdxPtr = copy2dArray(boneIndices, CanvasKit.HEAP32);
594 var boneWtPtr = copy2dArray(boneWeights, CanvasKit.HEAPF32);
595 var idxPtr = copy1dArray(indices, CanvasKit.HEAPU16);
596
597 var idxCount = (indices && indices.length) || 0;
598 // _MakeVertices will copy all the values in, so we are free to release
599 // the memory after.
600 var vertices = CanvasKit._MakeSkVertices(mode, positions.length, positionPtr,
601 texPtr, colorPtr, boneIdxPtr, boneWtPtr,
602 idxCount, idxPtr);
603 positionPtr && CanvasKit._free(positionPtr);
604 texPtr && CanvasKit._free(texPtr);
605 colorPtr && CanvasKit._free(colorPtr);
606 idxPtr && CanvasKit._free(idxPtr);
607 boneIdxPtr && CanvasKit._free(boneIdxPtr);
608 boneWtPtr && CanvasKit._free(boneWtPtr);
609 return vertices;
Kevin Lubick217056c2018-09-20 17:39:31 -0400610 }
611
Kevin Lubick217056c2018-09-20 17:39:31 -0400612}(Module)); // When this file is loaded in, the high level object is "Module";
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500613
614// Intentionally added outside the scope to allow usage in canvas2d.js and other
615// pre-js files. These names are unlikely to cause emscripten collisions.
616function radiansToDegrees(rad) {
617 return (rad / Math.PI) * 180;
618}
619
620function degreesToRadians(deg) {
621 return (deg / 180) * Math.PI;
622}
623