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