skia2/experimental/canvaskit/htmlcanvas/util.js
Kevin Lubick 53eabf6871 [canvaskit] Refactor Canvas2D JS into own files
Rather than one monolithic file, we now have one monolithic
file (canvascontext2d) and several smaller files (one per class,
and some helpers).

This should make the code navigation a little easier.

Bug: skia:
Change-Id: Ia191c2db778591af21d2a6126f053c17c4f677f1
Reviewed-on: https://skia-review.googlesource.com/c/175996
Reviewed-by: Florin Malita <fmalita@chromium.org>
2018-12-11 12:02:27 +00:00

36 lines
915 B
JavaScript

// General purpose utility functions go in this file.
function allAreFinite(args) {
for (var i = 0; i < args.length; i++) {
if (args[i] !== undefined && !Number.isFinite(args[i])) {
return false;
}
}
return true;
}
function toBase64String(bytes) {
if (isNode) {
return Buffer.from(bytes).toString('base64');
} else {
// From https://stackoverflow.com/a/25644409
// because the naive solution of
// btoa(String.fromCharCode.apply(null, bytes));
// would occasionally throw "Maximum call stack size exceeded"
var CHUNK_SIZE = 0x8000; //arbitrary number
var index = 0;
var length = bytes.length;
var result = '';
var slice;
while (index < length) {
slice = bytes.slice(index, Math.min(index + CHUNK_SIZE, length));
result += String.fromCharCode.apply(null, slice);
index += CHUNK_SIZE;
}
return btoa(result);
}
}