// Adds JS functions to augment the CanvasKit interface. // For example, if there is a wrapper around the C++ call or logic to allow // chaining, it should go here. // CanvasKit.onRuntimeInitialized is called after the WASM library has loaded. // Anything that modifies an exposed class (e.g. SkPath) should be set // after onRuntimeInitialized, otherwise, it can happen outside of that scope. CanvasKit.onRuntimeInitialized = function() { // All calls to 'this' need to go in externs.js so closure doesn't minify them away. // Add some helpers for matrices. This is ported from SkMatrix.cpp // to save complexity and overhead of going back and forth between // C++ and JS layers. // I would have liked to use something like DOMMatrix, except it // isn't widely supported (would need polyfills) and it doesn't // have a mapPoints() function (which could maybe be tacked on here). // If DOMMatrix catches on, it would be worth re-considering this usage. CanvasKit.SkMatrix = {}; function sdot() { // to be called with an even number of scalar args var acc = 0; for (var i=0; i < arguments.length-1; i+=2) { acc += arguments[i] * arguments[i+1]; } return acc; } // Private general matrix functions used in both 3x3s and 4x4s. // Return a square identity matrix of size n. var identityN = function(n) { var size = n*n; var m = new Array(size); while(size--) { m[size] = size%(n+1) == 0 ? 1.0 : 0.0; } return m; } // Stride, a function for compactly representing several ways of copying an array into another. // Write vector `v` into matrix `m`. `m` is a matrix encoded as an array in row-major // order. Its width is passed as `width`. `v` is an array with length < (m.length/width). // An element of `v` is copied into `m` starting at `offset` and moving `colStride` cols right // each row. // // For example, a width of 4, offset of 3, and stride of -1 would put the vector here. // _ _ 0 _ // _ 1 _ _ // 2 _ _ _ // _ _ _ 3 // var stride = function(v, m, width, offset, colStride) { for (var i=0; i