Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1 | // 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 Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 4 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 5 | // CanvasKit.onRuntimeInitialized is called after the WASM library has loaded. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 6 | // Anything that modifies an exposed class (e.g. Path) should be set |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 7 | // after onRuntimeInitialized, otherwise, it can happen outside of that scope. |
| 8 | CanvasKit.onRuntimeInitialized = function() { |
| 9 | // All calls to 'this' need to go in externs.js so closure doesn't minify them away. |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 10 | |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 11 | _scratchColor = CanvasKit.Malloc(Float32Array, 4); // 4 color scalars. |
Kevin Lubick | b42660f | 2020-06-03 09:58:27 -0400 | [diff] [blame] | 12 | _scratchColorPtr = _scratchColor['byteOffset']; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 13 | |
| 14 | _scratch4x4Matrix = CanvasKit.Malloc(Float32Array, 16); // 16 matrix scalars. |
Kevin Lubick | b42660f | 2020-06-03 09:58:27 -0400 | [diff] [blame] | 15 | _scratch4x4MatrixPtr = _scratch4x4Matrix['byteOffset']; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 16 | |
| 17 | _scratch3x3Matrix = CanvasKit.Malloc(Float32Array, 9); // 9 matrix scalars. |
Kevin Lubick | b42660f | 2020-06-03 09:58:27 -0400 | [diff] [blame] | 18 | _scratch3x3MatrixPtr = _scratch3x3Matrix['byteOffset']; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 19 | |
| 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 Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 26 | _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 Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 35 | // Create single copies of all three supported color spaces |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 36 | // 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 Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 40 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 41 | // 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 48 | CanvasKit.Matrix = {}; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 49 | 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 67 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 68 | |
| 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 88 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 89 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 90 | CanvasKit.Matrix.identity = function() { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 91 | return identityN(3); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | // Return the inverse (if it exists) of this matrix. |
Kevin Lubick | 9b56cea | 2020-04-06 08:16:18 -0400 | [diff] [blame] | 95 | // Otherwise, return null. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 96 | CanvasKit.Matrix.invert = function(m) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 97 | // Find the determinant by the sarrus rule. https://en.wikipedia.org/wiki/Rule_of_Sarrus |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 98 | 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 101 | Debug('Warning, uninvertible matrix'); |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 102 | return null; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 103 | } |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 104 | // 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 Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 110 | 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 Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 116 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 117 | // 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 120 | CanvasKit.Matrix.mapPoints = function(matrix, ptArr) { |
| 121 | if (IsDebug && (ptArr.length % 2)) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 122 | throw 'mapPoints requires an even length arr'; |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 123 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 124 | 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 Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 137 | |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 138 | function isnumber(val) { return !isNaN(val); } |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 139 | |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 140 | // generalized iterative algorithm for multiplying two matrices. |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 141 | function multiply(m1, m2, size) { |
| 142 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 143 | if (IsDebug && (!m1.every(isnumber) || !m2.every(isnumber))) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 144 | throw 'Some members of matrices are NaN m1='+m1+', m2='+m2+''; |
| 145 | } |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 146 | if (IsDebug && (m1.length !== m2.length)) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 147 | throw 'Undefined for matrices of different sizes. m1.length='+m1.length+', m2.length='+m2.length; |
| 148 | } |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 149 | if (IsDebug && (size*size !== m1.length)) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 150 | 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 Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 162 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 163 | } |
| 164 | return result; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 165 | } |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 166 | |
| 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 170 | if (IsDebug && (listOfMatrices.length < 2)) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 171 | 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 180 | } |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 181 | |
| 182 | // Accept any number 3x3 of matrices as arguments, multiply them together. |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 183 | // Matrix multiplication is associative but not commutative. the order of the arguments |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 184 | // matters, but it does not matter that this implementation multiplies them left to right. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 185 | CanvasKit.Matrix.multiply = function() { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 186 | return multiplyMany(3, arguments); |
| 187 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 188 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 189 | // 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 192 | CanvasKit.Matrix.rotated = function(radians, px, py) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 193 | 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 203 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 204 | CanvasKit.Matrix.scaled = function(sx, sy, px, py) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 205 | px = px || 0; |
| 206 | py = py || 0; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 207 | var m = stride([sx, sy], identityN(3), 3, 0, 1); |
| 208 | return stride([px-sx*px, py-sy*py], m, 3, 2, 0); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 209 | }; |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 210 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 211 | CanvasKit.Matrix.skewed = function(kx, ky, px, py) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 212 | px = px || 0; |
| 213 | py = py || 0; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 214 | var m = stride([kx, ky], identityN(3), 3, 1, -1); |
| 215 | return stride([-kx*px, -ky*py], m, 3, 2, 0); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 216 | }; |
Alexander Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 217 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 218 | CanvasKit.Matrix.translated = function(dx, dy) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 219 | return stride(arguments, identityN(3), 3, 2, 0); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 220 | }; |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 221 | |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 222 | // 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 225 | CanvasKit.Vector = {}; |
| 226 | CanvasKit.Vector.dot = function(a, b) { |
| 227 | if (IsDebug && (a.length !== b.length)) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 228 | 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 231 | }; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 232 | CanvasKit.Vector.lengthSquared = function(v) { |
| 233 | return CanvasKit.Vector.dot(v, v); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 234 | }; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 235 | CanvasKit.Vector.length = function(v) { |
| 236 | return Math.sqrt(CanvasKit.Vector.lengthSquared(v)); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 237 | }; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 238 | CanvasKit.Vector.mulScalar = function(v, s) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 239 | return v.map(function(i) { return i*s }); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 240 | }; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 241 | CanvasKit.Vector.add = function(a, b) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 242 | return a.map(function(v, i) { return v+b[i] }); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 243 | }; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 244 | CanvasKit.Vector.sub = function(a, b) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 245 | return a.map(function(v, i) { return v-b[i]; }); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 246 | }; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 247 | CanvasKit.Vector.dist = function(a, b) { |
| 248 | return CanvasKit.Vector.length(CanvasKit.Vector.sub(a, b)); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 249 | }; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 250 | CanvasKit.Vector.normalize = function(v) { |
| 251 | return CanvasKit.Vector.mulScalar(v, 1/CanvasKit.Vector.length(v)); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 252 | }; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 253 | CanvasKit.Vector.cross = function(a, b) { |
| 254 | if (IsDebug && (a.length !== 3 || a.length !== 3)) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 255 | 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 262 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 263 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 264 | // 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 Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 266 | // ported from C++ code in SkM44.cpp |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 267 | CanvasKit.M44 = {}; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 268 | // Create a 4x4 identity matrix |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 269 | CanvasKit.M44.identity = function() { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 270 | return identityN(4); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 271 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 272 | |
| 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 275 | CanvasKit.M44.translated = function(vec) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 276 | return stride(vec, identityN(4), 4, 3, 0); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 277 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 278 | // Create a 4x4 matrix representing a scaling by the provided 3-vec |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 279 | CanvasKit.M44.scaled = function(vec) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 280 | return stride(vec, identityN(4), 4, 0, 1); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 281 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 282 | // Create a 4x4 matrix representing a rotation about the provided axis 3-vec. |
| 283 | // axis does not need to be normalized. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 284 | CanvasKit.M44.rotated = function(axisVec, radians) { |
| 285 | return CanvasKit.M44.rotatedUnitSinCos( |
| 286 | CanvasKit.Vector.normalize(axisVec), Math.sin(radians), Math.cos(radians)); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 287 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 288 | // 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 Nifong | 3392ebe | 2020-06-01 09:21:36 -0400 | [diff] [blame] | 293 | // is incorrect. Prefer rotated(). |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 294 | CanvasKit.M44.rotatedUnitSinCos = function(axisVec, sinAngle, cosAngle) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 295 | 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 307 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 308 | // Create a 4x4 matrix representing a camera at eyeVec, pointed at centerVec. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 309 | 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 Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 313 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 314 | var m = CanvasKit.M44.identity(); |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 315 | // set each column's top three numbers |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 316 | stride(s, m, 4, 0, 0); |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 317 | stride(CanvasKit.Vector.cross(s, f), m, 4, 1, 0); |
| 318 | stride(CanvasKit.Vector.mulScalar(f, -1), m, 4, 2, 0); |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 319 | stride(eyeVec, m, 4, 3, 0); |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 320 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 321 | var m2 = CanvasKit.M44.invert(m); |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 322 | if (m2 === null) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 323 | return CanvasKit.M44.identity(); |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 324 | } |
| 325 | return m2; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 326 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 327 | // Create a 4x4 matrix representing a perspective. All arguments are scalars. |
| 328 | // angle is in radians. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 329 | 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 Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 332 | } |
| 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 342 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 343 | // Returns the number at the given row and column in matrix m. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 344 | CanvasKit.M44.rc = function(m, r, c) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 345 | return m[r*4+c]; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 346 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 347 | // Accepts any number of 4x4 matrix arguments, multiplies them left to right. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 348 | CanvasKit.M44.multiply = function() { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 349 | return multiplyMany(4, arguments); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 350 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 351 | |
| 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 355 | CanvasKit.M44.invert = function(m) { |
| 356 | if (IsDebug && !m.every(isnumber)) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 357 | 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 396 | Debug('Warning, uninvertible matrix'); |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 397 | 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 437 | if (!tmp.every(function(val) { return !isNaN(val) && val !== Infinity && val !== -Infinity; })) { |
| 438 | Debug('inverted matrix contains infinities or NaN '+tmp); |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 439 | return null; |
| 440 | } |
| 441 | return tmp; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 442 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 443 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 444 | CanvasKit.M44.transpose = function(m) { |
Nathaniel Nifong | cc5415a | 2020-02-23 14:26:33 -0500 | [diff] [blame] | 445 | 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 451 | }; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 452 | |
Nathaniel Nifong | 6130d50 | 2020-07-06 19:50:13 -0400 | [diff] [blame] | 453 | // Return the inverse of an SkM44. throw an error if it's not invertible |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 454 | CanvasKit.M44.mustInvert = function(m) { |
| 455 | var m2 = CanvasKit.M44.invert(m); |
Nathaniel Nifong | 6130d50 | 2020-07-06 19:50:13 -0400 | [diff] [blame] | 456 | if (m2 === null) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 457 | throw 'Matrix not invertible'; |
Nathaniel Nifong | 6130d50 | 2020-07-06 19:50:13 -0400 | [diff] [blame] | 458 | } |
| 459 | return m2; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 460 | }; |
Nathaniel Nifong | 6130d50 | 2020-07-06 19:50:13 -0400 | [diff] [blame] | 461 | |
| 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 475 | 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 Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 478 | 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 480 | 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 485 | }; |
Nathaniel Nifong | 6130d50 | 2020-07-06 19:50:13 -0400 | [diff] [blame] | 486 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 487 | // An ColorMatrix is a 4x4 color matrix that transforms the 4 color channels |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 488 | // 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 Nifong | cc5415a | 2020-02-23 14:26:33 -0500 | [diff] [blame] | 499 | var rScale = 0; |
| 500 | var gScale = 6; |
| 501 | var bScale = 12; |
| 502 | var aScale = 18; |
| 503 | |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 504 | var rPostTrans = 4; |
| 505 | var gPostTrans = 9; |
| 506 | var bPostTrans = 14; |
| 507 | var aPostTrans = 19; |
| 508 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 509 | CanvasKit.ColorMatrix = {}; |
| 510 | CanvasKit.ColorMatrix.identity = function() { |
Nathaniel Nifong | cc5415a | 2020-02-23 14:26:33 -0500 | [diff] [blame] | 511 | 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 517 | }; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 518 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 519 | CanvasKit.ColorMatrix.scaled = function(rs, gs, bs, as) { |
Nathaniel Nifong | cc5415a | 2020-02-23 14:26:33 -0500 | [diff] [blame] | 520 | 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 526 | }; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 527 | |
| 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 534 | CanvasKit.ColorMatrix.rotated = function(axis, sine, cosine) { |
| 535 | var m = CanvasKit.ColorMatrix.identity(); |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 536 | 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 Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 542 | }; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 543 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 544 | // m is a ColorMatrix (i.e. a Float32Array), and this sets the 4 "special" |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 545 | // params that will translate the colors after they are multiplied by the 4x4 matrix. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 546 | CanvasKit.ColorMatrix.postTranslate = function(m, dr, dg, db, da) { |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 547 | m[rPostTrans] += dr; |
| 548 | m[gPostTrans] += dg; |
| 549 | m[bPostTrans] += db; |
| 550 | m[aPostTrans] += da; |
| 551 | return m; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 552 | }; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 553 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 554 | // concat returns a new ColorMatrix that is the result of multiplying outer*inner |
| 555 | CanvasKit.ColorMatrix.concat = function(outer, inner) { |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 556 | 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 Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 573 | }; |
| 574 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 575 | CanvasKit.Path.MakeFromCmds = function(cmds) { |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 576 | var ptrLen = loadCmdsTypedArray(cmds); |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 577 | var path = CanvasKit.Path._MakeFromCmds(ptrLen[0], ptrLen[1]); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 578 | CanvasKit._free(ptrLen[0]); |
| 579 | return path; |
| 580 | }; |
| 581 | |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 582 | // The weights array is optional (only used for conics). |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 583 | CanvasKit.Path.MakeFromVerbsPointsWeights = function(verbs, pts, weights) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 584 | var verbsPtr = copy1dArray(verbs, 'HEAPU8'); |
| 585 | var pointsPtr = copy1dArray(pts, 'HEAPF32'); |
| 586 | var weightsPtr = copy1dArray(weights, 'HEAPF32'); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 587 | var numWeights = (weights && weights.length) || 0; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 588 | var path = CanvasKit.Path._MakeFromVerbsPointsWeights( |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 589 | 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 Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 595 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 596 | CanvasKit.Path.prototype.addArc = function(oval, startAngle, sweepAngle) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 597 | // see arc() for the HTMLCanvas version |
| 598 | // note input angles are degrees. |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 599 | var oPtr = copyRectToWasm(oval); |
| 600 | this._addArc(oPtr, startAngle, sweepAngle); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 601 | return this; |
| 602 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 603 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 604 | CanvasKit.Path.prototype.addOval = function(oval, isCCW, startIndex) { |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 605 | if (startIndex === undefined) { |
| 606 | startIndex = 1; |
| 607 | } |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 608 | var oPtr = copyRectToWasm(oval); |
| 609 | this._addOval(oPtr, !!isCCW, startIndex); |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 610 | return this; |
| 611 | }; |
| 612 | |
Kevin Lubick | a2535af | 2020-10-02 08:01:57 -0400 | [diff] [blame] | 613 | // TODO(kjlubick) clean up this API - split it apart if necessary |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 614 | CanvasKit.Path.prototype.addPath = function() { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 615 | // 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 Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 624 | if (typeof args[args.length-1] === 'boolean') { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 625 | 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 650 | Debug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 651 | return null; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 652 | } |
| 653 | return this; |
| 654 | }; |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 655 | |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 656 | // points is a 1d array of length 2n representing n points where the even indices |
| 657 | // will be treated as x coordinates and the odd indices will be treated as y coordinates. |
| 658 | // Like other APIs, this accepts a malloced type array or malloc obj. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 659 | CanvasKit.Path.prototype.addPoly = function(points, close) { |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 660 | var ptr = copy1dArray(points, 'HEAPF32'); |
| 661 | this._addPoly(ptr, points.length / 2, close); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 662 | freeArraysThatAreNotMallocedByUsers(ptr, points); |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 663 | return this; |
| 664 | }; |
| 665 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 666 | CanvasKit.Path.prototype.addRect = function(rect, isCCW) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 667 | var rPtr = copyRectToWasm(rect); |
| 668 | this._addRect(rPtr, !!isCCW); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 669 | return this; |
| 670 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 671 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 672 | CanvasKit.Path.prototype.addRRect = function(rrect, isCCW) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 673 | var rPtr = copyRRectToWasm(rrect); |
| 674 | this._addRRect(rPtr, !!isCCW); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 675 | return this; |
| 676 | }; |
| 677 | |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 678 | // The weights array is optional (only used for conics). |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 679 | CanvasKit.Path.prototype.addVerbsPointsWeights = function(verbs, points, weights) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 680 | var verbsPtr = copy1dArray(verbs, 'HEAPU8'); |
| 681 | var pointsPtr = copy1dArray(points, 'HEAPF32'); |
| 682 | var weightsPtr = copy1dArray(weights, 'HEAPF32'); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 683 | var numWeights = (weights && weights.length) || 0; |
| 684 | this._addVerbsPointsWeights(verbsPtr, verbs.length, pointsPtr, points.length, |
| 685 | weightsPtr, numWeights); |
| 686 | freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs); |
| 687 | freeArraysThatAreNotMallocedByUsers(pointsPtr, points); |
| 688 | freeArraysThatAreNotMallocedByUsers(weightsPtr, weights); |
| 689 | }; |
| 690 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 691 | CanvasKit.Path.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) { |
| 692 | // emulates the HTMLCanvas behavior. See addArc() for the Path version. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 693 | // Note input angles are radians. |
| 694 | var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius); |
| 695 | var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw); |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 696 | var temp = new CanvasKit.Path(); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 697 | temp.addArc(bounds, radiansToDegrees(startAngle), sweep); |
| 698 | this.addPath(temp, true); |
| 699 | temp.delete(); |
| 700 | return this; |
| 701 | }; |
| 702 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 703 | // Appends arc to Path. Arc added is part of ellipse |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 704 | // bounded by oval, from startAngle through sweepAngle. Both startAngle and |
| 705 | // sweepAngle are measured in degrees, where zero degrees is aligned with the |
| 706 | // positive x-axis, and positive sweeps extends arc clockwise. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 707 | CanvasKit.Path.prototype.arcToOval = function(oval, startAngle, sweepAngle, forceMoveTo) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 708 | var oPtr = copyRectToWasm(oval); |
| 709 | this._arcToOval(oPtr, startAngle, sweepAngle, forceMoveTo); |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 710 | return this; |
| 711 | }; |
| 712 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 713 | // Appends arc to Path. Arc is implemented by one or more conics weighted to |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 714 | // describe part of oval with radii (rx, ry) rotated by xAxisRotate degrees. Arc |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 715 | // curves from last point to (x, y), choosing one of four possible routes: |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 716 | // clockwise or counterclockwise, and smaller or larger. |
| 717 | |
| 718 | // Arc sweep is always less than 360 degrees. arcTo() appends line to (x, y) if |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 719 | // either radii are zero, or if last point equals (x, y). arcTo() scales radii |
| 720 | // (rx, ry) to fit last point and (x, y) if both are greater than zero but |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 721 | // too small. |
| 722 | |
| 723 | // arcToRotated() appends up to four conic curves. |
| 724 | // arcToRotated() implements the functionality of SVG arc, although SVG sweep-flag value |
| 725 | // is opposite the integer value of sweep; SVG sweep-flag uses 1 for clockwise, |
| 726 | // while kCW_Direction cast to int is zero. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 727 | CanvasKit.Path.prototype.arcToRotated = function(rx, ry, xAxisRotate, useSmallArc, isCCW, x, y) { |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 728 | this._arcToRotated(rx, ry, xAxisRotate, !!useSmallArc, !!isCCW, x, y); |
| 729 | return this; |
| 730 | }; |
| 731 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 732 | // Appends arc to Path, after appending line if needed. Arc is implemented by conic |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 733 | // weighted to describe part of circle. Arc is contained by tangent from |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 734 | // last Path point to (x1, y1), and tangent from (x1, y1) to (x2, y2). Arc |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 735 | // is part of circle sized to radius, positioned so it touches both tangent lines. |
| 736 | |
| 737 | // If last Path Point does not start Arc, arcTo appends connecting Line to Path. |
| 738 | // The length of Vector from (x1, y1) to (x2, y2) does not affect Arc. |
| 739 | |
| 740 | // Arc sweep is always less than 180 degrees. If radius is zero, or if |
| 741 | // tangents are nearly parallel, arcTo appends Line from last Path Point to (x1, y1). |
| 742 | |
| 743 | // arcToTangent appends at most one Line and one conic. |
| 744 | // arcToTangent implements the functionality of PostScript arct and HTML Canvas arcTo. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 745 | CanvasKit.Path.prototype.arcToTangent = function(x1, y1, x2, y2, radius) { |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 746 | this._arcToTangent(x1, y1, x2, y2, radius); |
| 747 | return this; |
| 748 | }; |
| 749 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 750 | CanvasKit.Path.prototype.close = function() { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 751 | this._close(); |
| 752 | return this; |
| 753 | }; |
| 754 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 755 | CanvasKit.Path.prototype.conicTo = function(x1, y1, x2, y2, w) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 756 | this._conicTo(x1, y1, x2, y2, w); |
| 757 | return this; |
| 758 | }; |
| 759 | |
Kevin Lubick | 7d96c5c | 2020-10-01 10:55:16 -0400 | [diff] [blame] | 760 | // Clients can pass in a Float32Array with length 4 to this and the results |
| 761 | // will be copied into that array. Otherwise, a new TypedArray will be allocated |
| 762 | // and returned. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 763 | CanvasKit.Path.prototype.computeTightBounds = function(optionalOutputArray) { |
Kevin Lubick | 7d96c5c | 2020-10-01 10:55:16 -0400 | [diff] [blame] | 764 | this._computeTightBounds(_scratchRectPtr); |
| 765 | var ta = _scratchRect['toTypedArray'](); |
| 766 | if (optionalOutputArray) { |
| 767 | optionalOutputArray.set(ta); |
| 768 | return optionalOutputArray; |
| 769 | } |
| 770 | return ta.slice(); |
| 771 | }; |
| 772 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 773 | CanvasKit.Path.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 774 | this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y); |
| 775 | return this; |
| 776 | }; |
| 777 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 778 | CanvasKit.Path.prototype.dash = function(on, off, phase) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 779 | if (this._dash(on, off, phase)) { |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 780 | return this; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 781 | } |
| 782 | return null; |
| 783 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 784 | |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 785 | // Clients can pass in a Float32Array with length 4 to this and the results |
| 786 | // will be copied into that array. Otherwise, a new TypedArray will be allocated |
| 787 | // and returned. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 788 | CanvasKit.Path.prototype.getBounds = function(optionalOutputArray) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 789 | this._getBounds(_scratchRectPtr); |
| 790 | var ta = _scratchRect['toTypedArray'](); |
| 791 | if (optionalOutputArray) { |
| 792 | optionalOutputArray.set(ta); |
| 793 | return optionalOutputArray; |
| 794 | } |
| 795 | return ta.slice(); |
| 796 | }; |
| 797 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 798 | CanvasKit.Path.prototype.lineTo = function(x, y) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 799 | this._lineTo(x, y); |
| 800 | return this; |
| 801 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 802 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 803 | CanvasKit.Path.prototype.moveTo = function(x, y) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 804 | this._moveTo(x, y); |
| 805 | return this; |
| 806 | }; |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 807 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 808 | CanvasKit.Path.prototype.offset = function(dx, dy) { |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 809 | this._transform(1, 0, dx, |
| 810 | 0, 1, dy, |
| 811 | 0, 0, 1); |
| 812 | return this; |
| 813 | }; |
| 814 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 815 | CanvasKit.Path.prototype.quadTo = function(cpx, cpy, x, y) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 816 | this._quadTo(cpx, cpy, x, y); |
| 817 | return this; |
| 818 | }; |
| 819 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 820 | CanvasKit.Path.prototype.rArcTo = function(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 821 | this._rArcTo(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy); |
| 822 | return this; |
| 823 | }; |
| 824 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 825 | CanvasKit.Path.prototype.rConicTo = function(dx1, dy1, dx2, dy2, w) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 826 | this._rConicTo(dx1, dy1, dx2, dy2, w); |
| 827 | return this; |
| 828 | }; |
| 829 | |
| 830 | // These params are all relative |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 831 | CanvasKit.Path.prototype.rCubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 832 | this._rCubicTo(cp1x, cp1y, cp2x, cp2y, x, y); |
| 833 | return this; |
| 834 | }; |
| 835 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 836 | CanvasKit.Path.prototype.rLineTo = function(dx, dy) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 837 | this._rLineTo(dx, dy); |
| 838 | return this; |
| 839 | }; |
| 840 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 841 | CanvasKit.Path.prototype.rMoveTo = function(dx, dy) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 842 | this._rMoveTo(dx, dy); |
| 843 | return this; |
| 844 | }; |
| 845 | |
| 846 | // These params are all relative |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 847 | CanvasKit.Path.prototype.rQuadTo = function(cpx, cpy, x, y) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 848 | this._rQuadTo(cpx, cpy, x, y); |
| 849 | return this; |
| 850 | }; |
| 851 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 852 | CanvasKit.Path.prototype.stroke = function(opts) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 853 | // Fill out any missing values with the default values. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 854 | opts = opts || {}; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 855 | opts['width'] = opts['width'] || 1; |
| 856 | opts['miter_limit'] = opts['miter_limit'] || 4; |
| 857 | opts['cap'] = opts['cap'] || CanvasKit.StrokeCap.Butt; |
| 858 | opts['join'] = opts['join'] || CanvasKit.StrokeJoin.Miter; |
| 859 | opts['precision'] = opts['precision'] || 1; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 860 | if (this._stroke(opts)) { |
| 861 | return this; |
| 862 | } |
| 863 | return null; |
| 864 | }; |
| 865 | |
Kevin Lubick | a2535af | 2020-10-02 08:01:57 -0400 | [diff] [blame] | 866 | // TODO(kjlubick) Change this to take a 3x3 or 4x4 matrix (optionally malloc'd) |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 867 | CanvasKit.Path.prototype.transform = function() { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 868 | // Takes 1 or 9 args |
| 869 | if (arguments.length === 1) { |
| 870 | // argument 1 should be a 6 or 9 element array. |
| 871 | var a = arguments[0]; |
| 872 | this._transform(a[0], a[1], a[2], |
| 873 | a[3], a[4], a[5], |
| 874 | a[6] || 0, a[7] || 0, a[8] || 1); |
| 875 | } else if (arguments.length === 6 || arguments.length === 9) { |
| 876 | // these arguments are the 6 or 9 members of the matrix |
| 877 | var a = arguments; |
| 878 | this._transform(a[0], a[1], a[2], |
| 879 | a[3], a[4], a[5], |
| 880 | a[6] || 0, a[7] || 0, a[8] || 1); |
| 881 | } else { |
| 882 | throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length; |
| 883 | } |
| 884 | return this; |
| 885 | }; |
| 886 | // isComplement is optional, defaults to false |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 887 | CanvasKit.Path.prototype.trim = function(startT, stopT, isComplement) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 888 | if (this._trim(startT, stopT, !!isComplement)) { |
| 889 | return this; |
| 890 | } |
| 891 | return null; |
| 892 | }; |
| 893 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 894 | CanvasKit.Image.prototype.encodeToData = function() { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 895 | if (!arguments.length) { |
| 896 | return this._encodeToData(); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 897 | } |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 898 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 899 | if (arguments.length === 2) { |
| 900 | var a = arguments; |
| 901 | return this._encodeToDataWithFormat(a[0], a[1]); |
Alexander Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 902 | } |
| 903 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 904 | throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 905 | }; |
Kevin Lubick | 1ba9c4d | 2019-02-22 10:04:06 -0500 | [diff] [blame] | 906 | |
Kevin Lubick | 652d790 | 2020-12-11 14:51:36 -0500 | [diff] [blame] | 907 | // makeShaderCubic returns a shader for a given image, allowing it to be used on |
| 908 | // a paint as well as other purposes. This shader will be higher quality than |
| 909 | // other shader functions. See CubicResampler in SkSamplingOptions.h for more information |
| 910 | // on the cubicResampler params. |
| 911 | CanvasKit.Image.prototype.makeShaderCubic = function(xTileMode, yTileMode, |
| 912 | cubicResamplerB, cubicResamplerC, |
| 913 | localMatrix) { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 914 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Kevin Lubick | 652d790 | 2020-12-11 14:51:36 -0500 | [diff] [blame] | 915 | return this._makeShaderCubic(xTileMode, yTileMode, cubicResamplerB, |
| 916 | cubicResamplerC, localMatrixPtr); |
| 917 | }; |
| 918 | |
| 919 | // makeShaderCubic returns a shader for a given image, allowing it to be used on |
| 920 | // a paint as well as other purposes. This shader will draw more quickly than |
| 921 | // other shader functions, but at a lower quality. |
| 922 | CanvasKit.Image.prototype.makeShaderOptions = function(xTileMode, yTileMode, |
| 923 | filterMode, mipmapMode, |
| 924 | localMatrix) { |
| 925 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
| 926 | return this._makeShaderOptions(xTileMode, yTileMode, filterMode, mipmapMode, localMatrixPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 927 | }; |
Kevin Lubick | a064c28 | 2019-04-04 09:28:53 -0400 | [diff] [blame] | 928 | |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 929 | function readPixels(source, srcX, srcY, imageInfo, destMallocObj, bytesPerRow) { |
| 930 | if (!bytesPerRow) { |
| 931 | bytesPerRow = 4 * imageInfo['width']; |
| 932 | if (imageInfo['colorType'] === CanvasKit.ColorType.RGBA_F16) { |
| 933 | bytesPerRow *= 2; |
| 934 | } |
| 935 | else if (imageInfo['colorType'] === CanvasKit.ColorType.RGBA_F32) { |
| 936 | bytesPerRow *= 4; |
| 937 | } |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 938 | } |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 939 | var pBytes = bytesPerRow * imageInfo.height; |
Kevin Lubick | c4ab087 | 2020-11-03 17:13:09 -0500 | [diff] [blame] | 940 | var pPtr; |
| 941 | if (destMallocObj) { |
| 942 | pPtr = destMallocObj['byteOffset']; |
| 943 | } else { |
| 944 | pPtr = CanvasKit._malloc(pBytes); |
| 945 | } |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 946 | |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 947 | if (!source._readPixels(imageInfo, pPtr, bytesPerRow, srcX, srcY)) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 948 | Debug('Could not read pixels with the given inputs'); |
Kevin Lubick | c4ab087 | 2020-11-03 17:13:09 -0500 | [diff] [blame] | 949 | if (!destMallocObj) { |
| 950 | CanvasKit._free(pPtr); |
| 951 | } |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 952 | return null; |
| 953 | } |
| 954 | |
Kevin Lubick | c4ab087 | 2020-11-03 17:13:09 -0500 | [diff] [blame] | 955 | // If the user provided us a buffer to copy into, we don't need to allocate a new TypedArray. |
| 956 | if (destMallocObj) { |
| 957 | return destMallocObj['toTypedArray'](); // Return the typed array wrapper w/o allocating. |
| 958 | } |
| 959 | |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 960 | // Put those pixels into a typed array of the right format and then |
| 961 | // make a copy with slice() that we can return. |
| 962 | var retVal = null; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 963 | switch (imageInfo['colorType']) { |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 964 | case CanvasKit.ColorType.RGBA_8888: |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 965 | case CanvasKit.ColorType.RGBA_F16: // there is no half-float JS type, so we return raw bytes. |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame] | 966 | retVal = new Uint8Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice(); |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 967 | break; |
| 968 | case CanvasKit.ColorType.RGBA_F32: |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame] | 969 | retVal = new Float32Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice(); |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 970 | break; |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 971 | default: |
| 972 | Debug('ColorType not yet supported'); |
| 973 | return null; |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | // Free the allocated pixels in the WASM memory |
| 977 | CanvasKit._free(pPtr); |
| 978 | return retVal; |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | CanvasKit.Image.prototype.readPixels = function(srcX, srcY, imageInfo, destMallocObj, |
| 982 | bytesPerRow) { |
| 983 | return readPixels(this, srcX, srcY, imageInfo, destMallocObj, bytesPerRow); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 984 | }; |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 985 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 986 | // Accepts an array of four numbers in the range of 0-1 representing a 4f color |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 987 | CanvasKit.Canvas.prototype.clear = function(color4f) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 988 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 989 | this._clear(cPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 990 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 991 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 992 | CanvasKit.Canvas.prototype.clipRRect = function(rrect, op, antialias) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 993 | var rPtr = copyRRectToWasm(rrect); |
| 994 | this._clipRRect(rPtr, op, antialias); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 995 | }; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 996 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 997 | CanvasKit.Canvas.prototype.clipRect = function(rect, op, antialias) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 998 | var rPtr = copyRectToWasm(rect); |
| 999 | this._clipRect(rPtr, op, antialias); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1000 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1001 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 1002 | // concat takes a 3x2, a 3x3, or a 4x4 matrix and upscales it (if needed) to 4x4. This is because |
| 1003 | // under the hood, SkCanvas uses a 4x4 matrix. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1004 | CanvasKit.Canvas.prototype.concat = function(matr) { |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 1005 | var matrPtr = copy4x4MatrixToWasm(matr); |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1006 | this._concat(matrPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1007 | }; |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 1008 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1009 | CanvasKit.Canvas.prototype.drawArc = function(oval, startAngle, sweepAngle, useCenter, paint) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1010 | var oPtr = copyRectToWasm(oval); |
| 1011 | this._drawArc(oPtr, startAngle, sweepAngle, useCenter, paint); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1012 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1013 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1014 | // atlas is an Image, e.g. from CanvasKit.MakeImageFromEncoded |
| 1015 | // srcRects, dstXforms, and colors should be CanvasKit.RectBuilder, CanvasKit.RSXFormBuilder, |
| 1016 | // and CanvasKit.ColorBuilder (fastest) |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1017 | // Or they can be an array of floats of length 4*number of destinations. |
| 1018 | // colors are optional and used to tint the drawn images using the optional blend mode |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1019 | // Colors may be an ColorBuilder, a Uint32Array of int colors, |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1020 | // a Flat Float32Array of float colors or a 2d Array of Float32Array(4) (deprecated) |
Kevin Lubick | 97440de | 2020-09-29 17:58:21 -0400 | [diff] [blame] | 1021 | // TODO(kjlubick) remove Builders - no longer needed now that Malloc is a thing. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1022 | CanvasKit.Canvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint, |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1023 | /*optional*/ blendMode, colors) { |
| 1024 | if (!atlas || !paint || !srcRects || !dstXforms) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1025 | Debug('Doing nothing since missing a required input'); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1026 | return; |
| 1027 | } |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1028 | |
| 1029 | // builder arguments report the length as the number of rects, but when passed as arrays |
| 1030 | // their.length attribute is 4x higher because it's the number of total components of all rects. |
| 1031 | // colors is always going to report the same length, at least until floats colors are supported |
| 1032 | // by this function. |
| 1033 | if (srcRects.length !== dstXforms.length) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1034 | Debug('Doing nothing since input arrays length mismatches'); |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 1035 | return; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1036 | } |
| 1037 | if (!blendMode) { |
| 1038 | blendMode = CanvasKit.BlendMode.SrcOver; |
| 1039 | } |
| 1040 | |
| 1041 | var srcRectPtr; |
| 1042 | if (srcRects.build) { |
| 1043 | srcRectPtr = srcRects.build(); |
| 1044 | } else { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1045 | srcRectPtr = copy1dArray(srcRects, 'HEAPF32'); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1046 | } |
| 1047 | |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1048 | var count = 1; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1049 | var dstXformPtr; |
| 1050 | if (dstXforms.build) { |
| 1051 | dstXformPtr = dstXforms.build(); |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1052 | count = dstXforms.length; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1053 | } else { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1054 | dstXformPtr = copy1dArray(dstXforms, 'HEAPF32'); |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1055 | count = dstXforms.length / 4; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1056 | } |
| 1057 | |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1058 | var colorPtr = nullptr; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1059 | if (colors) { |
| 1060 | if (colors.build) { |
| 1061 | colorPtr = colors.build(); |
| 1062 | } else { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1063 | colorPtr = copy1dArray(assureIntColors(colors), 'HEAPU32'); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1067 | this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, count, blendMode, paint); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1068 | |
| 1069 | if (srcRectPtr && !srcRects.build) { |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1070 | freeArraysThatAreNotMallocedByUsers(srcRectPtr, srcRects); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1071 | } |
| 1072 | if (dstXformPtr && !dstXforms.build) { |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1073 | freeArraysThatAreNotMallocedByUsers(dstXformPtr, dstXforms); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1074 | } |
| 1075 | if (colorPtr && !colors.build) { |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1076 | freeArraysThatAreNotMallocedByUsers(colorPtr, colors); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1077 | } |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1078 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1079 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1080 | CanvasKit.Canvas.prototype.drawColor = function (color4f, mode) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1081 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1082 | if (mode !== undefined) { |
| 1083 | this._drawColor(cPtr, mode); |
| 1084 | } else { |
| 1085 | this._drawColor(cPtr); |
| 1086 | } |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1087 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1088 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1089 | CanvasKit.Canvas.prototype.drawColorComponents = function (r, g, b, a, mode) { |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 1090 | var cPtr = copyColorComponentsToWasm(r, g, b, a); |
| 1091 | if (mode !== undefined) { |
| 1092 | this._drawColor(cPtr, mode); |
| 1093 | } else { |
| 1094 | this._drawColor(cPtr); |
| 1095 | } |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1096 | }; |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 1097 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1098 | CanvasKit.Canvas.prototype.drawDRRect = function(outer, inner, paint) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1099 | var oPtr = copyRRectToWasm(outer, _scratchRRectPtr); |
| 1100 | var iPtr = copyRRectToWasm(inner, _scratchRRect2Ptr); |
| 1101 | this._drawDRRect(oPtr, iPtr, paint); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1102 | }; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1103 | |
Kevin Lubick | 2d63349 | 2020-12-17 09:58:32 -0500 | [diff] [blame] | 1104 | CanvasKit.Canvas.prototype.drawImageNine = function(img, center, dest, filter, paint) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1105 | var cPtr = copyIRectToWasm(center); |
| 1106 | var dPtr = copyRectToWasm(dest); |
Kevin Lubick | 2d63349 | 2020-12-17 09:58:32 -0500 | [diff] [blame] | 1107 | this._drawImageNine(img, cPtr, dPtr, filter, paint || null); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1108 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1109 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1110 | CanvasKit.Canvas.prototype.drawImageRect = function(img, src, dest, paint, fastSample) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1111 | var sPtr = copyRectToWasm(src, _scratchRectPtr); |
| 1112 | var dPtr = copyRectToWasm(dest, _scratchRect2Ptr); |
| 1113 | this._drawImageRect(img, sPtr, dPtr, paint, !!fastSample); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1114 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1115 | |
Kevin Lubick | 72f4076 | 2020-12-16 16:00:55 -0500 | [diff] [blame] | 1116 | CanvasKit.Canvas.prototype.drawImageRectCubic = function(img, src, dest, B, C, paint) { |
| 1117 | var sPtr = copyRectToWasm(src, _scratchRectPtr); |
| 1118 | var dPtr = copyRectToWasm(dest, _scratchRect2Ptr); |
| 1119 | this._drawImageRectCubic(img, sPtr, dPtr, B, C, paint || null); |
| 1120 | }; |
| 1121 | |
| 1122 | CanvasKit.Canvas.prototype.drawImageRectOptions = function(img, src, dest, filter, mipmap, paint) { |
| 1123 | var sPtr = copyRectToWasm(src, _scratchRectPtr); |
| 1124 | var dPtr = copyRectToWasm(dest, _scratchRect2Ptr); |
| 1125 | this._drawImageRectOptions(img, sPtr, dPtr, filter, mipmap, paint || null); |
| 1126 | }; |
| 1127 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1128 | CanvasKit.Canvas.prototype.drawOval = function(oval, paint) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1129 | var oPtr = copyRectToWasm(oval); |
| 1130 | this._drawOval(oPtr, paint); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1131 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1132 | |
Kevin Lubick | 658cb71 | 2020-12-01 14:55:02 -0500 | [diff] [blame] | 1133 | // points is a 1d array of length 2n representing n points where the even indices |
| 1134 | // will be treated as x coordinates and the odd indices will be treated as y coordinates. |
| 1135 | // Like other APIs, this accepts a malloced type array or malloc obj. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1136 | CanvasKit.Canvas.prototype.drawPoints = function(mode, points, paint) { |
Kevin Lubick | 658cb71 | 2020-12-01 14:55:02 -0500 | [diff] [blame] | 1137 | var ptr = copy1dArray(points, 'HEAPF32'); |
| 1138 | this._drawPoints(mode, ptr, points.length / 2, paint); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1139 | freeArraysThatAreNotMallocedByUsers(ptr, points); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1140 | }; |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 1141 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1142 | CanvasKit.Canvas.prototype.drawRRect = function(rrect, paint) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1143 | var rPtr = copyRRectToWasm(rrect); |
| 1144 | this._drawRRect(rPtr, paint); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1145 | }; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1146 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1147 | CanvasKit.Canvas.prototype.drawRect = function(rect, paint) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1148 | var rPtr = copyRectToWasm(rect); |
| 1149 | this._drawRect(rPtr, paint); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1150 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1151 | |
Kevin Lubick | cbaea29 | 2021-01-13 14:16:58 -0500 | [diff] [blame] | 1152 | CanvasKit.Canvas.prototype.drawShadow = function(path, zPlaneParams, lightPos, lightRadius, |
| 1153 | ambientColor, spotColor, flags) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1154 | var ambiPtr = copyColorToWasmNoScratch(ambientColor); |
| 1155 | var spotPtr = copyColorToWasmNoScratch(spotColor); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1156 | this._drawShadow(path, zPlaneParams, lightPos, lightRadius, ambiPtr, spotPtr, flags); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1157 | freeArraysThatAreNotMallocedByUsers(ambiPtr, ambientColor); |
| 1158 | freeArraysThatAreNotMallocedByUsers(spotPtr, spotColor); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1159 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1160 | |
Kevin Lubick | cbaea29 | 2021-01-13 14:16:58 -0500 | [diff] [blame] | 1161 | CanvasKit.getShadowLocalBounds = function(ctm, path, zPlaneParams, lightPos, lightRadius, |
| 1162 | flags, optOutputRect) { |
| 1163 | var ctmPtr = copy3x3MatrixToWasm(ctm); |
| 1164 | var ok = this._getShadowLocalBounds(ctmPtr, path, zPlaneParams, lightPos, lightRadius, |
| 1165 | flags, _scratchRectPtr); |
| 1166 | if (!ok) { |
| 1167 | return null; |
| 1168 | } |
| 1169 | var ta = _scratchRect['toTypedArray'](); |
| 1170 | if (optOutputRect) { |
| 1171 | optOutputRect.set(ta); |
| 1172 | return optOutputRect; |
| 1173 | } |
| 1174 | return ta.slice(); |
| 1175 | }; |
| 1176 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 1177 | // getLocalToDevice returns a 4x4 matrix. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1178 | CanvasKit.Canvas.prototype.getLocalToDevice = function() { |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 1179 | // _getLocalToDevice will copy the values into the pointer. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1180 | this._getLocalToDevice(_scratch4x4MatrixPtr); |
| 1181 | return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1182 | }; |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 1183 | |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 1184 | // findMarkedCTM returns a 4x4 matrix, or null if a matrix was not found at |
| 1185 | // the provided marker. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1186 | CanvasKit.Canvas.prototype.findMarkedCTM = function(marker) { |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 1187 | // _getLocalToDevice will copy the values into the pointer. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1188 | var found = this._findMarkedCTM(marker, _scratch4x4MatrixPtr); |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 1189 | if (!found) { |
| 1190 | return null; |
| 1191 | } |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1192 | return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1193 | }; |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 1194 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 1195 | // getTotalMatrix returns the current matrix as a 3x3 matrix. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1196 | CanvasKit.Canvas.prototype.getTotalMatrix = function() { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1197 | // _getTotalMatrix will copy the values into the pointer. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1198 | this._getTotalMatrix(_scratch3x3MatrixPtr); |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1199 | // read them out into an array. TODO(kjlubick): If we change Matrix to be |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1200 | // typedArrays, then we should return a typed array here too. |
| 1201 | var rv = new Array(9); |
| 1202 | for (var i = 0; i < 9; i++) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1203 | rv[i] = CanvasKit.HEAPF32[_scratch3x3MatrixPtr/4 + i]; // divide by 4 to "cast" to float. |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1204 | } |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1205 | return rv; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1206 | }; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1207 | |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 1208 | CanvasKit.Canvas.prototype.readPixels = function(srcX, srcY, imageInfo, destMallocObj, |
| 1209 | bytesPerRow) { |
| 1210 | return readPixels(this, srcX, srcY, imageInfo, destMallocObj, bytesPerRow); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1211 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1212 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1213 | CanvasKit.Canvas.prototype.saveLayer = function(paint, boundsRect, backdrop, flags) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1214 | // bPtr will be 0 (nullptr) if boundsRect is undefined/null. |
| 1215 | var bPtr = copyRectToWasm(boundsRect); |
| 1216 | // These or clauses help emscripten, which does not deal with undefined well. |
| 1217 | return this._saveLayer(paint || null, bPtr, backdrop || null, flags || 0); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1218 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1219 | |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1220 | // pixels should be a Uint8Array or a plain JS array. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1221 | CanvasKit.Canvas.prototype.writePixels = function(pixels, srcWidth, srcHeight, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1222 | destX, destY, alphaType, colorType, colorSpace) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1223 | if (pixels.byteLength % (srcWidth * srcHeight)) { |
| 1224 | throw 'pixels length must be a multiple of the srcWidth * srcHeight'; |
| 1225 | } |
| 1226 | var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight); |
| 1227 | // supply defaults (which are compatible with HTMLCanvas's putImageData) |
| 1228 | alphaType = alphaType || CanvasKit.AlphaType.Unpremul; |
| 1229 | colorType = colorType || CanvasKit.ColorType.RGBA_8888; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1230 | colorSpace = colorSpace || CanvasKit.ColorSpace.SRGB; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1231 | var srcRowBytes = bytesPerPixel * srcWidth; |
| 1232 | |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1233 | var pptr = copy1dArray(pixels, 'HEAPU8'); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1234 | var ok = this._writePixels({ |
| 1235 | 'width': srcWidth, |
| 1236 | 'height': srcHeight, |
| 1237 | 'colorType': colorType, |
| 1238 | 'alphaType': alphaType, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1239 | 'colorSpace': colorSpace, |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1240 | }, pptr, srcRowBytes, destX, destY); |
| 1241 | |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1242 | freeArraysThatAreNotMallocedByUsers(pptr, pixels); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1243 | return ok; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1244 | }; |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 1245 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1246 | CanvasKit.ColorFilter.MakeBlend = function(color4f, mode) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1247 | var cPtr = copyColorToWasm(color4f); |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1248 | var result = CanvasKit.ColorFilter._MakeBlend(cPtr, mode); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1249 | return result; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1250 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1251 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1252 | // colorMatrix is an ColorMatrix (e.g. Float32Array of length 20) |
| 1253 | CanvasKit.ColorFilter.MakeMatrix = function(colorMatrix) { |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 1254 | if (!colorMatrix || colorMatrix.length !== 20) { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1255 | throw 'invalid color matrix'; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 1256 | } |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1257 | var fptr = copy1dArray(colorMatrix, 'HEAPF32'); |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 1258 | // We know skia memcopies the floats, so we can free our memory after the call returns. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1259 | var m = CanvasKit.ColorFilter._makeMatrix(fptr); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1260 | freeArraysThatAreNotMallocedByUsers(fptr, colorMatrix); |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 1261 | return m; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1262 | }; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 1263 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1264 | CanvasKit.ImageFilter.MakeMatrixTransform = function(matr, filterQuality, input) { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1265 | var matrPtr = copy3x3MatrixToWasm(matr); |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1266 | return CanvasKit.ImageFilter._MakeMatrixTransform(matrPtr, filterQuality, input); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1267 | }; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1268 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1269 | CanvasKit.Paint.prototype.getColor = function() { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1270 | this._getColor(_scratchColorPtr); |
| 1271 | return copyColorFromWasm(_scratchColorPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1272 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1273 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1274 | CanvasKit.Paint.prototype.setColor = function(color4f, colorSpace) { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1275 | colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1276 | // emscripten wouldn't bind undefined to the sk_sp<ColorSpace> expected here. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1277 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1278 | this._setColor(cPtr, colorSpace); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1279 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1280 | |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 1281 | // The color components here are expected to be floating point values (nominally between |
| 1282 | // 0.0 and 1.0, but with wider color gamuts, the values could exceed this range). To convert |
| 1283 | // between standard 8 bit colors and floats, just divide by 255 before passing them in. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1284 | CanvasKit.Paint.prototype.setColorComponents = function(r, g, b, a, colorSpace) { |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 1285 | colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1286 | // emscripten wouldn't bind undefined to the sk_sp<ColorSpace> expected here. |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 1287 | var cPtr = copyColorComponentsToWasm(r, g, b, a); |
| 1288 | this._setColor(cPtr, colorSpace); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1289 | }; |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 1290 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1291 | CanvasKit.PictureRecorder.prototype.beginRecording = function(bounds) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1292 | var bPtr = copyRectToWasm(bounds); |
| 1293 | return this._beginRecording(bPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1294 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1295 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1296 | CanvasKit.Surface.prototype.makeImageSnapshot = function(optionalBoundsRect) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1297 | var bPtr = copyIRectToWasm(optionalBoundsRect); |
| 1298 | return this._makeImageSnapshot(bPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1299 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1300 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1301 | CanvasKit.Surface.prototype.requestAnimationFrame = function(callback, dirtyRect) { |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 1302 | if (!this._cached_canvas) { |
| 1303 | this._cached_canvas = this.getCanvas(); |
| 1304 | } |
Elliot Evans | 1ec3b1a | 2020-07-23 10:25:58 -0400 | [diff] [blame] | 1305 | requestAnimationFrame(function() { |
Kevin Lubick | 3902628 | 2019-03-28 12:46:40 -0400 | [diff] [blame] | 1306 | if (this._context !== undefined) { |
| 1307 | CanvasKit.setCurrentContext(this._context); |
| 1308 | } |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 1309 | |
| 1310 | callback(this._cached_canvas); |
| 1311 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1312 | // We do not dispose() of the Surface here, as the client will typically |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 1313 | // call requestAnimationFrame again from within the supplied callback. |
| 1314 | // For drawing a single frame, prefer drawOnce(). |
Bryce Thomas | 9331ca0 | 2020-05-29 16:51:21 -0700 | [diff] [blame] | 1315 | this.flush(dirtyRect); |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 1316 | }.bind(this)); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1317 | }; |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 1318 | |
Kevin Lubick | 5237933 | 2020-01-27 10:01:25 -0500 | [diff] [blame] | 1319 | // drawOnce will dispose of the surface after drawing the frame using the provided |
| 1320 | // callback. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1321 | CanvasKit.Surface.prototype.drawOnce = function(callback, dirtyRect) { |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 1322 | if (!this._cached_canvas) { |
| 1323 | this._cached_canvas = this.getCanvas(); |
| 1324 | } |
Elliot Evans | 1ec3b1a | 2020-07-23 10:25:58 -0400 | [diff] [blame] | 1325 | requestAnimationFrame(function() { |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 1326 | if (this._context !== undefined) { |
| 1327 | CanvasKit.setCurrentContext(this._context); |
| 1328 | } |
| 1329 | callback(this._cached_canvas); |
| 1330 | |
Bryce Thomas | 9331ca0 | 2020-05-29 16:51:21 -0700 | [diff] [blame] | 1331 | this.flush(dirtyRect); |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 1332 | this.dispose(); |
| 1333 | }.bind(this)); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1334 | }; |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 1335 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1336 | CanvasKit.PathEffect.MakeDash = function(intervals, phase) { |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1337 | if (!phase) { |
| 1338 | phase = 0; |
| 1339 | } |
| 1340 | if (!intervals.length || intervals.length % 2 === 1) { |
| 1341 | throw 'Intervals array must have even length'; |
| 1342 | } |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1343 | var ptr = copy1dArray(intervals, 'HEAPF32'); |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1344 | var dpe = CanvasKit.PathEffect._MakeDash(ptr, intervals.length, phase); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1345 | freeArraysThatAreNotMallocedByUsers(ptr, intervals); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1346 | return dpe; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1347 | }; |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1348 | |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1349 | CanvasKit.Shader.MakeColor = function(color4f, colorSpace) { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1350 | colorSpace = colorSpace || null |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1351 | var cPtr = copyColorToWasm(color4f); |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1352 | return CanvasKit.Shader._MakeColor(cPtr, colorSpace); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1353 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1354 | |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1355 | // TODO(kjlubick) remove deprecated names. |
| 1356 | CanvasKit.Shader.Blend = CanvasKit.Shader.MakeBlend; |
| 1357 | CanvasKit.Shader.Color = CanvasKit.Shader.MakeColor; |
| 1358 | CanvasKit.Shader.Lerp = CanvasKit.Shader.MakeLerp; |
| 1359 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1360 | CanvasKit.Shader.MakeLinearGradient = function(start, end, colors, pos, mode, localMatrix, flags, colorSpace) { |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 1361 | colorSpace = colorSpace || null; |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1362 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1363 | var posPtr = copy1dArray(pos, 'HEAPF32'); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1364 | flags = flags || 0; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1365 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1366 | |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1367 | var lgs = CanvasKit.Shader._MakeLinearGradient(start, end, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, |
| 1368 | cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1369 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1370 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1371 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1372 | return lgs; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1373 | }; |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1374 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1375 | CanvasKit.Shader.MakeRadialGradient = function(center, radius, colors, pos, mode, localMatrix, flags, colorSpace) { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1376 | colorSpace = colorSpace || null |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1377 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1378 | var posPtr = copy1dArray(pos, 'HEAPF32'); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1379 | flags = flags || 0; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1380 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1381 | |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1382 | var rgs = CanvasKit.Shader._MakeRadialGradient(center, radius, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, |
| 1383 | cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1384 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1385 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1386 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1387 | return rgs; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1388 | }; |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1389 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1390 | CanvasKit.Shader.MakeSweepGradient = function(cx, cy, colors, pos, mode, localMatrix, flags, startAngle, endAngle, colorSpace) { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1391 | colorSpace = colorSpace || null |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1392 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1393 | var posPtr = copy1dArray(pos, 'HEAPF32'); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1394 | flags = flags || 0; |
| 1395 | startAngle = startAngle || 0; |
| 1396 | endAngle = endAngle || 360; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1397 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1398 | |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1399 | var sgs = CanvasKit.Shader._MakeSweepGradient(cx, cy, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, |
| 1400 | cPtrInfo.count, mode, |
| 1401 | startAngle, endAngle, flags, |
| 1402 | localMatrixPtr, colorSpace); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1403 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1404 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1405 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1406 | return sgs; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1407 | }; |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1408 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1409 | CanvasKit.Shader.MakeTwoPointConicalGradient = function(start, startRadius, end, endRadius, |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1410 | colors, pos, mode, localMatrix, flags, colorSpace) { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1411 | colorSpace = colorSpace || null |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1412 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1413 | var posPtr = copy1dArray(pos, 'HEAPF32'); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1414 | flags = flags || 0; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1415 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1416 | |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1417 | var rgs = CanvasKit.Shader._MakeTwoPointConicalGradient( |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1418 | start, startRadius, end, endRadius, cPtrInfo.colorPtr, cPtrInfo.colorType, |
| 1419 | posPtr, cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1420 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1421 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1422 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1423 | return rgs; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1424 | }; |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1425 | |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1426 | // Clients can pass in a Float32Array with length 4 to this and the results |
| 1427 | // will be copied into that array. Otherwise, a new TypedArray will be allocated |
| 1428 | // and returned. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1429 | CanvasKit.Vertices.prototype.bounds = function(optionalOutputArray) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1430 | this._bounds(_scratchRectPtr); |
| 1431 | var ta = _scratchRect['toTypedArray'](); |
| 1432 | if (optionalOutputArray) { |
| 1433 | optionalOutputArray.set(ta); |
| 1434 | return optionalOutputArray; |
| 1435 | } |
| 1436 | return ta.slice(); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1437 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1438 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1439 | // Run through the JS files that are added at compile time. |
| 1440 | if (CanvasKit._extraInitializations) { |
| 1441 | CanvasKit._extraInitializations.forEach(function(init) { |
| 1442 | init(); |
| 1443 | }); |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 1444 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1445 | }; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic. |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 1446 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1447 | // Accepts an object holding two canvaskit colors. |
| 1448 | // { |
Kevin Lubick | 3d00e3a | 2020-10-02 14:59:28 -0400 | [diff] [blame] | 1449 | // ambient: [r, g, b, a], |
| 1450 | // spot: [r, g, b, a], |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1451 | // } |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1452 | // Returns the same format. Note, if malloced colors are passed in, the memory |
| 1453 | // housing the passed in colors passed in will be overwritten with the computed |
| 1454 | // tonal colors. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1455 | CanvasKit.computeTonalColors = function(tonalColors) { |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1456 | // copy the colors into WASM |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1457 | var cPtrAmbi = copyColorToWasmNoScratch(tonalColors['ambient']); |
| 1458 | var cPtrSpot = copyColorToWasmNoScratch(tonalColors['spot']); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1459 | // The output of this function will be the same pointers we passed in. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1460 | this._computeTonalColors(cPtrAmbi, cPtrSpot); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1461 | // Read the results out. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1462 | var result = { |
| 1463 | 'ambient': copyColorFromWasm(cPtrAmbi), |
| 1464 | 'spot': copyColorFromWasm(cPtrSpot), |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1465 | }; |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1466 | // If the user passed us malloced colors in here, we don't want to clean them up. |
| 1467 | freeArraysThatAreNotMallocedByUsers(cPtrAmbi, tonalColors['ambient']); |
| 1468 | freeArraysThatAreNotMallocedByUsers(cPtrSpot, tonalColors['spot']); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1469 | return result; |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1470 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1471 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1472 | CanvasKit.LTRBRect = function(l, t, r, b) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1473 | return Float32Array.of(l, t, r, b); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1474 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 1475 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1476 | CanvasKit.XYWHRect = function(x, y, w, h) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1477 | return Float32Array.of(x, y, x+w, y+h); |
| 1478 | }; |
| 1479 | |
| 1480 | CanvasKit.LTRBiRect = function(l, t, r, b) { |
| 1481 | return Int32Array.of(l, t, r, b); |
| 1482 | }; |
| 1483 | |
| 1484 | CanvasKit.XYWHiRect = function(x, y, w, h) { |
| 1485 | return Int32Array.of(x, y, x+w, y+h); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1486 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 1487 | |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1488 | // RRectXY returns a TypedArray representing an RRect with the given rect and a radiusX and |
| 1489 | // radiusY for all 4 corners. |
Kevin Lubick | 7d644e1 | 2019-09-11 14:22:22 -0400 | [diff] [blame] | 1490 | CanvasKit.RRectXY = function(rect, rx, ry) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1491 | return Float32Array.of( |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1492 | rect[0], rect[1], rect[2], rect[3], |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1493 | rx, ry, |
| 1494 | rx, ry, |
| 1495 | rx, ry, |
| 1496 | rx, ry, |
| 1497 | ); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1498 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1499 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1500 | // data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer()) |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 1501 | CanvasKit.MakeAnimatedImageFromEncoded = function(data) { |
| 1502 | data = new Uint8Array(data); |
| 1503 | |
| 1504 | var iptr = CanvasKit._malloc(data.byteLength); |
| 1505 | CanvasKit.HEAPU8.set(data, iptr); |
| 1506 | var img = CanvasKit._decodeAnimatedImage(iptr, data.byteLength); |
| 1507 | if (!img) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1508 | Debug('Could not decode animated image'); |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 1509 | return null; |
| 1510 | } |
| 1511 | return img; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1512 | }; |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 1513 | |
| 1514 | // data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer()) |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1515 | CanvasKit.MakeImageFromEncoded = 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._decodeImage(iptr, data.byteLength); |
| 1521 | if (!img) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1522 | Debug('Could not decode image'); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1523 | return null; |
| 1524 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1525 | return img; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1526 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1527 | |
Elliot Evans | 2879619 | 2020-06-15 12:53:27 -0600 | [diff] [blame] | 1528 | // A variable to hold a canvasElement which can be reused once created the first time. |
| 1529 | var memoizedCanvas2dElement = null; |
| 1530 | |
| 1531 | // Alternative to CanvasKit.MakeImageFromEncoded. Allows for CanvasKit users to take advantage of |
| 1532 | // browser APIs to decode images instead of using codecs included in the CanvasKit wasm binary. |
| 1533 | // Expects that the canvasImageSource has already loaded/decoded. |
| 1534 | // CanvasImageSource reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasImageSource |
| 1535 | CanvasKit.MakeImageFromCanvasImageSource = function(canvasImageSource) { |
| 1536 | var width = canvasImageSource.width; |
| 1537 | var height = canvasImageSource.height; |
| 1538 | |
| 1539 | if (!memoizedCanvas2dElement) { |
| 1540 | memoizedCanvas2dElement = document.createElement('canvas'); |
| 1541 | } |
| 1542 | memoizedCanvas2dElement.width = width; |
| 1543 | memoizedCanvas2dElement.height = height; |
| 1544 | |
| 1545 | var ctx2d = memoizedCanvas2dElement.getContext('2d'); |
| 1546 | ctx2d.drawImage(canvasImageSource, 0, 0); |
| 1547 | |
| 1548 | var imageData = ctx2d.getImageData(0, 0, width, height); |
| 1549 | |
Kevin Lubick | ae0d3ff | 2020-11-18 11:23:15 -0500 | [diff] [blame] | 1550 | return CanvasKit.MakeImage({ |
| 1551 | 'width': width, |
| 1552 | 'height': height, |
| 1553 | 'alphaType': CanvasKit.AlphaType.Unpremul, |
| 1554 | 'colorType': CanvasKit.ColorType.RGBA_8888, |
| 1555 | 'colorSpace': CanvasKit.ColorSpace.SRGB |
| 1556 | }, imageData.data, 4 * width); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1557 | }; |
Elliot Evans | 2879619 | 2020-06-15 12:53:27 -0600 | [diff] [blame] | 1558 | |
Kevin Lubick | ae0d3ff | 2020-11-18 11:23:15 -0500 | [diff] [blame] | 1559 | // pixels may be an array but Uint8Array or Uint8ClampedArray is recommended, |
| 1560 | // with the bytes representing the pixel values. |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 1561 | // (e.g. each set of 4 bytes could represent RGBA values for a single pixel). |
Kevin Lubick | ae0d3ff | 2020-11-18 11:23:15 -0500 | [diff] [blame] | 1562 | CanvasKit.MakeImage = function(info, pixels, bytesPerRow) { |
| 1563 | var pptr = CanvasKit._malloc(pixels.length); |
| 1564 | CanvasKit.HEAPU8.set(pixels, pptr); // We always want to copy the bytes into the WASM heap. |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 1565 | // No need to _free pptr, Image takes it with SkData::MakeFromMalloc |
Kevin Lubick | ae0d3ff | 2020-11-18 11:23:15 -0500 | [diff] [blame] | 1566 | return CanvasKit._MakeImage(info, pptr, pixels.length, bytesPerRow); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1567 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1568 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1569 | // Colors may be a Uint32Array of int colors, a Flat Float32Array of float colors |
| 1570 | // or a 2d Array of Float32Array(4) (deprecated) |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 1571 | // the underlying Skia function accepts only int colors so it is recommended |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1572 | // to pass an array of int colors to avoid an extra conversion. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1573 | // ColorBuilder is not accepted. |
| 1574 | CanvasKit.MakeVertices = function(mode, positions, textureCoordinates, colors, |
| 1575 | indices, isVolatile) { |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 1576 | // Default isVolatile to true if not set |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 1577 | isVolatile = isVolatile === undefined ? true : isVolatile; |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1578 | var idxCount = (indices && indices.length) || 0; |
| 1579 | |
| 1580 | var flags = 0; |
| 1581 | // These flags are from SkVertices.h and should be kept in sync with those. |
| 1582 | if (textureCoordinates && textureCoordinates.length) { |
| 1583 | flags |= (1 << 0); |
| 1584 | } |
| 1585 | if (colors && colors.length) { |
| 1586 | flags |= (1 << 1); |
| 1587 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1588 | if (!isVolatile) { |
Mike Reed | 5caf935 | 2020-03-02 14:57:09 -0500 | [diff] [blame] | 1589 | flags |= (1 << 2); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1590 | } |
| 1591 | |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 1592 | var builder = new CanvasKit._VerticesBuilder(mode, positions.length / 2, idxCount, flags); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1593 | |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 1594 | copy1dArray(positions, 'HEAPF32', builder.positions()); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1595 | if (builder.texCoords()) { |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 1596 | copy1dArray(textureCoordinates, 'HEAPF32', builder.texCoords()); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1597 | } |
| 1598 | if (builder.colors()) { |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1599 | if (colors.build) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1600 | throw('Color builder not accepted by MakeVertices, use array of ints'); |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1601 | } else { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1602 | copy1dArray(assureIntColors(colors), 'HEAPU32', builder.colors()); |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1603 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1604 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1605 | if (builder.indices()) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1606 | copy1dArray(indices, 'HEAPU16', builder.indices()); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1607 | } |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 1608 | |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1609 | // Create the vertices, which owns the memory that the builder had allocated. |
| 1610 | return builder.detach(); |
Kevin Lubick | a4f218d | 2020-01-14 08:39:09 -0500 | [diff] [blame] | 1611 | }; |