blob: 5fa4dc2f52c374cc60206f1847885beff178ee1b [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 Lubick1a05fce2018-11-20 12:51:16 -05004
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05005// CanvasKit.onRuntimeInitialized is called after the WASM library has loaded.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04006// Anything that modifies an exposed class (e.g. Path) should be set
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05007// after onRuntimeInitialized, otherwise, it can happen outside of that scope.
8CanvasKit.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
Kevin Lubick93f1a382020-06-02 16:15:23 -040011 _scratchColor = CanvasKit.Malloc(Float32Array, 4); // 4 color scalars.
Kevin Lubickb42660f2020-06-03 09:58:27 -040012 _scratchColorPtr = _scratchColor['byteOffset'];
Kevin Lubick6aa38692020-06-01 11:25:47 -040013
14 _scratch4x4Matrix = CanvasKit.Malloc(Float32Array, 16); // 16 matrix scalars.
Kevin Lubickb42660f2020-06-03 09:58:27 -040015 _scratch4x4MatrixPtr = _scratch4x4Matrix['byteOffset'];
Kevin Lubick6aa38692020-06-01 11:25:47 -040016
17 _scratch3x3Matrix = CanvasKit.Malloc(Float32Array, 9); // 9 matrix scalars.
Kevin Lubickb42660f2020-06-03 09:58:27 -040018 _scratch3x3MatrixPtr = _scratch3x3Matrix['byteOffset'];
Kevin Lubickbe728012020-09-03 11:57:12 +000019
20 _scratchRRect = CanvasKit.Malloc(Float32Array, 12); // 4 scalars for rrect, 8 for radii.
21 _scratchRRectPtr = _scratchRRect['byteOffset'];
22
23 _scratchRRect2 = CanvasKit.Malloc(Float32Array, 12); // 4 scalars for rrect, 8 for radii.
24 _scratchRRect2Ptr = _scratchRRect2['byteOffset'];
25
Kevin Lubickf8823b52020-09-03 10:02:10 -040026 _scratchRect = CanvasKit.Malloc(Float32Array, 4);
27 _scratchRectPtr = _scratchRect['byteOffset'];
28
29 _scratchRect2 = CanvasKit.Malloc(Float32Array, 4);
30 _scratchRect2Ptr = _scratchRect2['byteOffset'];
31
32 _scratchIRect = CanvasKit.Malloc(Int32Array, 4);
33 _scratchIRectPtr = _scratchIRect['byteOffset'];
34
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040035 // Create single copies of all three supported color spaces
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040036 // These are sk_sp<ColorSpace>
37 CanvasKit.ColorSpace.SRGB = CanvasKit.ColorSpace._MakeSRGB();
38 CanvasKit.ColorSpace.DISPLAY_P3 = CanvasKit.ColorSpace._MakeDisplayP3();
39 CanvasKit.ColorSpace.ADOBE_RGB = CanvasKit.ColorSpace._MakeAdobeRGB();
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040040
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050041 // Add some helpers for matrices. This is ported from SkMatrix.cpp
42 // to save complexity and overhead of going back and forth between
43 // C++ and JS layers.
44 // I would have liked to use something like DOMMatrix, except it
45 // isn't widely supported (would need polyfills) and it doesn't
46 // have a mapPoints() function (which could maybe be tacked on here).
47 // If DOMMatrix catches on, it would be worth re-considering this usage.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040048 CanvasKit.Matrix = {};
Nathaniel Nifong77798b42020-02-21 17:15:22 -050049 function sdot() { // to be called with an even number of scalar args
50 var acc = 0;
51 for (var i=0; i < arguments.length-1; i+=2) {
52 acc += arguments[i] * arguments[i+1];
53 }
54 return acc;
55 }
56
57
58 // Private general matrix functions used in both 3x3s and 4x4s.
59 // Return a square identity matrix of size n.
60 var identityN = function(n) {
61 var size = n*n;
62 var m = new Array(size);
63 while(size--) {
64 m[size] = size%(n+1) == 0 ? 1.0 : 0.0;
65 }
66 return m;
Kevin Lubick9fe83912020-11-03 17:08:34 -050067 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -050068
69 // Stride, a function for compactly representing several ways of copying an array into another.
70 // Write vector `v` into matrix `m`. `m` is a matrix encoded as an array in row-major
71 // order. Its width is passed as `width`. `v` is an array with length < (m.length/width).
72 // An element of `v` is copied into `m` starting at `offset` and moving `colStride` cols right
73 // each row.
74 //
75 // For example, a width of 4, offset of 3, and stride of -1 would put the vector here.
76 // _ _ 0 _
77 // _ 1 _ _
78 // 2 _ _ _
79 // _ _ _ 3
80 //
81 var stride = function(v, m, width, offset, colStride) {
82 for (var i=0; i<v.length; i++) {
83 m[i * width + // column
84 (i * colStride + offset + width) % width // row
85 ] = v[i];
86 }
87 return m;
Kevin Lubick9fe83912020-11-03 17:08:34 -050088 };
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050089
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040090 CanvasKit.Matrix.identity = function() {
Nathaniel Nifong77798b42020-02-21 17:15:22 -050091 return identityN(3);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050092 };
93
94 // Return the inverse (if it exists) of this matrix.
Kevin Lubick9b56cea2020-04-06 08:16:18 -040095 // Otherwise, return null.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040096 CanvasKit.Matrix.invert = function(m) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -050097 // Find the determinant by the sarrus rule. https://en.wikipedia.org/wiki/Rule_of_Sarrus
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050098 var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7]
99 - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7];
100 if (!det) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400101 Debug('Warning, uninvertible matrix');
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500102 return null;
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500103 }
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500104 // Return the inverse by the formula adj(m)/det.
105 // adj (adjugate) of a 3x3 is the transpose of it's cofactor matrix.
106 // a cofactor matrix is a matrix where each term is +-det(N) where matrix N is the 2x2 formed
107 // by removing the row and column we're currently setting from the source.
108 // the sign alternates in a checkerboard pattern with a `+` at the top left.
109 // that's all been combined here into one expression.
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500110 return [
111 (m[4]*m[8] - m[5]*m[7])/det, (m[2]*m[7] - m[1]*m[8])/det, (m[1]*m[5] - m[2]*m[4])/det,
112 (m[5]*m[6] - m[3]*m[8])/det, (m[0]*m[8] - m[2]*m[6])/det, (m[2]*m[3] - m[0]*m[5])/det,
113 (m[3]*m[7] - m[4]*m[6])/det, (m[1]*m[6] - m[0]*m[7])/det, (m[0]*m[4] - m[1]*m[3])/det,
114 ];
115 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500116
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500117 // Maps the given points according to the passed in matrix.
118 // Results are done in place.
119 // See SkMatrix.h::mapPoints for the docs on the math.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400120 CanvasKit.Matrix.mapPoints = function(matrix, ptArr) {
121 if (IsDebug && (ptArr.length % 2)) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500122 throw 'mapPoints requires an even length arr';
Kevin Lubickb9db3902018-11-26 11:47:54 -0500123 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500124 for (var i = 0; i < ptArr.length; i+=2) {
125 var x = ptArr[i], y = ptArr[i+1];
126 // Gx+Hy+I
127 var denom = matrix[6]*x + matrix[7]*y + matrix[8];
128 // Ax+By+C
129 var xTrans = matrix[0]*x + matrix[1]*y + matrix[2];
130 // Dx+Ey+F
131 var yTrans = matrix[3]*x + matrix[4]*y + matrix[5];
132 ptArr[i] = xTrans/denom;
133 ptArr[i+1] = yTrans/denom;
134 }
135 return ptArr;
136 };
Kevin Lubickb9db3902018-11-26 11:47:54 -0500137
Kevin Lubick9fe83912020-11-03 17:08:34 -0500138 function isnumber(val) { return !isNaN(val); }
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500139
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400140 // generalized iterative algorithm for multiplying two matrices.
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500141 function multiply(m1, m2, size) {
142
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400143 if (IsDebug && (!m1.every(isnumber) || !m2.every(isnumber))) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500144 throw 'Some members of matrices are NaN m1='+m1+', m2='+m2+'';
145 }
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400146 if (IsDebug && (m1.length !== m2.length)) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500147 throw 'Undefined for matrices of different sizes. m1.length='+m1.length+', m2.length='+m2.length;
148 }
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400149 if (IsDebug && (size*size !== m1.length)) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500150 throw 'Undefined for non-square matrices. array size was '+size;
151 }
152
153 var result = Array(m1.length);
154 for (var r = 0; r < size; r++) {
155 for (var c = 0; c < size; c++) {
156 // accumulate a sum of m1[r,k]*m2[k, c]
157 var acc = 0;
158 for (var k = 0; k < size; k++) {
159 acc += m1[size * r + k] * m2[size * k + c];
160 }
161 result[r * size + c] = acc;
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500162 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500163 }
164 return result;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500165 }
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500166
167 // Accept an integer indicating the size of the matrices being multiplied (3 for 3x3), and any
168 // number of matrices following it.
169 function multiplyMany(size, listOfMatrices) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400170 if (IsDebug && (listOfMatrices.length < 2)) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500171 throw 'multiplication expected two or more matrices';
172 }
173 var result = multiply(listOfMatrices[0], listOfMatrices[1], size);
174 var next = 2;
175 while (next < listOfMatrices.length) {
176 result = multiply(result, listOfMatrices[next], size);
177 next++;
178 }
179 return result;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500180 }
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500181
182 // Accept any number 3x3 of matrices as arguments, multiply them together.
Kevin Lubick9e2d3842020-04-01 13:42:15 -0400183 // Matrix multiplication is associative but not commutative. the order of the arguments
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500184 // matters, but it does not matter that this implementation multiplies them left to right.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400185 CanvasKit.Matrix.multiply = function() {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500186 return multiplyMany(3, arguments);
187 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500188
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500189 // Return a matrix representing a rotation by n radians.
190 // px, py optionally say which point the rotation should be around
191 // with the default being (0, 0);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400192 CanvasKit.Matrix.rotated = function(radians, px, py) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500193 px = px || 0;
194 py = py || 0;
195 var sinV = Math.sin(radians);
196 var cosV = Math.cos(radians);
197 return [
198 cosV, -sinV, sdot( sinV, py, 1 - cosV, px),
199 sinV, cosV, sdot(-sinV, px, 1 - cosV, py),
200 0, 0, 1,
201 ];
202 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400203
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400204 CanvasKit.Matrix.scaled = function(sx, sy, px, py) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500205 px = px || 0;
206 py = py || 0;
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500207 var m = stride([sx, sy], identityN(3), 3, 0, 1);
208 return stride([px-sx*px, py-sy*py], m, 3, 2, 0);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500209 };
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500210
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400211 CanvasKit.Matrix.skewed = function(kx, ky, px, py) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500212 px = px || 0;
213 py = py || 0;
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500214 var m = stride([kx, ky], identityN(3), 3, 1, -1);
215 return stride([-kx*px, -ky*py], m, 3, 2, 0);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500216 };
Alexander Khovansky3e119332018-11-15 02:01:19 +0300217
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400218 CanvasKit.Matrix.translated = function(dx, dy) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500219 return stride(arguments, identityN(3), 3, 2, 0);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500220 };
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500221
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500222 // Functions for manipulating vectors.
223 // Loosely based off of SkV3 in SkM44.h but skia also has SkVec2 and Skv4. This combines them and
224 // works on vectors of any length.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400225 CanvasKit.Vector = {};
226 CanvasKit.Vector.dot = function(a, b) {
227 if (IsDebug && (a.length !== b.length)) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500228 throw 'Cannot perform dot product on arrays of different length ('+a.length+' vs '+b.length+')';
229 }
230 return a.map(function(v, i) { return v*b[i] }).reduce(function(acc, cur) { return acc + cur; });
Kevin Lubick9fe83912020-11-03 17:08:34 -0500231 };
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400232 CanvasKit.Vector.lengthSquared = function(v) {
233 return CanvasKit.Vector.dot(v, v);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500234 };
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400235 CanvasKit.Vector.length = function(v) {
236 return Math.sqrt(CanvasKit.Vector.lengthSquared(v));
Kevin Lubick9fe83912020-11-03 17:08:34 -0500237 };
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400238 CanvasKit.Vector.mulScalar = function(v, s) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500239 return v.map(function(i) { return i*s });
Kevin Lubick9fe83912020-11-03 17:08:34 -0500240 };
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400241 CanvasKit.Vector.add = function(a, b) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500242 return a.map(function(v, i) { return v+b[i] });
Kevin Lubick9fe83912020-11-03 17:08:34 -0500243 };
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400244 CanvasKit.Vector.sub = function(a, b) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500245 return a.map(function(v, i) { return v-b[i]; });
Kevin Lubick9fe83912020-11-03 17:08:34 -0500246 };
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400247 CanvasKit.Vector.dist = function(a, b) {
248 return CanvasKit.Vector.length(CanvasKit.Vector.sub(a, b));
Kevin Lubick9fe83912020-11-03 17:08:34 -0500249 };
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400250 CanvasKit.Vector.normalize = function(v) {
251 return CanvasKit.Vector.mulScalar(v, 1/CanvasKit.Vector.length(v));
Kevin Lubick9fe83912020-11-03 17:08:34 -0500252 };
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400253 CanvasKit.Vector.cross = function(a, b) {
254 if (IsDebug && (a.length !== 3 || a.length !== 3)) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500255 throw 'Cross product is only defined for 3-dimensional vectors (a.length='+a.length+', b.length='+b.length+')';
256 }
257 return [
258 a[1]*b[2] - a[2]*b[1],
259 a[2]*b[0] - a[0]*b[2],
260 a[0]*b[1] - a[1]*b[0],
261 ];
Kevin Lubick9fe83912020-11-03 17:08:34 -0500262 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500263
Kevin Lubickc1d08982020-04-06 13:52:15 -0400264 // Functions for creating and manipulating (row-major) 4x4 matrices. Accepted in place of
265 // SkM44 in canvas methods, for the same reasons as the 3x3 matrices above.
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500266 // ported from C++ code in SkM44.cpp
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400267 CanvasKit.M44 = {};
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500268 // Create a 4x4 identity matrix
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400269 CanvasKit.M44.identity = function() {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500270 return identityN(4);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500271 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500272
273 // Anything named vec below is an array of length 3 representing a vector/point in 3D space.
274 // Create a 4x4 matrix representing a translate by the provided 3-vec
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400275 CanvasKit.M44.translated = function(vec) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500276 return stride(vec, identityN(4), 4, 3, 0);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500277 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500278 // Create a 4x4 matrix representing a scaling by the provided 3-vec
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400279 CanvasKit.M44.scaled = function(vec) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500280 return stride(vec, identityN(4), 4, 0, 1);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500281 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500282 // Create a 4x4 matrix representing a rotation about the provided axis 3-vec.
283 // axis does not need to be normalized.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400284 CanvasKit.M44.rotated = function(axisVec, radians) {
285 return CanvasKit.M44.rotatedUnitSinCos(
286 CanvasKit.Vector.normalize(axisVec), Math.sin(radians), Math.cos(radians));
Kevin Lubick9fe83912020-11-03 17:08:34 -0500287 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500288 // Create a 4x4 matrix representing a rotation about the provided normalized axis 3-vec.
289 // Rotation is provided redundantly as both sin and cos values.
290 // This rotate can be used when you already have the cosAngle and sinAngle values
291 // so you don't have to atan(cos/sin) to call roatated() which expects an angle in radians.
292 // this does no checking! Behavior for invalid sin or cos values or non-normalized axis vectors
Nathaniel Nifong3392ebe2020-06-01 09:21:36 -0400293 // is incorrect. Prefer rotated().
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400294 CanvasKit.M44.rotatedUnitSinCos = function(axisVec, sinAngle, cosAngle) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500295 var x = axisVec[0];
296 var y = axisVec[1];
297 var z = axisVec[2];
298 var c = cosAngle;
299 var s = sinAngle;
300 var t = 1 - c;
301 return [
302 t*x*x + c, t*x*y - s*z, t*x*z + s*y, 0,
303 t*x*y + s*z, t*y*y + c, t*y*z - s*x, 0,
304 t*x*z - s*y, t*y*z + s*x, t*z*z + c, 0,
305 0, 0, 0, 1
306 ];
Kevin Lubick9fe83912020-11-03 17:08:34 -0500307 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500308 // Create a 4x4 matrix representing a camera at eyeVec, pointed at centerVec.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400309 CanvasKit.M44.lookat = function(eyeVec, centerVec, upVec) {
310 var f = CanvasKit.Vector.normalize(CanvasKit.Vector.sub(centerVec, eyeVec));
311 var u = CanvasKit.Vector.normalize(upVec);
312 var s = CanvasKit.Vector.normalize(CanvasKit.Vector.cross(f, u));
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500313
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400314 var m = CanvasKit.M44.identity();
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500315 // set each column's top three numbers
Kevin Lubickc1d08982020-04-06 13:52:15 -0400316 stride(s, m, 4, 0, 0);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400317 stride(CanvasKit.Vector.cross(s, f), m, 4, 1, 0);
318 stride(CanvasKit.Vector.mulScalar(f, -1), m, 4, 2, 0);
Kevin Lubickc1d08982020-04-06 13:52:15 -0400319 stride(eyeVec, m, 4, 3, 0);
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500320
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400321 var m2 = CanvasKit.M44.invert(m);
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500322 if (m2 === null) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400323 return CanvasKit.M44.identity();
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500324 }
325 return m2;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500326 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500327 // Create a 4x4 matrix representing a perspective. All arguments are scalars.
328 // angle is in radians.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400329 CanvasKit.M44.perspective = function(near, far, angle) {
330 if (IsDebug && (far <= near)) {
331 throw 'far must be greater than near when constructing M44 using perspective.';
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500332 }
333 var dInv = 1 / (far - near);
334 var halfAngle = angle / 2;
335 var cot = Math.cos(halfAngle) / Math.sin(halfAngle);
336 return [
337 cot, 0, 0, 0,
338 0, cot, 0, 0,
339 0, 0, (far+near)*dInv, 2*far*near*dInv,
340 0, 0, -1, 1,
341 ];
Kevin Lubick9fe83912020-11-03 17:08:34 -0500342 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500343 // Returns the number at the given row and column in matrix m.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400344 CanvasKit.M44.rc = function(m, r, c) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500345 return m[r*4+c];
Kevin Lubick9fe83912020-11-03 17:08:34 -0500346 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500347 // Accepts any number of 4x4 matrix arguments, multiplies them left to right.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400348 CanvasKit.M44.multiply = function() {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500349 return multiplyMany(4, arguments);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500350 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500351
352 // Invert the 4x4 matrix if it is invertible and return it. if not, return null.
353 // taken from SkM44.cpp (altered to use row-major order)
354 // m is not altered.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400355 CanvasKit.M44.invert = function(m) {
356 if (IsDebug && !m.every(isnumber)) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500357 throw 'some members of matrix are NaN m='+m;
358 }
359
360 var a00 = m[0];
361 var a01 = m[4];
362 var a02 = m[8];
363 var a03 = m[12];
364 var a10 = m[1];
365 var a11 = m[5];
366 var a12 = m[9];
367 var a13 = m[13];
368 var a20 = m[2];
369 var a21 = m[6];
370 var a22 = m[10];
371 var a23 = m[14];
372 var a30 = m[3];
373 var a31 = m[7];
374 var a32 = m[11];
375 var a33 = m[15];
376
377 var b00 = a00 * a11 - a01 * a10;
378 var b01 = a00 * a12 - a02 * a10;
379 var b02 = a00 * a13 - a03 * a10;
380 var b03 = a01 * a12 - a02 * a11;
381 var b04 = a01 * a13 - a03 * a11;
382 var b05 = a02 * a13 - a03 * a12;
383 var b06 = a20 * a31 - a21 * a30;
384 var b07 = a20 * a32 - a22 * a30;
385 var b08 = a20 * a33 - a23 * a30;
386 var b09 = a21 * a32 - a22 * a31;
387 var b10 = a21 * a33 - a23 * a31;
388 var b11 = a22 * a33 - a23 * a32;
389
390 // calculate determinate
391 var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
392 var invdet = 1.0 / det;
393
394 // bail out if the matrix is not invertible
395 if (det === 0 || invdet === Infinity) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400396 Debug('Warning, uninvertible matrix');
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500397 return null;
398 }
399
400 b00 *= invdet;
401 b01 *= invdet;
402 b02 *= invdet;
403 b03 *= invdet;
404 b04 *= invdet;
405 b05 *= invdet;
406 b06 *= invdet;
407 b07 *= invdet;
408 b08 *= invdet;
409 b09 *= invdet;
410 b10 *= invdet;
411 b11 *= invdet;
412
413 // store result in row major order
414 var tmp = [
415 a11 * b11 - a12 * b10 + a13 * b09,
416 a12 * b08 - a10 * b11 - a13 * b07,
417 a10 * b10 - a11 * b08 + a13 * b06,
418 a11 * b07 - a10 * b09 - a12 * b06,
419
420 a02 * b10 - a01 * b11 - a03 * b09,
421 a00 * b11 - a02 * b08 + a03 * b07,
422 a01 * b08 - a00 * b10 - a03 * b06,
423 a00 * b09 - a01 * b07 + a02 * b06,
424
425 a31 * b05 - a32 * b04 + a33 * b03,
426 a32 * b02 - a30 * b05 - a33 * b01,
427 a30 * b04 - a31 * b02 + a33 * b00,
428 a31 * b01 - a30 * b03 - a32 * b00,
429
430 a22 * b04 - a21 * b05 - a23 * b03,
431 a20 * b05 - a22 * b02 + a23 * b01,
432 a21 * b02 - a20 * b04 - a23 * b00,
433 a20 * b03 - a21 * b01 + a22 * b00,
434 ];
435
436
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400437 if (!tmp.every(function(val) { return !isNaN(val) && val !== Infinity && val !== -Infinity; })) {
438 Debug('inverted matrix contains infinities or NaN '+tmp);
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500439 return null;
440 }
441 return tmp;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500442 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500443
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400444 CanvasKit.M44.transpose = function(m) {
Nathaniel Nifongcc5415a2020-02-23 14:26:33 -0500445 return [
446 m[0], m[4], m[8], m[12],
447 m[1], m[5], m[9], m[13],
448 m[2], m[6], m[10], m[14],
449 m[3], m[7], m[11], m[15],
450 ];
Kevin Lubick9fe83912020-11-03 17:08:34 -0500451 };
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500452
Nathaniel Nifong6130d502020-07-06 19:50:13 -0400453 // Return the inverse of an SkM44. throw an error if it's not invertible
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400454 CanvasKit.M44.mustInvert = function(m) {
455 var m2 = CanvasKit.M44.invert(m);
Nathaniel Nifong6130d502020-07-06 19:50:13 -0400456 if (m2 === null) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000457 throw 'Matrix not invertible';
Nathaniel Nifong6130d502020-07-06 19:50:13 -0400458 }
459 return m2;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500460 };
Nathaniel Nifong6130d502020-07-06 19:50:13 -0400461
462 // returns a matrix that sets up a 3D perspective view from a given camera.
463 //
464 // area - a rect describing the viewport. (0, 0, canvas_width, canvas_height) suggested
465 // zscale - a scalar describing the scale of the z axis. min(width, height)/2 suggested
466 // cam - an object with the following attributes
467 // const cam = {
468 // 'eye' : [0, 0, 1 / Math.tan(Math.PI / 24) - 1], // a 3D point locating the camera
469 // 'coa' : [0, 0, 0], // center of attention - the 3D point the camera is looking at.
470 // 'up' : [0, 1, 0], // a unit vector pointing in the camera's up direction, because eye and coa alone leave roll unspecified.
471 // 'near' : 0.02, // near clipping plane
472 // 'far' : 4, // far clipping plane
473 // 'angle': Math.PI / 12, // field of view in radians
474 // };
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400475 CanvasKit.M44.setupCamera = function(area, zscale, cam) {
476 var camera = CanvasKit.M44.lookat(cam['eye'], cam['coa'], cam['up']);
477 var perspective = CanvasKit.M44.perspective(cam['near'], cam['far'], cam['angle']);
Kevin Lubickf8823b52020-09-03 10:02:10 -0400478 var center = [(area[0] + area[2])/2, (area[1] + area[3])/2, 0];
479 var viewScale = [(area[2] - area[0])/2, (area[3] - area[1])/2, zscale];
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400480 var viewport = CanvasKit.M44.multiply(
481 CanvasKit.M44.translated(center),
482 CanvasKit.M44.scaled(viewScale));
483 return CanvasKit.M44.multiply(
484 viewport, perspective, camera, CanvasKit.M44.mustInvert(viewport));
Kevin Lubick9fe83912020-11-03 17:08:34 -0500485 };
Nathaniel Nifong6130d502020-07-06 19:50:13 -0400486
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400487 // An ColorMatrix is a 4x4 color matrix that transforms the 4 color channels
Kevin Lubickd3729342019-09-12 11:11:25 -0400488 // with a 1x4 matrix that post-translates those 4 channels.
489 // For example, the following is the layout with the scale (S) and post-transform
490 // (PT) items indicated.
491 // RS, 0, 0, 0 | RPT
492 // 0, GS, 0, 0 | GPT
493 // 0, 0, BS, 0 | BPT
494 // 0, 0, 0, AS | APT
495 //
496 // Much of this was hand-transcribed from SkColorMatrix.cpp, because it's easier to
497 // deal with a Float32Array of length 20 than to try to expose the SkColorMatrix object.
498
Nathaniel Nifongcc5415a2020-02-23 14:26:33 -0500499 var rScale = 0;
500 var gScale = 6;
501 var bScale = 12;
502 var aScale = 18;
503
Kevin Lubickd3729342019-09-12 11:11:25 -0400504 var rPostTrans = 4;
505 var gPostTrans = 9;
506 var bPostTrans = 14;
507 var aPostTrans = 19;
508
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400509 CanvasKit.ColorMatrix = {};
510 CanvasKit.ColorMatrix.identity = function() {
Nathaniel Nifongcc5415a2020-02-23 14:26:33 -0500511 var m = new Float32Array(20);
512 m[rScale] = 1;
513 m[gScale] = 1;
514 m[bScale] = 1;
515 m[aScale] = 1;
516 return m;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500517 };
Kevin Lubickd3729342019-09-12 11:11:25 -0400518
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400519 CanvasKit.ColorMatrix.scaled = function(rs, gs, bs, as) {
Nathaniel Nifongcc5415a2020-02-23 14:26:33 -0500520 var m = new Float32Array(20);
521 m[rScale] = rs;
522 m[gScale] = gs;
523 m[bScale] = bs;
524 m[aScale] = as;
525 return m;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500526 };
Kevin Lubickd3729342019-09-12 11:11:25 -0400527
528 var rotateIndices = [
529 [6, 7, 11, 12],
530 [0, 10, 2, 12],
531 [0, 1, 5, 6],
532 ];
533 // axis should be 0, 1, 2 for r, g, b
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400534 CanvasKit.ColorMatrix.rotated = function(axis, sine, cosine) {
535 var m = CanvasKit.ColorMatrix.identity();
Kevin Lubickd3729342019-09-12 11:11:25 -0400536 var indices = rotateIndices[axis];
537 m[indices[0]] = cosine;
538 m[indices[1]] = sine;
539 m[indices[2]] = -sine;
540 m[indices[3]] = cosine;
541 return m;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500542 };
Kevin Lubickd3729342019-09-12 11:11:25 -0400543
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400544 // m is a ColorMatrix (i.e. a Float32Array), and this sets the 4 "special"
Kevin Lubickd3729342019-09-12 11:11:25 -0400545 // params that will translate the colors after they are multiplied by the 4x4 matrix.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400546 CanvasKit.ColorMatrix.postTranslate = function(m, dr, dg, db, da) {
Kevin Lubickd3729342019-09-12 11:11:25 -0400547 m[rPostTrans] += dr;
548 m[gPostTrans] += dg;
549 m[bPostTrans] += db;
550 m[aPostTrans] += da;
551 return m;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500552 };
Kevin Lubickd3729342019-09-12 11:11:25 -0400553
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400554 // concat returns a new ColorMatrix that is the result of multiplying outer*inner
555 CanvasKit.ColorMatrix.concat = function(outer, inner) {
Kevin Lubickd3729342019-09-12 11:11:25 -0400556 var m = new Float32Array(20);
557 var index = 0;
558 for (var j = 0; j < 20; j += 5) {
559 for (var i = 0; i < 4; i++) {
560 m[index++] = outer[j + 0] * inner[i + 0] +
561 outer[j + 1] * inner[i + 5] +
562 outer[j + 2] * inner[i + 10] +
563 outer[j + 3] * inner[i + 15];
564 }
565 m[index++] = outer[j + 0] * inner[4] +
566 outer[j + 1] * inner[9] +
567 outer[j + 2] * inner[14] +
568 outer[j + 3] * inner[19] +
569 outer[j + 4];
570 }
571
572 return m;
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400573 };
574
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400575 CanvasKit.Path.MakeFromCmds = function(cmds) {
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400576 var ptrLen = loadCmdsTypedArray(cmds);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400577 var path = CanvasKit.Path._MakeFromCmds(ptrLen[0], ptrLen[1]);
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400578 CanvasKit._free(ptrLen[0]);
579 return path;
580 };
581
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400582 // The weights array is optional (only used for conics).
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400583 CanvasKit.Path.MakeFromVerbsPointsWeights = function(verbs, pts, weights) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000584 var verbsPtr = copy1dArray(verbs, 'HEAPU8');
585 var pointsPtr = copy1dArray(pts, 'HEAPF32');
586 var weightsPtr = copy1dArray(weights, 'HEAPF32');
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400587 var numWeights = (weights && weights.length) || 0;
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400588 var path = CanvasKit.Path._MakeFromVerbsPointsWeights(
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400589 verbsPtr, verbs.length, pointsPtr, pts.length, weightsPtr, numWeights);
590 freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs);
591 freeArraysThatAreNotMallocedByUsers(pointsPtr, pts);
592 freeArraysThatAreNotMallocedByUsers(weightsPtr, weights);
593 return path;
594 };
Kevin Lubickd3729342019-09-12 11:11:25 -0400595
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400596 CanvasKit.Path.prototype.addArc = function(oval, startAngle, sweepAngle) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500597 // see arc() for the HTMLCanvas version
598 // note input angles are degrees.
Kevin Lubickf8823b52020-09-03 10:02:10 -0400599 var oPtr = copyRectToWasm(oval);
600 this._addArc(oPtr, startAngle, sweepAngle);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500601 return this;
602 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400603
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400604 CanvasKit.Path.prototype.addOval = function(oval, isCCW, startIndex) {
Kevin Lubicke384df42019-08-26 15:48:09 -0400605 if (startIndex === undefined) {
606 startIndex = 1;
607 }
Kevin Lubickf8823b52020-09-03 10:02:10 -0400608 var oPtr = copyRectToWasm(oval);
609 this._addOval(oPtr, !!isCCW, startIndex);
Kevin Lubicke384df42019-08-26 15:48:09 -0400610 return this;
611 };
612
Kevin Lubicka2535af2020-10-02 08:01:57 -0400613 // TODO(kjlubick) clean up this API - split it apart if necessary
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400614 CanvasKit.Path.prototype.addPath = function() {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500615 // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
616 // The last arg is optional and chooses between add or extend mode.
617 // The options for the remaining args are:
618 // - an array of 6 or 9 parameters (perspective is optional)
619 // - the 9 parameters of a full matrix or
620 // the 6 non-perspective params of a matrix.
621 var args = Array.prototype.slice.call(arguments);
622 var path = args[0];
623 var extend = false;
Kevin Lubickbe728012020-09-03 11:57:12 +0000624 if (typeof args[args.length-1] === 'boolean') {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500625 extend = args.pop();
626 }
627 if (args.length === 1) {
628 // Add path, unchanged. Use identity matrix
629 this._addPath(path, 1, 0, 0,
630 0, 1, 0,
631 0, 0, 1,
632 extend);
633 } else if (args.length === 2) {
634 // User provided the 9 params of a full matrix as an array.
635 var a = args[1];
636 this._addPath(path, a[0], a[1], a[2],
637 a[3], a[4], a[5],
638 a[6] || 0, a[7] || 0, a[8] || 1,
639 extend);
640 } else if (args.length === 7 || args.length === 10) {
641 // User provided the 9 params of a (full) matrix directly.
642 // (or just the 6 non perspective ones)
643 // These are in the same order as what Skia expects.
644 var a = args;
645 this._addPath(path, a[1], a[2], a[3],
646 a[4], a[5], a[6],
647 a[7] || 0, a[8] || 0, a[9] || 1,
648 extend);
649 } else {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400650 Debug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400651 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500652 }
653 return this;
654 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400655
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500656 // points is either an array of [x, y] where x and y are numbers or
657 // a typed array from Malloc where the even indices will be treated
658 // as x coordinates and the odd indices will be treated as y coordinates.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400659 CanvasKit.Path.prototype.addPoly = function(points, close) {
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500660 var ptr;
661 var n;
662 // This was created with CanvasKit.Malloc, so assume the user has
663 // already been filled with data.
664 if (points['_ck']) {
665 ptr = points.byteOffset;
666 n = points.length/2;
667 } else {
Kevin Lubicka2535af2020-10-02 08:01:57 -0400668 // TODO(kjlubick) deprecate and remove the 2d array input
Kevin Lubickbe728012020-09-03 11:57:12 +0000669 ptr = copy2dArray(points, 'HEAPF32');
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500670 n = points.length;
671 }
672 this._addPoly(ptr, n, close);
Kevin Lubickcf118922020-05-28 14:43:38 -0400673 freeArraysThatAreNotMallocedByUsers(ptr, points);
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500674 return this;
675 };
676
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400677 CanvasKit.Path.prototype.addRect = function(rect, isCCW) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400678 var rPtr = copyRectToWasm(rect);
679 this._addRect(rPtr, !!isCCW);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500680 return this;
681 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400682
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400683 CanvasKit.Path.prototype.addRRect = function(rrect, isCCW) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400684 var rPtr = copyRRectToWasm(rrect);
685 this._addRRect(rPtr, !!isCCW);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500686 return this;
687 };
688
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400689 // The weights array is optional (only used for conics).
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400690 CanvasKit.Path.prototype.addVerbsPointsWeights = function(verbs, points, weights) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000691 var verbsPtr = copy1dArray(verbs, 'HEAPU8');
692 var pointsPtr = copy1dArray(points, 'HEAPF32');
693 var weightsPtr = copy1dArray(weights, 'HEAPF32');
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400694 var numWeights = (weights && weights.length) || 0;
695 this._addVerbsPointsWeights(verbsPtr, verbs.length, pointsPtr, points.length,
696 weightsPtr, numWeights);
697 freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs);
698 freeArraysThatAreNotMallocedByUsers(pointsPtr, points);
699 freeArraysThatAreNotMallocedByUsers(weightsPtr, weights);
700 };
701
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400702 CanvasKit.Path.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
703 // emulates the HTMLCanvas behavior. See addArc() for the Path version.
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500704 // Note input angles are radians.
705 var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
706 var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400707 var temp = new CanvasKit.Path();
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500708 temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
709 this.addPath(temp, true);
710 temp.delete();
711 return this;
712 };
713
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400714 // Appends arc to Path. Arc added is part of ellipse
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400715 // bounded by oval, from startAngle through sweepAngle. Both startAngle and
716 // sweepAngle are measured in degrees, where zero degrees is aligned with the
717 // positive x-axis, and positive sweeps extends arc clockwise.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400718 CanvasKit.Path.prototype.arcToOval = function(oval, startAngle, sweepAngle, forceMoveTo) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400719 var oPtr = copyRectToWasm(oval);
720 this._arcToOval(oPtr, startAngle, sweepAngle, forceMoveTo);
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400721 return this;
722 };
723
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400724 // Appends arc to Path. Arc is implemented by one or more conics weighted to
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400725 // describe part of oval with radii (rx, ry) rotated by xAxisRotate degrees. Arc
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400726 // curves from last point to (x, y), choosing one of four possible routes:
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400727 // clockwise or counterclockwise, and smaller or larger.
728
729 // Arc sweep is always less than 360 degrees. arcTo() appends line to (x, y) if
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400730 // either radii are zero, or if last point equals (x, y). arcTo() scales radii
731 // (rx, ry) to fit last point and (x, y) if both are greater than zero but
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400732 // too small.
733
734 // arcToRotated() appends up to four conic curves.
735 // arcToRotated() implements the functionality of SVG arc, although SVG sweep-flag value
736 // is opposite the integer value of sweep; SVG sweep-flag uses 1 for clockwise,
737 // while kCW_Direction cast to int is zero.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400738 CanvasKit.Path.prototype.arcToRotated = function(rx, ry, xAxisRotate, useSmallArc, isCCW, x, y) {
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400739 this._arcToRotated(rx, ry, xAxisRotate, !!useSmallArc, !!isCCW, x, y);
740 return this;
741 };
742
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400743 // Appends arc to Path, after appending line if needed. Arc is implemented by conic
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400744 // weighted to describe part of circle. Arc is contained by tangent from
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400745 // last Path point to (x1, y1), and tangent from (x1, y1) to (x2, y2). Arc
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400746 // is part of circle sized to radius, positioned so it touches both tangent lines.
747
748 // If last Path Point does not start Arc, arcTo appends connecting Line to Path.
749 // The length of Vector from (x1, y1) to (x2, y2) does not affect Arc.
750
751 // Arc sweep is always less than 180 degrees. If radius is zero, or if
752 // tangents are nearly parallel, arcTo appends Line from last Path Point to (x1, y1).
753
754 // arcToTangent appends at most one Line and one conic.
755 // arcToTangent implements the functionality of PostScript arct and HTML Canvas arcTo.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400756 CanvasKit.Path.prototype.arcToTangent = function(x1, y1, x2, y2, radius) {
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400757 this._arcToTangent(x1, y1, x2, y2, radius);
758 return this;
759 };
760
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400761 CanvasKit.Path.prototype.close = function() {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500762 this._close();
763 return this;
764 };
765
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400766 CanvasKit.Path.prototype.conicTo = function(x1, y1, x2, y2, w) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500767 this._conicTo(x1, y1, x2, y2, w);
768 return this;
769 };
770
Kevin Lubick7d96c5c2020-10-01 10:55:16 -0400771 // Clients can pass in a Float32Array with length 4 to this and the results
772 // will be copied into that array. Otherwise, a new TypedArray will be allocated
773 // and returned.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400774 CanvasKit.Path.prototype.computeTightBounds = function(optionalOutputArray) {
Kevin Lubick7d96c5c2020-10-01 10:55:16 -0400775 this._computeTightBounds(_scratchRectPtr);
776 var ta = _scratchRect['toTypedArray']();
777 if (optionalOutputArray) {
778 optionalOutputArray.set(ta);
779 return optionalOutputArray;
780 }
781 return ta.slice();
782 };
783
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400784 CanvasKit.Path.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500785 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
786 return this;
787 };
788
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400789 CanvasKit.Path.prototype.dash = function(on, off, phase) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500790 if (this._dash(on, off, phase)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400791 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500792 }
793 return null;
794 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400795
Kevin Lubickf8823b52020-09-03 10:02:10 -0400796 // Clients can pass in a Float32Array with length 4 to this and the results
797 // will be copied into that array. Otherwise, a new TypedArray will be allocated
798 // and returned.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400799 CanvasKit.Path.prototype.getBounds = function(optionalOutputArray) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400800 this._getBounds(_scratchRectPtr);
801 var ta = _scratchRect['toTypedArray']();
802 if (optionalOutputArray) {
803 optionalOutputArray.set(ta);
804 return optionalOutputArray;
805 }
806 return ta.slice();
807 };
808
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400809 CanvasKit.Path.prototype.lineTo = function(x, y) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500810 this._lineTo(x, y);
811 return this;
812 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400813
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400814 CanvasKit.Path.prototype.moveTo = function(x, y) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500815 this._moveTo(x, y);
816 return this;
817 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400818
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400819 CanvasKit.Path.prototype.offset = function(dx, dy) {
Kevin Lubicke384df42019-08-26 15:48:09 -0400820 this._transform(1, 0, dx,
821 0, 1, dy,
822 0, 0, 1);
823 return this;
824 };
825
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400826 CanvasKit.Path.prototype.quadTo = function(cpx, cpy, x, y) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500827 this._quadTo(cpx, cpy, x, y);
828 return this;
829 };
830
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400831 CanvasKit.Path.prototype.rArcTo = function(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400832 this._rArcTo(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy);
833 return this;
834 };
835
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400836 CanvasKit.Path.prototype.rConicTo = function(dx1, dy1, dx2, dy2, w) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400837 this._rConicTo(dx1, dy1, dx2, dy2, w);
838 return this;
839 };
840
841 // These params are all relative
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400842 CanvasKit.Path.prototype.rCubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400843 this._rCubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
844 return this;
845 };
846
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400847 CanvasKit.Path.prototype.rLineTo = function(dx, dy) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400848 this._rLineTo(dx, dy);
849 return this;
850 };
851
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400852 CanvasKit.Path.prototype.rMoveTo = function(dx, dy) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400853 this._rMoveTo(dx, dy);
854 return this;
855 };
856
857 // These params are all relative
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400858 CanvasKit.Path.prototype.rQuadTo = function(cpx, cpy, x, y) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400859 this._rQuadTo(cpx, cpy, x, y);
860 return this;
861 };
862
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400863 CanvasKit.Path.prototype.stroke = function(opts) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500864 // Fill out any missing values with the default values.
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500865 opts = opts || {};
Kevin Lubick6aa38692020-06-01 11:25:47 -0400866 opts['width'] = opts['width'] || 1;
867 opts['miter_limit'] = opts['miter_limit'] || 4;
868 opts['cap'] = opts['cap'] || CanvasKit.StrokeCap.Butt;
869 opts['join'] = opts['join'] || CanvasKit.StrokeJoin.Miter;
870 opts['precision'] = opts['precision'] || 1;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500871 if (this._stroke(opts)) {
872 return this;
873 }
874 return null;
875 };
876
Kevin Lubicka2535af2020-10-02 08:01:57 -0400877 // TODO(kjlubick) Change this to take a 3x3 or 4x4 matrix (optionally malloc'd)
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400878 CanvasKit.Path.prototype.transform = function() {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500879 // Takes 1 or 9 args
880 if (arguments.length === 1) {
881 // argument 1 should be a 6 or 9 element array.
882 var a = arguments[0];
883 this._transform(a[0], a[1], a[2],
884 a[3], a[4], a[5],
885 a[6] || 0, a[7] || 0, a[8] || 1);
886 } else if (arguments.length === 6 || arguments.length === 9) {
887 // these arguments are the 6 or 9 members of the matrix
888 var a = arguments;
889 this._transform(a[0], a[1], a[2],
890 a[3], a[4], a[5],
891 a[6] || 0, a[7] || 0, a[8] || 1);
892 } else {
893 throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
894 }
895 return this;
896 };
897 // isComplement is optional, defaults to false
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400898 CanvasKit.Path.prototype.trim = function(startT, stopT, isComplement) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500899 if (this._trim(startT, stopT, !!isComplement)) {
900 return this;
901 }
902 return null;
903 };
904
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400905 CanvasKit.Image.prototype.encodeToData = function() {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500906 if (!arguments.length) {
907 return this._encodeToData();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400908 }
Kevin Lubick53965c92018-10-11 08:51:55 -0400909
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500910 if (arguments.length === 2) {
911 var a = arguments;
912 return this._encodeToDataWithFormat(a[0], a[1]);
Alexander Khovansky3e119332018-11-15 02:01:19 +0300913 }
914
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500915 throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500916 };
Kevin Lubick1ba9c4d2019-02-22 10:04:06 -0500917
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400918 CanvasKit.Image.prototype.makeShader = function(xTileMode, yTileMode, localMatrix) {
Kevin Lubick6bffe392020-04-02 15:24:15 -0400919 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Kevin Lubick6aa38692020-06-01 11:25:47 -0400920 return this._makeShader(xTileMode, yTileMode, localMatrixPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500921 };
Kevin Lubicka064c282019-04-04 09:28:53 -0400922
Kevin Lubickc4ab0872020-11-03 17:13:09 -0500923 CanvasKit.Image.prototype.readPixels = function(imageInfo, srcX, srcY, destMallocObj) {
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400924 var rowBytes;
Kevin Lubickbe728012020-09-03 11:57:12 +0000925 // Important to use ['string'] notation here, otherwise the closure compiler will
Kevin Lubick319524b2020-01-22 15:29:14 -0500926 // minify away the colorType.
Kevin Lubickbe728012020-09-03 11:57:12 +0000927 switch (imageInfo['colorType']) {
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400928 case CanvasKit.ColorType.RGBA_8888:
929 rowBytes = imageInfo.width * 4; // 1 byte per channel == 4 bytes per pixel in 8888
930 break;
931 case CanvasKit.ColorType.RGBA_F32:
932 rowBytes = imageInfo.width * 16; // 4 bytes per channel == 16 bytes per pixel in F32
933 break;
934 default:
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400935 Debug('Colortype not yet supported');
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400936 return;
937 }
938 var pBytes = rowBytes * imageInfo.height;
Kevin Lubickc4ab0872020-11-03 17:13:09 -0500939 var pPtr;
940 if (destMallocObj) {
941 pPtr = destMallocObj['byteOffset'];
942 } else {
943 pPtr = CanvasKit._malloc(pBytes);
944 }
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400945
946 if (!this._readPixels(imageInfo, pPtr, rowBytes, srcX, srcY)) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400947 Debug('Could not read pixels with the given inputs');
Kevin Lubickc4ab0872020-11-03 17:13:09 -0500948 if (!destMallocObj) {
949 CanvasKit._free(pPtr);
950 }
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400951 return null;
952 }
953
Kevin Lubickc4ab0872020-11-03 17:13:09 -0500954 // If the user provided us a buffer to copy into, we don't need to allocate a new TypedArray.
955 if (destMallocObj) {
956 return destMallocObj['toTypedArray'](); // Return the typed array wrapper w/o allocating.
957 }
958
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400959 // Put those pixels into a typed array of the right format and then
960 // make a copy with slice() that we can return.
961 var retVal = null;
Kevin Lubickbe728012020-09-03 11:57:12 +0000962 switch (imageInfo['colorType']) {
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400963 case CanvasKit.ColorType.RGBA_8888:
Bryce Thomas1fa54042020-01-14 13:46:30 -0800964 retVal = new Uint8Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice();
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400965 break;
966 case CanvasKit.ColorType.RGBA_F32:
Bryce Thomas1fa54042020-01-14 13:46:30 -0800967 retVal = new Float32Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice();
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400968 break;
969 }
970
971 // Free the allocated pixels in the WASM memory
972 CanvasKit._free(pPtr);
973 return retVal;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500974 };
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400975
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400976 // Accepts an array of four numbers in the range of 0-1 representing a 4f color
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400977 CanvasKit.Canvas.prototype.clear = function(color4f) {
Kevin Lubick6aa38692020-06-01 11:25:47 -0400978 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400979 this._clear(cPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500980 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400981
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400982 CanvasKit.Canvas.prototype.clipRRect = function(rrect, op, antialias) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000983 var rPtr = copyRRectToWasm(rrect);
984 this._clipRRect(rPtr, op, antialias);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500985 };
Kevin Lubickbe728012020-09-03 11:57:12 +0000986
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400987 CanvasKit.Canvas.prototype.clipRect = function(rect, op, antialias) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400988 var rPtr = copyRectToWasm(rect);
989 this._clipRect(rPtr, op, antialias);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500990 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400991
Kevin Lubickc1d08982020-04-06 13:52:15 -0400992 // concat takes a 3x2, a 3x3, or a 4x4 matrix and upscales it (if needed) to 4x4. This is because
993 // under the hood, SkCanvas uses a 4x4 matrix.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400994 CanvasKit.Canvas.prototype.concat = function(matr) {
Kevin Lubickc1d08982020-04-06 13:52:15 -0400995 var matrPtr = copy4x4MatrixToWasm(matr);
Kevin Lubick6bffe392020-04-02 15:24:15 -0400996 this._concat(matrPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500997 };
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400998
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400999 CanvasKit.Canvas.prototype.drawArc = function(oval, startAngle, sweepAngle, useCenter, paint) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001000 var oPtr = copyRectToWasm(oval);
1001 this._drawArc(oPtr, startAngle, sweepAngle, useCenter, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001002 };
Kevin Lubickf8823b52020-09-03 10:02:10 -04001003
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001004 // atlas is an Image, e.g. from CanvasKit.MakeImageFromEncoded
1005 // srcRects, dstXforms, and colors should be CanvasKit.RectBuilder, CanvasKit.RSXFormBuilder,
1006 // and CanvasKit.ColorBuilder (fastest)
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001007 // Or they can be an array of floats of length 4*number of destinations.
1008 // colors are optional and used to tint the drawn images using the optional blend mode
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001009 // Colors may be an ColorBuilder, a Uint32Array of int colors,
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001010 // a Flat Float32Array of float colors or a 2d Array of Float32Array(4) (deprecated)
Kevin Lubick97440de2020-09-29 17:58:21 -04001011 // TODO(kjlubick) remove Builders - no longer needed now that Malloc is a thing.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001012 CanvasKit.Canvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint,
Kevin Lubickee91c072019-03-29 10:39:52 -04001013 /*optional*/ blendMode, colors) {
1014 if (!atlas || !paint || !srcRects || !dstXforms) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001015 Debug('Doing nothing since missing a required input');
Kevin Lubickee91c072019-03-29 10:39:52 -04001016 return;
1017 }
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001018
1019 // builder arguments report the length as the number of rects, but when passed as arrays
1020 // their.length attribute is 4x higher because it's the number of total components of all rects.
1021 // colors is always going to report the same length, at least until floats colors are supported
1022 // by this function.
1023 if (srcRects.length !== dstXforms.length) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001024 Debug('Doing nothing since input arrays length mismatches');
Nathaniel Nifonge5d32542020-03-26 09:27:48 -04001025 return;
Kevin Lubickee91c072019-03-29 10:39:52 -04001026 }
1027 if (!blendMode) {
1028 blendMode = CanvasKit.BlendMode.SrcOver;
1029 }
1030
1031 var srcRectPtr;
1032 if (srcRects.build) {
1033 srcRectPtr = srcRects.build();
1034 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001035 srcRectPtr = copy1dArray(srcRects, 'HEAPF32');
Kevin Lubickee91c072019-03-29 10:39:52 -04001036 }
1037
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001038 var count = 1;
Kevin Lubickee91c072019-03-29 10:39:52 -04001039 var dstXformPtr;
1040 if (dstXforms.build) {
1041 dstXformPtr = dstXforms.build();
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001042 count = dstXforms.length;
Kevin Lubickee91c072019-03-29 10:39:52 -04001043 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001044 dstXformPtr = copy1dArray(dstXforms, 'HEAPF32');
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001045 count = dstXforms.length / 4;
Kevin Lubickee91c072019-03-29 10:39:52 -04001046 }
1047
Kevin Lubick6bffe392020-04-02 15:24:15 -04001048 var colorPtr = nullptr;
Kevin Lubickee91c072019-03-29 10:39:52 -04001049 if (colors) {
1050 if (colors.build) {
1051 colorPtr = colors.build();
1052 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001053 colorPtr = copy1dArray(assureIntColors(colors), 'HEAPU32');
Kevin Lubickee91c072019-03-29 10:39:52 -04001054 }
1055 }
1056
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001057 this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, count, blendMode, paint);
Kevin Lubickee91c072019-03-29 10:39:52 -04001058
1059 if (srcRectPtr && !srcRects.build) {
Kevin Lubickcf118922020-05-28 14:43:38 -04001060 freeArraysThatAreNotMallocedByUsers(srcRectPtr, srcRects);
Kevin Lubickee91c072019-03-29 10:39:52 -04001061 }
1062 if (dstXformPtr && !dstXforms.build) {
Kevin Lubickcf118922020-05-28 14:43:38 -04001063 freeArraysThatAreNotMallocedByUsers(dstXformPtr, dstXforms);
Kevin Lubickee91c072019-03-29 10:39:52 -04001064 }
1065 if (colorPtr && !colors.build) {
Kevin Lubickcf118922020-05-28 14:43:38 -04001066 freeArraysThatAreNotMallocedByUsers(colorPtr, colors);
Kevin Lubickee91c072019-03-29 10:39:52 -04001067 }
Kevin Lubick9fe83912020-11-03 17:08:34 -05001068 };
Kevin Lubickee91c072019-03-29 10:39:52 -04001069
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001070 CanvasKit.Canvas.prototype.drawColor = function (color4f, mode) {
Kevin Lubick6aa38692020-06-01 11:25:47 -04001071 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001072 if (mode !== undefined) {
1073 this._drawColor(cPtr, mode);
1074 } else {
1075 this._drawColor(cPtr);
1076 }
Kevin Lubick9fe83912020-11-03 17:08:34 -05001077 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001078
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001079 CanvasKit.Canvas.prototype.drawColorComponents = function (r, g, b, a, mode) {
Kevin Lubick93f1a382020-06-02 16:15:23 -04001080 var cPtr = copyColorComponentsToWasm(r, g, b, a);
1081 if (mode !== undefined) {
1082 this._drawColor(cPtr, mode);
1083 } else {
1084 this._drawColor(cPtr);
1085 }
Kevin Lubick9fe83912020-11-03 17:08:34 -05001086 };
Kevin Lubick93f1a382020-06-02 16:15:23 -04001087
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001088 CanvasKit.Canvas.prototype.drawDRRect = function(outer, inner, paint) {
Kevin Lubickbe728012020-09-03 11:57:12 +00001089 var oPtr = copyRRectToWasm(outer, _scratchRRectPtr);
1090 var iPtr = copyRRectToWasm(inner, _scratchRRect2Ptr);
1091 this._drawDRRect(oPtr, iPtr, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001092 };
Kevin Lubickbe728012020-09-03 11:57:12 +00001093
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001094 CanvasKit.Canvas.prototype.drawImageNine = function(img, center, dest, paint) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001095 var cPtr = copyIRectToWasm(center);
1096 var dPtr = copyRectToWasm(dest);
1097 this._drawImageNine(img, cPtr, dPtr, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001098 };
Kevin Lubickf8823b52020-09-03 10:02:10 -04001099
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001100 CanvasKit.Canvas.prototype.drawImageRect = function(img, src, dest, paint, fastSample) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001101 var sPtr = copyRectToWasm(src, _scratchRectPtr);
1102 var dPtr = copyRectToWasm(dest, _scratchRect2Ptr);
1103 this._drawImageRect(img, sPtr, dPtr, paint, !!fastSample);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001104 };
Kevin Lubickf8823b52020-09-03 10:02:10 -04001105
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001106 CanvasKit.Canvas.prototype.drawOval = function(oval, paint) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001107 var oPtr = copyRectToWasm(oval);
1108 this._drawOval(oPtr, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001109 };
Kevin Lubickf8823b52020-09-03 10:02:10 -04001110
Kevin Lubick37ab53e2019-11-11 10:06:08 -05001111 // points is either an array of [x, y] where x and y are numbers or
1112 // a typed array from Malloc where the even indices will be treated
1113 // as x coordinates and the odd indices will be treated as y coordinates.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001114 CanvasKit.Canvas.prototype.drawPoints = function(mode, points, paint) {
Kevin Lubick37ab53e2019-11-11 10:06:08 -05001115 var ptr;
1116 var n;
1117 // This was created with CanvasKit.Malloc, so assume the user has
1118 // already been filled with data.
1119 if (points['_ck']) {
1120 ptr = points.byteOffset;
1121 n = points.length/2;
1122 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001123 ptr = copy2dArray(points, 'HEAPF32');
Kevin Lubick37ab53e2019-11-11 10:06:08 -05001124 n = points.length;
1125 }
1126 this._drawPoints(mode, ptr, n, paint);
Kevin Lubickcf118922020-05-28 14:43:38 -04001127 freeArraysThatAreNotMallocedByUsers(ptr, points);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001128 };
Kevin Lubick37ab53e2019-11-11 10:06:08 -05001129
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001130 CanvasKit.Canvas.prototype.drawRRect = function(rrect, paint) {
Kevin Lubickbe728012020-09-03 11:57:12 +00001131 var rPtr = copyRRectToWasm(rrect);
1132 this._drawRRect(rPtr, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001133 };
Kevin Lubickbe728012020-09-03 11:57:12 +00001134
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001135 CanvasKit.Canvas.prototype.drawRect = function(rect, paint) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001136 var rPtr = copyRectToWasm(rect);
1137 this._drawRect(rPtr, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001138 };
Kevin Lubickf8823b52020-09-03 10:02:10 -04001139
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001140 CanvasKit.Canvas.prototype.drawShadow = function(path, zPlaneParams, lightPos, lightRadius, ambientColor, spotColor, flags) {
Kevin Lubick6aa38692020-06-01 11:25:47 -04001141 var ambiPtr = copyColorToWasmNoScratch(ambientColor);
1142 var spotPtr = copyColorToWasmNoScratch(spotColor);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001143 this._drawShadow(path, zPlaneParams, lightPos, lightRadius, ambiPtr, spotPtr, flags);
Kevin Lubickcf118922020-05-28 14:43:38 -04001144 freeArraysThatAreNotMallocedByUsers(ambiPtr, ambientColor);
1145 freeArraysThatAreNotMallocedByUsers(spotPtr, spotColor);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001146 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001147
Kevin Lubickc1d08982020-04-06 13:52:15 -04001148 // getLocalToDevice returns a 4x4 matrix.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001149 CanvasKit.Canvas.prototype.getLocalToDevice = function() {
Kevin Lubickc1d08982020-04-06 13:52:15 -04001150 // _getLocalToDevice will copy the values into the pointer.
Kevin Lubick6aa38692020-06-01 11:25:47 -04001151 this._getLocalToDevice(_scratch4x4MatrixPtr);
1152 return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001153 };
Kevin Lubickc1d08982020-04-06 13:52:15 -04001154
Nathaniel Nifong00de91c2020-05-06 16:22:33 -04001155 // findMarkedCTM returns a 4x4 matrix, or null if a matrix was not found at
1156 // the provided marker.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001157 CanvasKit.Canvas.prototype.findMarkedCTM = function(marker) {
Nathaniel Nifong00de91c2020-05-06 16:22:33 -04001158 // _getLocalToDevice will copy the values into the pointer.
Kevin Lubick6aa38692020-06-01 11:25:47 -04001159 var found = this._findMarkedCTM(marker, _scratch4x4MatrixPtr);
Nathaniel Nifong00de91c2020-05-06 16:22:33 -04001160 if (!found) {
1161 return null;
1162 }
Kevin Lubick6aa38692020-06-01 11:25:47 -04001163 return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001164 };
Nathaniel Nifong00de91c2020-05-06 16:22:33 -04001165
Kevin Lubickc1d08982020-04-06 13:52:15 -04001166 // getTotalMatrix returns the current matrix as a 3x3 matrix.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001167 CanvasKit.Canvas.prototype.getTotalMatrix = function() {
Kevin Lubick6bffe392020-04-02 15:24:15 -04001168 // _getTotalMatrix will copy the values into the pointer.
Kevin Lubick6aa38692020-06-01 11:25:47 -04001169 this._getTotalMatrix(_scratch3x3MatrixPtr);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001170 // read them out into an array. TODO(kjlubick): If we change Matrix to be
Kevin Lubick6bffe392020-04-02 15:24:15 -04001171 // typedArrays, then we should return a typed array here too.
1172 var rv = new Array(9);
1173 for (var i = 0; i < 9; i++) {
Kevin Lubick6aa38692020-06-01 11:25:47 -04001174 rv[i] = CanvasKit.HEAPF32[_scratch3x3MatrixPtr/4 + i]; // divide by 4 to "cast" to float.
Kevin Lubick6bffe392020-04-02 15:24:15 -04001175 }
Kevin Lubick6bffe392020-04-02 15:24:15 -04001176 return rv;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001177 };
Kevin Lubick6bffe392020-04-02 15:24:15 -04001178
Kevin Lubickc4ab0872020-11-03 17:13:09 -05001179 // TODO(kjlubick) align this API with Image.readPixels
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001180 CanvasKit.Canvas.prototype.readPixels = function(x, y, w, h, alphaType,
Kevin Lubickc4ab0872020-11-03 17:13:09 -05001181 colorType, colorSpace, dstRowBytes,
1182 destMallocObj) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001183 // supply defaults (which are compatible with HTMLCanvas's getImageData)
1184 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
1185 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001186 colorSpace = colorSpace || CanvasKit.ColorSpace.SRGB;
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001187 var pixBytes = 4;
1188 if (colorType === CanvasKit.ColorType.RGBA_F16) {
1189 pixBytes = 8;
1190 }
1191 dstRowBytes = dstRowBytes || (pixBytes * w);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001192
Kevin Lubickc4ab0872020-11-03 17:13:09 -05001193 var len = h * dstRowBytes;
1194 var pPtr;
1195 if (destMallocObj) {
1196 pPtr = destMallocObj['byteOffset'];
1197 } else {
1198 pPtr = CanvasKit._malloc(len);
1199 }
1200
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001201 var ok = this._readPixels({
1202 'width': w,
1203 'height': h,
Kevin Lubick52b9f372018-12-04 13:57:36 -05001204 'colorType': colorType,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001205 'alphaType': alphaType,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001206 'colorSpace': colorSpace,
Kevin Lubickc4ab0872020-11-03 17:13:09 -05001207 }, pPtr, dstRowBytes, x, y);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001208 if (!ok) {
Kevin Lubickc4ab0872020-11-03 17:13:09 -05001209 if (!destMallocObj) {
1210 CanvasKit._free(pPtr);
1211 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001212 return null;
1213 }
1214
Kevin Lubickc4ab0872020-11-03 17:13:09 -05001215 // If the user provided us a buffer to copy into, we don't need to allocate a new TypedArray.
1216 if (destMallocObj) {
1217 return destMallocObj['toTypedArray'](); // Return the typed array wrapper w/o allocating.
1218 }
1219
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001220 // The first typed array is just a view into memory. Because we will
1221 // be free-ing that, we call slice to make a persistent copy.
Kevin Lubickc4ab0872020-11-03 17:13:09 -05001222 var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pPtr, len).slice();
1223 CanvasKit._free(pPtr);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001224 return pixels;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001225 };
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001226
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001227 CanvasKit.Canvas.prototype.saveLayer = function(paint, boundsRect, backdrop, flags) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001228 // bPtr will be 0 (nullptr) if boundsRect is undefined/null.
1229 var bPtr = copyRectToWasm(boundsRect);
1230 // These or clauses help emscripten, which does not deal with undefined well.
1231 return this._saveLayer(paint || null, bPtr, backdrop || null, flags || 0);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001232 };
Kevin Lubickf8823b52020-09-03 10:02:10 -04001233
Kevin Lubickcf118922020-05-28 14:43:38 -04001234 // pixels should be a Uint8Array or a plain JS array.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001235 CanvasKit.Canvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001236 destX, destY, alphaType, colorType, colorSpace) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001237 if (pixels.byteLength % (srcWidth * srcHeight)) {
1238 throw 'pixels length must be a multiple of the srcWidth * srcHeight';
1239 }
1240 var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
1241 // supply defaults (which are compatible with HTMLCanvas's putImageData)
1242 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
1243 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001244 colorSpace = colorSpace || CanvasKit.ColorSpace.SRGB;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001245 var srcRowBytes = bytesPerPixel * srcWidth;
1246
Kevin Lubickbe728012020-09-03 11:57:12 +00001247 var pptr = copy1dArray(pixels, 'HEAPU8');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001248 var ok = this._writePixels({
1249 'width': srcWidth,
1250 'height': srcHeight,
1251 'colorType': colorType,
1252 'alphaType': alphaType,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001253 'colorSpace': colorSpace,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001254 }, pptr, srcRowBytes, destX, destY);
1255
Kevin Lubickcf118922020-05-28 14:43:38 -04001256 freeArraysThatAreNotMallocedByUsers(pptr, pixels);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001257 return ok;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001258 };
Kevin Lubick52b9f372018-12-04 13:57:36 -05001259
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001260 CanvasKit.ColorFilter.MakeBlend = function(color4f, mode) {
Kevin Lubick6aa38692020-06-01 11:25:47 -04001261 var cPtr = copyColorToWasm(color4f);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001262 var result = CanvasKit.ColorFilter._MakeBlend(cPtr, mode);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001263 return result;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001264 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001265
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001266 // colorMatrix is an ColorMatrix (e.g. Float32Array of length 20)
1267 CanvasKit.ColorFilter.MakeMatrix = function(colorMatrix) {
Kevin Lubickd3729342019-09-12 11:11:25 -04001268 if (!colorMatrix || colorMatrix.length !== 20) {
Kevin Lubick6bffe392020-04-02 15:24:15 -04001269 throw 'invalid color matrix';
Kevin Lubickd3729342019-09-12 11:11:25 -04001270 }
Kevin Lubickbe728012020-09-03 11:57:12 +00001271 var fptr = copy1dArray(colorMatrix, 'HEAPF32');
Kevin Lubickd3729342019-09-12 11:11:25 -04001272 // We know skia memcopies the floats, so we can free our memory after the call returns.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001273 var m = CanvasKit.ColorFilter._makeMatrix(fptr);
Kevin Lubickcf118922020-05-28 14:43:38 -04001274 freeArraysThatAreNotMallocedByUsers(fptr, colorMatrix);
Kevin Lubickd3729342019-09-12 11:11:25 -04001275 return m;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001276 };
Kevin Lubickd3729342019-09-12 11:11:25 -04001277
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001278 CanvasKit.ImageFilter.MakeMatrixTransform = function(matr, filterQuality, input) {
Kevin Lubick6bffe392020-04-02 15:24:15 -04001279 var matrPtr = copy3x3MatrixToWasm(matr);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001280 return CanvasKit.ImageFilter._MakeMatrixTransform(matrPtr, filterQuality, input);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001281 };
Kevin Lubick6bffe392020-04-02 15:24:15 -04001282
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001283 CanvasKit.Paint.prototype.getColor = function() {
Kevin Lubick6aa38692020-06-01 11:25:47 -04001284 this._getColor(_scratchColorPtr);
1285 return copyColorFromWasm(_scratchColorPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001286 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001287
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001288 CanvasKit.Paint.prototype.setColor = function(color4f, colorSpace) {
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001289 colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001290 // emscripten wouldn't bind undefined to the sk_sp<ColorSpace> expected here.
Kevin Lubick6aa38692020-06-01 11:25:47 -04001291 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001292 this._setColor(cPtr, colorSpace);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001293 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001294
Kevin Lubick93f1a382020-06-02 16:15:23 -04001295 // The color components here are expected to be floating point values (nominally between
1296 // 0.0 and 1.0, but with wider color gamuts, the values could exceed this range). To convert
1297 // between standard 8 bit colors and floats, just divide by 255 before passing them in.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001298 CanvasKit.Paint.prototype.setColorComponents = function(r, g, b, a, colorSpace) {
Kevin Lubick93f1a382020-06-02 16:15:23 -04001299 colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001300 // emscripten wouldn't bind undefined to the sk_sp<ColorSpace> expected here.
Kevin Lubick93f1a382020-06-02 16:15:23 -04001301 var cPtr = copyColorComponentsToWasm(r, g, b, a);
1302 this._setColor(cPtr, colorSpace);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001303 };
Kevin Lubick93f1a382020-06-02 16:15:23 -04001304
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001305 CanvasKit.PictureRecorder.prototype.beginRecording = function(bounds) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001306 var bPtr = copyRectToWasm(bounds);
1307 return this._beginRecording(bPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001308 };
Kevin Lubickf8823b52020-09-03 10:02:10 -04001309
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001310 CanvasKit.Surface.prototype.makeImageSnapshot = function(optionalBoundsRect) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001311 var bPtr = copyIRectToWasm(optionalBoundsRect);
1312 return this._makeImageSnapshot(bPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001313 };
Kevin Lubickf8823b52020-09-03 10:02:10 -04001314
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001315 CanvasKit.Surface.prototype.requestAnimationFrame = function(callback, dirtyRect) {
Kevin Lubick359a7e32019-03-19 09:34:37 -04001316 if (!this._cached_canvas) {
1317 this._cached_canvas = this.getCanvas();
1318 }
Elliot Evans1ec3b1a2020-07-23 10:25:58 -04001319 requestAnimationFrame(function() {
Kevin Lubick39026282019-03-28 12:46:40 -04001320 if (this._context !== undefined) {
1321 CanvasKit.setCurrentContext(this._context);
1322 }
Kevin Lubick359a7e32019-03-19 09:34:37 -04001323
1324 callback(this._cached_canvas);
1325
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001326 // We do not dispose() of the Surface here, as the client will typically
Bryce Thomas2c5b8562020-01-22 13:49:41 -08001327 // call requestAnimationFrame again from within the supplied callback.
1328 // For drawing a single frame, prefer drawOnce().
Bryce Thomas9331ca02020-05-29 16:51:21 -07001329 this.flush(dirtyRect);
Kevin Lubick359a7e32019-03-19 09:34:37 -04001330 }.bind(this));
Kevin Lubick9fe83912020-11-03 17:08:34 -05001331 };
Kevin Lubick359a7e32019-03-19 09:34:37 -04001332
Kevin Lubick52379332020-01-27 10:01:25 -05001333 // drawOnce will dispose of the surface after drawing the frame using the provided
1334 // callback.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001335 CanvasKit.Surface.prototype.drawOnce = function(callback, dirtyRect) {
Bryce Thomas2c5b8562020-01-22 13:49:41 -08001336 if (!this._cached_canvas) {
1337 this._cached_canvas = this.getCanvas();
1338 }
Elliot Evans1ec3b1a2020-07-23 10:25:58 -04001339 requestAnimationFrame(function() {
Bryce Thomas2c5b8562020-01-22 13:49:41 -08001340 if (this._context !== undefined) {
1341 CanvasKit.setCurrentContext(this._context);
1342 }
1343 callback(this._cached_canvas);
1344
Bryce Thomas9331ca02020-05-29 16:51:21 -07001345 this.flush(dirtyRect);
Bryce Thomas2c5b8562020-01-22 13:49:41 -08001346 this.dispose();
1347 }.bind(this));
Kevin Lubick9fe83912020-11-03 17:08:34 -05001348 };
Bryce Thomas2c5b8562020-01-22 13:49:41 -08001349
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001350 CanvasKit.PathEffect.MakeDash = function(intervals, phase) {
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001351 if (!phase) {
1352 phase = 0;
1353 }
1354 if (!intervals.length || intervals.length % 2 === 1) {
1355 throw 'Intervals array must have even length';
1356 }
Kevin Lubickbe728012020-09-03 11:57:12 +00001357 var ptr = copy1dArray(intervals, 'HEAPF32');
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001358 var dpe = CanvasKit.PathEffect._MakeDash(ptr, intervals.length, phase);
Kevin Lubickcf118922020-05-28 14:43:38 -04001359 freeArraysThatAreNotMallocedByUsers(ptr, intervals);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001360 return dpe;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001361 };
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001362
Kevin Lubick421ba882020-10-15 13:07:33 -04001363 CanvasKit.Shader.MakeColor = function(color4f, colorSpace) {
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001364 colorSpace = colorSpace || null
Kevin Lubick6aa38692020-06-01 11:25:47 -04001365 var cPtr = copyColorToWasm(color4f);
Kevin Lubick421ba882020-10-15 13:07:33 -04001366 return CanvasKit.Shader._MakeColor(cPtr, colorSpace);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001367 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001368
Kevin Lubick421ba882020-10-15 13:07:33 -04001369 // TODO(kjlubick) remove deprecated names.
1370 CanvasKit.Shader.Blend = CanvasKit.Shader.MakeBlend;
1371 CanvasKit.Shader.Color = CanvasKit.Shader.MakeColor;
1372 CanvasKit.Shader.Lerp = CanvasKit.Shader.MakeLerp;
1373
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001374 CanvasKit.Shader.MakeLinearGradient = function(start, end, colors, pos, mode, localMatrix, flags, colorSpace) {
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001375 colorSpace = colorSpace || null
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001376 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubick421ba882020-10-15 13:07:33 -04001377 var posPtr = copy1dArray(pos, 'HEAPF32');
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001378 flags = flags || 0;
Kevin Lubick6bffe392020-04-02 15:24:15 -04001379 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001380
Kevin Lubick421ba882020-10-15 13:07:33 -04001381 var lgs = CanvasKit.Shader._MakeLinearGradient(start, end, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr,
1382 cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001383
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001384 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001385 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001386 return lgs;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001387 };
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001388
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001389 CanvasKit.Shader.MakeRadialGradient = function(center, radius, colors, pos, mode, localMatrix, flags, colorSpace) {
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001390 colorSpace = colorSpace || null
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001391 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubick421ba882020-10-15 13:07:33 -04001392 var posPtr = copy1dArray(pos, 'HEAPF32');
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001393 flags = flags || 0;
Kevin Lubick6bffe392020-04-02 15:24:15 -04001394 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001395
Kevin Lubick421ba882020-10-15 13:07:33 -04001396 var rgs = CanvasKit.Shader._MakeRadialGradient(center, radius, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr,
1397 cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001398
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001399 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001400 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001401 return rgs;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001402 };
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001403
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001404 CanvasKit.Shader.MakeSweepGradient = function(cx, cy, colors, pos, mode, localMatrix, flags, startAngle, endAngle, colorSpace) {
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001405 colorSpace = colorSpace || null
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001406 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubick421ba882020-10-15 13:07:33 -04001407 var posPtr = copy1dArray(pos, 'HEAPF32');
Dan Field3d44f732020-03-16 09:17:30 -07001408 flags = flags || 0;
1409 startAngle = startAngle || 0;
1410 endAngle = endAngle || 360;
Kevin Lubick6bffe392020-04-02 15:24:15 -04001411 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Dan Field3d44f732020-03-16 09:17:30 -07001412
Kevin Lubick421ba882020-10-15 13:07:33 -04001413 var sgs = CanvasKit.Shader._MakeSweepGradient(cx, cy, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr,
1414 cPtrInfo.count, mode,
1415 startAngle, endAngle, flags,
1416 localMatrixPtr, colorSpace);
Dan Field3d44f732020-03-16 09:17:30 -07001417
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001418 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001419 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Dan Field3d44f732020-03-16 09:17:30 -07001420 return sgs;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001421 };
Dan Field3d44f732020-03-16 09:17:30 -07001422
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001423 CanvasKit.Shader.MakeTwoPointConicalGradient = function(start, startRadius, end, endRadius,
Kevin Lubick421ba882020-10-15 13:07:33 -04001424 colors, pos, mode, localMatrix, flags, colorSpace) {
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001425 colorSpace = colorSpace || null
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001426 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubick421ba882020-10-15 13:07:33 -04001427 var posPtr = copy1dArray(pos, 'HEAPF32');
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001428 flags = flags || 0;
Kevin Lubick6bffe392020-04-02 15:24:15 -04001429 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001430
Kevin Lubick421ba882020-10-15 13:07:33 -04001431 var rgs = CanvasKit.Shader._MakeTwoPointConicalGradient(
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001432 start, startRadius, end, endRadius, cPtrInfo.colorPtr, cPtrInfo.colorType,
1433 posPtr, cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001434
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001435 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001436 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001437 return rgs;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001438 };
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001439
Kevin Lubickf8823b52020-09-03 10:02:10 -04001440 // Clients can pass in a Float32Array with length 4 to this and the results
1441 // will be copied into that array. Otherwise, a new TypedArray will be allocated
1442 // and returned.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001443 CanvasKit.Vertices.prototype.bounds = function(optionalOutputArray) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001444 this._bounds(_scratchRectPtr);
1445 var ta = _scratchRect['toTypedArray']();
1446 if (optionalOutputArray) {
1447 optionalOutputArray.set(ta);
1448 return optionalOutputArray;
1449 }
1450 return ta.slice();
Kevin Lubick9fe83912020-11-03 17:08:34 -05001451 };
Kevin Lubickf8823b52020-09-03 10:02:10 -04001452
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001453 // Run through the JS files that are added at compile time.
1454 if (CanvasKit._extraInitializations) {
1455 CanvasKit._extraInitializations.forEach(function(init) {
1456 init();
1457 });
Kevin Lubickeb2f6b02018-11-29 15:07:02 -05001458 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001459}; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubickeb2f6b02018-11-29 15:07:02 -05001460
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001461// Accepts an object holding two canvaskit colors.
1462// {
Kevin Lubick3d00e3a2020-10-02 14:59:28 -04001463// ambient: [r, g, b, a],
1464// spot: [r, g, b, a],
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001465// }
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001466// Returns the same format. Note, if malloced colors are passed in, the memory
1467// housing the passed in colors passed in will be overwritten with the computed
1468// tonal colors.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001469CanvasKit.computeTonalColors = function(tonalColors) {
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001470 // copy the colors into WASM
Kevin Lubick6aa38692020-06-01 11:25:47 -04001471 var cPtrAmbi = copyColorToWasmNoScratch(tonalColors['ambient']);
1472 var cPtrSpot = copyColorToWasmNoScratch(tonalColors['spot']);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001473 // The output of this function will be the same pointers we passed in.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001474 this._computeTonalColors(cPtrAmbi, cPtrSpot);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001475 // Read the results out.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001476 var result = {
1477 'ambient': copyColorFromWasm(cPtrAmbi),
1478 'spot': copyColorFromWasm(cPtrSpot),
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001479 };
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001480 // If the user passed us malloced colors in here, we don't want to clean them up.
1481 freeArraysThatAreNotMallocedByUsers(cPtrAmbi, tonalColors['ambient']);
1482 freeArraysThatAreNotMallocedByUsers(cPtrSpot, tonalColors['spot']);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001483 return result;
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001484};
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001485
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001486CanvasKit.LTRBRect = function(l, t, r, b) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001487 return Float32Array.of(l, t, r, b);
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001488};
Kevin Lubick1a05fce2018-11-20 12:51:16 -05001489
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001490CanvasKit.XYWHRect = function(x, y, w, h) {
Kevin Lubickf8823b52020-09-03 10:02:10 -04001491 return Float32Array.of(x, y, x+w, y+h);
1492};
1493
1494CanvasKit.LTRBiRect = function(l, t, r, b) {
1495 return Int32Array.of(l, t, r, b);
1496};
1497
1498CanvasKit.XYWHiRect = function(x, y, w, h) {
1499 return Int32Array.of(x, y, x+w, y+h);
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001500};
Kevin Lubick1a05fce2018-11-20 12:51:16 -05001501
Kevin Lubickbe728012020-09-03 11:57:12 +00001502// RRectXY returns a TypedArray representing an RRect with the given rect and a radiusX and
1503// radiusY for all 4 corners.
Kevin Lubick7d644e12019-09-11 14:22:22 -04001504CanvasKit.RRectXY = function(rect, rx, ry) {
Kevin Lubickbe728012020-09-03 11:57:12 +00001505 return Float32Array.of(
Kevin Lubickf8823b52020-09-03 10:02:10 -04001506 rect[0], rect[1], rect[2], rect[3],
Kevin Lubickbe728012020-09-03 11:57:12 +00001507 rx, ry,
1508 rx, ry,
1509 rx, ry,
1510 rx, ry,
1511 );
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001512};
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001513
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001514// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubick6b921b72019-09-18 16:18:17 -04001515CanvasKit.MakeAnimatedImageFromEncoded = function(data) {
1516 data = new Uint8Array(data);
1517
1518 var iptr = CanvasKit._malloc(data.byteLength);
1519 CanvasKit.HEAPU8.set(data, iptr);
1520 var img = CanvasKit._decodeAnimatedImage(iptr, data.byteLength);
1521 if (!img) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001522 Debug('Could not decode animated image');
Kevin Lubick6b921b72019-09-18 16:18:17 -04001523 return null;
1524 }
1525 return img;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001526};
Kevin Lubick6b921b72019-09-18 16:18:17 -04001527
1528// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001529CanvasKit.MakeImageFromEncoded = function(data) {
1530 data = new Uint8Array(data);
1531
1532 var iptr = CanvasKit._malloc(data.byteLength);
1533 CanvasKit.HEAPU8.set(data, iptr);
1534 var img = CanvasKit._decodeImage(iptr, data.byteLength);
1535 if (!img) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001536 Debug('Could not decode image');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001537 return null;
1538 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001539 return img;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001540};
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001541
Elliot Evans28796192020-06-15 12:53:27 -06001542// A variable to hold a canvasElement which can be reused once created the first time.
1543var memoizedCanvas2dElement = null;
1544
1545// Alternative to CanvasKit.MakeImageFromEncoded. Allows for CanvasKit users to take advantage of
1546// browser APIs to decode images instead of using codecs included in the CanvasKit wasm binary.
1547// Expects that the canvasImageSource has already loaded/decoded.
1548// CanvasImageSource reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasImageSource
1549CanvasKit.MakeImageFromCanvasImageSource = function(canvasImageSource) {
1550 var width = canvasImageSource.width;
1551 var height = canvasImageSource.height;
1552
1553 if (!memoizedCanvas2dElement) {
1554 memoizedCanvas2dElement = document.createElement('canvas');
1555 }
1556 memoizedCanvas2dElement.width = width;
1557 memoizedCanvas2dElement.height = height;
1558
1559 var ctx2d = memoizedCanvas2dElement.getContext('2d');
1560 ctx2d.drawImage(canvasImageSource, 0, 0);
1561
1562 var imageData = ctx2d.getImageData(0, 0, width, height);
1563
1564 return CanvasKit.MakeImage(
1565 imageData.data,
1566 width,
1567 height,
1568 CanvasKit.AlphaType.Unpremul,
1569 CanvasKit.ColorType.RGBA_8888,
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001570 CanvasKit.ColorSpace.SRGB
Elliot Evans28796192020-06-15 12:53:27 -06001571 );
Kevin Lubick9fe83912020-11-03 17:08:34 -05001572};
Elliot Evans28796192020-06-15 12:53:27 -06001573
1574// pixels may be any Typed Array, but Uint8Array or Uint8ClampedArray is recommended,
1575// with bytes representing the pixel values.
Kevin Lubickeda0b432019-12-02 08:26:48 -05001576// (e.g. each set of 4 bytes could represent RGBA values for a single pixel).
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001577CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType, colorSpace) {
Kevin Lubickeda0b432019-12-02 08:26:48 -05001578 var bytesPerPixel = pixels.length / (width * height);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001579 var info = {
1580 'width': width,
1581 'height': height,
1582 'alphaType': alphaType,
1583 'colorType': colorType,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001584 'colorSpace': colorSpace,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001585 };
Kevin Lubickbe728012020-09-03 11:57:12 +00001586 var pptr = copy1dArray(pixels, 'HEAPU8');
Kevin Lubickeda0b432019-12-02 08:26:48 -05001587 // No need to _free pptr, Image takes it with SkData::MakeFromMalloc
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001588
Kevin Lubickeda0b432019-12-02 08:26:48 -05001589 return CanvasKit._MakeImage(info, pptr, pixels.length, width * bytesPerPixel);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001590};
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001591
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001592// Colors may be a Uint32Array of int colors, a Flat Float32Array of float colors
1593// or a 2d Array of Float32Array(4) (deprecated)
1594// the underlying skia function accepts only int colors so it is recommended
1595// to pass an array of int colors to avoid an extra conversion.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001596// ColorBuilder is not accepted.
1597CanvasKit.MakeVertices = function(mode, positions, textureCoordinates, colors,
1598 indices, isVolatile) {
Kevin Lubickb3574c92019-03-06 08:25:36 -05001599 // Default isVolitile to true if not set
1600 isVolatile = isVolatile === undefined ? true : isVolatile;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001601 var idxCount = (indices && indices.length) || 0;
1602
1603 var flags = 0;
1604 // These flags are from SkVertices.h and should be kept in sync with those.
1605 if (textureCoordinates && textureCoordinates.length) {
1606 flags |= (1 << 0);
1607 }
1608 if (colors && colors.length) {
1609 flags |= (1 << 1);
1610 }
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001611 if (!isVolatile) {
Mike Reed5caf9352020-03-02 14:57:09 -05001612 flags |= (1 << 2);
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001613 }
1614
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001615 var builder = new CanvasKit._VerticesBuilder(mode, positions.length, idxCount, flags);
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001616
Kevin Lubickbe728012020-09-03 11:57:12 +00001617 copy2dArray(positions, 'HEAPF32', builder.positions());
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001618 if (builder.texCoords()) {
Kevin Lubickbe728012020-09-03 11:57:12 +00001619 copy2dArray(textureCoordinates, 'HEAPF32', builder.texCoords());
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001620 }
1621 if (builder.colors()) {
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001622 if (colors.build) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001623 throw('Color builder not accepted by MakeVertices, use array of ints');
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001624 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001625 copy1dArray(assureIntColors(colors), 'HEAPU32', builder.colors());
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001626 }
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001627 }
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001628 if (builder.indices()) {
Kevin Lubickbe728012020-09-03 11:57:12 +00001629 copy1dArray(indices, 'HEAPU16', builder.indices());
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001630 }
Kevin Lubickb3574c92019-03-06 08:25:36 -05001631
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001632 // Create the vertices, which owns the memory that the builder had allocated.
1633 return builder.detach();
Kevin Lubicka4f218d2020-01-14 08:39:09 -05001634};